Browse Source

Merge branch 'dev'

青苗 8 years ago
parent
commit
1b82ba6fba

+ 9 - 3
mybatis-plus/src/main/java/com/baomidou/mybatisplus/annotations/TableName.java

@@ -22,7 +22,7 @@ import java.lang.annotation.Target;
 
 /**
  * <p>
- * 数据库表
+ * 数据库表相关
  * </p>
  * 
  * @author hubin
@@ -34,9 +34,15 @@ public @interface TableName {
 
 	/*
 	 * <p>
-	 * 实体对应的表名【必须】
+	 * 实体对应的表名
 	 * </p>
 	 */
-	String value();
+	String value() default "";
 
+	/*
+	 * <p>
+	 * 实体映射结果集
+	 * </p>
+	 */
+	String resultMap() default "";
 }

+ 58 - 26
mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/AutoSqlInjector.java

@@ -251,7 +251,7 @@ public class AutoSqlInjector implements ISqlInjector {
 		SqlMethod sqlMethod = SqlMethod.DELETE_SELECTIVE;
 		String sql = String.format(sqlMethod.getSql(), table.getTableName(), sqlWhere(table, false));
 		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
-		this.addMappedStatement(mapperClass, sqlMethod, sqlSource, SqlCommandType.DELETE, Integer.class);
+		this.addDeleteMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource);
 	}
 	
 	/**
@@ -267,7 +267,7 @@ public class AutoSqlInjector implements ISqlInjector {
 		SqlMethod sqlMethod = SqlMethod.DELETE_BY_MAP;
 		String sql = String.format(sqlMethod.getSql(), table.getTableName(), sqlWhereByMap());
 		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, Map.class);
-		this.addMappedStatement(mapperClass, sqlMethod, sqlSource, SqlCommandType.DELETE, Integer.class);
+		this.addDeleteMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource);
 	}
 
 	/**
@@ -296,7 +296,7 @@ public class AutoSqlInjector implements ISqlInjector {
 			String sql = String.format(sqlMethod.getSql(), table.getTableName(), table.getKeyColumn(), table.getKeyColumn());
 			sqlSource = new RawSqlSource(configuration, sql, Object.class);
 		}
-		this.addMappedStatement(mapperClass, sqlMethod, sqlSource, SqlCommandType.DELETE, Integer.class);
+		this.addDeleteMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource);
 	}
 
 	/**
@@ -410,7 +410,7 @@ public class AutoSqlInjector implements ISqlInjector {
 			sqlSource = new RawSqlSource(configuration, String.format(sqlMethod.getSql(), sqlSelectColumns(table, false),
 					table.getTableName(), table.getKeyColumn(), table.getKeyProperty()), Object.class);
 		}
-		this.addMappedStatement(mapperClass, sqlMethod, sqlSource, SqlCommandType.SELECT, modelClass);
+		this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, table);
 	}
 	
 	/**
@@ -426,7 +426,7 @@ public class AutoSqlInjector implements ISqlInjector {
 		SqlMethod sqlMethod = SqlMethod.SELECT_BY_MAP;
 		String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(table, false), table.getTableName(), sqlWhereByMap());
 		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, Map.class);
-		this.addMappedStatement(mapperClass, sqlMethod, sqlSource, SqlCommandType.SELECT, modelClass);
+		this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, table);
 	}
 
 	/**
@@ -443,7 +443,7 @@ public class AutoSqlInjector implements ISqlInjector {
 		SqlMethod sqlMethod = SqlMethod.SELECT_ONE;
 		String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(table, false), table.getTableName(), sqlWhere(table, false));
 		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
-		this.addMappedStatement(mapperClass, sqlMethod, sqlSource, SqlCommandType.SELECT, modelClass);
+		this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, table);
 	}
 	
 	/**
@@ -460,7 +460,7 @@ public class AutoSqlInjector implements ISqlInjector {
 		SqlMethod sqlMethod = SqlMethod.SELECT_COUNT;
 		String sql = String.format(sqlMethod.getSql(), table.getTableName(), sqlWhere(table, true));
 		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
-		this.addMappedStatement(mapperClass, sqlMethod, sqlSource, SqlCommandType.SELECT, Integer.class);
+		this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, Integer.class, null);
 	}
 	
 	/**
@@ -490,7 +490,7 @@ public class AutoSqlInjector implements ISqlInjector {
 		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.addMappedStatement(mapperClass, sqlMethod, sqlSource, SqlCommandType.SELECT, modelClass);
+		this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, table);
 	}
 
 	/**
@@ -531,6 +531,16 @@ public class AutoSqlInjector implements ISqlInjector {
 	 * @return
 	 */
 	protected String sqlSelectColumns(TableInfo table, boolean entityWrapper) {
+		/*
+		 * 存在 resultMap 映射返回 *
+		 */
+		if (null != table.getResultMap()) {
+			return "*";
+		}
+
+		/*
+		 * 返回所有查询字段内容
+		 */
 		StringBuilder columns = new StringBuilder();
 		if (entityWrapper) {
 			columns.append("<choose><when test=\"ew != null and ew.sqlSelect != null\">${ew.sqlSelect}</when><otherwise>");
@@ -598,32 +608,54 @@ public class AutoSqlInjector implements ISqlInjector {
 		return where.toString();
 	}
 
-	protected MappedStatement addMappedStatement(Class<?> mapperClass, SqlMethod sm, SqlSource sqlSource,
-			SqlCommandType sqlCommandType, Class<?> resultType) {
-		return this.addMappedStatement(mapperClass, sm.getMethod(), sqlSource, sqlCommandType, resultType);
-	}
+	/*
+	 * 查询
+	 */
+	public MappedStatement addSelectMappedStatement(Class<?> mapperClass, String id, SqlSource sqlSource,
+			Class<?> resultType, TableInfo table) {
+		if (null != table) {
+			String resultMap = table.getResultMap();
+			if (null != resultMap) {
+				/* 返回 resultMap 映射结果集 */
+				return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.SELECT, null, resultMap,
+						null, new NoKeyGenerator(), null, null);
+			}
+		}
 
-	protected MappedStatement addMappedStatement(Class<?> mapperClass, String method, SqlSource sqlSource,
-			SqlCommandType sqlCommandType, Class<?> resultType) {
-		return this.addMappedStatement(mapperClass, method, sqlSource, sqlCommandType, null, resultType,
-				new NoKeyGenerator(), null, null);
+		/* 普通查询 */
+		return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.SELECT, null, null,
+				resultType, new NoKeyGenerator(), null, null);
 	}
 
-	protected MappedStatement addInsertMappedStatement(Class<?> mapperClass, Class<?> modelClass, String id,
+	/*
+	 * 插入
+	 */
+	public MappedStatement addInsertMappedStatement(Class<?> mapperClass, Class<?> modelClass, String id,
 			SqlSource sqlSource, KeyGenerator keyGenerator, String keyProperty, String keyColumn) {
-		return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.INSERT, modelClass, Integer.class,
-				keyGenerator, keyProperty, keyColumn);
+		return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.INSERT, modelClass, null,
+				Integer.class, keyGenerator, keyProperty, keyColumn);
+	}
+	
+	/*
+	 * 删除
+	 */
+	public MappedStatement addDeleteMappedStatement(Class<?> mapperClass, String id, SqlSource sqlSource) {
+		return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.DELETE, null, null,
+				Integer.class, new NoKeyGenerator(), null, null);
 	}
 
