首页 > 数据库 >springboot 接入通用tkmapper和pagehelper,boot starter版,mysql5.7,boot2.7.9

springboot 接入通用tkmapper和pagehelper,boot starter版,mysql5.7,boot2.7.9

时间:2023-05-22 16:56:03浏览次数:58  
标签:boot2.7 return springboot weekendCriteria mysql5.7 value Fn public fn

1、pom文件引入

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.4.6</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.27</version>
</dependency>
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>4.2.2</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.11</version>
</dependency>
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-base</artifactId>
    <version>1.0.0</version>
</dependency>
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
</dependency>
View Code

 

2、yuml配置文件

mapper:
    enum-as-simple-type: true #开启枚举转字符串
mybatis:
    mapper-locations: classpath*:mapper/*Mapper.xml
    type-aliases-package: com.xxx.*.model
    configuration:
        map-underscore-to-camel-case: true  #开启驼峰下划线互转

3、再启动配置或者在有@Configuration注解的类上添加@MapperScan(basePackages="com.xxxx.*.mapper"),例如

import org.springframework.context.annotation.Configuration;
import tk.mybatis.spring.annotation.MapperScan; @Configuration @MapperScan(basePackages="com.xxxx.*.mapper") public class MapperBeanInit { }

4、创建BaseMapper合并多个常用mapper

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

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 tk.mybatis.mapper.additional.select.SelectByPropertyMapper;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.special.InsertListMapper;
import tk.mybatis.mapper.lambda.WrapperLambdaDelete;
import tk.mybatis.mapper.lambda.WrapperLambdaQuery;
import tk.mybatis.mapper.lambda.WrapperLambdaUpdate;
import tk.mybatis.mapper.weekend.Weekend;

public interface BaseMapper<T> extends Mapper<T>, InsertListMapper<T>,SelectByPropertyMapper<T>{

    default <RE extends BaseSerializable> RE selectByPrimaryKey(Object key,Class<RE> returnClass) {
        T t = selectByPrimaryKey(key);
        return covert(t,returnClass);
    }

    default <RE extends BaseSerializable> RE selectOne(T t,Class<RE> returnClass) {
        t = selectOne(t);
        return covert(t,returnClass);
    }


    default <RE extends BaseSerializable> List<RE> select(T t,Class<RE> returnClass) {
        List<T> ts = select(t);
        return covertList(ts,returnClass);
    }

    default <RE extends BaseSerializable> Page<RE> selectPage(T t,Class<RE> returnClass) {
        Page<T> ts = (Page<T>) select(t);
        return (Page<RE>) covertList(ts,returnClass);
    }

    default <RE extends BaseSerializable> Page<RE> selectPageByExample(Object example,Class<RE> returnClass) {
        Page<T> ts = (Page<T>) selectByExample(example);
        return (Page<RE>) covertList(ts,returnClass);
    }

    default <RE extends BaseSerializable> List<RE> selectAll(Class<RE> returnClass) {
        List<T> ts = selectAll();
        return covertList(ts,returnClass);
    }


    default <RE extends BaseSerializable> List<RE> selectByExample(Object example,Class<RE> returnClass) {
        List<T> ts = selectByExample(example);
        return covertList(ts,returnClass);
    }

    default <RE extends BaseSerializable> RE selectOneByExample(Object example,Class<RE> returnClass) {
        T ts = selectOneByExample(example);
        return covert(ts,returnClass);
    }
    
    default <RE extends BaseSerializable> RE selectOneByExampleLimitOne(Weekend<T> example,Class<RE> returnClass) {
        RowBounds rowBounds = new RowBounds(0, 1);
        List<T> ts = selectByExampleAndRowBounds(example, rowBounds);
        List<RE> covertList = covertList(ts,returnClass);
        if(ObjectUtil.isNotEmpty(covertList)) {
            return covertList.get(0);
        }
        return null;
    }
    
    default T selectOneByExampleLimitOne(Weekend<T> example) {
        RowBounds rowBounds = new RowBounds(0, 1);
        List<T> ts = selectByExampleAndRowBounds(example, rowBounds);
        if(ObjectUtil.isNotEmpty(ts)) {
            return ts.get(0);
        }
        return null;
    }
    
    default T selectOneByLimitOne(T t) {
        RowBounds rowBounds = new RowBounds(0, 1);
        List<T> ts = selectByRowBounds(t,rowBounds);
        if(ObjectUtil.isNotEmpty(ts)) {
            return ts.get(0);
        }
        return null;
    }



    @SuppressWarnings("unchecked")
    default Class<T> thisTClass() {
        Class<?> class1 = getClass();
        Class<?> interfaces = class1.getInterfaces()[0];
        Type[] genericInterfaces = interfaces.getGenericInterfaces();
        Type type = genericInterfaces[0];
        if( type instanceof ParameterizedType pType){
            Type clazz = pType.getActualTypeArguments()[0];
            if( clazz instanceof Class<?> ){
                return (Class<T>) clazz;
            }
        }
        return null;
    }

    default <RE extends BaseSerializable> List<RE> covertList(List<T> ts,Class<RE> returnClass){
        List<RE> responses;
        if(ts instanceof Page) {
            responses = new Page<>();
        }else {
            responses = new ArrayList<>();
        }
        for (T t : ts) {
            responses.add(covert(t,returnClass));
        }
        return responses;
    }

    default <RE extends BaseSerializable> RE covert(T t , Class<RE> returnClass) {
        if(t != null) {
            RE response = null;
            try {
                response = ReflectUtil.newInstanceIfPossible(returnClass);
               BeanUtil.copyProperties(t, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return response;
        }
        return null;
    }
    

    
    //*********以下是我自己实现的三个类似mybatis plus的链式调用,不用可以删除,要用具体的类在下方提供*********//
    default WrapperLambdaQuery<T> lambdaQuery(){
        return new WrapperLambdaQuery<T>(this,thisTClass());
    }
    
    default WrapperLambdaUpdate<T> lambdaUpdate(){
        return new WrapperLambdaUpdate<T>(this,thisTClass());
    }
    
    default WrapperLambdaDelete<T> lambdaDelete(){
        return new WrapperLambdaDelete<T>(this,thisTClass());
    }

}
View Code

 

