浏览代码

优化例子

zhou-hao 5 年之前
父节点
当前提交
58bffcc9a6

+ 1 - 1
hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/main/java/org/hswebframework/web/dao/mybatis/builder/EasyOrmSqlBuilder.java

@@ -153,7 +153,7 @@ public class EasyOrmSqlBuilder {
 
             if (resultMapping.getNestedResultMapId() != null) {
                 ResultMap nests = MybatisUtils.getResultMap(resultMapping.getNestedResultMapId());
-                List<ResultMapping> resultMappings = new ArrayList<>(nests.getResultMappings());
+                Set<ResultMapping> resultMappings = new HashSet<>(nests.getResultMappings());
                 resultMappings.addAll(nests.getIdResultMappings());
                 for (ResultMapping mapping : resultMappings) {
                     metaData.addAll(createColumn(resultMapping.getProperty(),

+ 5 - 2
hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/test/java/org/hswebframework/web/dao/crud/TestCrud.java

@@ -1,5 +1,6 @@
 package org.hswebframework.web.dao.crud;
 
+import lombok.SneakyThrows;
 import org.apache.ibatis.session.SqlSessionFactory;
 import org.hswebframework.ezorm.core.param.QueryParam;
 import org.hswebframework.ezorm.rdb.executor.SqlExecutor;
@@ -53,12 +54,13 @@ public class TestCrud extends AbstractTransactionalJUnit4SpringContextTests {
                 ")");
         sqlExecutor.exec("\n" +
                 "create table h_nest_table(\n" +
-                "  id BIGINT AUTO_INCREMENT PRIMARY KEY,\n" +
+                "  id BIGINT PRIMARY KEY,\n" +
                 "  name VARCHAR(32)\n" +
                 ")");
     }
 
     @Test
+    @SneakyThrows
     public void testCRUD() {
 
         DataSourceHolder.databaseSwitcher().use("PUBLIC");
@@ -69,13 +71,14 @@ public class TestCrud extends AbstractTransactionalJUnit4SpringContextTests {
         entity.setDataTypes(new DataType[]{DataType.TYPE1, DataType.TYPE3});
         testDao.insert(entity);
         Assert.assertNotNull(entity.getId());
+        sqlExecutor.insert("insert into h_nest_table (id,name) values(#{id},'1234')",entity);
 
         QueryParamEntity query = new QueryParamEntity();
         //any in
         query.where("dataTypes$in$any", Arrays.asList(DataType.TYPE1, DataType.TYPE2));
 
         //#102
-        query.where("createTime", "2017-11-10");
+        //query.where("createTime", "2017-11-10");
 
 
 //        DataSourceHolder.tableSwitcher().use("h_test", "h_test2");

+ 3 - 0
hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/test/java/org/hswebframework/web/dao/crud/TestEntity.java

@@ -47,4 +47,7 @@ public class TestEntity implements org.hswebframework.web.commons.entity.Entity
     @CollectionTable(name = "nest_table")
     private NestEntity nest;
 
+    @CollectionTable(name = "nest_table2")
+    private NestEntity nest2;
+
 }

+ 10 - 4
hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/test/resources/org/hswebframework/web/dao/test/TestDao.xml

@@ -10,9 +10,12 @@
 
     <resultMap id="TestNestResultMap" type="org.hswebframework.web.dao.crud.TestEntity">
         <id property="id" column="id" javaType="Long" jdbcType="INTEGER"/>
-<!--        <result property="nest.name" column="nest_table.name" javaType="String" jdbcType="VARCHAR"/>-->
-<!--        <association property="nest" column="nest_table">-->
-<!--            <result column="name" jdbcType="VARCHAR" javaType="String"/>-->
+<!--                <result property="nest2.name" column="nest_table2.name" javaType="String" jdbcType="VARCHAR"/>-->
+<!--        <association property="nest" javaType="org.hswebframework.web.dao.crud.NestEntity" columnPrefix="nest_table." column="nest_table">-->
+<!--            <result property="name" column="name" jdbcType="VARCHAR" javaType="String"/>-->
+<!--        </association>-->
+<!--        <association property="nest2" javaType="org.hswebframework.web.dao.crud.NestEntity" columnPrefix="nest_table2." column="nest_table2">-->
+<!--            <result property="name" column="name" jdbcType="VARCHAR" javaType="String"/>-->
 <!--        </association>-->
     </resultMap>
 
@@ -22,7 +25,8 @@
         <bind name="tableName" value="'h_test'"/>
     </sql>
 
-    <insert id="insert" keyColumn="id" keyProperty="id" useGeneratedKeys="true" parameterType="org.hswebframework.web.dao.crud.TestEntity">
+    <insert id="insert" keyColumn="id" keyProperty="id" useGeneratedKeys="true"
+            parameterType="org.hswebframework.web.dao.crud.TestEntity">
         <include refid="config"/>
         <include refid="BasicMapper.buildInsertSql"/>
     </insert>
@@ -55,6 +59,7 @@
         <include refid="BasicMapper.buildSelectField"/>
         from ${_fullTableName} h_test
         left join ${_databasePrefix}h_nest_table nest_table on nest_table.id=h_test.id
+        left join ${_databasePrefix}h_nest_table nest_table2 on nest_table2.id=h_test.id
         <where>
             <include refid="BasicMapper.buildWhere"/>
         </where>
@@ -68,6 +73,7 @@
         count(1)
         from ${_fullTableName} h_test
         left join ${_databasePrefix}h_nest_table nest_table on nest_table.id=h_test.id
+        left join ${_databasePrefix}h_nest_table nest_table2 on nest_table2.id=h_test.id
         <where>
             <include refid="BasicMapper.buildWhere"/>
         </where>