本文参考:https://www.oschina.net/news/91631/mapper-3-4-6-released
以下代码需要使用JDK1.8环境,因为使用了接口的新特性
maven版本依赖:
<dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.4.6</version> </dependency>
spring引用方式:
<dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>1.1.7</version> </dependency>
工具类:
import tk.mybatis.mapper.entity.Example; import tk.mybatis.mapper.weekend.WeekendSqls; import java.util.Iterator; import java.util.Map; /*** * 构造查询条件 */ public interface SQLCondition { /*** * {@code key}属性等于{@code value}的记录 * @param key * @param value * @param targetClass * @param <T> * @return */ default <T> Example.Builder sqlAnd(String key, Object value, Class<T> targetClass) { return sqlAnd(key,value,targetClass,new String[0]); } static <T> Example.Builder builder(Class<T> targetClass) { return Example.builder(targetClass); } default <T> Example.Builder sqlAnd(String key, Object value, Class<T> targetClass, String ...excludeProperties) { return builder(targetClass).andWhere(WeekendSqls.custom().andEqualTo(key,value)); } default <T> Example.Builder sqlAnd(Map<String,Object> nameValuePair, Class<T> targetClass) { WeekendSqls<T> criteria = WeekendSqls.custom(); for (Iterator<Map.Entry<String, Object>> it = nameValuePair.entrySet().iterator();it.hasNext();) { Map.Entry<String, Object> entry = it.next(); String name = entry.getKey(); Object value = entry.getValue(); criteria.andEqualTo(name,value); } return builder(targetClass).where(criteria); } default <T> Example.Builder orderBy(String fieldName, Class<T> targetClass) { return builder(targetClass).orderBy(fieldName); } default <T> Example.Builder orderBy(Class<T> targetClass,String ...fieldNames) { return builder(targetClass).orderBy(fieldNames); } default <T> Example.Builder orderByDesc(String fieldName, Class<T> targetClass) { return builder(targetClass).orderByDesc(fieldName); } default <T> Example.Builder orderByDesc(Class<T> targetClass,String fieldNames) { return builder(targetClass).orderByDesc(fieldNames); } /*** * 模糊查询{@code key}属性like {@code value} 尚未测试/未按照构造器模式重构 * @param key * @param value * @param targetClass * @param <T> * @return */ default <T> Example sqlLike(String key, String value, Class<T> targetClass) { Example example = new Example(targetClass); Example.Criteria criteria = example.createCriteria(); criteria.andLike(key,value); return example; } }
食用方法:
在你的接口继承自这个接口
import com.xxx.web.open.bean.ApiServicePkg; import com.xxx.web.open.dto.ApiServicePkgDto; import java.util.List; import java.util.Map; public interface ApiServicePkgService extends SQLCondition { ApiServicePkg getById(Integer packageId); ApiServicePkgDto getDtoById(Integer packageId); List<ApiServicePkg> queryList(Map<String,Object> params); List<ApiServicePkg> queryList(); List<ApiServicePkgDto> queryDtoList(); }
实现类
import com.xxx.web.open.bean.ApiServicePkg; import com.xxx.web.open.dto.ApiServicePkgDto; import com.xxx.web.open.mapper.ApiServicePkgMapper; import com.xxx.web.open.service.ApiServicePkgService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Collections; import java.util.List; import java.util.Map; @Service public class ApiServicePkgServiceImpl implements ApiServicePkgService { @Autowired ApiServicePkgMapper servicePkgMapper; @Override public ApiServicePkg getById(Integer packageId) { return servicePkgMapper.getById(packageId); } @Override public ApiServicePkgDto getDtoById(Integer packageId) { return servicePkgMapper.getDtoById(packageId); } @Override public List<ApiServicePkg> queryList(Map<String, Object> params) { //把Map里面的键值对用来构造sql and条件,total字段正序排序 return servicePkgMapper.selectByExample(sqlAnd(params,ApiServicePkg.class).orderByAsc("total").build()); } @Override public List<ApiServicePkg> queryList() { //查询enabled=1(true)的行,根据total字段正序排序 return servicePkgMapper.selectByExample(sqlAnd("enabled",true,ApiServicePkg.class).orderByAsc("total").build()); } @Override public List<ApiServicePkgDto> queryDtoList() { return servicePkgMapper.queryDtoList(Collections.emptyMap()); } }
2020-2-12更新
新增根据字段名字段between and,大于等于小于、大于等于小于等于、大于小于等于等sql判断功能
重构后如下:
import com.google.common.collect.ImmutableMap; import org.springframework.util.CollectionUtils; import tk.mybatis.mapper.entity.Example; import tk.mybatis.mapper.weekend.WeekendSqls; import java.util.Collections; import java.util.Iterator; import java.util.Map; /*** * 构造动态查询条件 */ public interface DynamicSQLCondition { /*** * 构造一个默认对象 * @param targetClass * @param <T> * @return */ static <T> Example.Builder builder(Class<T> targetClass) { return Example.builder(targetClass); } /*** * @{code key}大于start且小于等于end * @param key * @param start * @param end * @param targetClass * @param <T> * @param <V> * @return */ default <T,V> Example sqlGreaterThanAndLessThanOrEqualTo(String key, V start, V end, Class<T> targetClass) { return sqlGreaterThanAndLessThanOrEqualTo(null,key,start,end,targetClass); } /*** * @{code key}大于start且小于等于end * @param param * @param key * @param start * @param end * @param targetClass * @param <T> * @param <V> * @return */ default <T,V> Example sqlGreaterThanAndLessThanOrEqualTo(Map<String, Object> param, String key, V start, V end, Class<T> targetClass) { Example example = new Example(targetClass); Example.Criteria criteria = example.createCriteria(); criteria.andGreaterThan(key,start); criteria.andLessThanOrEqualTo(key,end); if(!CollectionUtils.isEmpty(param)) criteria.andAllEqualTo(param); return example; } /*** * @{code key}大于等于start且小于end * @param key * @param start * @param end * @param targetClass * @param <T> * @param <V> * @return */ default <T,V> Example sqlGreaterThanOrEqualToAndLessThan(String key, V start, V end, Class<T> targetClass) { return sqlGreaterThanOrEqualToAndLessThan(null,key,start,end,targetClass); } default <T,V> Example sqlGreaterThanOrEqualToAndLessThan(Map<String, Object> param, String key, V start, V end, Class<T> targetClass) { Example example = new Example(targetClass); Example.Criteria criteria = example.createCriteria(); criteria.andGreaterThanOrEqualTo(key,start); criteria.andLessThan(key,end); if(!CollectionUtils.isEmpty(param)) criteria.andAllEqualTo(param); return example; } /*** * @{code key}大于等于start且小于end * @param key * @param start * @param end * @param targetClass * @param <T> * @param <V> * @return */ default <T,V> Example sqlGreaterThanOrEqualToAndLessThanOrEqualTo(String key, V start, V end, Class<T> targetClass) { return sqlGreaterThanOrEqualToAndLessThanOrEqualTo(null,key,start,end,targetClass); } default <T,V> Example sqlGreaterThanOrEqualToAndLessThanOrEqualTo(Map<String, Object> param, String key, V start, V end, Class<T> targetClass) { Example example = new Example(targetClass); Example.Criteria criteria = example.createCriteria(); criteria.andGreaterThanOrEqualTo(key,start); criteria.andLessThanOrEqualTo(key,end); if(!CollectionUtils.isEmpty(param)) criteria.andAllEqualTo(param); return example; } /*** * @{code key}大于start且小于end * @param key * @param start * @param end * @param targetClass * @param <T> * @param <V> * @return */ default <T,V> Example sqlBetween(String key, V start, V end, Class<T> targetClass) { return sqlBetween(null,key,start,end,targetClass); } /*** * @{code key}大于start且小于end * @param param * @param key * @param start * @param end * @param targetClass * @param <T> * @param <V> * @return */ default <T,V> Example sqlBetween(Map<String, Object> param, String key, V start, V end, Class<T> targetClass) { Example example = new Example(targetClass); Example.Criteria criteria = example.createCriteria(); criteria.andBetween(key, start, end); if(!CollectionUtils.isEmpty(param)) criteria.andAllEqualTo(param); return example; } /*** * {@link DynamicSQLCondition.sqlAnd(String,Object,Class<T>)} * @param k1 * @param v1 * @param targetClass * @param <T> * @param <V> * @return */ default <T, V> Example.Builder sqlAnd(String k1, V v1, Class<T> targetClass) { return sqlAnd(ImmutableMap.of(k1, v1), targetClass); } default <T, V> Example.Builder sqlAnd(String k1, V v1, String k2, V v2, Class<T> targetClass) { return sqlAnd(ImmutableMap.of(k1, v1, k2, v2), targetClass); } default <T, V> Example.Builder sqlAnd(String k1, V v1, String k2, V v2, String k3, V v3, Class<T> targetClass) { return sqlAnd(ImmutableMap.of(k1, v1, k2, v2, k3, v3), targetClass); } default <T, V> Example.Builder sqlAnd(String k1, V v1, String k2, V v2, String k3, V v3, String k4, V v4, Class<T> targetClass) { return sqlAnd(ImmutableMap.of(k1, v1, k2, v2, k3, v3, k4, v4), targetClass); } default <T, K, V> Example.Builder sqlAnd0(String key, V value, Class<T> targetClass, String... excludeProperties) { // Example example = new Example(targetClass); // Example.Criteria criteria = example.createCriteria(); // criteria.andEqualTo(key,value); // if(excludeProperties!=null) // { // if(excludeProperties.length>0) // { // example.excludeProperties(excludeProperties); // } // } // return example; return builder(targetClass).andWhere(WeekendSqls.custom().andEqualTo(key, value)); } /*** * {@code key}属性等于{@code value}的记录 * @param key * @param value * @param targetClass * @param <T> * @return */ @Deprecated default <T> Example.Builder sqlAnd0(String key, Object value, Class<T> targetClass) { return sqlAnd(Collections.singletonMap(key,value),targetClass); } default <T> Example.Builder sqlAnd(String key, Object value, Class<T> targetClass, String... excludeProperties) { // Example example = new Example(targetClass); // Example.Criteria criteria = example.createCriteria(); // criteria.andEqualTo(key,value); // if(excludeProperties!=null) // { // if(excludeProperties.length>0) // { // example.excludeProperties(excludeProperties); // } // } // return example; return builder(targetClass).andWhere(WeekendSqls.custom().andEqualTo(key, value)); } default <T> Example.Builder sqlAnd(Map<String, Object> nameValuePair, Class<T> targetClass) { // Example example = new Example(targetClass); // Example.Criteria criteria = example.createCriteria(); // // for (Iterator<Map.Entry<String, Object>> it = nameValuePair.entrySet().iterator();it.hasNext();) // { // Map.Entry<String, Object> entry = it.next(); // String name = entry.getKey(); // Object value = entry.getValue(); // //criteria.andAllEqualTo(nameValuePair); // criteria.andEqualTo(name,value); // } // //criteria.andEqualTo(key,value); WeekendSqls<T> criteria = WeekendSqls.custom(); for (Iterator<Map.Entry<String, Object>> it = nameValuePair.entrySet().iterator(); it.hasNext(); ) { Map.Entry<String, Object> entry = it.next(); String name = entry.getKey(); Object value = entry.getValue(); criteria.andEqualTo(name, value); } return builder(targetClass).where(criteria); } default <T> Example.Builder orderBy(String fieldName, Class<T> targetClass) { return builder(targetClass).orderBy(fieldName); // Example example = new Example(targetClass); // example.orderBy(fieldName); // return example; } default <T> Example.Builder orderBy(Class<T> targetClass, String... fieldNames) { return builder(targetClass).orderBy(fieldNames); } default <T> Example.Builder orderByDesc(String fieldName, Class<T> targetClass) { return builder(targetClass).orderByDesc(fieldName); } default <T> Example.Builder orderByDesc(Class<T> targetClass, String fieldNames) { return builder(targetClass).orderByDesc(fieldNames); } /*** * 模糊查询{@code key}属性like {@code value} * @param key * @param value * @param targetClass * @param <T> * @return */ default <T> Example sqlLike(String key, String value, Class<T> targetClass) { Example example = new Example(targetClass); Example.Criteria criteria = example.createCriteria(); criteria.andLike(key, value); return example; } }
新增代码使用方法:
public interface ApiServiceConsumerService extends DynamicSQLCondition{ int queryCount(Integer serviceId,String startTime, String endTime); } @Service public class ApiServiceConsumerServiceImpl implements ApiServiceConsumerService { @Override public int queryCount(Integer serviceId, String startTime, String endTime) { Example example = sqlGreaterThanOrEqualToAndLessThanOrEqualTo(Collections.singletonMap("serviceId", serviceId), "createTime", startTime, endTime, ApiServiceConsumer.class); return serviceConsumerMapper.selectCountByExample(example); } }