03_整合jdbc和mybatis
张创琦 2022.03.11
1 整合jdbc
- 导入 tb_user.sql 文件
- 引入依赖和 mysql 数据库驱动
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
配置连接池
- Dependencies -> jdbc -> HikariCP (速度非常快的连接池)
- application.properties
# 连接四大参数
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=root spring.datasource.password=123
# 可省略,SpringBoot自动推断
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.hikari.idle-timeout=60000
spring.datasource.hikari.maximum-pool-size=30
spring.datasource.hikari.minimum-idle=10
- com.lxs.domain. User.java
public class User implements Serializable {
private Long id;
// 用户名
//自动转换下换线到驼峰命名user_name -> userName
private String userName;
// 密 码
private String password;
// 姓 名
private String name;
// 年 龄
private Integer age;
// 性别,1男性,2女性
private Integer sex;
// 出生日期
private Date birthday;
// 创建时间
private Date created;
// 更新时间
private Date updated;
// 备 注
private String note;
// toString 和 getter setter 方法
}
- com.lxs.dao. JdbcDao
@Repository
public class JdbcDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<User> findAll() {
return jdbcTemplate.query("select * from tb_user", new BeanPropertyRowMapper<> (User.class));
}
}
- test.java.com.lxs.dao.JdbcDaoTest
@RunWith(SpringRunner.class) @SpringBootTest
public class JdbcDaoTest {
@Autowired
private JdbcDao jdbcDao;
@Test
public void findAll() {
List<User> list = jdbcDao.findAll();
for (User user : list) {
System.out.println(user);
}
}
}
测试结果:
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...'}
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...'}
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...'}
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...'}
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
- 配置 pom.xml 文件
<!--mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency
- application.properties
# mybatis 别名扫描
mybatis.type-aliases-package=com.lxs.domain
# mapper.xml文件位置,如果没有映射文件,请注释掉
mybatis.mapper-locations=classpath:mappers/*.xml
直接使用刚才 jdbc 用到的实体类
配置接口文件
package com.lxs.dao;
import com.lxs.domain.User;
import java.util.List;
//ctrl shift t 快速创建测试类
// 6.1方法
//@Mapper
public interface UserDao {
public List<User> findAll();
}
- 映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lxs.dao.UserDao">
<select id="findAll" resultType="user">
select * from tb_user
</select>
</mapper>
Mapper的加载接口代理对象方式
- 在 Dao 接口使用 @Mapper 注解(不推荐)
- 在 Application.java 中设置 MapperScan, 注解扫描的包(推荐)
@MapperScan("com.lxs.dao")
public class Application {
...
}
测试
- 引入测试构建
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
- 测试代码
package com.lxs.dao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserDaoTest {
@Autowired
private UserDao userDao; // 虽然报错, 但是可以运行成功...
@Test
public void findAll() {
userDao.findAll().forEach(user -> {
System.out.println(user);
});
}
}
运行结果
2022-03-11 19:59:06.056 DEBUG 12600 --- [ main] com.lxs.dao.UserDao.findAll : ==> Preparing: select * from tb_user
2022-03-11 19:59:06.101 DEBUG 12600 --- [ main] com.lxs.dao.UserDao.findAll : ==> Parameters:
2022-03-11 19:59:06.139 DEBUG 12600 --- [ main] com.lxs.dao.UserDao.findAll : <== Total: 5
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...'}
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...'}
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...'}
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...'}
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...'}