5、使用示例

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserVO getById(Long userId) {
        return userMapper.selectByPrimaryKey(userId, UserVO.class);
    }

    @Override
    public UserVO getById_plus(Long userId) {
        return userMapper.lambdaQuery().eq(User::getId, userId).one(UserVO.class);
    }
}

 

6、其他扩展类FnGet、WrapperLambda、WrapperLambdaQuery、WrapperLambdaUpdate、WrapperLambdaDelete,新建并放入包tk.mybatis.mapper.lambda

FnGet

package tk.mybatis.mapper;

import java.util.ArrayList;
import java.util.List;

import tk.mybatis.mapper.weekend.Fn;
import tk.mybatis.mapper.weekend.reflection.Reflections;

/**
 * 获取get,is方法的属性
 */
public class FnGet{

    public static <T,R> String fieldName(Fn<T,R> fn) {
        return Reflections.fnToFieldName(fn);
    }
    
    @SafeVarargs
    public static <T,R> String[] fieldNames(Fn<T,R> ... fns) {
        List<String> fields = new ArrayList<>(fns.length);
        for (Fn<T,R> fn : fns) {
            fields.add(fieldName(fn));
        }
        return fields.toArray(new String[] {});
    }
    
}

 

WrapperLambda

class WrapperLambda<T> {
    
    protected Weekend<T> exampe;
    
    protected BaseMapper<T> mapper;
    
    protected WeekendCriteria<T,Object> weekendCriteria;
    
    protected Class<T> entityClass;
    
    WrapperLambda(BaseMapper<T> mapper,Class<T> entityClass){
        this.mapper = mapper;
        this.exampe = Weekend.of(entityClass);
        this.entityClass = entityClass;
        this.weekendCriteria = exampe.weekendCriteria();
    }
    
}

WrapperLambdaQuery

package tk.mybatis.mapper.lambda;

import java.util.List;

import com.github.pagehelper.Page;

import cn.hutool.core.util.ObjectUtil;
import tk.mybatis.mapper.FnGet;
import tk.mybatis.mapper.weekend.Fn;
import tk.mybatis.mapper.weekend.reflection.Reflections;

public final class WrapperLambdaQuery<T> extends WrapperLambda<T>{

    public WrapperLambdaQuery(BaseMapper<T> mapper,Class<T> entityClass){
        super(mapper, entityClass);
    }


    public WrapperLambdaQuery<T> eq(Fn<T, Object> fn, Object value) {
        weekendCriteria.andEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }

    public WrapperLambdaQuery<T> ne(Fn<T, Object> fn, Object value) {
        weekendCriteria.andNotEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }
    public WrapperLambdaQuery<T> isNull(Fn<T, Object> fn) {
        weekendCriteria.andIsNull(Reflections.fnToFieldName(fn));
        return this;
    }
    
    public WrapperLambdaQuery<T> isNotNull(Fn<T, Object> fn) {
        weekendCriteria.andIsNotNull(Reflections.fnToFieldName(fn));
        return this;
    }

    /**
     * 大于
     */
    public WrapperLambdaQuery<T> gt(Fn<T, Object> fn, Object value) {
        weekendCriteria.andGreaterThan(Reflections.fnToFieldName(fn), value);
        return this;
    }
    
    /**
     * 大于等于
     */
    public WrapperLambdaQuery<T> ge(Fn<T, Object> fn, Object value) {
        weekendCriteria.andGreaterThanOrEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }

