当前位置:   article > 正文

SpringMVC第四天(SSM整合)

SpringMVC第四天(SSM整合)

SSM整合流程

1.创建工程

2.SSM整合

        ①Spring

                SpringConfig

  1. package com.cacb.config;
  2. import org.springframework.context.annotation.ComponentScan;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.context.annotation.Import;
  5. import org.springframework.context.annotation.PropertySource;
  6. @Configuration
  7. @ComponentScan("com.cacb.service")
  8. @PropertySource("jdbc.properties")
  9. @Import({JdbcConfig.class,MybatisConfig.class})
  10. public class SpringConfig {
  11. }

        ②MyBatis

                MybatisConfig

  1. package com.cacb.config;
  2. import org.mybatis.spring.SqlSessionFactoryBean;
  3. import org.mybatis.spring.mapper.MapperScannerConfigurer;
  4. import org.springframework.context.annotation.Bean;
  5. import javax.sql.DataSource;
  6. public class MybatisConfig {
  7. @Bean
  8. public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
  9. SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
  10. factoryBean.setDataSource(dataSource);
  11. factoryBean.setTypeAliasesPackage("com.cacb.domain");
  12. return factoryBean;
  13. }
  14. @Bean
  15. public MapperScannerConfigurer mapperScannerConfigurer(){
  16. MapperScannerConfigurer msc = new MapperScannerConfigurer();
  17. msc.setBasePackage("com.cacb.dao");
  18. return msc;
  19. }
  20. }

                JdbcConfig

  1. package com.cacb.config;
  2. import com.alibaba.druid.pool.DruidDataSource;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.context.annotation.Bean;
  5. import javax.sql.DataSource;
  6. public class JdbcConfig {
  7. @Value("${jdbc.driver}")
  8. private String driver;
  9. @Value("${jdbc.url}")
  10. private String url;
  11. @Value("${jdbc.username}")
  12. private String username;
  13. @Value("${jdbc.password}")
  14. private String password;
  15. @Bean
  16. public DataSource dataSource(){
  17. DruidDataSource dataSource = new DruidDataSource();
  18. dataSource.setDriverClassName(driver);
  19. dataSource.setUrl(url);
  20. dataSource.setUsername(username);
  21. dataSource.setPassword(password);
  22. return dataSource;
  23. }
  24. }

                jbdc.properties

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql:"//localhost:3306/cary_cacb
  3. jdbc.username=
  4. jdbc.password=

        ③SpringMVC

                ServletConfig

  1. package com.cacb.config;
  2. import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
  3. public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
  4. @Override
  5. protected Class<?>[] getRootConfigClasses() {
  6. return new Class[]{SpringConfig.class};
  7. }
  8. @Override
  9. protected Class<?>[] getServletConfigClasses() {
  10. return new Class[]{SpringMvcConfig.class};
  11. }
  12. @Override
  13. protected String[] getServletMappings() {
  14. return new String[]{"/"};
  15. }
  16. }

                SpringMvcConfig

  1. package com.cacb.config;
  2. import org.springframework.context.annotation.ComponentScan;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  5. @Configuration
  6. @ComponentScan("com.cacb.controller")
  7. @EnableWebMvc
  8. public class SpringMvcConfig {
  9. }

