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/springbootspring.datasource.username=root spring.datasource.password=123# 可省略,SpringBoot自动推断spring.datasource.driverClassName=com.mysql.jdbc.Driverspring.datasource.hikari.idle-timeout=60000spring.datasource.hikari.maximum-pool-size=30spring.datasource.hikari.minimum-idle=10
- com.lxs.domain. User.java
public class User implements Serializable {private Long id;// 用户名//自动转换下换线到驼峰命名user_name -> userNameprivate 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
@Repositorypublic class JdbcDao {@Autowiredprivate 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) @SpringBootTestpublic class JdbcDaoTest {@Autowiredprivate JdbcDao jdbcDao;@Testpublic 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方法//@Mapperpublic interface UserDao {public List<User> findAll();}
- 映射文件
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapperPUBLIC "-//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)@SpringBootTestpublic class UserDaoTest {@Autowiredprivate UserDao userDao; // 虽然报错, 但是可以运行成功...@Testpublic 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_user2022-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: 5User{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...'}
