Reference article: https://www.oschina.net/news/91631/mapper-3-4-6-released
The following code requires the use of JDK1.8 environment, because of the use of the new features of the interface
maven version dependency:
<dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.4.6</version> </dependency>
spring reference:
<dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>1.1.7</version> </dependency>
Tools:
Import tk.mybatis.mapper.entity.Example; Import tk.mybatis.mapper.weekend.WeekendSqls; Import the java.util.Iterator; Import a java.util.Map; / ** * * query configured * / public interface SQLCondition { / ** * * { @code Key attribute equal} { @code value recording} * @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); } /*** * 模糊查询{@codekey} {properties like @code value not tested} / not constructed according to the reconstruction mode * @param Key * @param value * @param targetClass * @param <T> * @return * / default <T> Example sqlLike (String Key, String value, Class <T> targetClass) { Example Example = new new Example (targetClass); Example.Criteria Criteria = example.createCriteria (); criteria.andLike (Key, value); return Example; } }
Usage:
In your interface inherits from this interface
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(); }
Implementation class
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, (the Map <String , Object> the params) { // the Map value pairs used to construct the inside sql and conditions, total positive sequence field ordering return servicePkgMapper.selectByExample (sqlAnd (the 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()); } }