环境搭建

创建SpringBoot项目与整合JDBC一致,使用Druid数据源。pom.xml加入MyBatis依赖。

image-20200404152014577

1
2
3
4
5
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>

配置数据库连接信息,使用Druid数据源,与其不变。

image-20200404152223659

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
spring:
datasource:
username: root
password: kylin
# 假如时区报错了,就增加一个时区的配置
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource


#Spring Boot 默认是不注入这些属性值的,需要自己绑定
#druid 数据源专有配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true

#配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
#如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority
#则导入 log4j 依赖即可,Maven 地址: https://mvnrepository.com/artifact/log4j/log4j
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

image-20200404153854304

给数据库建表,导入department.sql和employee.sql。SpringBoot连接数据库之后自动建表。首先需要再SpringBoot文件中配置两张建表sql的位置

image-20200404155531606

SpringBoot2.0x版本自动运行建表sql,必须配置参数initialization-mode: always

运行项目后,就会自动运行Sql语句。进行建表操作

image-20200404152633313

image-20200404155952056

image-20200404160007955

为了每次运行此项目都自动运行建表语句,我们把他给注释掉

image-20200404160115063

创建相对应的JavaBean

image-20200404152809902

image-20200404152837076

注解版

使用MyBatis注解来进行对数据库的操作

image-20200404153344201

image-20200404153533402

由于没有Mybatis的配置文件。而数据库表中的d_id,department_name字段与bean中不一致。所以需要配置mybatis自动驼峰命名转换。

image-20200404154050880

image-20200404154107205

image-20200404154257033

image-20200404154315556

自定义MyBatis的配置规则,给容器中添加一个ConfigurationCustomizer。相当于mybatis.xml配置文件

image-20200404154600019

configuration.setMapUnderscoreToCamelCase(true);开启自动驼峰命名转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Configuration
public class MyBatisConfig {

@Bean
public ConfigurationCustomizer configurationCustomizer(){
return new ConfigurationCustomizer(){
@Override
public void customize(org.apache.ibatis.session.Configuration configuration) {
//开启自动驼峰命名转换 department_name
configuration.setMapUnderscoreToCamelCase(true);
}
};
}
}

编写controller

image-20200404153627933

测试插入

image-20200404163022843

image-20200404163042399

由于id是自增主键,我们插入时是没有Id的值。/dept?departmentName=xx所以获取到的department对象中的Id时null,而在数据库中id自增。

image-20200404163632051

这时我们可以在Mapper中使用@Options注解@Options(useGeneratedKeys =true,keyProperty = "id")插入完成后,主键会重新封装进来。

image-20200404163938417

再次测试插入

image-20200404164048023

测试通过Id获取对象

image-20200404164133304

通过注解版整合Mybatis成功!!

配置文件版

首先在SpringBoot配置文件中配置mybatis和mapper配置文件所在位置。

image-20200404164438360

mybatis配置文件

image-20200404164546726

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!--开启驼峰命名-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<package name="com.kylin.bean"/>
</typeAliases>
</configuration>

创建Mapper接口

image-20200404164651796

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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.kylin.mapper.EmployeeMapper">
<select id="getEmpById" resultType="employee">
select * from employee where id = #{id}
</select>

<insert id="insertEmp">
insert into employee(lastName,email,gender,d_id) values (#{lastName},#{email},#{gender},#{dId})
</insert>
</mapper>

编写Mapper.xml

image-20200404164801220

编写Controller

image-20200404170143760

测试插入

image-20200404170247354

与上面一样的原因,导致id为null。我们在mapper配置文件中配置属性userGeneratedKeys="true"keyProperty="id"

image-20200404170734140

image-20200404170814055

image-20200404170833620

测试查询

image-20200404170911606

通过配置文件版整合Mybatis成功!!

两种方式可以混合使用!!