3.功能模块

        表与实体块

  1. package com.cacb.domain;
  2. public class Book {
  3. private Integer id;
  4. private String type;
  5. private String name;
  6. private String description;
  7. public Integer getId() {
  8. return id;
  9. }
  10. public void setId(Integer id) {
  11. this.id = id;
  12. }
  13. public String getType() {
  14. return type;
  15. }
  16. public void setType(String type) {
  17. this.type = type;
  18. }
  19. public String getName() {
  20. return name;
  21. }
  22. public void setName(String name) {
  23. this.name = name;
  24. }
  25. public String getDescription() {
  26. return description;
  27. }
  28. public void setDescription(String description) {
  29. this.description = description;
  30. }
  31. @Override
  32. public String toString() {
  33. return "Book{" +
  34. "id=" + id +
  35. ", type='" + type + '\'' +
  36. ", name='" + name + '\'' +
  37. ", description='" + description + '\'' +
  38. '}';
  39. }
  40. }

        dao(接口+自动代理)

  1. package com.cacb.dao;
  2. import com.cacb.domain.Book;
  3. import org.apache.ibatis.annotations.Delete;
  4. import org.apache.ibatis.annotations.Insert;
  5. import org.apache.ibatis.annotations.Select;
  6. import org.apache.ibatis.annotations.Update;
  7. import java.util.List;
  8. public interface BookDao {
  9. @Insert("insert into tbl_book values(null,#{type},#{name},#{description})")
  10. public void save(Book book);
  11. @Update("update tbl_boook set type=#{type},name=#{name},description=#{description} where id = #{id}")
  12. public void update(Book book);
  13. @Delete("delete from tbl_book where id = #{id}")
  14. public void delete(Integer id);
  15. @Select("select * from tbl_book where id = #{id}")
  16. public Book getById(Integer id);
  17. @Select("select * from tbl_book")
  18. public List<Book> getAll();
  19. }

        service(接口+实现类)

  1. package com.cacb.service;
  2. import com.cacb.domain.Book;
  3. import java.util.List;
  4. public interface BookService {
  5. public boolean save(Book book);
  6. public boolean update(Book book);
  7. public boolean delete(Integer id);
  8. public Book getById(Integer id);
  9. public List<Book> getAll();
  10. }
  1. package com.cacb.service.impl;
  2. import com.cacb.dao.BookDao;
  3. import com.cacb.domain.Book;
  4. import com.cacb.service.BookService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import java.util.List;
  8. @Service
  9. public class BookServiceImpl implements BookService {
  10. @Autowired
  11. private BookDao bookDao;
  12. @Override
  13. public boolean save(Book book) {
  14. bookDao.save(book);
  15. return true;
  16. }
  17. @Override
  18. public boolean update(Book book) {
  19. bookDao.update(book);
  20. return true;
  21. }
  22. @Override
  23. public boolean delete(Integer id) {
  24. bookDao.delete(id);
  25. return true;
  26. }
  27. @Override
  28. public Book getById(Integer id) {
  29. bookDao.getById(id);
  30. return null;
  31. }
  32. @Override
  33. public List<Book> getAll() {
  34. bookDao.getAll();
  35. return null;
  36. }
  37. }

 

                业务层接口测试

  1. package com.cacb.service;
  2. import com.cacb.config.SpringConfig;
  3. import com.cacb.domain.Book;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.test.context.ContextConfiguration;
  8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  9. @RunWith(SpringJUnit4ClassRunner.class)
  10. @ContextConfiguration(classes = SpringConfig.class)
  11. public class BookServiceTest {
  12. @Autowired
  13. private BookService bookService;
  14. @Test
  15. public void testGetById(){
  16. Book book = bookService.getById(1);
  17. System.out.println(book);
  18. }
  19. }

        controller

  1. package com.cacb.controller;
  2. import com.cacb.domain.Book;
  3. import com.cacb.service.BookService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.*;
  6. import java.util.List;
  7. @RestController
  8. @RequestMapping("/books")
  9. public class BookController {
  10. @Autowired
  11. private BookService bookService;
  12. @PostMapping
  13. public boolean save(@RequestBody Book book) {
  14. return bookService.save(book);
  15. }
  16. @PutMapping
  17. public boolean update(@RequestBody Book book) {
  18. return bookService.update(book);
  19. }
  20. @DeleteMapping( "/{id}")
  21. public boolean delete(@PathVariable Integer id) {
  22. return bookService.delete(id);
  23. }
  24. @GetMapping("/{id}")
  25. public Book getById(@PathVariable Integer id) {
  26. return bookService.getById(id);
  27. }
  28. @GetMapping
  29. public List<Book> getAll() {
  30. return bookService.getAll();
  31. }
  32. }

                表现层接口测试

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/43534?site
推荐阅读
相关标签
  

闽ICP备14008679号