springboot3 集成mybatis 和通用mapper

打印 上一主题 下一主题

主题 937|帖子 937|积分 2811

xml版本查看:https://www.cnblogs.com/binz/p/6564490.html
springboot3.x以前的版本查看 https://www.cnblogs.com/binz/p/17421063.html
springboot3.x查看  https://www.cnblogs.com/binz/p/17654403.html
1、pom引用
  1. <parent>
  2.     <groupId>org.springframework.boot</groupId>
  3.     <artifactId>spring-boot-starter-parent</artifactId>
  4.     <version>3.1.2</version>
  5. </parent>
  6. <dependencies>
  7.      <dependency>
  8.    
  9. </dependencies>        <groupId>org.springframework.boot</groupId>
  10.    
  11. </dependencies>        <artifactId>spring-boot-starter-web</artifactId>
  12.    
  13. </dependencies>        <exclusions>
  14.    
  15. </dependencies>        
  16. </dependencies>    <exclusion>
  17.    
  18. </dependencies>        
  19. </dependencies>        <artifactId>spring-boot-starter-tomcat</artifactId>
  20.    
  21. </dependencies>        
  22. </dependencies>        <groupId>org.springframework.boot</groupId>
  23.    
  24. </dependencies>        
  25. </dependencies>    </exclusion>
  26.    
  27. </dependencies>        </exclusions>
  28.    
  29. </dependencies>    </dependency>
  30.    
  31. </dependencies>   
  32.    
  33. </dependencies>   
  34.    
  35. </dependencies>    <dependency>
  36.    
  37. </dependencies>        <groupId>org.springframework.boot</groupId>
  38.    
  39. </dependencies>        <artifactId>spring-boot-starter-undertow</artifactId>
  40.    
  41. </dependencies>    </dependency>
  42.    
  43. </dependencies>    <dependency>
  44.    
  45. </dependencies>        <groupId>com.github.pagehelper</groupId>
  46.    
  47. </dependencies>        <artifactId>pagehelper-spring-boot-starter</artifactId>
  48.    
  49. </dependencies>        <version>1.4.7</version>
  50.    
  51. </dependencies>    </dependency><br>
  52.     <br>   
  53.     <dependency>
  54.    
  55. </dependencies>        <groupId>io.mybatis</groupId>
  56.    
  57. </dependencies>        <artifactId>mybatis-mapper</artifactId>
  58.    
  59. </dependencies>        <version>2.1.1</version>
  60.     </dependency><br>   
  61.     <dependency>
  62.    
  63. </dependencies>        <groupId>org.mybatis.spring.boot</groupId>
  64.    
  65. </dependencies>        <artifactId>mybatis-spring-boot-starter</artifactId>
  66.    
  67. </dependencies>        <version>3.0.2</version>
  68.     </dependency><br><br><br>
复制代码
    
   
</dependencies>        
           
                          com.github.xiaoymin
                          knife4j-openapi3-jakarta-spring-boot-starter
                          4.3.0
                    
   
</dependencies>        
  1.    
  2. </dependencies>   
