03_整合jdbc和mybatis

张创琦 2022.03.11

1 整合jdbc

  1. 导入 tb_user.sql 文件
  2. 引入依赖和 mysql 数据库驱动
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-jdbc</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-test</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>mysql</groupId>
  11. <artifactId>mysql-connector-java</artifactId>
  12. <version>5.1.46</version>
  13. </dependency>
  1. 配置连接池

    1. Dependencies -> jdbc -> HikariCP (速度非常快的连接池)
  2. application.properties
  1. # 连接四大参数
  2. spring.datasource.url=jdbc:mysql://localhost:3306/springboot
  3. spring.datasource.username=root spring.datasource.password=123
  4. # 可省略,SpringBoot自动推断
  5. spring.datasource.driverClassName=com.mysql.jdbc.Driver
  6. spring.datasource.hikari.idle-timeout=60000
  7. spring.datasource.hikari.maximum-pool-size=30
  8. spring.datasource.hikari.minimum-idle=10
  1. com.lxs.domain. User.java
  1. public class User implements Serializable {
  2. private Long id;
  3. // 用户名
  4. //自动转换下换线到驼峰命名user_name -> userName
  5. private String userName;
  6. // 密 码
  7. private String password;
  8. // 姓 名
  9. private String name;
  10. // 年 龄
  11. private Integer age;
  12. // 性别,1男性,2女性
  13. private Integer sex;
  14. // 出生日期
  15. private Date birthday;
  16. // 创建时间
  17. private Date created;
  18. // 更新时间
  19. private Date updated;
  20. // 备 注
  21. private String note;
  22. // toString 和 getter setter 方法
  23. }
  1. com.lxs.dao. JdbcDao
  1. @Repository
  2. public class JdbcDao {
  3. @Autowired
  4. private JdbcTemplate jdbcTemplate;
  5. public List<User> findAll() {
  6. return jdbcTemplate.query("select * from tb_user", new BeanPropertyRowMapper<> (User.class));
  7. }
  8. }
  1. test.java.com.lxs.dao.JdbcDaoTest
  1. @RunWith(SpringRunner.class) @SpringBootTest
  2. public class JdbcDaoTest {
  3. @Autowired
  4. private JdbcDao jdbcDao;
  5. @Test
  6. public void findAll() {
  7. List<User> list = jdbcDao.findAll();
  8. for (User user : list) {
  9. System.out.println(user);
  10. }
  11. }
  12. }

测试结果:

  1. User{id=1, userName='zhangsan', password='1', name='张三', age=18, sex=1, birthday=2019-02-27 00:00:00.0, created=2019-02-27 00:00:00.0, updated=2019-02-27 00:00:00.0, note='在学习Java...'}
  2. User{id=2, userName='lisi', password='1', name='李四', age=18, sex=1, birthday=2019-02-27 00:00:00.0, created=2019-02-27 00:00:00.0, updated=2019-02-27 00:00:00.0, note='在学习Java...'}
  3. User{id=3, userName='wangwu', password='1', name='王五', age=18, sex=1, birthday=2019-02-27 00:00:00.0, created=2019-02-27 00:00:00.0, updated=2019-02-27 00:00:00.0, note='在学习Java...'}
  4. User{id=4, userName='fanbingbing', password='1', name='范冰冰', age=18, sex=2, birthday=2019-02-27 00:00:00.0, created=2019-02-27 00:00:00.0, updated=2019-02-27 00:00:00.0, note='在学习Java...'}
  5. User{id=5, userName='guodegang', password='1', name='郭德纲', age=18, sex=1, birthday=2019-02-27 00:00:00.0, created=2019-02-27 00:00:00.0, updated=2019-02-27 00:00:00.0, note='在学习Java...'}

2 整合mybatis

  1. 配置 pom.xml 文件
  1. <!--mybatis -->
  2. <dependency>
  3. <groupId>org.mybatis.spring.boot</groupId>
  4. <artifactId>mybatis-spring-boot-starter</artifactId>
  5. <version>1.3.2</version>
  6. </dependency
  1. application.properties
  1. # mybatis 别名扫描
  2. mybatis.type-aliases-package=com.lxs.domain
  3. # mapper.xml文件位置,如果没有映射文件,请注释掉
  4. mybatis.mapper-locations=classpath:mappers/*.xml
  1. 直接使用刚才 jdbc 用到的实体类

  2. 配置接口文件

  1. package com.lxs.dao;
  2. import com.lxs.domain.User;
  3. import java.util.List;
  4. //ctrl shift t 快速创建测试类
  5. // 6.1方法
  6. //@Mapper
  7. public interface UserDao {
  8. public List<User> findAll();
  9. }
  1. 映射文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.lxs.dao.UserDao">
  5. <select id="findAll" resultType="user">
  6. select * from tb_user
  7. </select>
  8. </mapper>
  1. Mapper的加载接口代理对象方式

    1. 在 Dao 接口使用 @Mapper 注解(不推荐)
    2. 在 Application.java 中设置 MapperScan, 注解扫描的包(推荐)
  1. @MapperScan("com.lxs.dao")
  2. public class Application {
  3. ...
  4. }

测试

  1. 引入测试构建
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-test</artifactId>
  4. </dependency>
  1. 测试代码
  1. package com.lxs.dao;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import org.springframework.test.context.junit4.SpringRunner;
  7. import static org.junit.Assert.*;
  8. @RunWith(SpringRunner.class)
  9. @SpringBootTest
  10. public class UserDaoTest {
  11. @Autowired
  12. private UserDao userDao; // 虽然报错, 但是可以运行成功...
  13. @Test
  14. public void findAll() {
  15. userDao.findAll().forEach(user -> {
  16. System.out.println(user);
  17. });
  18. }
  19. }

运行结果

  1. 2022-03-11 19:59:06.056 DEBUG 12600 --- [ main] com.lxs.dao.UserDao.findAll : ==> Preparing: select * from tb_user
  2. 2022-03-11 19:59:06.101 DEBUG 12600 --- [ main] com.lxs.dao.UserDao.findAll : ==> Parameters:
  3. 2022-03-11 19:59:06.139 DEBUG 12600 --- [ main] com.lxs.dao.UserDao.findAll : <== Total: 5
  4. User{id=1, userName='null', password='1', name='张三', age=18, sex=1, birthday=Wed Feb 27 00:00:00 CST 2019, created=Wed Feb 27 00:00:00 CST 2019, updated=Wed Feb 27 00:00:00 CST 2019, note='在学习Java...'}
  5. User{id=2, userName='null', password='1', name='李四', age=18, sex=1, birthday=Wed Feb 27 00:00:00 CST 2019, created=Wed Feb 27 00:00:00 CST 2019, updated=Wed Feb 27 00:00:00 CST 2019, note='在学习Java...'}
  6. User{id=3, userName='null', password='1', name='王五', age=18, sex=1, birthday=Wed Feb 27 00:00:00 CST 2019, created=Wed Feb 27 00:00:00 CST 2019, updated=Wed Feb 27 00:00:00 CST 2019, note='在学习Java...'}
  7. User{id=4, userName='null', password='1', name='范冰冰', age=18, sex=2, birthday=Wed Feb 27 00:00:00 CST 2019, created=Wed Feb 27 00:00:00 CST 2019, updated=Wed Feb 27 00:00:00 CST 2019, note='在学习Java...'}
  8. User{id=5, userName='null', password='1', name='郭德纲', age=18, sex=1, birthday=Wed Feb 27 00:00:00 CST 2019, created=Wed Feb 27 00:00:00 CST 2019, updated=Wed Feb 27 00:00:00 CST 2019, note='在学习Java...'}