    /**
     * 小于
     */
    public WrapperLambdaQuery<T> lt(Fn<T, Object> fn, Object value) {
        weekendCriteria.andLessThan(Reflections.fnToFieldName(fn), value);
        return this;
    }

    /**
     * 小于等于
     */
    public WrapperLambdaQuery<T> le(Fn<T, Object> fn, Object value) {
        weekendCriteria.andLessThanOrEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }

    public WrapperLambdaQuery<T> in(Fn<T, Object> fn, @SuppressWarnings("rawtypes") Iterable values) {
        weekendCriteria.andIn(Reflections.fnToFieldName(fn), values);
        return this;
    }

    public WrapperLambdaQuery<T> notIn(Fn<T, Object> fn, @SuppressWarnings("rawtypes") Iterable values) {
        weekendCriteria.andNotIn(Reflections.fnToFieldName(fn), values);
        return this;
    }

    public WrapperLambdaQuery<T> between(Fn<T, Object> fn, Object value1, Object value2) {
        weekendCriteria.andBetween(Reflections.fnToFieldName(fn), value1, value2);
        return this;
    }

    public WrapperLambdaQuery<T> notBetween(Fn<T, Object> fn, Object value1, Object value2) {
        weekendCriteria.andNotBetween(Reflections.fnToFieldName(fn), value1, value2);
        return this;
    }

    public WrapperLambdaQuery<T> leftLike(Fn<T, Object> fn, String value) {
        if(StringUtil.isNotBlank(value)) {
            weekendCriteria.andLike(Reflections.fnToFieldName(fn), "%" + value);
        }
        return this;
    }

    public WrapperLambdaQuery<T> rightLike(Fn<T, Object> fn, String value) {
        if(StringUtil.isNotBlank(value)) {
            weekendCriteria.andLike(Reflections.fnToFieldName(fn), value + "%");
        }
        return this;
    }

    public WrapperLambdaQuery<T> like(Fn<T, Object> fn, String value) {

        if(StringUtil.isNotBlank(value)) {
            weekendCriteria.andLike(Reflections.fnToFieldName(fn),"%" + value +"%");
        }
        return this;
    }

    public WrapperLambdaQuery<T> notLike(Fn<T, Object> fn, String value) {
        if(StringUtil.isNotBlank(value)) {
            weekendCriteria.andNotLike(Reflections.fnToFieldName(fn),"%" + value +"%");
        }
        return this;
    }

    public WrapperLambdaQuery<T> orIsNull(Fn<T, Object> fn) {
        weekendCriteria.orIsNull(Reflections.fnToFieldName(fn));
        return this;
    }

    public WrapperLambdaQuery<T> orIsNotNull(Fn<T, Object> fn) {
        weekendCriteria.orIsNotNull(Reflections.fnToFieldName(fn));
        return this;
    }

    /**
     * 或者相等
     */
    public WrapperLambdaQuery<T> orEq(Fn<T, Object> fn, Object value) {
        weekendCriteria.orEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }

    /**
     * 或者不等于
     */
    public WrapperLambdaQuery<T> orNe(Fn<T, Object> fn, Object value) {
        weekendCriteria.orNotEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }

    /**
     * 或者大于
     */
    public WrapperLambdaQuery<T> orGt(Fn<T, Object> fn, Object value) {
        weekendCriteria.orGreaterThan(Reflections.fnToFieldName(fn), value);
        return this;
    }

    /**
     * 或者大于等于
     */
    public WrapperLambdaQuery<T> orGe(Fn<T, Object> fn, Object value) {
        weekendCriteria.orGreaterThanOrEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }

    /**
     * 或者小于
     */
    public WrapperLambdaQuery<T> orLt(Fn<T, Object> fn, Object value) {
        weekendCriteria.orLessThan(Reflections.fnToFieldName(fn), value);
        return this;
    }

    /**
     * 或者小于等于
     */
    public WrapperLambdaQuery<T> orLe(Fn<T, Object> fn, Object value) {
        weekendCriteria.orLessThanOrEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }

    public WrapperLambdaQuery<T> orIn(Fn<T, Object> fn, @SuppressWarnings("rawtypes") Iterable values) {
        weekendCriteria.orIn(Reflections.fnToFieldName(fn), values);
        return this;
    }

    public WrapperLambdaQuery<T> orNotIn(Fn<T, Object> fn, @SuppressWarnings("rawtypes") Iterable values) {
        weekendCriteria.orNotIn(Reflections.fnToFieldName(fn), values);
        return this;
    }

    public WrapperLambdaQuery<T> orBetween(Fn<T, Object> fn, Object value1, Object value2) {
        weekendCriteria.orBetween(Reflections.fnToFieldName(fn), value1, value2);
        return this;
    }

    public WrapperLambdaQuery<T> orNotBetween(Fn<T, Object> fn, Object value1, Object value2) {
        weekendCriteria.orNotBetween(Reflections.fnToFieldName(fn), value1, value2);
        return this;
    }