复制代码
2、新建自己的BaseMapper
  1. import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;import java.util.ArrayList;import java.util.List;import java.util.Optional;import org.apache.ibatis.session.RowBounds;import com.github.pagehelper.Page;import cn.hutool.core.bean.BeanUtil;import cn.hutool.core.util.ObjectUtil;import cn.hutool.core.util.ReflectUtil;import io.mybatis.mapper.Mapper;import io.mybatis.mapper.example.Example;import io.mybatis.mapper.example.LambdaExampleWrapper;public interface BaseMapper extends Mapper{   
  2. </dependencies>    List selectList(T entity, RowBounds rowBounds);    default T getById(Long id) {   
  3. </dependencies>    return selectByPrimaryKey(id).orElse(null);    }    default  RE getById(Long id,Class returnClass) {   
  4. </dependencies>    T t = getById(id);   
  5. </dependencies>    if(t != null) {   
  6. </dependencies>        return covert(t,returnClass);   
  7. </dependencies>    }   
  8. </dependencies>    return null;    }    default T one(T query) {   
  9. </dependencies>    return selectOne(query).orElse(null);    }    default  RE one(T query,Class returnClass) {   
  10. </dependencies>    Optional optional = selectOne(query);   
  11. </dependencies>    if(optional.isPresent()) {   
  12. </dependencies>        T t = optional.get();   
  13. </dependencies>        return covert(t,returnClass);   
  14. </dependencies>    }   
  15. </dependencies>    return null;    }    default  List select(T t,Class returnClass) {   
  16. </dependencies>    List ts = selectList(t);   
  17. </dependencies>    return covertList(ts,returnClass);    }    default  Page selectPage(T t,Class returnClass) {   
  18. </dependencies>    Page ts = (Page) selectList(t);   
  19. </dependencies>    return (Page) covertList(ts,returnClass);    }    default  Page selectPageByExample(Example example,Class returnClass) {   
  20. </dependencies>    Page ts = (Page) selectByExample(example);   
  21. </dependencies>    return (Page) covertList(ts,returnClass);    }    default  List selectByExample(Example example,Class returnClass) {   
  22. </dependencies>    List ts = selectByExample(example);   
  23. </dependencies>    return covertList(ts,returnClass);    }    default  RE selectOneByExample(Example example,Class returnClass) {   
  24. </dependencies>    Optional optional = selectOneByExample(example);   
  25. </dependencies>    if(optional.isPresent()) {   
  26. </dependencies>        T t = optional.get();   
  27. </dependencies>        return covert(t,returnClass);   
  28. </dependencies>    }   
  29. </dependencies>    return null;    }    default  RE selectOneByExampleLimitOne(Example example,Class returnClass) {   
  30. </dependencies>    T t = selectOneByExampleLimitOne(example);   
  31. </dependencies>    if(t != null) {   
  32. </dependencies>        return covert(t, returnClass);   
  33. </dependencies>    }   
  34. </dependencies>    return null;    }    default T selectOneByExampleLimitOne(Example example) {   
  35. </dependencies>    RowBounds rowBounds = new RowBounds(0, 1);   
  36. </dependencies>    List ts = selectByExample(example, rowBounds);   
  37. </dependencies>    if(ObjectUtil.isNotEmpty(ts)) {   
  38. </dependencies>        return ts.get(0);   
  39. </dependencies>    }   
  40. </dependencies>    return null;    }    default T selectOneByLimitOne(T t) {   
  41. </dependencies>    RowBounds rowBounds = new RowBounds(0, 1);   
  42. </dependencies>    List ts = selectList(t,rowBounds);   
  43. </dependencies>    if(ObjectUtil.isNotEmpty(ts)) {   
  44. </dependencies>        return ts.get(0);   
  45. </dependencies>    }   
  46. </dependencies>    return null;    }    @SuppressWarnings("unchecked")    default Class thisTClass() {   
  47. </dependencies>    Class class1 = getClass();   
  48. </dependencies>    Class interfaces = class1.getInterfaces()[0];   
  49. </dependencies>    Type[] genericInterfaces = interfaces.getGenericInterfaces();   
  50. </dependencies>    Type type = genericInterfaces[0];   
  51. </dependencies>    if( type instanceof ParameterizedType){   
  52. </dependencies>        ParameterizedType pType = (ParameterizedType) type;   
  53. </dependencies>        Type clazz = pType.getActualTypeArguments()[0];   
  54. </dependencies>        if( clazz instanceof Class ){   
  55. </dependencies>        
  56. </dependencies>    return (Class) clazz;   
  57. </dependencies>        }   
  58. </dependencies>    }   
  59. </dependencies>    return null;    }    default  List covertList(List ts,Class returnClass){   
  60. </dependencies>    List responses;   
  61. </dependencies>    if(ts instanceof Page) {   
  62. </dependencies>        responses = new Page();   
  63. </dependencies>    }else {   
  64. </dependencies>        responses = new ArrayList();   
  65. </dependencies>    }   
  66. </dependencies>    for (T t : ts) {   
  67. </dependencies>        responses.add(covert(t,returnClass));   
  68. </dependencies>    }   
  69. </dependencies>    return responses;    }    default  RE covert(T t , Class returnClass) {   
  70. </dependencies>    if(t != null) {   
  71. </dependencies>        RE response = null;   
  72. </dependencies>        try {   
  73. </dependencies>        
  74. </dependencies>    response = ReflectUtil.newInstanceIfPossible(returnClass);   
  75. </dependencies>        
  76. </dependencies>    BeanUtil.copy(t, response);   
  77. </dependencies>        } catch (Exception e) {   
  78. </dependencies>        
  79. </dependencies>    e.printStackTrace();   
  80. </dependencies>        }   
  81. </dependencies>        return response;   
  82. </dependencies>    }   
  83. </dependencies>    return null;    }    //自带的wrapper()个人觉得还缺点东西,就自己复制了一份出来微调了,根据情况使用,不需要就删除,需要对应的代码在下方    default LambdaExampleWrapper lambdaWrapper() {   
  84. </dependencies>    return new LambdaExampleWrapper(BaseMapper.this, example());    }}
