Spring Boot 整合 MyBatis
2023年11月17日大约 4 分钟约 870 字
应用实例
修改 application.yml 注释掉
# 配置druid和监控功能
# druid:
# stat-view-servlet:
# enabled: true
# login-username: jack
# login-password: 123
# reset-enable: false
# web-stat-filter: #配置web监控
# enabled: true
# url-pattern: /*
# exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
# filter:
# stat: #sql监控
# slow-sql-millis: 1000
# log-slow-sql: true
# enabled: true
# wall: #配置sql防火墙
# enabled: true
# config:
# drop-table-allow: false # 不允许删除表
# select-all-column-allow: false # 不允许查询所有字段的语句
将 DruidDataSourceConfig.java 注释打开
创建数据库和表
CREATE DATABASE `springboot_mybatis`
use `springboot_mybatis`
CREATE TABLE `monster` (`id` INT NOT NULL AUTO_INCREMENT,
`age` INT NOT NULL,
`birthday` DATE DEFAULT NULL,
`email` VARCHAR(255) DEFAULT NULL,
`gender` char(1) DEFAULT NULL,
`name` VARCHAR(255) DEFAULT NULL,
`salary` DOUBLE NOT NULL,
PRIMARY KEY (`id`)
) CHARSET=utf8
SELECT * FROM `monster`
insert into monster values(null, 20, '2000-11-11', 'nmw@sohu.com', '男', '牛魔王', 5000.88);
insert into monster values(null, 10, '2011-11-11', 'bgj@sohu.com', '女', '白骨精', 8000.88);
创建Maven项目
修改 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lzw</groupId>
<artifactId>springboot-mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<!--导入springboot父工程-规定写法-->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.5.3</version>
</parent>
<!--引入相关的依赖-->
<dependencies>
<!--引入web starter-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入mybatis starter-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!--引入mysql驱动: 这使用版本仲裁 8.0.26-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--引入配置处理器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<!--引入lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--引入test starter-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--引入druid依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.17</version>
</dependency>
</dependencies>
</project>
创建 src/main/resources/application.yml
server:
port: 9090
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot_mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456
创建 src/main/java/com/lzw/springboot/mybatis/Application.java
package com.lzw.springboot.mybatis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author LiAng
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
创建 src/main/java/com/lzw/springboot/mybatis/config/DruidDataSourceConfig.java
package com.lzw.springboot.mybatis.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.sql.SQLException;
/**
* @author LiAng
*/
@Configuration
public class DruidDataSourceConfig {
@ConfigurationProperties("spring.datasource")
@Bean
public DataSource dataSource() throws SQLException {
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
}
}
创建 src/test/java/com/lzw/springboot/mybatis/ApplicationTest.java
package com.lzw.springboot.mybatis;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.annotation.Resource;
/**
* @author LiAng
*/
@SpringBootTest
public class ApplicationTest {
@Resource
private JdbcTemplate jdbcTemplate;
@Test
public void t1(){
//输出。看下当前的数据源
System.out.println(jdbcTemplate.getDataSource().getClass());
}
}
测试看看数据源

创建 src/main/java/com/lzw/springboot/mybatis/bean/Monster.java
package com.lzw.springboot.mybatis.bean;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.util.Date;
/**
* @author LiAng
*/
@Data
public class Monster {
private Integer id;
private Integer age;
private Date birthday;
private String email;
private String name;
private String gender;
private Double salary;
}
创建 src/main/java/com/lzw/springboot/mybatis/mapper/MonsterMapper.java
package com.lzw.springboot.mybatis.mapper;
import com.lzw.springboot.mybatis.bean.Monster;
import org.apache.ibatis.annotations.Mapper;
/**
* @author LiAng
* 在Mapper接口使用 @Mapper 就会扫描,并将Mapper接口对象注入
*/
@Mapper
public interface MonsterMapper {
//根据id返回Monster对象
public Monster getMonsterById(Integer id);
}
创建 src/main/resources/mapper/MonsterMapper.xml
<?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.lzw.springboot.mybatis.mapper.MonsterMapper">
<!--配置getMonsterById-->
<select id="getMonsterById" resultType="com.lzw.springboot.mybatis.bean.Monster">
SELECT * FROM `monster` WHERE id=#{id}
</select>
</mapper>
修改 application.yml
server:
port: 9090
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot_mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456
mybatis:
# 指定要扫描的 mapper.xml
mapper-locations: classpath:mapper/*.xml
修改 ApplicationTest.java
@SpringBootTest
public class ApplicationTest {
@Resource
private MonsterMapper monsterMapper;
//测试MonsterMapper接口
@Test
public void getMonsterById(){
Monster monster = monsterMapper.getMonsterById(1);
System.out.println(monster);
}
}
测试


注意事项和细节说明
spring boot 整合 mybatis 取出的日期,出现 8 小时时差解决方案

修改 Monster.java
//这里通过注解来解决时区问题
//GMT 就是格林尼治标准时间
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date birthday;
