四、Spring 整合
1.Spring 整合 Junit
1.1新建项目结构
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-C5NO1Cbv-1691041833897)(…/…/Lombok/笔记/picture/image-20221029161945629.png)]
1.2导入依赖
-
导入 junit 与 Spring 依赖
<!-- 添加 spring 依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.17.RELEASE</version> </dependency> <!-- 添加 junit 依赖--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- 添加 spring test 整合依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.7.RELEASE</version> </dependency>
1.3正常的写 spring 代码
-
添加 mapper
@Repository public class StudentMapperImpl implements StudentMapper { public void save() { System.out.println("保存操作"); } }
-
添加配置类
@Configuration @ComponentScan("cn.sycoder") public class SpringConfig { }
1.4通过测试类拿去mapper
-
先建立测试类
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {SpringConfig.class}) public class StudentTest { @Autowired private StudentMapper studentMapper; @Test public void testStudentMapper(){ studentMapper.save(); } }
1.5@Runwith
说明 | @Runwith |
---|---|
位置 | 测试类上方 |
作用 | 测试类注解,设置 junit 运行器的 |
属性 | 使用测试运行的环境,SpringJUnit4ClassRunner.class |
1.5@ContextConfiguration
说明 | @ContextConfiguration |
---|---|
位置 | 测试类上方 |
作用 | 设置 junit 加载 spring 配置类 |
属性 | classes:核心配置类,可以传入多个 locations:配置文件的文件路径名称 |
2.Spring整合JdbcTemplate
- 概述:jdbcTemplate 是 spring 对于学过的 jdbc 的封装,实现了方便操作数据库
2.1导入依赖
-
导入依赖
<!-- 添加 orm 依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>5.2.17.RELEASE</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.2.8</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.22</version> </dependency>
2.2建立 jdbc.propreties
-
创建配置文件
jdbc.username=root jdbc.password=123456 jdbc.driverClassName=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis
2.3配置druid 连接池
-
配置类
@Configuration @PropertySource("jdbc.properties") public class DruidConfig { @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; @Value("${jdbc.url}") private String url; @Value("${jdbc.driverClassName}") private String driver; @Bean public DataSource dataSource(){ final DruidDataSource source = new DruidDataSource(); source.setUsername(username); source.setPassword(password); source.setDriverClassName(driver); source.setUrl(url); return source; } }
-
配置 jdbcTemplate 类
@Configuration @PropertySource("jdbc.properties") public class DruidConfig { @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; @Value("${jdbc.url}") private String url; @Value("${jdbc.driverClassName}") private String driver; @Bean public DataSource dataSource(){ final DruidDataSource source = new DruidDataSource(); source.setUsername(username); source.setPassword(password); source.setDriverClassName(driver); source.setUrl(url); return source; } @Bean public JdbcTemplate jdbcTemplate(DataSource dataSource){ JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); return jdbcTemplate; } }
2.4修改 mapper 实现类做保存和查询操作
-
修改保存
@Repository public class StudentMapperImpl implements StudentMapper { @Autowired private JdbcTemplate template; public void save() { String sql = "insert into student value(null,?,?)"; template.update(sql,"sy",18); } public Student getById(Long id) { Object student = template.queryForObject("select * from student where id = ?", new BeanPropertyRowMapper(Student.class), id); return (Student) student; // return null; } }