    public WrapperLambdaQuery<T> orLike(Fn<T, Object> fn, String value) {
        if(StringUtil.isNotBlank(value)) {
            weekendCriteria.orLike(Reflections.fnToFieldName(fn),"%" + value +"%");
        }
        return this;
    }

    public WrapperLambdaQuery<T> orNotLike(Fn<T, Object> fn, String value) {
        if(StringUtil.isNotBlank(value)) {
            weekendCriteria.orNotLike(Reflections.fnToFieldName(fn),"%" + value +"%");
        }
        return this;
    }

    public WrapperLambdaQuery<T> and(WrapperLambdaQuery<T> wrapperLambda){
        exampe.and(wrapperLambda.weekendCriteria);
        return this;
    }

    public WrapperLambdaQuery<T> or(WrapperLambdaQuery<T> wrapperLambda){
        exampe.or(wrapperLambda.weekendCriteria);
        return this;
    }

    public WrapperLambdaQuery<T> createWrapper(){
        return new WrapperLambdaQuery<>(mapper, entityClass);
    }

    /**
     * 排除查询字段,优先级低于 selectProperties
     *
     * @param fns 属性名的可变参数
     * @return
     */
    @SafeVarargs
    public final <R> WrapperLambdaQuery<T> excludeProperties(Fn<T, R>... fns) {
        String[] properties = FnGet.fieldNames(fns);
        exampe.excludeProperties(properties);
        return this;
    }

    /**
     * 指定要查询的属性列 - 这里会自动映射到表字段
     *
     * @param fns
     * @return
     */
    @SafeVarargs
    public final <R> WrapperLambdaQuery<T> selectProperties(Fn<T, R>... fns) {
        String[] properties = FnGet.fieldNames(fns);
        exampe.selectProperties(properties);
        return this;
    }


    public WrapperLambdaQuery<T>  orderBy(Fn<T, ?> fn) {
        exampe.orderBy(Reflections.fnToFieldName(fn));
        return this;
    }

    public WrapperLambdaQuery<T>  orderDescBy(Fn<T, ?> fn) {
        exampe.orderBy(Reflections.fnToFieldName(fn)).desc();
        return this;
    }

    public List<T> list(){
        return mapper.selectByExample(exampe);
    }
    public Page<T> listPage(){
        List<T> ts = mapper.selectByExample(exampe);
        Page<T> pageRes = new Page<>();
        pageRes.addAll(ts);
        if(ts instanceof Page<T> pageTs) {
            pageRes.setTotal(pageTs.getTotal());
            return pageRes;
        }
        return pageRes;

    }

    public <RE> List<RE> list(Class<RE> returnClass){
        List<T> ts = mapper.selectByExample(exampe);
        List<RE> res = BeanUtil.copys(ts, returnClass);
        if(ts instanceof Page<T> pageTs) {
            Page<RE> pageRes = new Page<>();
            pageRes.addAll(res);
            pageRes.setTotal(pageTs.getTotal());
            return pageRes;
        }
        return res;
    }

    public <RE> Page<RE> listPage(Class<RE> returnClass){
        List<T> ts = (List<T>) mapper.selectByExample(exampe);
        List<RE> res = BeanUtil.copys(ts, returnClass);
        Page<RE> pageRes = new Page<>();
        pageRes.addAll(res);
        if(ts instanceof Page<T> pageTs) {
            pageRes.setTotal(pageTs.getTotal());
            return pageRes;
        }
        return pageRes;
    }

    public T one(){
        return mapper.selectOneByExample(exampe);
    }

    public <RE> RE one(Class<RE> returnClass){
        T one = one();
        if(one == null) {
            return null;
        }
        return BeanUtil.copy(one, returnClass);
    }
    public T oneLimit(){
        List<T> ts = mapper.selectByExample(exampe);
        return ObjectUtil.isNotEmpty(ts) ? ts.get(0) : null;
    }

    public <RE> RE oneLimit(Class<RE> returnClass){
        T t = oneLimit();
        if(t == null) {
            return null;
        }
        return BeanUtil.copy(t, returnClass);
    }

    public int count(){
        return mapper.selectCountByExample(exampe);
    }

}
View Code

 

WrapperLambdaUpdate

package tk.mybatis.mapper.lambda;

import java.util.function.BiConsumer;

import cn.hutool.core.util.ReflectUtil;
import tk.mybatis.mapper.weekend.Fn;
import tk.mybatis.mapper.weekend.reflection.Reflections;

public final class WrapperLambdaUpdate<T> extends WrapperLambda<T>{
    
    private T entity;
    
    public WrapperLambdaUpdate(BaseMapper<T> mapper, Class<T> entityClass) {
        super(mapper, entityClass);
    }
    

    public WrapperLambdaUpdate<T> eq(Fn<T, Object> fn, Object value) {
        weekendCriteria.andEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }

