【MybatisPlus】SpringBoot3整合MybatisPlus 目录一、依赖二、yml 配置三、xml 配置四、config 配置五、使用实体类Mapper.javaService.javaServiceImpl.javaMapper.xml六、逻辑删除一、依赖!-- springboot3 / mybatis-plus 配置mybatis使用 3.5.16 版本避免版本冲突 -- dependency groupIdorg.mybatis/groupId artifactIdmybatis/artifactId version3.5.16/version /dependency dependency groupIdcom.baomidou/groupId artifactIdmybatis-plus-spring-boot3-starter/artifactId version${mybatis-plus-spring-boot3.version}/version /dependency dependency groupIdcom.baomidou/groupId artifactIdmybatis-plus-jsqlparser/artifactId version${mybatis-plus-spring-boot3.version}/version /dependencymybatis-plus-spring-boot3.version3.5.10/mybatis-plus-spring-boot3.version二、yml 配置application.yml# MyBatis配置 #mybatis: # # 搜索指定包别名 # typeAliasesPackage: com.leslie.**.domain # # 配置mapper的扫描找到所有的mapper.xml映射文件 # mapperLocations: classpath*:mapper/**/*Mapper.xml # # 加载全局的配置文件 # configLocation: classpath:mybatis/mybatis-config.xml # mybatis-plus配置 mybatis-plus: # 配置mapper的扫描找到所有的mapper.xml映射文件 mapper-locations: classpath*:mapper/**/*Mapper.xml # 搜索指定包别名多个用【;】分隔 type-aliases-package: com.leslie.**.domain; global-config: #数据库相关配置 db-config: #主键类型 AUTO:数据库ID自增, INPUT:用户输入ID, ID_WORKER:全局唯一ID (数字类型唯一ID), UUID:全局唯一ID UUID; id-type: AUTO logic-delete-value: 1 # 逻辑删除时is_delete 为删除的值 logic-not-delete-value: 0 # 逻辑删除时is_delete 为未删除的值 banner: false #原生配置 configuration: map-underscore-to-camel-case: true cache-enabled: false call-setters-on-nulls: true # log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #日志输出 # PageHelper分页插件 pagehelper: helperDialect: mysql supportMethodsArguments: true params: countcountSql三、xml 配置mybatis-config.xml?xml version1.0 encodingUTF-8 ? !DOCTYPE configuration PUBLIC -//mybatis.org//DTD Config 3.0//EN http://mybatis.org/dtd/mybatis-3-config.dtd configuration !-- 全局参数 -- settings !-- 使全局的映射器启用或禁用缓存 -- setting namecacheEnabled valuetrue / !-- 允许JDBC 支持自动生成主键 -- setting nameuseGeneratedKeys valuetrue / !-- 配置默认的执行器.SIMPLE就是普通执行器;REUSE执行器会重用预处理语句(prepared statements);BATCH执行器将重用语句并执行批量更新 -- setting namedefaultExecutorType valueSIMPLE / !-- 指定 MyBatis 所用日志的具体实现 -- setting namelogImpl valueSLF4J / !-- 使用驼峰命名法转换字段 -- !-- setting namemapUnderscoreToCamelCase valuetrue/ -- /settings /configuration四、config 配置MybatisPlusConfig.javaimport com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.annotation.EnableTransactionManagement; /** * Mybatis Plus 配置 */ EnableTransactionManagement(proxyTargetClass true) Configuration public class MybatisPlusConfig { Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor new MybatisPlusInterceptor(); // 分页插件 interceptor.addInnerInterceptor(paginationInnerInterceptor()); // 乐观锁插件 interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor()); // 阻断插件 interceptor.addInnerInterceptor(blockAttackInnerInterceptor()); return interceptor; } /** * 分页插件自动识别数据库类型 a hrefhttps://baomidou.com/guide/interceptor-pagination.html.../a */ public PaginationInnerInterceptor paginationInnerInterceptor() { PaginationInnerInterceptor paginationInnerInterceptor new PaginationInnerInterceptor(); // 设置数据库类型为mysql paginationInnerInterceptor.setDbType(DbType.MYSQL); // 设置最大单页限制数量默认 500 条-1 不受限制 paginationInnerInterceptor.setMaxLimit(-1L); return paginationInnerInterceptor; } /** * 乐观锁插件 a hrefhttps://baomidou.com/guide/interceptor-optimistic-locker.html.../a */ public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() { return new OptimisticLockerInnerInterceptor(); } /** * 如果是对全表的删除或更新操作就会终止该操作 a hrefhttps://baomidou.com/guide/interceptor-block-attack.html.../a */ public BlockAttackInnerInterceptor blockAttackInnerInterceptor() { return new BlockAttackInnerInterceptor(); } }五、使用实体类import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; import com.alibaba.excel.annotation.ExcelProperty; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableLogic; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; import java.io.Serial; import java.io.Serializable; import java.time.LocalDate; /** * author Leslie Lee * version 2003/04/01 * date 1956/09/12 */ Data TableName(test) ExcelIgnoreUnannotated public class TestEntity implements Serializable { Serial private static final long serialVersionUID 1L; // TableId(type IdType.AUTO) private Integer id; ExcelProperty(姓名) private String name; ExcelProperty(性别) private String sex; private LocalDate createTime; private LocalDate updateTime; TableLogic private Integer isDelete; }TableName(表名)当实体类名与数据库表名不一致时需用此注解来标明对应的表TableId(type IdType.AUTO)如果 id 为自增可用此注解进行标识Mapper.javaimport com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.leslie.ai.domain.TestEntity; import org.apache.ibatis.annotations.Mapper; import java.util.List; /** * Mapper 测试 * * author Leslie Lee * TIME 1956/09/12 * Version 2003/04/01 */ Mapper public interface TestEntityMapper extends BaseMapperTestEntity { ListTestEntity queryTestEntityList(TestEntity o); }Mapper动态代理与 Mapper.xml 对应extends BaseMapper实体类提供基本的增删改查方法Service.java里面的方法需与 Mapper.java 中对应deleteById 方法实现是用的自带的删除方法所以不对应也不影响但是需要重写 sql 语句的方法都要对应import com.leslie.ai.domain.TestEntity; import java.util.List; /** * Service * * author Leslie Lee * TIME 1956/09/12 * Version 2003/04/01 */ public interface TestEntityService { ListTestEntity queryTestEntityList(TestEntity o); int deleteById(Integer id); }ServiceImpl.java直接使用 this.baseMapper 可以拿到Mapper.java 中对应的方法import com.leslie.ai.domain.TestEntity; import com.leslie.ai.mapper.TestEntityMapper; import com.leslie.ai.service.TestEntityService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import java.util.List; /** * ServiceImpl * * author Leslie Lee * TIME 1956/09/12 * Version 2003/04/01 */ Service public class TestEntityServiceImpl extends ServiceImplTestEntityMapper, TestEntity implements TestEntityService { Override public ListTestEntity queryTestEntityList(TestEntity o) { return this.baseMapper.queryTestEntityList(o); } Override public int deleteById(Integer id) { return this.baseMapper.deleteById(id); } }Mapper.xml此类文件放在 resources 文件夹下建议文件路径与实体类一致namespace对应实体类全路径resultMap方法返回参数实体类使用表字段名驼峰命名时不需要再写字段对应关系?xml version1.0 encodingUTF-8 ? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacecom.leslie.ai.mapper.TestEntityMapper !-- 实体类 -- resultMap typeTestEntity idTestEntityResult !-- result propertycreateTime columncreate_time jdbcTypeTIMESTAMP/-- /resultMap !-- sql语句 -- sql idselectTestEntityList /sql !-- 方法 -- select idqueryTestEntityList parameterTypeTestEntity resultMapTestEntityResult include refidselectTestEntityList/ where if testfieldName ! null and fieldName ! and tableFieldName like concat(%, #{fieldName}, %)/if if teststartDate ! null and startDate ! and applicationDate gt; #{startDate}/if if testendDate ! null and endDate ! and applicationDate lt; #{endDate}/if /where /select /mapper六、逻辑删除通过更改表的某一字段状态区分是否删除实体类实体类增加字段isDelete 对应表字段 is_delete 也可以用其他字段对应就行添加注解TableLogicTableLogic private Integer isDelete;还可以添加两个字段创建时间、修改时间private LocalDate createTime; // create_time private LocalDate updateTime; // update_timeyml 配置logic-delete-value: 1 # 逻辑删除时is_delete 为删除的值 logic-not-delete-value: 0 # 逻辑删除时is_delete 为未删除的值数据库创建时间、修改时间字段名就用下面的两个然后设置默认值CURRENT_TIMESTAMP(2)修改时间会在表数据变动时自动更新Leslie Lee 随笔