From 88820ae39d43f3fb1401dfc53322f815b17ed5db Mon Sep 17 00:00:00 2001 From: wangtan Date: Sun, 26 Aug 2018 16:37:19 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=88=86=E9=A1=B5=E6=89=8B=E5=8A=A8?= =?UTF-8?q?=E6=8C=87=E5=AE=9Atotal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fastmybatis/core/query/Pageable.java | 12 +++++++++++ .../gitee/fastmybatis/core/query/Query.java | 21 ++++++++++++++++++- .../fastmybatis/core/util/MapperUtil.java | 8 ++++++- .../test/java/com/myapp/TUserMapperTest.java | 1 + 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/query/Pageable.java b/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/query/Pageable.java index 2a60d8c..1b740ab 100644 --- a/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/query/Pageable.java +++ b/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/query/Pageable.java @@ -23,4 +23,16 @@ public interface Pageable { * @return 是否查询全部 */ boolean getIsQueryAll(); + + /** + * 返回总记录数 + * @return 返回总记录数 + */ + int getTotal(); + + /** + * 是否设置总记录数 + * @return 返回是否设置总记录数 + */ + boolean getIsSetTotal(); } diff --git a/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/query/Query.java b/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/query/Query.java index facef31..8c7d6eb 100644 --- a/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/query/Query.java +++ b/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/query/Query.java @@ -62,7 +62,9 @@ public class Query implements Queryable { private int start; /** 分页大小 */ private int limit; - + /** 总记录数,默认为-1,表示没有设置总记录数 */ + private int total = -1; + /** 排序信息 */ private LinkedHashSet orderInfo; @@ -435,6 +437,23 @@ public class Query implements Queryable { return this.start; } + @Override + public int getTotal() { + return total; + } + + @Override + public boolean getIsSetTotal(){ + if (total != -1 ){ + return true;//不为-1,设置了总记录数 + } + return false; + } + + public void setTotal(int total) { + this.total = total; + } + @Override public int getLimit() { return this.limit; diff --git a/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/util/MapperUtil.java b/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/util/MapperUtil.java index a3470b0..c0d970a 100644 --- a/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/util/MapperUtil.java +++ b/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/util/MapperUtil.java @@ -101,7 +101,13 @@ public class MapperUtil { list = mapper.list(query); total = list.size(); } else { - total = mapper.getCount(query); + if (query.getIsSetTotal()){ + //如果设置了total总记录数,直接获取该total + total = query.getTotal(); + }else { + //如果没有设置total,先去count执行一下sql + total = mapper.getCount(query); + } // 如果有数据 if (total > 0) { list = mapper.list(query); diff --git a/fastmybatis-demo/fastmybatis-demo-springboot/src/test/java/com/myapp/TUserMapperTest.java b/fastmybatis-demo/fastmybatis-demo-springboot/src/test/java/com/myapp/TUserMapperTest.java index 2925022..16f5280 100644 --- a/fastmybatis-demo/fastmybatis-demo-springboot/src/test/java/com/myapp/TUserMapperTest.java +++ b/fastmybatis-demo/fastmybatis-demo-springboot/src/test/java/com/myapp/TUserMapperTest.java @@ -169,6 +169,7 @@ public class TUserMapperTest extends FastmybatisSpringbootApplicationTests { // 添加查询条件 query.eq("username", "张三").page(1, 2) // 分页查询,按页码分,通常使用这种。 // .limit(start, offset) // 分页查询,这种是偏移量分页 + // .setTotal(4);//手动设置总记录数,可选,设置后可减少一次sql请求 ; // 分页信息 -- Gitee From 4c30e36f58721334af196bdf28aa2616fde62ec7 Mon Sep 17 00:00:00 2001 From: wangtan Date: Sun, 26 Aug 2018 20:01:19 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=8C=E6=89=B9?= =?UTF-8?q?=E9=87=8F=E4=BF=9D=E5=AD=98=E6=97=B6=E5=BF=BD=E7=95=A5=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E5=86=85=E5=AE=B9=EF=BC=8C=E9=87=87=E7=94=A8UNION?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E5=BC=8F=E6=89=B9=E9=87=8Finsert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fastmybatis/core/mapper/SaveMapper.java | 8 +++++ .../fastmybatis/tpl/microsoftsqlserver.vm | 25 ++++++++++++++- .../main/resources/fastmybatis/tpl/mysql.vm | 25 ++++++++++++++- .../main/resources/fastmybatis/tpl/oracle.vm | 26 ++++++++++++++- .../resources/fastmybatis/tpl/postgresql.vm | 25 ++++++++++++++- .../main/resources/fastmybatis/tpl/sqlite.vm | 25 ++++++++++++++- .../test/java/com/myapp/TUserMapperTest.java | 32 +++++++++++++++++++ 7 files changed, 161 insertions(+), 5 deletions(-) diff --git a/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/mapper/SaveMapper.java b/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/mapper/SaveMapper.java index 2cec5df..08cb0a3 100644 --- a/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/mapper/SaveMapper.java +++ b/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/mapper/SaveMapper.java @@ -45,4 +45,12 @@ public interface SaveMapper extends Mapper { */ int saveMulti(@Param("entitys") List entitys); + /** + * 批量保存,兼容更多的数据库版本,忽略重复行.
+ * 此方式采用union的方式批量insert,如果是mysql或sqlserver2008及以上推荐saveBatch()方法. + * + * @param entitys + * @return 受影响行数 + */ + int saveMultiSet(@Param("entitys") List entitys); } diff --git a/fastmybatis-core/src/main/resources/fastmybatis/tpl/microsoftsqlserver.vm b/fastmybatis-core/src/main/resources/fastmybatis/tpl/microsoftsqlserver.vm index 4cabed5..4ebb7a0 100644 --- a/fastmybatis-core/src/main/resources/fastmybatis/tpl/microsoftsqlserver.vm +++ b/fastmybatis-core/src/main/resources/fastmybatis/tpl/microsoftsqlserver.vm @@ -253,7 +253,30 @@ #end - + + + + INSERT INTO [${table.tableName}] + ( + #set ($i=0) + #foreach($column in $columns) + #if(!${column.isIdentityPk}) + #if($i > 0),#end [${column.columnName}] +#set($i=$i +1) + #end + #end + ) + + SELECT #set ($i=0) + #foreach($column in $columns) + #if(!${column.isIdentityPk}) + #if($i > 0),#end ${column.mybatisInsertValuePrefix} + #set($i=$i +1) + #end + #end + + + UPDATE [${table.tableName}] diff --git a/fastmybatis-core/src/main/resources/fastmybatis/tpl/mysql.vm b/fastmybatis-core/src/main/resources/fastmybatis/tpl/mysql.vm index b1c0a4a..054cfbe 100644 --- a/fastmybatis-core/src/main/resources/fastmybatis/tpl/mysql.vm +++ b/fastmybatis-core/src/main/resources/fastmybatis/tpl/mysql.vm @@ -246,7 +246,30 @@ #end - + + + + INSERT INTO `${table.tableName}` + ( + #set ($i=0) + #foreach($column in $columns) + #if(!${column.isIdentityPk}) + #if($i > 0),#end `${column.columnName}` +#set($i=$i +1) + #end + #end + ) + + SELECT #set ($i=0) + #foreach($column in $columns) + #if(!${column.isIdentityPk}) + #if($i > 0),#end ${column.mybatisInsertValuePrefix} + #set($i=$i +1) + #end + #end + + + UPDATE `${table.tableName}` diff --git a/fastmybatis-core/src/main/resources/fastmybatis/tpl/oracle.vm b/fastmybatis-core/src/main/resources/fastmybatis/tpl/oracle.vm index 28111fc..9e2d1ec 100644 --- a/fastmybatis-core/src/main/resources/fastmybatis/tpl/oracle.vm +++ b/fastmybatis-core/src/main/resources/fastmybatis/tpl/oracle.vm @@ -259,7 +259,31 @@ FROM dual - + + + + INSERT INTO ${table.tableName} + ( + #set ($i=0) + #foreach($column in $columns) + #if(!${column.isIdentityPk}) + #if($i > 0),#end ${column.columnName} +#set($i=$i +1) + #end + #end + ) + + SELECT #set ($i=0) + #foreach($column in $columns) + #if(!${column.isIdentityPk}) + #if($i > 0),#end ${column.mybatisInsertValuePrefix} + #set($i=$i +1) + #end + #end + FROM dual + + + UPDATE ${table.tableName} diff --git a/fastmybatis-core/src/main/resources/fastmybatis/tpl/postgresql.vm b/fastmybatis-core/src/main/resources/fastmybatis/tpl/postgresql.vm index 75ef005..8ffc703 100644 --- a/fastmybatis-core/src/main/resources/fastmybatis/tpl/postgresql.vm +++ b/fastmybatis-core/src/main/resources/fastmybatis/tpl/postgresql.vm @@ -248,7 +248,30 @@ #end - + + + + INSERT INTO "${table.tableName}" + ( + #set ($i=0) + #foreach($column in $columns) + #if(!${column.isIdentityPk}) + #if($i > 0),#end "${column.columnName}" +#set($i=$i +1) + #end + #end + ) + + SELECT #set ($i=0) + #foreach($column in $columns) + #if(!${column.isIdentityPk}) + #if($i > 0),#end ${column.mybatisInsertValuePrefix} + #set($i=$i +1) + #end + #end + + + UPDATE "${table.tableName}" diff --git a/fastmybatis-core/src/main/resources/fastmybatis/tpl/sqlite.vm b/fastmybatis-core/src/main/resources/fastmybatis/tpl/sqlite.vm index 1666c34..ad1230f 100644 --- a/fastmybatis-core/src/main/resources/fastmybatis/tpl/sqlite.vm +++ b/fastmybatis-core/src/main/resources/fastmybatis/tpl/sqlite.vm @@ -236,7 +236,30 @@ #end - + + + + INSERT INTO `${table.tableName}` + ( + #set ($i=0) + #foreach($column in $columns) + #if(!${column.isIdentityPk}) + #if($i > 0),#end `${column.columnName}` +#set($i=$i +1) + #end + #end + ) + + SELECT #set ($i=0) + #foreach($column in $columns) + #if(!${column.isIdentityPk}) + #if($i > 0),#end ${column.mybatisInsertValuePrefix} + #set($i=$i +1) + #end + #end + + + UPDATE `${table.tableName}` diff --git a/fastmybatis-demo/fastmybatis-demo-springboot/src/test/java/com/myapp/TUserMapperTest.java b/fastmybatis-demo/fastmybatis-demo-springboot/src/test/java/com/myapp/TUserMapperTest.java index 16f5280..ad625ab 100644 --- a/fastmybatis-demo/fastmybatis-demo-springboot/src/test/java/com/myapp/TUserMapperTest.java +++ b/fastmybatis-demo/fastmybatis-demo-springboot/src/test/java/com/myapp/TUserMapperTest.java @@ -369,6 +369,38 @@ public class TUserMapperTest extends FastmybatisSpringbootApplicationTests { System.out.println("saveMulti --> " + i); } + /** + * 批量添加,兼容更多数据库版本,忽略重复行,采用union + * + *
+     * INSERT INTO `t_user` ( `username` , `state` , `isdel` , `remark` , `add_time` , `money` , `left_money` )
+     * SELECT ? , ? , ? , ? , ? , ? , ?
+     * UNION
+     * SELECT ? , ? , ? , ? , ? , ? , ?
+     * UNION
+     * SELECT ? , ? , ? , ? , ? , ? , ?
+     * 
+ */ + @Test + public void testInsertMultiSet() { + List users = new ArrayList<>(); + + for (int i = 0; i < 3; i++) { // 创建3个重复对象 + TUser user = new TUser(); + user.setUsername("username" + 1); + user.setMoney(new BigDecimal(1)); + user.setRemark("remark" + 1); + user.setState((byte)0); + user.setIsdel(false); + user.setAddTime(new Date()); + user.setLeftMoney(200F); + users.add(user); + } + + int i = mapper.saveMultiSet(users); // 返回成功数 + + System.out.println("saveMulti --> " + i); + } /** * 事务回滚 */ -- Gitee