-	protected MappedStatement addUpdateMappedStatement(Class<?> mapperClass, Class<?> modelClass, String id,
+	/*
+	 * 更新
+	 */
+	public MappedStatement addUpdateMappedStatement(Class<?> mapperClass, Class<?> modelClass, String id,
 			SqlSource sqlSource) {
-		return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.UPDATE, modelClass, Integer.class,
-				new NoKeyGenerator(), null, null);
+		return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.UPDATE, modelClass, null,
+				Integer.class, new NoKeyGenerator(), null, null);
 	}
 
-	protected MappedStatement addMappedStatement(Class<?> mapperClass, String id, SqlSource sqlSource,
-			SqlCommandType sqlCommandType, Class<?> parameterClass, Class<?> resultType, KeyGenerator keyGenerator,
-			String keyProperty, String keyColumn) {
+	public MappedStatement addMappedStatement(Class<?> mapperClass, String id, SqlSource sqlSource,
+			SqlCommandType sqlCommandType, Class<?> parameterClass, String resultMap, Class<?> resultType,
+			KeyGenerator keyGenerator, String keyProperty, String keyColumn) {
 		String statementName = mapperClass.getName() + "." + id;
 		if (configuration.hasStatement(statementName)) {
 			System.err.println("{" + statementName + "} Has been loaded by XML or SqlProvider, ignoring the injection of the SQL.");
@@ -635,7 +667,7 @@ public class AutoSqlInjector implements ISqlInjector {
 			isSelect = true;
 		}
 		return builderAssistant.addMappedStatement(id, sqlSource, StatementType.PREPARED, sqlCommandType, null, null, null,
-				parameterClass, null, resultType, null, !isSelect, isSelect, false, keyGenerator, keyProperty, keyColumn,
+				parameterClass, resultMap, resultType, null, !isSelect, isSelect, false, keyGenerator, keyProperty, keyColumn,
 				configuration.getDatabaseId(), languageDriver, null);
 	}
 

+ 1 - 1
mybatis-plus/src/main/java/com/baomidou/mybatisplus/plugins/PerformanceInterceptor.java

@@ -71,7 +71,7 @@ public class PerformanceInterceptor implements Interceptor {
 		Object result = invocation.proceed();
 		long end = System.currentTimeMillis();
 		long timing = end - start;
-		System.err.println(" Time:" + timing + " ms" + " - ID:" + statementId + "\n Execute SQL:" + sql);
+		System.err.println(" Time:" + timing + " ms" + " - ID:" + statementId + "\n Execute SQL:" + sql + "\n");
 		if (maxTime >= 1 && timing > maxTime) {
 			throw new MybatisPlusException(" The SQL execution time is too large, please optimize ! ");
 		}

+ 18 - 17
mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit/TableInfo.java

@@ -39,6 +39,11 @@ public class TableInfo {
 	 */
 	private String tableName;
 
+	/**
+	 * 表映射结果集
+	 */
+	private String resultMap;
+
 	/**
 	 * <p>
 	 * 主键是否有存在字段名与属性名关联
@@ -62,63 +67,59 @@ public class TableInfo {
 	 */
 	private List<TableFieldInfo> fieldList;
 
-
 	public IdType getIdType() {
 		return idType;
 	}
 
-
-	public void setIdType( IdType idType ) {
+	public void setIdType(IdType idType) {
 		this.idType = idType;
 	}
 
-
 	public String getTableName() {
 		return tableName;
 	}
 
-
-	public void setTableName( String tableName ) {
+	public void setTableName(String tableName) {
 		this.tableName = tableName;
 	}
 
+	public String getResultMap() {
+		return resultMap;
+	}
+
+	public void setResultMap(String resultMap) {
+		this.resultMap = resultMap;
+	}
 
 	public boolean isKeyRelated() {
 		return keyRelated;
 	}
 
-
-	public void setKeyRelated( boolean keyRelated ) {
+	public void setKeyRelated(boolean keyRelated) {
 		this.keyRelated = keyRelated;
 	}
 
-
 	public String getKeyProperty() {
 		return keyProperty;
 	}
 
-
-	public void setKeyProperty( String keyProperty ) {
+	public void setKeyProperty(String keyProperty) {
 		this.keyProperty = keyProperty;
 	}
 
-
 	public String getKeyColumn() {
 		return keyColumn;
 	}
 
-
-	public void setKeyColumn( String keyColumn ) {
+	public void setKeyColumn(String keyColumn) {
 		this.keyColumn = keyColumn;
 	}
 
-
 	public List<TableFieldInfo> getFieldList() {
 		return fieldList;
 	}
 
-
-	public void setFieldList( List<TableFieldInfo> fieldList ) {
+	public void setFieldList(List<TableFieldInfo> fieldList) {
 		this.fieldList = fieldList;
 	}
 

+ 6 - 1
mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit/TableInfoHelper.java

@@ -75,11 +75,16 @@ public class TableInfoHelper {
 
 		/* 表名 */
 		TableName table = clazz.getAnnotation(TableName.class);
-		if (table != null && table.value() != null && table.value().trim().length() > 0) {
+		if (table != null && StringUtils.isNotEmpty(table.value())) {
 			tableInfo.setTableName(table.value());
 		} else {
 			tableInfo.setTableName(StringUtils.camelToUnderline(clazz.getSimpleName()));
 		}
+		
+		/* 表结果集映射 */
+		if (table != null && StringUtils.isNotEmpty(table.resultMap())) {
+			tableInfo.setResultMap(table.resultMap());
+		}
 
 		List<TableFieldInfo> fieldList = new ArrayList<TableFieldInfo>();
 		List<Field> list = getAllFields(clazz);

+ 1 - 2
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/MySqlInjector.java

@@ -16,7 +16,6 @@
 package com.baomidou.mybatisplus.test.mysql;
 
 import org.apache.ibatis.builder.MapperBuilderAssistant;
-import org.apache.ibatis.mapping.SqlCommandType;
 import org.apache.ibatis.mapping.SqlSource;
 import org.apache.ibatis.session.Configuration;
 
@@ -48,7 +47,7 @@ public class MySqlInjector extends AutoSqlInjector {
 		/* mapper 接口方法名一致 */
 		String method = "deleteAll";
 		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
-		this.addMappedStatement(mapperClass, method, sqlSource, SqlCommandType.DELETE, Integer.class);
+		this.addDeleteMappedStatement(mapperClass, method, sqlSource);
 	}
 
 }

+ 54 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/TransactionalTest.java

@@ -0,0 +1,54 @@
+/**
+ * Copyright (c) 2011-2014, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.test.mysql;
+
+import java.io.InputStream;
+
+import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.session.SqlSessionFactory;
+
+import com.baomidou.mybatisplus.MybatisSessionFactoryBuilder;
+import com.baomidou.mybatisplus.test.mysql.entity.User;
+import com.baomidou.mybatisplus.toolkit.IdWorker;
+
+public class TransactionalTest {
+
+	/**
+	 * <p>
+	 * 事务测试
+	 * </p>
+	 */
+	public static void main(String[] args) {
+		/*
+		 * 加载配置文件
+		 */
+		InputStream in = TransactionalTest.class.getClassLoader().getResourceAsStream("mysql-config.xml");
+		MybatisSessionFactoryBuilder mf = new MybatisSessionFactoryBuilder();
+		SqlSessionFactory sessionFactory = mf.build(in);
+		SqlSession sqlSession = sessionFactory.openSession();
+		
+		/**
+		 * 插入
+		 */
+		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
+		int rlt = userMapper.insert(new User(IdWorker.getId(), "1", 1, 1));
+		System.err.println("--------- insertInjector --------- " + rlt);
+
+//		session.commit();
+//		sqlSession.close();
+	}
+
+}

+ 2 - 2
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/entity/User.java

@@ -30,8 +30,8 @@ import com.baomidou.mybatisplus.annotations.TableName;
  * @author hubin
  * @Date 2016-01-23
  */
-/* 表名 注解 */
-@TableName("user")
+/* 表名 value 注解【 驼峰命名可无 】, resultMap 注解测试【 映射 xml 的 resultMap 内容 】 */
+@TableName(resultMap = "userMap")
 public class User implements Serializable {
 
 	/* 表字段注解,false 表中不存在的字段,可无该注解 默认 true */

+ 5 - 5
mybatis-plus/src/test/resources/mysql-config.xml

@@ -75,14 +75,14 @@
 	<!-- 环境配置 -->
 	<environments default="development">
 		<environment id="development">
-			<transactionManager type="JDBC">
-				<property name="" value="" />
-			</transactionManager>
-			<dataSource type="UNPOOLED">
+			<!-- 使用 JDBC 管理事务 -->
+			<transactionManager type="JDBC" />
+			<!-- 数据库连接池 -->
+			<dataSource type="POOLED">
 				<property name="driver" value="com.mysql.jdbc.Driver" />
 				<property name="url" value="jdbc:mysql://localhost:3306/mybatis-plus" />
 				<property name="username" value="root" />
-				<property name="password" value="" />
+				<property name="password" value="123456" />
 			<!-- 
 				<property name="driver" value="${jdbc.driver}" />
 				<property name="url" value="${jdbc.url}" />

+ 9 - 0
mybatis-plus/src/test/resources/mysql/UserMapper.xml

@@ -9,4 +9,13 @@
 	<select id="selectListRow" resultType="User">
 		select test_id AS id,name,age,test_type AS testType from user
 	</select>
+	
+	<!-- 测试 resultMap 结果集注入【 注意,实体需要注解 !! 】 -->
+	<resultMap type="User" id="userMap">
+		<id column="id" property="id"/>
+		<result column="test_id" property="id"/>
+		<result column="name" property="name"/>
+		<result column="age" property="age"/>
+		<result column="test_type" property="testType"/>
+	</resultMap>
 </mapper>