    public WrapperLambdaUpdate<T> ne(Fn<T, Object> fn, Object value) {
        weekendCriteria.andNotEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }
    public WrapperLambdaUpdate<T> isNull(Fn<T, Object> fn) {
        weekendCriteria.andIsNull(Reflections.fnToFieldName(fn));
        return this;
    }
    
    public WrapperLambdaUpdate<T> isNotNull(Fn<T, Object> fn) {
        weekendCriteria.andIsNotNull(Reflections.fnToFieldName(fn));
        return this;
    }
    /**
     * 大于
     */
    public WrapperLambdaUpdate<T> gt(Fn<T, Object> fn, Object value) {
        weekendCriteria.andGreaterThan(Reflections.fnToFieldName(fn), value);
        return this;
    }
    
    /**
     * 大于等于
     */
    public WrapperLambdaUpdate<T> ge(Fn<T, Object> fn, Object value) {
        weekendCriteria.andGreaterThanOrEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }

    /**
     * 小于
     */
    public WrapperLambdaUpdate<T> lt(Fn<T, Object> fn, Object value) {
        weekendCriteria.andLessThan(Reflections.fnToFieldName(fn), value);
        return this;
    }

    /**
     * 小于等于
     */
    public WrapperLambdaUpdate<T> le(Fn<T, Object> fn, Object value) {
        weekendCriteria.andLessThanOrEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }

    public WrapperLambdaUpdate<T> in(Fn<T, Object> fn, @SuppressWarnings("rawtypes") Iterable values) {
        weekendCriteria.andIn(Reflections.fnToFieldName(fn), values);
        return this;
    }

    public WrapperLambdaUpdate<T> notIn(Fn<T, Object> fn, @SuppressWarnings("rawtypes") Iterable values) {
        weekendCriteria.andNotIn(Reflections.fnToFieldName(fn), values);
        return this;
    }

    public WrapperLambdaUpdate<T> between(Fn<T, Object> fn, Object value1, Object value2) {
        weekendCriteria.andBetween(Reflections.fnToFieldName(fn), value1, value2);
        return this;
    }

    public WrapperLambdaUpdate<T> notBetween(Fn<T, Object> fn, Object value1, Object value2) {
        weekendCriteria.andNotBetween(Reflections.fnToFieldName(fn), value1, value2);
        return this;
    }

    public WrapperLambdaUpdate<T> leftLike(Fn<T, Object> fn, String value) {
        if(StringUtil.isNotBlank(value)) {
            weekendCriteria.andLike(Reflections.fnToFieldName(fn), "%" + value);
        }
        return this;
    }

    public WrapperLambdaUpdate<T> rightLike(Fn<T, Object> fn, String value) {
        if(StringUtil.isNotBlank(value)) {
            weekendCriteria.andLike(Reflections.fnToFieldName(fn), value + "%");
        }
        return this;
    }

    public WrapperLambdaUpdate<T> like(Fn<T, Object> fn, String value) {
        if(StringUtil.isNotBlank(value)) {
            weekendCriteria.andLike(Reflections.fnToFieldName(fn),"%" + value +"%");
        }
        return this;
    }

    public WrapperLambdaUpdate<T> notLike(Fn<T, Object> fn, String value) {
        if(StringUtil.isNotBlank(value)) {
            weekendCriteria.andNotLike(Reflections.fnToFieldName(fn),"%" + value +"%");
        }
        return this;
    }

    public WrapperLambdaUpdate<T> orIsNull(Fn<T, Object> fn) {
        weekendCriteria.orIsNull(Reflections.fnToFieldName(fn));
        return this;
    }

    public WrapperLambdaUpdate<T> orIsNotNull(Fn<T, Object> fn) {
        weekendCriteria.orIsNotNull(Reflections.fnToFieldName(fn));
        return this;
    }

    /**
     * 或者相等
     */
    public WrapperLambdaUpdate<T> orEq(Fn<T, Object> fn, Object value) {
        weekendCriteria.orEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }

    /**
     * 或者不等于
     */
    public WrapperLambdaUpdate<T> orNe(Fn<T, Object> fn, Object value) {
        weekendCriteria.orNotEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }

    /**
     * 或者大于
     */
    public WrapperLambdaUpdate<T> orGt(Fn<T, Object> fn, Object value) {
        weekendCriteria.orGreaterThan(Reflections.fnToFieldName(fn), value);
        return this;
    }

    /**
     * 或者大于等于
     */
    public WrapperLambdaUpdate<T> orGe(Fn<T, Object> fn, Object value) {
        weekendCriteria.orGreaterThanOrEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }

    /**
     * 或者小于
     */
    public WrapperLambdaUpdate<T> orLt(Fn<T, Object> fn, Object value) {
        weekendCriteria.orLessThan(Reflections.fnToFieldName(fn), value);
        return this;
    }