复制代码
View Code3、新建表模型,注意注解使用和以前版本有区别,但是兼容一些之前javax.persistence的一些基本注解
  1. import java.util.Date;import io.mybatis.provider.Entity;import lombok.Data;@Data@Entity.Table("system_user")public class User {    @Entity.Column(id = true,insertable = false,updatable = false)    private Long id;    /**    * 姓名    */    private String realname;    /**    * 手机号    */    private String mobile;    /**    * 密码    */    private String password;    /**    * 身份证号    */    private String idcard;   
  2. </dependencies>    /**    * 头像    */    private String avatar;    /**    * 最后登录时间    */    private String lastLoginIp;    /**    * 最后登录时间    */    private Date lastLoginTime;    /**    * 创建人    */    private Long createBy;    /**    * 创建时间    */    private Date createTime;    /**    * 修改人    */    private Long updateBy;    /**    * update_time    */    private Date updateTime;}
复制代码
View Code4、创建对应业务的mapper继承BaseMapper
  1. import com.xxx.core.base.BaseMapper;
  2. import com.xxx.system.model.User;
  3. public interface UserMapper extends BaseMapper<User>{
  4. }
复制代码
5、启动类扫描自己的mapper目录 @MapperScan
  1. import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cache.annotation.EnableCaching;import org.springframework.context.annotation.ComponentScan;import org.springframework.scheduling.annotation.EnableScheduling;@EnableCaching@EnableScheduling@SpringBootApplication@MapperScan(basePackages="com.xxx.*.mapper")@ComponentScan(basePackages = { "com.xxx.*.config", "com.xxx.*.controller", "com.xxx.*.job" })public class OperateApiStarted {    public static void main(String[] args) {   
  2. </dependencies>    SpringApplication.run(OperateApiStarted.class, args);    }}
复制代码
 6、创建io.mybatis.mapper.example.LambdaExampleWrapper
