ruoyi-集成mybatis-plus

1,增加依赖,在ruoyi-common的pom.xml中dependencies片段里增加


    com.baomidou
    mybatis-plus-boot-starter
    3.4.2

2,修改配置文件,在ruoyi-admin的src-main-resources文件夹下的application.yml中增加以下片段,并注释原有的mybatis片段

MyBatis Plus配置

mybatis-plus:
  # 搜索指定包别名
  typeAliasesPackage: com.ruoyi.**.domain
  # 配置mapper的扫描,找到所有的mapper.xml映射文件
  mapperLocations: classpath*:mapper/**/*Mapper.xml
  # 加载全局的配置文件
  configLocation: classpath:mybatis/mybatis-config.xml

3,com.ruoyi.framework.config路径下增加文件MybatisPlusConfig.java,原来的MybatisConfig.java删掉

package com.ruoyi.framework.config;

import 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 配置
 *
 * @author ruoyi
 */
@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;
    }

    /**
     * 分页插件,自动识别数据库类型 https://baomidou.com/guide/interceptor-pagination.html
     */
    public PaginationInnerInterceptor paginationInnerInterceptor()
    {
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        // 设置数据库类型为mysql
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        paginationInnerInterceptor.setMaxLimit(-1L);
        return paginationInnerInterceptor;
    }

    /**
     * 乐观锁插件 https://baomidou.com/guide/interceptor-optimistic-locker.html
     */
    public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor()
    {
        return new OptimisticLockerInnerInterceptor();
    }

    /**
     * 如果是对全表的删除或更新操作,就会终止该操作 https://baomidou.com/guide/interceptor-block-attack.html
     */
    public BlockAttackInnerInterceptor blockAttackInnerInterceptor()
    {
        return new BlockAttackInnerInterceptor();
    }
}

4,创建基类,在com.ruoyi.common.core.domain文件夹下创建名为BaseEntityToMybatisPlus.java的实体类,代码如下

package com.ruoyi.common.core.domain;

/**
 * @ClassName BaseEntityToMybatisPlus
 * @Description TODO
 * @Date 2022/6/21 17:47
 * @Version 1.0
 **/
import com.baomidou.mybatisplus.annotation.TableField;
import com.ruoyi.common.annotation.Excel;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class BaseEntityToMybatisPlus implements Serializable {
    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    /** 搜索值 */
    @TableField(exist = false)
    private String searchValue;

    /** 创建者 */
    private String createBy;

    /** 创建时间 */
    private Date createTime;

    /** 更新者 */
    private String updateBy;

    /** 更新时间 */
    private Date updateTime;

    /** 备注 */
    @Excel(name = "备注")
    private String remark;

    /** 请求参数 */
    @TableField(exist = false)
    private Map params;

    public String getSearchValue()
    {
        return searchValue;
    }

    public void setSearchValue(String searchValue)
    {
        this.searchValue = searchValue;
    }

    public String getCreateBy()
    {
        return createBy;
    }

    public void setCreateBy(String createBy)
    {
        this.createBy = createBy;
    }

    public Date getCreateTime()
    {
        return createTime;
    }

    public void setCreateTime(Date createTime)
    {
        this.createTime = createTime;
    }

    public String getUpdateBy()
    {
        return updateBy;
    }

    public void setUpdateBy(String updateBy)
    {
        this.updateBy = updateBy;
    }

    public Date getUpdateTime()
    {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime)
    {
        this.updateTime = updateTime;
    }

    public String getRemark()
    {
        return remark;
    }

    public void setRemark(String remark)
    {
        this.remark = remark;
    }

    public Map getParams()
    {
        if (params == null)
        {
            params = new HashMap<>();
        }
        return params;
    }

    public void setParams(Map params)
    {
        this.params = params;
    }
}

至此集成完成,其他domain继承最后的类即可

MyBatis-plus自带的方法遇到 invalid bound statement (not found)
检查是否设置了主键(@tableid)

在使用lambda表达式的时候警告:String is not a functional interface
前面创建对象的时候使用的是QueryWrapper 但是在使用中使用了lambda表达式,他就会把表达式识别为字符串 (就会提示字符串不是一个功能接口)。
解决方法1,增加.lambda()

QueryWrapper queryWrapper = new QueryWrapper<>();
        if (t.getAtt()!=null){
            queryWrapper.lambda().eq(T::getAtt,t.getAtt());//增加.lambda()
        }

解决方法2,直接使用LambdaQueryWrapper

LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper();
if (t.getAtt()!=null){
    queryWrapper.eq(T::getAtt,t.getAtt());
}

你可能感兴趣的:(mybatis,java,spring,boot)