Browse Source

完善saveOrUpdate

Caratacus 9 years ago
parent
commit
e0e7949373

+ 24 - 14
mybatis-plus/src/main/java/com/baomidou/framework/service/IService.java

@@ -15,12 +15,12 @@
  */
 package com.baomidou.framework.service;
 
-import java.util.List;
-import java.util.Map;
-
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.baomidou.mybatisplus.plugins.Page;
 
+import java.util.List;
+import java.util.Map;
+
 /**
  * <p>
  * 顶级 Service
@@ -31,17 +31,6 @@ import com.baomidou.mybatisplus.plugins.Page;
  */
 public interface IService<T, PK> {
 
-	/**
-	 * <p>
-	 * TableId 注解存在更新记录,否插入一条记录
-	 * </p>
-	 * 
-	 * @param entity
-	 *            实体对象
-	 * @return boolean
-	 */
-	boolean insertOrUpdate(T entity);
-
 	/**
 	 * <p>
 	 * 插入一条记录
@@ -178,6 +167,27 @@ public interface IService<T, PK> {
 	 */
 	boolean updateBatchById(List<T> entityList);
 
+	/**
+	 * <p>
+	 * TableId 注解存在更新记录,否插入一条记录
+	 * </p>
+	 *
+	 * @param entity
+	 *            实体对象
+	 * @return boolean
+	 */
+	boolean insertOrUpdate(T entity);
+	/**
+	 * <p>
+	 * TableId 注解存在更新记录,否插入一条记录 (选择字段, null 字段不插入)
+	 * </p>
+	 *
+	 * @param entity
+	 *            实体对象
+	 * @return boolean
+	 */
+	boolean insertOrUpdateSelective(T entity);
+
 	/**
 	 * <p>
 	 * 根据 ID 查询

+ 32 - 12
mybatis-plus/src/main/java/com/baomidou/framework/service/impl/ServiceImpl.java

@@ -15,20 +15,20 @@
  */
 package com.baomidou.framework.service.impl;
 
-import java.io.Serializable;
-import java.lang.reflect.Method;
-import java.util.List;
-import java.util.Map;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.transaction.annotation.Transactional;
-
 import com.baomidou.framework.service.IService;
 import com.baomidou.mybatisplus.mapper.BaseMapper;
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.baomidou.mybatisplus.plugins.Page;
+import com.baomidou.mybatisplus.toolkit.StringUtils;
 import com.baomidou.mybatisplus.toolkit.TableInfo;
 import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.io.Serializable;
+import java.lang.reflect.Method;
+import java.util.List;
+import java.util.Map;
 
 /**
  * <p>
@@ -54,19 +54,31 @@ public class ServiceImpl<M extends BaseMapper<T, PK>, T, PK extends Serializable
 		return result >= 1;
 	}
 
+	/**
+	 * <p>
+	 * TableId 注解存在更新记录,否插入一条记录
+	 * </p>
+	 *
+	 * @param entity
+	 *            实体对象
+	 * @param selective
+	 *            true 选择字段 false 不选择字段
+	 * @return boolean
+	 */
 	@Transactional(rollbackFor = Exception.class)
-	public boolean insertOrUpdate(T entity) {
+	public boolean insertOrUpdate(T entity, boolean selective) {
 		if (null != entity) {
 			Class<?> cls = entity.getClass();
 			TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
 			if (null != tableInfo) {
 				try {
-					Method m = cls.getMethod("get" + tableInfo.getKeyProperty());
+					// TODO 没有@TableId 是否应该抛出异常
+					Method m = cls.getMethod("get" + StringUtils.capitalize(tableInfo.getKeyProperty()));
 					Object idVal = m.invoke(entity);
 					if (null != idVal) {
-						return updateById(entity);
+						return selective ? updateSelectiveById(entity) : updateById(entity);
 					} else {
-						return insert(entity);
+						return selective ? insertSelective(entity) : insert(entity);
 					}
 				} catch (Exception e) {
 					e.printStackTrace();
@@ -76,6 +88,14 @@ public class ServiceImpl<M extends BaseMapper<T, PK>, T, PK extends Serializable
 		return false;
 	}
 
+	public boolean insertOrUpdate(T entity) {
+		return insertOrUpdate(entity, false);
+	}
+
+	public boolean insertOrUpdateSelective(T entity) {
+		return insertOrUpdate(entity, true);
+	}
+
 	@Transactional(rollbackFor = Exception.class)
 	public boolean insert(T entity) {
 		return retBool(baseMapper.insert(entity));

+ 22 - 1
mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit/StringUtils.java

@@ -151,7 +151,7 @@ public class StringUtils {
 	 * 使用单引号包含字符串
 	 * </p>
 	 *
-	 * @param srcStr
+	 * @param obj
 	 *            原字符串
 	 * @return 单引号包含的原字符串
 	 */
@@ -163,4 +163,25 @@ public class StringUtils {
 		return srcStr;
 	}
 
+	/**
+	 * 字符串第一个字母大写
+	 * 
+	 * @param str
+	 * @return
+	 */
+	public static String capitalize(final String str) {
+		int strLen;
+		if (str == null || (strLen = str.length()) == 0) {
+			return str;
+		}
+
+		final char firstChar = str.charAt(0);
+		if (Character.isTitleCase(firstChar)) {
+			// already capitalized
+			return str;
+		}
+
+		return new StringBuilder(strLen).append(Character.toTitleCase(firstChar)).append(str.substring(1)).toString();
+	}
+
 }