• SSM整合流程(整合配置、功能模块开发、接口测试)


    目录

    一、创建新的maven工程

    创建相应的功能模块

    添加相应的jar包

     二、SSM整合

    Spring:

    SpringConfig配置类

    MyBatis:

    MybatisConfig配置类

    JdbcConfig配置类

    jdbc.properties配置文件

    SpringMVC:

    ServletConfig配置类

    SpringMvcConfig配置类

    三、功能模块

    表与实体类:

    MySQL数据表字段及数据

    Book实体类

    dao(接口+自动代理):

    BookDao接口

    service(接口+实现类):

    BookService接口

    BookServiceImpl实现类

    基于Restful的Controller开发

    三、接口测试

    业务层接口测试(整合JUnit):

    BookServiceTest测试类

    表现层接口测试(PostMan):

    保存图书

    修改图书

    删除图书

    查询单个图书

    查询所有图书


    一、创建新的maven工程

    创建相应的功能模块

    添加相应的jar包

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0</modelVersion>
    5. <groupId>com.itheima</groupId>
    6. <artifactId>springmvc_08_ssm</artifactId>
    7. <version>1.0-SNAPSHOT</version>
    8. <packaging>war</packaging>
    9. <dependencies>
    10. <dependency>
    11. <groupId>org.springframework</groupId>
    12. <artifactId>spring-webmvc</artifactId>
    13. <version>5.2.10.RELEASE</version>
    14. </dependency>
    15. <dependency>
    16. <groupId>org.springframework</groupId>
    17. <artifactId>spring-jdbc</artifactId>
    18. <version>5.2.10.RELEASE</version>
    19. </dependency>
    20. <dependency>
    21. <groupId>org.springframework</groupId>
    22. <artifactId>spring-test</artifactId>
    23. <version>5.2.10.RELEASE</version>
    24. </dependency>
    25. <dependency>
    26. <groupId>org.mybatis</groupId>
    27. <artifactId>mybatis</artifactId>
    28. <version>3.5.6</version>
    29. </dependency>
    30. <dependency>
    31. <groupId>org.mybatis</groupId>
    32. <artifactId>mybatis-spring</artifactId>
    33. <version>1.3.0</version>
    34. </dependency>
    35. <dependency>
    36. <groupId>mysql</groupId>
    37. <artifactId>mysql-connector-java</artifactId>
    38. <version>5.1.47</version>
    39. </dependency>
    40. <dependency>
    41. <groupId>com.alibaba</groupId>
    42. <artifactId>druid</artifactId>
    43. <version>1.1.16</version>
    44. </dependency>
    45. <dependency>
    46. <groupId>junit</groupId>
    47. <artifactId>junit</artifactId>
    48. <version>4.12</version>
    49. <scope>test</scope>
    50. </dependency>
    51. <dependency>
    52. <groupId>javax.servlet</groupId>
    53. <artifactId>javax.servlet-api</artifactId>
    54. <version>3.1.0</version>
    55. <scope>provided</scope>
    56. </dependency>
    57. <dependency>
    58. <groupId>com.fasterxml.jackson.core</groupId>
    59. <artifactId>jackson-databind</artifactId>
    60. <version>2.9.0</version>
    61. </dependency>
    62. </dependencies>
    63. <build>
    64. <plugins>
    65. <plugin>
    66. <groupId>org.apache.tomcat.maven</groupId>
    67. <artifactId>tomcat7-maven-plugin</artifactId>
    68. <version>2.1</version>
    69. <configuration>
    70. <port>80</port>
    71. <path>/</path>
    72. </configuration>
    73. </plugin>
    74. </plugins>
    75. </build>
    76. </project>

     二、SSM整合

    Spring:

    SpringConfig配置类

    1. import org.springframework.context.annotation.ComponentScan;
    2. import org.springframework.context.annotation.Configuration;
    3. import org.springframework.context.annotation.Import;
    4. import org.springframework.context.annotation.PropertySource;
    5. import org.springframework.transaction.annotation.EnableTransactionManagement;
    6. @Configuration
    7. @ComponentScan({"com.itheima.service"})
    8. @PropertySource("classpath:jdbc.properties")
    9. @Import({JdbcConfig.class,MyBatisConfig.class})
    10. @EnableTransactionManagement
    11. public class SpringConfig {
    12. }

    MyBatis:

    MybatisConfig配置类

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

    JdbcConfig配置类

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

    jdbc.properties配置文件

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

    SpringMVC:

    ServletConfig配置类

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

    SpringMvcConfig配置类

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

    三、功能模块

    表与实体类:

    MySQL数据表字段及数据

     Book实体类

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

    dao(接口+自动代理):

    BookDao接口

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

    service(接口+实现类):

    BookService接口

    1. import com.itheima.domain.Book;
    2. import org.springframework.transaction.annotation.Transactional;
    3. import java.util.List;
    4. @Transactional
    5. public interface BookService {
    6. /**
    7. * 保存
    8. * @param book
    9. * @return
    10. */
    11. public boolean save(Book book);
    12. /**
    13. * 修改
    14. * @param book
    15. * @return
    16. */
    17. public boolean update(Book book);
    18. /**
    19. * 按id删除
    20. * @param id
    21. * @return
    22. */
    23. public boolean delete(Integer id);
    24. /**
    25. * 按id查询
    26. * @param id
    27. * @return
    28. */
    29. public Book getById(Integer id);
    30. /**
    31. * 查询全部
    32. * @return
    33. */
    34. public List<Book> getAll();
    35. }

    BookServiceImpl实现类

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

    基于Restful的Controller开发

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

    三、接口测试

    业务层接口测试(整合JUnit):

    BookServiceTest测试类

    1. import com.itheima.config.SpringConfig;
    2. import com.itheima.domain.Book;
    3. import org.junit.Test;
    4. import org.junit.runner.RunWith;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.test.context.ContextConfiguration;
    7. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    8. import java.util.List;
    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. @Test
    20. public void testGetAll(){
    21. List<Book> all = bookService.getAll();
    22. System.out.println(all);
    23. }
    24. }

     测试结果

    表现层接口测试(PostMan):

    保存图书

     PostMan及MySQL变化

     修改图书

      PostMan及MySQL变化

     删除图书

      PostMan及MySQL变化

     查询单个图书

       PostMan变化

     查询所有图书

     

  • 相关阅读:
    linux 下 rm 为什么要这么写?
    C语言实现——简易通讯录
    Hudi查询类型/视图总结
    大数据必学Java基础(九十七):事务及回滚点
    day06-Flex布局
    【Oracle】调用HTTP接口
    KubeEdge v1.15.0发布!新增5大特性
    多目标权重融合方式
    【DevOps核心理念基础】1. 什么是 devops
    Java8 list.stream()操作使用心得
  • 原文地址:https://blog.csdn.net/m0_61961937/article/details/125529208