Caratacus 8 gadi atpakaļ
vecāks
revīzija
fb96e56ac7

+ 10 - 7
mybatis-plus/src/main/java/com/baomidou/mybatisplus/activerecord/Model.java

@@ -279,10 +279,7 @@ public abstract class Model<T extends Model> implements Serializable {
 	 */
 	public Page<T> selectPage(Page<T> page, Wrapper<T> wrapper) {
 		Map<String, Object> map = new HashMap<String, Object>();
-		if (wrapper != null && StringUtils.isNotEmpty(page.getOrderByField())) {
-			wrapper.orderBy(page.getOrderByField());
-			wrapper.allEq(page.getCondition());
-		}
+		SqlHelper.fillWrapper(page, wrapper);
 		map.put("ew", wrapper);
 		List<T> tl = sqlSession().selectList(sqlStatement(SqlMethod.SELECT_PAGE), map, page);
 		page.setRecords(tl);
@@ -347,7 +344,7 @@ public abstract class Model<T extends Model> implements Serializable {
 	 * 获取Session 默认自动提交
 	 * <p/>
 	 */
-	private SqlSession sqlSession() {
+	protected SqlSession sqlSession() {
 		return SqlHelper.sqlSession(getClass());
 	}
 
@@ -357,11 +354,17 @@ public abstract class Model<T extends Model> implements Serializable {
 	 * @param sqlMethod
 	 * @return
 	 */
-	private String sqlStatement(SqlMethod sqlMethod) {
+	protected String sqlStatement(SqlMethod sqlMethod) {
 		return sqlStatement(sqlMethod.getMethod());
 	}
 
-	private String sqlStatement(String sqlMethod) {
+	/**
+	 * 获取SqlStatement
+	 *
+	 * @param sqlMethod
+	 * @return
+	 */
+	protected String sqlStatement(String sqlMethod) {
 		return SqlHelper.table(getClass()).getSqlStatement(sqlMethod);
 	}
 

+ 13 - 0
mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/SqlHelper.java

@@ -18,6 +18,7 @@ package com.baomidou.mybatisplus.mapper;
 import com.baomidou.mybatisplus.entity.GlobalConfiguration;
 import com.baomidou.mybatisplus.entity.TableInfo;
 import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
+import com.baomidou.mybatisplus.plugins.Page;
 import com.baomidou.mybatisplus.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
 import org.apache.ibatis.logging.Log;
@@ -172,4 +173,16 @@ public class SqlHelper {
 		return null;
 	}
 
+	/**
+	 * 填充Wrapper
+	 *
+	 * @param page
+	 * @param wrapper
+	 */
+	public static void fillWrapper(Page<?> page, Wrapper<?> wrapper) {
+		if (null != wrapper) {
+			wrapper.orderBy(page.getOrderByField(), page.isAsc());
+			wrapper.allEq(page.getCondition());
+		}
+	}
 }

+ 21 - 13
mybatis-plus/src/main/java/com/baomidou/mybatisplus/service/impl/ServiceImpl.java

@@ -16,6 +16,7 @@
 package com.baomidou.mybatisplus.service.impl;
 
 import com.baomidou.mybatisplus.entity.TableInfo;
+import com.baomidou.mybatisplus.enums.SqlMethod;
 import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
 import com.baomidou.mybatisplus.mapper.BaseMapper;
 import com.baomidou.mybatisplus.mapper.SqlHelper;
@@ -163,7 +164,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
 		try {
 			int size = entityList.size();
 			for (int i = 0; i < size; i++) {
-				baseMapper.insert(entityList.get(i));
+				batchSqlSession.insert(sqlStatement(SqlMethod.INSERT_ONE), entityList.get(i));
 				if (i % batchSize == 0) {
 					batchSqlSession.flushStatements();
 				}
@@ -212,7 +213,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
 		try {
 			int size = entityList.size();
 			for (int i = 0; i < size; i++) {
-				baseMapper.updateById(entityList.get(i));
+				batchSqlSession.update(sqlStatement(SqlMethod.UPDATE_BY_ID), entityList.get(i));
 				if (i % 30 == 0) {
 					batchSqlSession.flushStatements();
 				}
@@ -272,27 +273,34 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
 
 	@SuppressWarnings({ "rawtypes", "unchecked" })
 	public Page<Map<String, Object>> selectMapsPage(Page page, Wrapper<T> wrapper) {
-		fillWrapper(page, wrapper);
+		SqlHelper.fillWrapper(page, wrapper);
 		page.setRecords(baseMapper.selectMapsPage(page, wrapper));
 		return page;
 	}
 
 	public Page<T> selectPage(Page<T> page, Wrapper<T> wrapper) {
-		fillWrapper(page, wrapper);
+		SqlHelper.fillWrapper(page, wrapper);
 		page.setRecords(baseMapper.selectPage(page, wrapper));
 		return page;
 	}
 
 	/**
-	 * 填充Wrapper
-	 * 
-	 * @param page
-	 * @param wrapper
+	 * 获取SqlStatement
+	 *
+	 * @param sqlMethod
+	 * @return
 	 */
-	protected void fillWrapper(Page<T> page, Wrapper<T> wrapper) {
-		if (null != wrapper) {
-			wrapper.orderBy(page.getOrderByField(), page.isAsc());
-			wrapper.allEq(page.getCondition());
-		}
+	protected String sqlStatement(SqlMethod sqlMethod) {
+		return sqlStatement(sqlMethod.getMethod());
+	}
+
+	/**
+	 * 获取SqlStatement
+	 *
+	 * @param sqlMethod
+	 * @return
+	 */
+	protected String sqlStatement(String sqlMethod) {
+		return SqlHelper.table(currentModleClass()).getSqlStatement(sqlMethod);
 	}
 }