瀏覽代碼

注入ByMap方法添加校验(根据Mybatis-Plus全局校验机制校验)

Caratacus 8 年之前
父節點
當前提交
2847aa2755

+ 21 - 15
mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/AutoSqlInjector.java

@@ -15,13 +15,13 @@
  */
 package com.baomidou.mybatisplus.mapper;
 
-import java.lang.reflect.ParameterizedType;
-import java.lang.reflect.Type;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.logging.Logger;
-
+import com.baomidou.mybatisplus.MybatisConfiguration;
+import com.baomidou.mybatisplus.annotations.FieldStrategy;
+import com.baomidou.mybatisplus.annotations.IdType;
+import com.baomidou.mybatisplus.toolkit.SqlReservedWords;
+import com.baomidou.mybatisplus.toolkit.TableFieldInfo;
+import com.baomidou.mybatisplus.toolkit.TableInfo;
+import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
 import org.apache.ibatis.builder.MapperBuilderAssistant;
 import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
 import org.apache.ibatis.executor.keygen.KeyGenerator;
@@ -34,13 +34,12 @@ import org.apache.ibatis.scripting.LanguageDriver;
 import org.apache.ibatis.scripting.defaults.RawSqlSource;
 import org.apache.ibatis.session.Configuration;
 
-import com.baomidou.mybatisplus.MybatisConfiguration;
-import com.baomidou.mybatisplus.annotations.FieldStrategy;
-import com.baomidou.mybatisplus.annotations.IdType;
-import com.baomidou.mybatisplus.toolkit.SqlReservedWords;
-import com.baomidou.mybatisplus.toolkit.TableFieldInfo;
-import com.baomidou.mybatisplus.toolkit.TableInfo;
-import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.logging.Logger;
 
 
 /**
@@ -537,14 +536,21 @@ public class AutoSqlInjector implements ISqlInjector {
 	protected String sqlWhereByMap() {
 		StringBuilder where = new StringBuilder();
 		where.append("\n<if test=\"cm!=null and !cm.isEmpty\">");
-		where.append("\n WHERE ");
+		where.append("\n<where> ");
 		where.append("\n<foreach collection=\"cm.keys\" item=\"k\" separator=\"AND\"> ");
+		if (MybatisConfiguration.FIELD_STRATEGY == FieldStrategy.NOT_EMPTY){
+			where.append("\n<if test=\"cm[k] != null and cm[k] != ''\">");
+		}else{
+			where.append("\n<if test=\"cm[k] != null\">");
+		}
 		if (DBType.MYSQL.equals(dbType)) {
 			where.append("\n`${k}` = #{cm[${k}]}");
 		}else{
 			where.append("\n${k} = #{cm[${k}]}");
 		}
+		where.append("\n</if>");
 		where.append("\n</foreach>");
+		where.append("\n</where> ");
 		where.append("\n</if>");
 		return where.toString();
 	}

+ 31 - 11
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/NoXMLTest.java

@@ -16,14 +16,15 @@
 package com.baomidou.mybatisplus.test.mysql;
 
 import com.baomidou.mybatisplus.MybatisSessionFactoryBuilder;
-import com.baomidou.mybatisplus.test.mysql.entity.Test;
 import com.baomidou.mybatisplus.toolkit.IdWorker;
-
 import org.apache.ibatis.session.SqlSession;
 import org.apache.ibatis.session.SqlSessionFactory;
+import org.junit.Test;
 
 import java.io.InputStream;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 /**
  * <p>
@@ -34,23 +35,42 @@ import java.util.List;
  * @date 2016-09-26
  */
 public class NoXMLTest {
-
-	public static void main(String[] args) {
-		/*
-		 * 加载配置文件
-		 */
+	private SqlSession sqlSession;
+	{
 		InputStream in = NoXMLTest.class.getClassLoader().getResourceAsStream("mysql-config.xml");
 		MybatisSessionFactoryBuilder mf = new MybatisSessionFactoryBuilder();
 		SqlSessionFactory sessionFactory = mf.build(in);
-		SqlSession sqlSession = sessionFactory.openSession();
+		sqlSession = sessionFactory.openSession();
+	}
+
+	/**
+	 * 测试ByMap忽略null
+	 */
+	@Test
+	public void test1() {
+		/**
+		 * 查询是否有结果
+		 */
+		TestMapper testMapper = sqlSession.getMapper(TestMapper.class);
+		Map<String, Object> map = new HashMap();
+		map.put("type", null);
+		map.put("id", null);
+		System.out.println(testMapper.selectByMap(map));
+	}
+
+	/**
+	 * 测试没有XML注入CRUD
+	 */
+	@Test
+	public void test2() {
 		/**
 		 * 查询是否有结果
 		 */
 		TestMapper testMapper = sqlSession.getMapper(TestMapper.class);
-		testMapper.insert(new Test(IdWorker.getId(), "Caratacus"));
-		List<Test> tests = testMapper.selectList(null);
+		testMapper.insert(new com.baomidou.mybatisplus.test.mysql.entity.Test(IdWorker.getId(), "Caratacus"));
+		List<com.baomidou.mybatisplus.test.mysql.entity.Test> tests = testMapper.selectList(null);
 		if (null != tests) {
-			for (Test test : tests) {
+			for (com.baomidou.mybatisplus.test.mysql.entity.Test test : tests) {
 				System.out.println("id:"+test.getId()+ " , type:" + test.getType());
 			}
 		} else {