    /**
     * 或者小于等于
     */
    public WrapperLambdaUpdate<T> orLe(Fn<T, Object> fn, Object value) {
        weekendCriteria.orLessThanOrEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }

    public WrapperLambdaUpdate<T> orIn(Fn<T, Object> fn, @SuppressWarnings("rawtypes") Iterable values) {
        weekendCriteria.orIn(Reflections.fnToFieldName(fn), values);
        return this;
    }

    public WrapperLambdaUpdate<T> orNotIn(Fn<T, Object> fn, @SuppressWarnings("rawtypes") Iterable values) {
        weekendCriteria.orNotIn(Reflections.fnToFieldName(fn), values);
        return this;
    }

    public WrapperLambdaUpdate<T> orBetween(Fn<T, Object> fn, Object value1, Object value2) {
        weekendCriteria.orBetween(Reflections.fnToFieldName(fn), value1, value2);
        return this;
    }

    public WrapperLambdaUpdate<T> orNotBetween(Fn<T, Object> fn, Object value1, Object value2) {
        weekendCriteria.orNotBetween(Reflections.fnToFieldName(fn), value1, value2);
        return this;
    }

    public WrapperLambdaUpdate<T> orLike(Fn<T, Object> fn, String value) {
        if(StringUtil.isNotBlank(value)) {
            weekendCriteria.orLike(Reflections.fnToFieldName(fn),"%" + value +"%");
        }
        return this;
    }

    public WrapperLambdaUpdate<T> orNotLike(Fn<T, Object> fn, String value) {
        if(StringUtil.isNotBlank(value)) {
            weekendCriteria.orNotLike(Reflections.fnToFieldName(fn),"%" + value +"%");
        }
        return this;
    }
    
    /**
     * 
     * @param fn   Entity::setXXXX
     * @param value 要设置的值
     * @return
     */
    public <V> WrapperLambdaUpdate<T> set(BiConsumer<T, V> fn, V value){
        if(this.entity == null) {
            this.entity =  ReflectUtil.newInstanceIfPossible(entityClass);
        }
        fn.accept(entity, value);
        return this;
    }
    
    public WrapperLambdaUpdate<T> and(WrapperLambdaUpdate<T> wrapperLambda){
        exampe.and(wrapperLambda.weekendCriteria);
        return this;
    }

    public WrapperLambdaUpdate<T> or(WrapperLambdaUpdate<T> wrapperLambda){
        exampe.or(wrapperLambda.weekendCriteria);
        return this;
    }

    public WrapperLambdaUpdate<T> createWrapper(){
        return new WrapperLambdaUpdate<>(mapper, entityClass);
    }
    
    public Integer update() {
        if(this.entity == null) {
            throw new BaseException("请先调用set方法传入要修改的值,或者调用update(t)");
        }
        return mapper.updateByExampleSelective(this.entity, exampe);
    }

    public Integer update(T t) {
        if(this.entity != null) {
            throw new BaseException("请先删除set方法");
        }
        return mapper.updateByExampleSelective(t, exampe);
    }
    public Integer updateAndNull(T t) {
        if(this.entity != null) {
            throw new BaseException("请先删除set方法");
        }
        return mapper.updateByExample(t, exampe);
    }
}
View Code

 

WrapperLambdaDelete

package tk.mybatis.mapper.lambda;


import tk.mybatis.mapper.weekend.Fn;
import tk.mybatis.mapper.weekend.reflection.Reflections;

public final class WrapperLambdaDelete<T> extends WrapperLambda<T>{

    public WrapperLambdaDelete(BaseMapper<T> mapper, Class<T> entityClass) {
        super(mapper, entityClass);
    }

    public WrapperLambdaDelete<T> eq(Fn<T, Object> fn, Object value) {
        weekendCriteria.andEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }

    public WrapperLambdaDelete<T> ne(Fn<T, Object> fn, Object value) {
        weekendCriteria.andNotEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }
    
    public WrapperLambdaDelete<T> isNull(Fn<T, Object> fn) {
        weekendCriteria.andIsNull(Reflections.fnToFieldName(fn));
        return this;
    }
    
    public WrapperLambdaDelete<T> isNotNull(Fn<T, Object> fn) {
        weekendCriteria.andIsNotNull(Reflections.fnToFieldName(fn));
        return this;
    }

    /**
     * 大于
     */
    public WrapperLambdaDelete<T> gt(Fn<T, Object> fn, Object value) {
        weekendCriteria.andGreaterThan(Reflections.fnToFieldName(fn), value);
        return this;
    }
    
    /**
     * 大于等于
     */
    public WrapperLambdaDelete<T> ge(Fn<T, Object> fn, Object value) {
        weekendCriteria.andGreaterThanOrEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }

    /**
     * 小于
     */
    public WrapperLambdaDelete<T> lt(Fn<T, Object> fn, Object value) {
        weekendCriteria.andLessThan(Reflections.fnToFieldName(fn), value);
        return this;
    }

