五、SSM整合
1.整合Mybatis
1.1springmvc 整合回顾
-
导入坐标
<dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.2.17.RELEASE</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.6</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.0</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.29</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.16</version></dependency>
-
创建数据库配置文件
jdbc.url=jdbc:mysql://localhost:3306/ssm jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.username=root jdbc.password=123456
-
提供jdbcconfig
public class DbConfig {@Value("${jdbc.url}")private String url;@Value("${jdbc.driver}")private String driver;@Value("${jdbc.username}")private String username;@Value("${jdbc.password}")private String password;/*** 配置德鲁伊连接池* @return*/@Beanpublic DataSource dataSource(){DruidDataSource source = new DruidDataSource();source.setUrl(url);source.setDriverClassName(driver);source.setPassword(password);source.setUsername(username);return source;}@Beanpublic PlatformTransactionManager transactionManager(DataSource dataSource){DataSourceTransactionManager manager = new DataSourceTransactionManager();manager.setDataSource(dataSource);return manager;}}
-
springconfig
@Configuration @ComponentScan(value = {"cn.sycoder.service","cn.sycoder.dao"}) @EnableTransactionManagement @PropertySource("classpath:db.properties") @Import({DbConfig.class,MybatisConfig.class}) public class SpringConfig { }
-
mybatis 交给spring管理
public class MybatisConfig {@Beanpublic SqlSessionFactoryBean sessionFactoryBean(DataSource dataSource){SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setTypeAliasesPackage("cn.sycoder.domain");return bean;}@Beanpublic MapperScannerConfigurer mapperScannerConfigurer(){MapperScannerConfigurer configurer = new MapperScannerConfigurer();configurer.setBasePackage("cn.sycoder.dao");return configurer;} }
1.2SpringBoot整合Mybatis
1.2.1创建模块
-
创建模块并填入基础信息
-
添加依赖
-
等价于手动添加配置依赖
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.2</version> </dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope> </dependency>
-
1.2.2添加配置
-
添加mysql配置
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/springboot_ssmusername: rootpassword: 123456
1.2.3创建mapper并且测试
-
创建 domain
@Data public class Item {private Long id;private String name;private String remark; }
-
创建 mapper
@Mapper public interface ItemMapper {@Insert("insert into item(name,remark) value(#{name},#{remark})")void insert(Item item);@Select("select * from item where id = #{id}")public Item getById(Long id); }
-
测试验证
@SpringBootTest class SpringbootSsmApplicationTests {@AutowiredItemMapper mapper;@Testvoid contextLoads() {Item item = new Item();item.setName("上云 javase 精讲");item.setRemark("课程免费,知识全面");mapper.insert(item);System.out.println(mapper.getById(1L));}}
1.2.4总结
- 使用SpringBoot 整合真的太方便了
- 需要添加 mybatis 的依赖也即mybatis-spring-boot-starter
- @Mapper 将Mapper 映射交给容器管理
- 如果有下划线你觉得难受,添加 @Repository就可以解决(不解决也行)
2.整合Druid
2.1目前使用的数据连接池
-
默认springboot会给我们使用 Hikari 连接池
-
整合德鲁伊
- 导入对应starter
- 修改配置即可
2.2导入依赖
-
导入依赖
<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.15</version> </dependency>
2.3修改配置
-
配置如下
spring:datasource:druid:url: jdbc:mysql://localhost:3306/springboot_ssmusername: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driver
-
日志查看
-
配置总结
- 导入Druid starter
- 提供配置文件
3.整合JUnit
-
导入依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope> </dependency>
-
传统方式
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = SpringConfig.class) public class ItemTest {@AutowiredIItemService service;@Testpublic void save(){Item item = new Item();item.setName("单元测试");item.setRemark("单元测试");item.setType("单元测试");boolean save = service.save(item);System.out.println(save);} }
3.1@SpringBootTest
-
现在的使用
@SpringBootTest class SpringbootSsmApplicationTests {@AutowiredItemMapper mapper;@Testvoid contextLoads() {Item item = new Item();item.setName("上云 javase 精讲");item.setRemark("课程免费,知识全面");mapper.insert(item);System.out.println(mapper.getById(1L));}}
4.整合其它总结
- 导入对应的 starter
- 需要写配置的提供配置