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