    /**
     * 小于等于
     */
    public WrapperLambdaDelete<T> le(Fn<T, Object> fn, Object value) {
        weekendCriteria.andLessThanOrEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }

    public WrapperLambdaDelete<T> in(Fn<T, Object> fn, @SuppressWarnings("rawtypes") Iterable values) {
        weekendCriteria.andIn(Reflections.fnToFieldName(fn), values);
        return this;
    }

    public WrapperLambdaDelete<T> notIn(Fn<T, Object> fn, @SuppressWarnings("rawtypes") Iterable values) {
        weekendCriteria.andNotIn(Reflections.fnToFieldName(fn), values);
        return this;
    }

    public WrapperLambdaDelete<T> between(Fn<T, Object> fn, Object value1, Object value2) {
        weekendCriteria.andBetween(Reflections.fnToFieldName(fn), value1, value2);
        return this;
    }

    public WrapperLambdaDelete<T> notBetween(Fn<T, Object> fn, Object value1, Object value2) {
        weekendCriteria.andNotBetween(Reflections.fnToFieldName(fn), value1, value2);
        return this;
    }

    public WrapperLambdaDelete<T> leftLike(Fn<T, Object> fn, String value) {
        if(StringUtil.isNotBlank(value)) {
            weekendCriteria.andLike(Reflections.fnToFieldName(fn), "%" + value);
        }
        return this;
    }

    public WrapperLambdaDelete<T> rightLike(Fn<T, Object> fn, String value) {
        if(StringUtil.isNotBlank(value)) {
            weekendCriteria.andLike(Reflections.fnToFieldName(fn), value + "%");
        }
        return this;
    }

    public WrapperLambdaDelete<T> like(Fn<T, Object> fn, String value) {
        if(StringUtil.isNotBlank(value)) {
            weekendCriteria.andLike(Reflections.fnToFieldName(fn),"%" + value +"%");
        }
        return this;
    }

    public WrapperLambdaDelete<T> notLike(Fn<T, Object> fn, String value) {
        if(StringUtil.isNotBlank(value)) {
            weekendCriteria.andNotLike(Reflections.fnToFieldName(fn),"%" + value +"%");
        }
        return this;
    }

    public WrapperLambdaDelete<T> orIsNull(Fn<T, Object> fn) {
        weekendCriteria.orIsNull(Reflections.fnToFieldName(fn));
        return this;
    }

    public WrapperLambdaDelete<T> orIsNotNull(Fn<T, Object> fn) {
        weekendCriteria.orIsNotNull(Reflections.fnToFieldName(fn));
        return this;
    }

    /**
     * 或者相等
     */
    public WrapperLambdaDelete<T> orEq(Fn<T, Object> fn, Object value) {
        weekendCriteria.orEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }

    /**
     * 或者不等于
     */
    public WrapperLambdaDelete<T> orNe(Fn<T, Object> fn, Object value) {
        weekendCriteria.orNotEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }

    /**
     * 或者大于
     */
    public WrapperLambdaDelete<T> orGt(Fn<T, Object> fn, Object value) {
        weekendCriteria.orGreaterThan(Reflections.fnToFieldName(fn), value);
        return this;
    }

    /**
     * 或者大于等于
     */
    public WrapperLambdaDelete<T> orGe(Fn<T, Object> fn, Object value) {
        weekendCriteria.orGreaterThanOrEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }

    /**
     * 或者小于
     */
    public WrapperLambdaDelete<T> orLt(Fn<T, Object> fn, Object value) {
        weekendCriteria.orLessThan(Reflections.fnToFieldName(fn), value);
        return this;
    }

    /**
     * 或者小于等于
     */
    public WrapperLambdaDelete<T> orLe(Fn<T, Object> fn, Object value) {
        weekendCriteria.orLessThanOrEqualTo(Reflections.fnToFieldName(fn), value);
        return this;
    }

    public WrapperLambdaDelete<T> orIn(Fn<T, Object> fn, @SuppressWarnings("rawtypes") Iterable values) {
        weekendCriteria.orIn(Reflections.fnToFieldName(fn), values);
        return this;
    }

    public WrapperLambdaDelete<T> orNotIn(Fn<T, Object> fn, @SuppressWarnings("rawtypes") Iterable values) {
        weekendCriteria.orNotIn(Reflections.fnToFieldName(fn), values);
        return this;
    }

    public WrapperLambdaDelete<T> orBetween(Fn<T, Object> fn, Object value1, Object value2) {
        weekendCriteria.orBetween(Reflections.fnToFieldName(fn), value1, value2);
        return this;
    }

    public WrapperLambdaDelete<T> orNotBetween(Fn<T, Object> fn, Object value1, Object value2) {
        weekendCriteria.orNotBetween(Reflections.fnToFieldName(fn), value1, value2);
        return this;
    }