[code]package io.mybatis.mapper.example;import java.io.Serializable;import java.util.Arrays;import java.util.List;import java.util.function.Function;import java.util.function.Supplier;import java.util.stream.Collectors;import java.util.stream.Stream;import org.apache.ibatis.exceptions.TooManyResultsException;import org.apache.ibatis.session.RowBounds;import com.github.pagehelper.Page;import com.xxx.BaseMapper;import com.xxx.BeanUtil;import io.mybatis.common.util.Assert;import io.mybatis.mapper.fn.Fn;public class LambdaExampleWrapper {    private final BaseMapper    baseMapper;    private final Example   
</dependencies>      example;    private       Example.Criteria current;    public LambdaExampleWrapper(BaseMapper baseMapper, Example example) {   
</dependencies>    this.baseMapper = baseMapper;   
</dependencies>    this.example = example;   
</dependencies>    this.current = example.createCriteria();    }    /**     * or 一组条件     *     * @return 条件     */    public LambdaExampleWrapper or() {   
</dependencies>    this.current = this.example.or();   
</dependencies>    return this;    }    /**     * 获取查询条件     */    public Example example() {   
</dependencies>    return example;    }    /**     * 清除条件,可重用     */    public LambdaExampleWrapper clear() {   
</dependencies>    this.example.clear();   
</dependencies>    this.current = example.createCriteria();   
</dependencies>    return this;    }    /**     * 指定查询列     *     * @param fns 方法引用     */    @SafeVarargs    public final LambdaExampleWrapper select(Fn... fns) {   
</dependencies>    this.example.selectColumns(fns);   
</dependencies>    return this;    }    /**     * 排除指定查询列     *     * @param fns 方法引用     */    @SafeVarargs    public final LambdaExampleWrapper exclude(Fn... fns) {   
</dependencies>    this.example.excludeColumns(fns);   
</dependencies>    return this;    }    /**     * 设置起始 SQL     *     * @param startSql 起始 SQL,添加到 SQL 前,注意防止 SQL 注入     */    public LambdaExampleWrapper startSql(String startSql) {   
</dependencies>    this.example.setStartSql(startSql);   
</dependencies>    return this;    }    /**     * 设置结尾 SQL     *     * @param endSql 结尾 SQL,添加到 SQL 最后,注意防止 SQL 注入     */    public LambdaExampleWrapper endSql(String endSql) {   
</dependencies>    this.example.setEndSql(endSql);   
</dependencies>    return this;    }    /**     * 通过方法引用方式设置排序字段     *     * @param fn    排序列的方法引用     * @param order 排序方式     * @return Example     */    public LambdaExampleWrapper orderBy(Fn fn, Example.Order order) {   
</dependencies>    this.example.orderBy(fn, order);   
</dependencies>    return this;    }    /**     * 支持使用字符串形式来设置 order by,用以支持一些特殊的排序方案     *     * @param orderByCondition 排序字符串(不会覆盖已有的排序内容)     * @return Example     */    public LambdaExampleWrapper orderBy(String orderByCondition) {   
</dependencies>    this.example.orderBy(orderByCondition);   
</dependencies>    return this;    }    /**     * 支持使用字符串形式来设置 order by,用以支持一些特殊的排序方案     *     * @param orderByCondition 排序字符串构造方法,比如通过数组集合循环拼接等     * @return Example     */    public LambdaExampleWrapper orderBy(Supplier orderByCondition) {   
</dependencies>    this.example.orderBy(orderByCondition);   
</dependencies>    return this;    }    /**     * 支持使用字符串形式来设置 order by,用以支持一些特殊的排序方案     *     * @param useOrderBy       条件表达式,true使用,false不使用 字符串排序     * @param orderByCondition 排序字符串构造方法,比如通过数组集合循环拼接等     * @return Example     */    public LambdaExampleWrapper orderBy(boolean useOrderBy, Supplier orderByCondition) {   
</dependencies>    return useOrderBy ? this.orderBy(orderByCondition) : this;    }    /**     * 通过方法引用方式设置排序字段,升序排序     *     * @param fns 排序列的方法引用     * @return Example     */    @SafeVarargs    public final LambdaExampleWrapper orderByAsc(Fn... fns) {   
</dependencies>    this.example.orderByAsc(fns);   
</dependencies>    return this;    }    /**     * 通过方法引用方式设置排序字段,降序排序     *     * @param fns 排序列的方法引用     * @return Example     */    @SafeVarargs    public final LambdaExampleWrapper orderByDesc(Fn... fns) {   
</dependencies>    this.example.orderByDesc(fns);   
</dependencies>    return this;    }    /**     * 设置 distince     */    public LambdaExampleWrapper distinct() {   
</dependencies>    this.example.setDistinct(true);   
</dependencies>    return this;    }    /**     * 设置更新字段和值     *     * @param useSet 表达式条件, true 使用,false 不使用     * @param setSql "column = value"     */    public LambdaExampleWrapper set(boolean useSet, String setSql) {   
</dependencies>    return useSet ? set(setSql) : this;    }    /**     * 设置更新字段和值     *     * @param setSql "column = value"     */    public LambdaExampleWrapper set(String setSql) {   
</dependencies>    this.example.set(setSql);   
</dependencies>    return this;    }    /**     * 设置更新字段和值     *     * @param useSet 表达式条件, true 使用,false 不使用     * @param fn     字段     * @param value  值     */    public LambdaExampleWrapper set(boolean useSet, Fn fn, Object value) {   
</dependencies>    return useSet ? set(fn, value) : this;    }    /**     * 设置更新字段和值     *     * @param useSet   表达式条件, true 使用,false 不使用     * @param fn       字段     * @param supplier 值构造函数     */    public LambdaExampleWrapper set(boolean useSet, Fn fn, Supplier supplier) {   
</dependencies>    return useSet ? set(fn, supplier.get()) : this;    }    /**     * 设置更新字段和值     *     * @param fn    字段     * @param value 值     */    public LambdaExampleWrapper set(Fn fn, Object value) {   
</dependencies>    this.example.set(fn, value);   
</dependencies>    return this;    }    /**     * 指定字段为 null     *     * @param useCondition 表达式条件, true 使用,false 不使用     * @param fn   
</dependencies>       字段对应的 get 方法引用     */    public LambdaExampleWrapper isNull(boolean useCondition, Fn fn) {   
</dependencies>    return useCondition ? isNull(fn) : this;    }    /**     * 指定字段为 null     *     * @param fn 字段对应的 get 方法引用     */    public LambdaExampleWrapper isNull(Fn fn) {   
</dependencies>    this.current.addCriterion(fn.toColumn() + " IS NULL");   
</dependencies>    return this;    }    /**     * 指定字段不为 null     *     * @param useCondition 表达式条件, true 使用,false 不使用     * @param fn   
</dependencies>       字段对应的 get 方法引用     */    public LambdaExampleWrapper isNotNull(boolean useCondition, Fn fn) {   
</dependencies>    return useCondition ? isNotNull(fn) : this;    }    /**     * 指定字段不为 null     *     * @param fn 字段对应的 get 方法引用     */    public LambdaExampleWrapper isNotNull(Fn fn) {   
</dependencies>    this.current.addCriterion(fn.toColumn() + " IS NOT NULL");   
</dependencies>    return this;    }    /**     * 字段 = 值     *     * @param useCondition 表达式条件, true 使用,false 不使用     * @param fn   
</dependencies>       字段对应的 get 方法引用     * @param value   
</dependencies>    值     */    public LambdaExampleWrapper eq(boolean useCondition, Fn fn, Object value) {   
</dependencies>    return useCondition ? eq(fn, value) : this;    }    /**     * 字段 = 值     *     * @param useCondition 表达式条件, true 使用,false 不使用     * @param fn   
</dependencies>       字段对应的 get 方法引用     * @param supplier     值构造函数     */    public LambdaExampleWrapper eq(boolean useCondition, Fn fn, Supplier supplier) {   
</dependencies>    return useCondition ? eq(fn, supplier.get()) : this;    }    /**     * 字段 = 值     *     * @param fn    字段对应的 get 方法引用     * @param value 值     */    public LambdaExampleWrapper eq(Fn fn, Object value) {   
</dependencies>    this.current.addCriterion(fn.toColumn() + " =", value);   
</dependencies>    return this;    }    /**     * 字段 != 值     *     * @param useCondition 表达式条件, true 使用,false 不使用     * @param fn   
</dependencies>       字段对应的 get 方法引用     * @param supplier     值构造函数     */    public LambdaExampleWrapper ne(boolean useCondition, Fn fn, Supplier supplier) {   
</dependencies>    return useCondition ? ne(fn, supplier.get()) : this;    }    /**     * 字段 != 值     *     * @param useCondition 表达式条件, true 使用,false 不使用     * @param fn   
</dependencies>       字段对应的 get 方法引用     * @param value   
</dependencies>    值     */    public LambdaExampleWrapper ne(boolean useCondition, Fn fn, Object value) {   
</dependencies>    return useCondition ? ne(fn, value) : this;    }    /**     * 字段 != 值     *     * @param fn    字段对应的 get 方法引用     * @param value 值     */    public LambdaExampleWrapper ne(Fn fn, Object value) {   
</dependencies>    this.current.addCriterion(fn.toColumn() + " ", value);   
</dependencies>    return this;    }    /**     * 字段 > 值     *     * @param useCondition 表达式条件, true 使用,false 不使用     * @param fn   
</dependencies>       字段对应的 get 方法引用     * @param supplier     值构造函数     */    public LambdaExampleWrapper gt(boolean useCondition, Fn fn, Supplier supplier) {   
</dependencies>    return useCondition ? gt(fn, supplier.get()) : this;    }    /**     * 字段 > 值     *     * @param useCondition 表达式条件, true 使用,false 不使用     * @param fn   
</dependencies>       字段对应的 get 方法引用     * @param value   
</dependencies>    值     */    public LambdaExampleWrapper gt(boolean useCondition, Fn fn, Object value) {   
</dependencies>    return useCondition ? gt(fn, value) : this;    }    /**     * 字段 > 值     *     * @param fn    字段对应的 get 方法引用     * @param value 值     */    public LambdaExampleWrapper gt(Fn fn, Object value) {   
</dependencies>    this.current.addCriterion(fn.toColumn() + " >", value);   
</dependencies>    return this;    }    /**     * 字段 >= 值     *     * @param useCondition 表达式条件, true 使用,false 不使用     * @param fn   
</dependencies>       字段对应的 get 方法引用     * @param supplier     值构造函数     */    public LambdaExampleWrapper ge(boolean useCondition, Fn fn, Supplier supplier) {   
</dependencies>    return useCondition ? ge(fn, supplier.get()) : this;    }    /**     * 字段 >= 值     *     * @param useCondition 表达式条件, true 使用,false 不使用     * @param fn   
</dependencies>       字段对应的 get 方法引用     * @param value   
</dependencies>    值     */    public LambdaExampleWrapper ge(boolean useCondition, Fn fn, Object value) {   
</dependencies>    return useCondition ? ge(fn, value) : this;    }    /**     * 字段 >= 值     *     * @param fn    字段对应的 get 方法引用     * @param value 值     */    public LambdaExampleWrapper ge(Fn fn, Object value) {   
</dependencies>    this.current.addCriterion(fn.toColumn() + " >=", value);   
</dependencies>    return this;    }    /**     * 字段 < 值     *     * @param useCondition 表达式条件, true 使用,false 不使用     * @param fn   
</dependencies>       字段对应的 get 方法引用     */    public LambdaExampleWrapper lt(boolean useCondition, Fn fn, Supplier supplier) {   
</dependencies>    return useCondition ? lt(fn, supplier.get()) : this;    }    /**     * 字段 < 值     *     * @param useCondition 表达式条件, true 使用,false 不使用     * @param fn   
</dependencies>       字段对应的 get 方法引用     * @param value   
</dependencies>    值     */    public LambdaExampleWrapper lt(boolean useCondition, Fn fn, Object value) {   
</dependencies>    return useCondition ? lt(fn, value) : this;    }    /**     * 字段 < 值     *     * @param fn    字段对应的 get 方法引用     * @param value 值     */    public LambdaExampleWrapper lt(Fn fn, Object value) {   
</dependencies>    this.current.addCriterion(fn.toColumn() + "

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

卖不甜枣

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表