**# 安装**
- - JDK 8+
- - Maven or Gradle
## Spring Boot2
中心仓库地址 其他版本
https://central.sonatype.com/artifact/com.baomidou/mybatis-plus-boot-starter
Maven
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter<artifactId>
<version>3.5.9</version>
</dependency>
## Spring Boot3
中心仓库地址 其他版本
https://central.sonatype.com/artifact/com.baomidou/mybatis-plus-spring-boot3-starter/overview
Maven
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.9</version>
</dependency>
**# 配置**
**mybatis-plus:**
### config-location
指定 MyBatis 配置文件的位置。
mybatis-plus
config-location: classpath:/mybatis-config.xml #指定 MyBatis 配置文件的位置。
### mapper-locations
指定 Mapper 映射文件的位置。
mybatis-plus
mapper-locations: classpath:/mapper/**.xml #指定 Mapper 映射文件的位置。
配置多个mapper路径,逗号分隔
#mapper-locations: classpath:mapper/*.xml,classpath:mapper/other/*.xml
### type-aliases-package && type-aliases-super-type
指定实体类包位置。与type-aliases-super-type可以一起使用
mybatis-plus
type-aliases-package: com.your.domain #指定实体类包位置。
type-aliases-super-type: com.your.domain.BaseEntity #仅扫描指定父类的子类。
### type-handlers-package
指定 TypeHandler 扫描路径,用于注册自定义类型转换器。 如数据加密,数据格式转换等
mybatis-plus
type-handlers-package: com.example.demo.typeHandler #指定 TypeHandler 扫描路径,用于注册自定义类型转换器。
type-handlers-package使用 自定义TypeHandler
package com.example.demo.typeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;
import javax.xml.crypto.Data;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@MappedJdbcTypes({JdbcType.BIGINT}) //对应数据库类型
@MappedTypes({String.class}) //java数据类型
public class MyDateTypeHandler implements TypeHandler<String> {
/**
* 入库前的类型转换
*/
@Override
public void setParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
System.out.println("setParameter:" + parameter);
String s = "parameter";
System.out.println("转换后:" + s);
ps.setString(i, s);
}
/**
* 查询后的数据处理 根据列名获取返回结果
*/
@Override
public String getResult(ResultSet rs, String columnName) throws SQLException {
String originRes = rs.getString(columnName);
System.out.println("getResult:" + originRes);
return originRes+"";
}
/**
* 查询后的数据处理 根据列下标获取返回结果
*/
@Override
public String getResult(ResultSet rs, int columnIndex) throws SQLException {
return null;
}
/**
* 查询后的数据处理 根据列下标获取返回结果(存储过程)
*/
@Override
public String getResult(CallableStatement cs, int columnIndex) throws SQLException {
return null;
}
}
使用
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="">
<id column="id" property="id" />
<result column="name" property="name"/>
<result column="age" property="age" typeHandler="com.example.demo.typeHandler.MyDateTypeHandler" />
</resultMap>
<!-- 新增 -->
<insert id="insertEncrypt">
insert into account (id, name, age)
values (#{id}, #{name}, #{age, typeHandler=com.example.demo.typeHandler.MyDateTypeHandler})
</insert>
### check-config-location (spring boot专属)
指定启动时是否检查 MyBatis XML 文件的存在 默认不检查
mybatis-plus
check-config-location: true #指定启动时是否检查 MyBatis XML 文件的存在
### executor-type (spring boot专属)
指定 MyBatis 的执行器类型,包括 SIMPLE、REUSE 和 BATCH。
- SIMPLE是默认执行器,根据对应的sql直接执行,不会做一些额外的操作。
- REUSE 是重用执行器,可以重用预处理语句,可以提高批量操作的性能。
- BATCH是批量执行器,可以批量执行sql,可以提高批量操作的性能。
mybatis-plus
executor-type: reuse #指定 MyBatis 的执行器类型,包括 SIMPLE、REUSE 和 BATCH。
### configuration-properties
指定 MyBatis 配置属性文件,用于抽离配置,实现不同环境的配置部署。
mybatis-plus
configuration-properties: classpath:/mybatis-properties.properties #指定 MyBatis 配置属性文件,用于抽离配置。
1139

被折叠的 条评论
为什么被折叠?



