文章目录
- 前言
- 一.SSM整合流程
- 二.整合配置
- 2.1添加依赖
- 2.2创建项目包结构
- 2.3创建SpringConfig配置类
- 2.4创建JdbcConfig配置类
- 2.5创建MybatisConfig配置类
- 2.6创建jdbc.properties
- 2.7创建SpringMVC配置类
- 2.8创建Web项目入口配置类
- 三.功能模块
- 3.1创建模型类
- 3.2编写Dao接口
- 3.3编写Service接口和实现类
- 3.4编写Contorller类
- 四.单元测试
- 4.1新建测试类
- 4.2注入Service类
- 4.3编写测试方法
前言
框架用的好效率自然高!
🏳️🌈给大家推荐一个Java面试刷题网站牛客网,懒羊羊祝你早日成神:Java刷题面试宝藏网站
一.SSM整合流程
1.创建工程
- 创建一个Maven的web工程
- pom.xml添加SSM需要的依赖jar包
- 编写Web项目的入口配置类,实现
AbstractAnnotationConfigDispatcherServletInitializer重写以下方法- getRootConfigClasses() :返回Spring的配置类->需要SpringConfig配置类
- getServletConfigClasses() :返回SpringMVC的配置类->需要SpringMvcConfig配置类
- getServletMappings() : 设置SpringMVC请求拦截路径规则
- getServletFilters() :设置过滤器,解决POST请求中文乱码问题
2.SSM整合
Spring
| SpringConfig | Spring配置类 |
|---|---|
| 标识该类为配置类 | @Configuration |
| 扫描Service所在的包 | @ComponentScan |
| 在Service层要管理事务 | @EnableTransactionManagement |
| 读取外部的properties配置文件 | @PropertySource |
| 整合Mybatis需要引入Mybatis相关配置类 | @Import |
第三方数据源配置类 JdbcConfig
| 第三方数据源配置类 | JdbcConfig |
|---|---|
| 构建DataSource数据源,DruidDataSouroce | @Bean @Value |
| 构建平台事务管理器,DataSourceTransactionManager | @Bean |
Mybatis配置类 MybatisConfig
| 第三方数据源配置类 | JdbcConfig |
|---|---|
| 构建SqlSessionFactoryBean,设置别名扫描与数据源 | @Bean |
| 构建MapperScannerConfigurer并设置DAO层的包扫描 | @Bean |
SpringMVC配置类SpringMvcConfig
| SpringMVC配置类 | SpringMvcConfig |
|---|---|
| 标识该类为配置类 | @Configuration |
| 扫描Controller所在的包 | @Configuration |
| 开启SpringMVC注解支持 | @EnableWebMvc |
二.整合配置
2.1添加依赖
pom.xml添加SSM所需要的依赖jar包

2.2创建项目包结构
- config目录存放的是相关的配置类
- controller编写的是Controller类
- dao存放的是Dao接口,因为使用的是Mapper接口代理方式,所以没有实现类包
- service存的是Service接口,impl存放的是Service实现类
- resources:存入的是配置文件,如Jdbc.properties
- webapp:目录可以存放静态资源
- test/java:存放的是测试类

2.3创建SpringConfig配置类

2.4创建JdbcConfig配置类

2.5创建MybatisConfig配置类

2.6创建jdbc.properties

2.7创建SpringMVC配置类

2.8创建Web项目入口配置类

三.功能模块
3.1创建模型类
public class Book {private Integer id;private String type;private String name;private String description;}
3.2编写Dao接口
public interface BookDao {// @Insert("insert into tbl_book values(null,#{type},#{name},#{description})")@Insert("insert into tbl_book (type,name,description) values(#{type},#{name},#{description})")public void save(Book book);@Update("update tbl_book set type = #{type}, name = #{name}, description = #{description} where id = #{id}")public void update(Book book);@Delete("delete from tbl_book where id = #{id}")public void delete(Integer id);@Select("select * from tbl_book where id = #{id}")public Book getById(Integer id);@Select("select * from tbl_book")public List<Book> getAll();
}
3.3编写Service接口和实现类
@Transactional
public interface BookService {public boolean save(Book book);public boolean update(Book book);public boolean delete(Integer id);public Book getById(Integer id);public List<Book> getAll();
}
@Service
public class BookServiceImpl implements BookService {@Autowiredprivate BookDao bookDao;public boolean save(Book book) { bookDao.save(book); return true; }public boolean update(Book book) { bookDao.update(book); return true; }public boolean delete(Integer id) {bookDao.delete(id); return true; }public Book getById(Integer id) { return bookDao.getById(id); }public List<Book> getAll() { return bookDao.getAll(); }
}
3.4编写Contorller类
@RestController
@RequestMapping("/books")
public class BookController {@Autowiredprivate BookService bookService;@PostMappingpublic boolean save(@RequestBody Book book) {return bookService.save(book);}@PutMappingpublic boolean update(@RequestBody Book book) {return bookService.update(book);}@DeleteMapping("/{id}")public boolean delete(@PathVariable Integer id) {return bookService.delete(id);}@GetMapping("/{id}")public Book getById(@PathVariable Integer id) {return bookService.getById(id);}@GetMappingpublic List<Book> getAll() {return bookService.getAll();}
}
四.单元测试
4.1新建测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class BookServiceTest {}
4.2注入Service类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class BookServiceTest {@Autowiredprivate BookService bookService;
}
4.3编写测试方法
我们先来对查询进行单元测试。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class BookServiceTest {@Autowiredprivate BookService bookService;@Testpublic void testGetById(){Book book = bookService.getById(1);System.out.println(book);}@Testpublic void testGetAll(){List<Book> all = bookService.getAll();System.out.println(all);}}


