    public WrapperLambdaDelete<T> orLike(Fn<T, Object> fn, String value) {
        if(StringUtil.isNotBlank(value)) {
            weekendCriteria.orLike(Reflections.fnToFieldName(fn),"%" + value +"%");
        }
        return this;
    }

    public WrapperLambdaDelete<T> orNotLike(Fn<T, Object> fn, String value) {
        if(StringUtil.isNotBlank(value)) {
            weekendCriteria.orNotLike(Reflections.fnToFieldName(fn),"%" + value +"%");
        }
        return this;
    }
    
    public WrapperLambdaDelete<T> and(WrapperLambdaQuery<T> wrapperLambda){
        exampe.and(wrapperLambda.weekendCriteria);
        return this;
    }

    public WrapperLambdaDelete<T> or(WrapperLambdaDelete<T> wrapperLambda){
        exampe.or(wrapperLambda.weekendCriteria);
        return this;
    }

    public WrapperLambdaDelete<T> createWrapper(){
        return new WrapperLambdaDelete<>(mapper, entityClass);
    }
    
    public Integer delete() {
        return mapper.deleteByExample(exampe);
    }
}
View Code

 

标签:boot2.7,return,springboot,weekendCriteria,mysql5.7,value,Fn,public,fn
From: https://www.cnblogs.com/binz/p/17421063.html

相关文章

  • 基于springBoot框架JAVA语言汽车销售管理系统功能实现
    运行环境idea(推荐)、jdk1.8、mysql5.X(不能为8驱动不匹配)、maven项目技术springboot+vue项目描述本项目基于springboot以及Vue开发。针对汽车销售提供客户信息、车辆信息、订单信息、销售人员管理、财务报表等功能,提供经理和销售两种角色进行管理。1.安装好node环境2.......
  • SigNoz采集springboot应用metries、trace
    设置从repo的 Releases下载opentelemetry-javaagent.jar并将JAR放在您的首选目录中。JAR文件包含代理和检测库。opentelemetry-java-instrumentation添加-javaagent:path/to/opentelemetry-javaagent.jar和其他配置到您的JVM启动参数并启动您的应用程序:直接在启动命令上:java......
  • 基于springboot的宿舍管理系统实现宿管、后勤权限功能
    项目描述系统中的用户分为三类,分别为学生、宿管、后勤。这三类用户拥有不同的操作权限。系统中用到的技术包括Axios,Echarts,POI。特色功能有人性化的宿舍分配,可视化的账单管理、房间管理、记账管理,Excel文件上传后数据导入数据库等等。运行环境idea+mysql5.7+jdk1.8+maven3......
  • springboot自定义拦截器
    之前使用过滤器实现了拦截没有登录的请求,现在使用springboot的拦截器实现1.LoginCheckInterceptor.javapackagecom.minqiliang.interceptor;importcom.alibaba.fastjson.JSONObject;importcom.minqiliang.pojo.Result;importcom.minqiliang.utils.JwtUtils;importlomb......
  • java基于的springboot+vue4S店车辆管理系统,车辆销售管理系统,附源码+数据库+lw文档+PPT
    1、项目介绍首先介绍了4S店车辆管理的技术发展背景与发展现状,然后遵循软件常规开发流程,首先针对系统选取适用的语言和开发平台,根据需求分析制定模块并设计数据库结构,再根据系统总体功能模块的设计绘制系统的功能模块图,流程图以及E-R图。然后,设计框架并根据设计的框架编写代码以实......
  • springboot集成websocket
    导入依赖1<dependency>2<groupId>org.springframework.boot</groupId>3<artifactId>spring-boot-starter-websocket</artifactId>4</dependency>编写配置类@ConfigurationpublicclassWebSocketConfig{@Beanpub......
  • springboot+mybatis逆向生成xxxmapper+xxxmapper.xml和xxx实体类
    1.新建springboot工程pom文件如下<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="ht......
  • SpringBoot利用自定义注解实现多数据源
    自定义多数据源SpringBoot利用自定义注解实现多数据源,前置知识:注解、Aop、SpringBoot整合Mybaits1、搭建工程创建一个SpringBoot工程,并引入依赖<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring......
  • java基于springboot+vue的土特产在线销售平台、特产在线销售商城,附源码+数据库+lw文档
    1、项目介绍考虑到实际生活中在藏区特产销售管理方面的需要以及对该系统认真的分析,将系统权限按管理员和用户这两类涉及用户划分。(1)管理员功能需求管理员登陆后,主要模块包括首页、个人中心、用户管理、特产信息管理、特产分类管理、特产分类管理、特产评分管理、系统管理、订单......
  • SpringBoot读取Yml配置文件工具类
    SpringBoot读取Yml配置文件工具类在某些特定的环境,需要在非SpringBean中读取Yml文件,可以使用以下方式读取:需要依赖<dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>24.1-jre</v......