一、pom引入
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.0.1-jre</version>
</dependency>
二、yml配置
# 数据源配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://120.77.241.70:3306/test?characterEncoding=utf-8&useSSL=false
username: root
password: 123456
# mybatis 配置
mybatis:
# xml文件
mapper-locations: classpath:mapper/*.xml
# type-aliases-package: com.afei.mybatis.model
# 驼峰
configuration:
map-underscore-to-camel-case: true
# type转换器配置
type-handlers-package: com.afei.mybatis.interception
三、实现
1、启动类增加扫描mapper接口注解
@MapperScan("com.afei.mybatis.mapper")
2、编写 xml、mapper接口
<?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.afei.mybatis.mapper.EmpMapper">
<resultMap id="map" type="com.afei.mybatis.model.Emp" >
<id column="id" property="id" />
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="age" property="age" jdbcType="INTEGER"/>
<result column="status" property="status" jdbcType="VARCHAR" typeHandler="com.afei.mybatis.interception.EmpStatusEnumTypeHandler"/>
</resultMap>
<!-- typeHandler设置 -->
<insert id="insertEmp">
insert into emp(name,age,status) values(#{name},#{age},#{status,typeHandler=com.afei.mybatis.interception.EmpStatusEnumTypeHandler})
</insert>
<select id="selectList" resultMap="map">
SELECT * FROM emp
</select>
</mapper>
public interface EmpMapper {
List<Emp> selectList();
void insertEmp(Emp emp);
}