浏览代码

SelectOne、SelectCount支持

Caratacus 8 年之前
父节点
当前提交
a0f97df7a5

+ 22 - 0
mybatis-plus/src/main/java/com/baomidou/framework/service/IService.java

@@ -243,6 +243,17 @@ public interface IService<T, PK> {
 	 */
 	int selectCount(T entity);
 
+	/**
+	 * <p>
+	 * 根据 EntityWrapper 条件,查询总记录数
+	 * </p>
+	 *
+	 * @param entityWrapper
+	 *            实体对象
+	 * @return int
+	 */
+	int selectCount(EntityWrapper<T> entityWrapper);
+
 	/**
 	 * <p>
 	 * 查询列表
@@ -254,6 +265,17 @@ public interface IService<T, PK> {
 	 */
 	List<T> selectList(EntityWrapper<T> entityWrapper);
 
+	/**
+	 * <p>
+	 * 根据 EntityWrapper,查询一条记录
+	 * </p>
+	 *
+	 * @param entityWrapper
+	 *            实体对象
+	 * @return T
+	 */
+	T selectOne(EntityWrapper<T> entityWrapper);
+
 	/**
 	 * <p>
 	 * 翻页查询

+ 13 - 3
mybatis-plus/src/main/java/com/baomidou/framework/service/impl/ServiceImpl.java

@@ -20,6 +20,7 @@ import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
 import com.baomidou.mybatisplus.mapper.BaseMapper;
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.baomidou.mybatisplus.plugins.Page;
+import com.baomidou.mybatisplus.toolkit.CollectionUtil;
 import com.baomidou.mybatisplus.toolkit.ReflectionKit;
 import com.baomidou.mybatisplus.toolkit.TableInfo;
 import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
@@ -168,9 +169,18 @@ public class ServiceImpl<M extends BaseMapper<T, PK>, T, PK extends Serializable
 		return baseMapper.selectOne(entity);
 	}
 
-	public int selectCount(T entity) {
-		return baseMapper.selectCount(entity);
-	}
+    public T selectOne(EntityWrapper<T> entityWrapper) {
+        List<T> list = baseMapper.selectList(entityWrapper);
+        return CollectionUtil.isNotEmpty(list) ? list.get(0) : null;
+    }
+
+    public int selectCount(T entity) {
+        return baseMapper.selectCount(entity);
+    }
+
+    public int selectCount(EntityWrapper<T> entityWrapper) {
+        return baseMapper.selectCountByEW(entityWrapper);
+    }
 
 	public List<T> selectList(EntityWrapper<T> entityWrapper) {
 		return baseMapper.selectList(entityWrapper);

+ 32 - 4
mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/AutoSqlInjector.java

@@ -118,6 +118,7 @@ public class AutoSqlInjector implements ISqlInjector {
 			this.injectSelectByMapSql(mapperClass, modelClass, table);
 			this.injectSelectOneSql(mapperClass, modelClass, table);
 			this.injectSelectCountSql(mapperClass, modelClass, table);
+			this.injectSelectCountByEWSql(SqlMethod.SELECT_COUNT_EW, mapperClass, modelClass, table);
 			this.injectSelectListSql(SqlMethod.SELECT_LIST, mapperClass, modelClass, table);
 			this.injectSelectListSql(SqlMethod.SELECT_PAGE, mapperClass, modelClass, table);
 
@@ -487,13 +488,42 @@ public class AutoSqlInjector implements ISqlInjector {
 	 * <p>
 	 * 注入实体查询记录列表 SQL 语句
 	 * </p>
-	 * 
+	 *
 	 * @param sqlMethod
 	 * @param mapperClass
 	 * @param modelClass
 	 * @param table
 	 */
 	protected void injectSelectListSql(SqlMethod sqlMethod, Class<?> mapperClass, Class<?> modelClass, TableInfo table) {
+		String where = getStringBuilder(table);
+		String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(table, true), table.getTableName(), where);
+		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
+		this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, table);
+	}
+	/**
+	 * <p>
+	 * 注入EntityWrapper查询总记录数 SQL 语句
+	 * </p>
+	 *
+	 * @param sqlMethod
+	 * @param mapperClass
+	 * @param modelClass
+	 * @param table
+	 */
+	protected void injectSelectCountByEWSql(SqlMethod sqlMethod, Class<?> mapperClass, Class<?> modelClass, TableInfo table) {
+		String where = getStringBuilder(table);
+		String sql = String.format(sqlMethod.getSql(), table.getTableName(), where);
+		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
+		this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, Integer.class, null);
+	}
+
+	/**
+	 * EntityWrapper方式获取select where
+	 *
+	 * @param table
+	 * @return String
+	 */
+	private String getStringBuilder(TableInfo table) {
 		StringBuilder where = new StringBuilder("\n<if test=\"ew!=null\">");
 		where.append("\n<if test=\"ew.entity!=null\">\n<where>");
 		where.append("\n<if test=\"ew.entity.").append(table.getKeyProperty()).append("!=null\">\n");
@@ -508,9 +538,7 @@ public class AutoSqlInjector implements ISqlInjector {
 		where.append("\n</where>\n</if>");
 		where.append("\n<if test=\"ew.sqlSegment!=null\">\n${ew.sqlSegment}\n</if>");
 		where.append("\n</if>");
-		String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(table, true), table.getTableName(), where.toString());
-		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
-		this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, table);
+		return where.toString();
 	}
 
 	/**

+ 12 - 3
mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/BaseMapper.java

@@ -15,13 +15,13 @@
  */
 package com.baomidou.mybatisplus.mapper;
 
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.session.RowBounds;
+
 import java.io.Serializable;
 import java.util.List;
 import java.util.Map;
 
-import org.apache.ibatis.annotations.Param;
-import org.apache.ibatis.session.RowBounds;
-
 /**
  * <p>
  * Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
@@ -244,4 +244,13 @@ public interface BaseMapper<T, PK extends Serializable> {
 	 */
 	List<T> selectPage( RowBounds rowBounds, @Param("ew" ) EntityWrapper<T> entityWrapper);
 
+	/**
+	 * <p>
+	 * 根据 EntityWrapper 条件,查询总记录数
+	 * </p>
+	 * @param entityWrapper
+	 * 				实体对象
+	 * @return int
+	 */
+	int selectCountByEW(@Param("ew") EntityWrapper<T> entityWrapper);
 }

+ 1 - 0
mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/SqlMethod.java

@@ -59,6 +59,7 @@ public enum SqlMethod {
 	SELECT_BATCH("selectBatchIds", "根据ID集合,批量查询数据", "<script>SELECT %s FROM %s WHERE %s IN (%s)</script>"),
 	SELECT_ONE("selectOne", "查询满足条件一条数据", "<script>SELECT %s FROM %s %s</script>"),
 	SELECT_COUNT("selectCount", "查询满足条件总记录数", "<script>SELECT COUNT(1) FROM %s %s</script>"),
+	SELECT_COUNT_EW("selectCountByEW", "查询满足条件总记录数", "<script>SELECT COUNT(1) FROM %s %s</script>"),
 	SELECT_LIST("selectList", "查询满足条件所有数据", "<script>SELECT %s FROM %s %s</script>"),
 	SELECT_PAGE("selectPage", "查询满足条件所有数据(并翻页)", "<script>SELECT %s FROM %s %s</script>");