diff --git a/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/mapper/UpdateMapper.java b/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/mapper/UpdateMapper.java index e55edc6efe4908f313f4f08cb8a155c617f81755..6affa2fc8a142f358336f856d483d6ad0bdccc78 100644 --- a/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/mapper/UpdateMapper.java +++ b/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/mapper/UpdateMapper.java @@ -28,6 +28,24 @@ public interface UpdateMapper<E> extends Mapper<E> { */ int updateIgnoreNull(E entity); + /** + * 根据条件更新<br> + *<pre> + *{@literal + * Query query = new Query().eq("state", 2); + * TUser user = new TUser(); + * user.setUsername("李四"); + * int i = mapper.updateByQuery(user, query); + * } + * 对应SQL: UPDATE `t_user` SET `username`=? WHERE state = ? + *</pre> + * @param entity 待更新的数据 + * @param query 更新条件 + * @param ignoreProperties 忽略更新的属性名 + * @return 受影响行数 + */ + int updateByQuery(@Param("entity") E entity, @Param("query") Query query, @Param("ignoreProperties") String... ignoreProperties); + /** * 根据条件更新,map中的数据转化成update语句set部分,key为数据库字段名<br> *<pre> diff --git a/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/support/CommonService.java b/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/support/CommonService.java index 325d7b592f133ddf61cad9fe4a8ce6b9d380b57d..d5baeecdf4991948154b028d6466903e03ebf848 100644 --- a/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/support/CommonService.java +++ b/fastmybatis-core/src/main/java/com/gitee/fastmybatis/core/support/CommonService.java @@ -753,6 +753,27 @@ public interface CommonService<E, I, Mapper extends CrudMapper<E, I>> { return getMapperRunner().run(mapper -> mapper.updateIgnoreNull(entity)); } + /** + * 根据条件更新<br> + * <pre> + * {@literal + * Query query = new Query().eq("state", 2)); + * TUser user = new TUser()); + * user.setUsername("李四")); + * int i = mapper.updateByQuery(user, query)); + * } + * 对应SQL: UPDATE `t_user` SET `username`=? WHERE state = ? + * </pre> + * + * @param entity 待更新的数据 + * @param query 更新条件 + * @param ignoreProperties 忽略更新的属性名 + * @return 受影响行数 + */ + default int updateByQuery(E entity, Query query, String... ignoreProperties) { + Objects.requireNonNull(entity); + return getMapperRunner().run(mapper -> mapper.updateByQuery(entity, query, ignoreProperties)); + } /** * 根据条件更新,map中的数据转化成update语句set部分,key为数据库字段名<br> diff --git a/fastmybatis-core/src/main/resources/fastmybatis/tpl/microsoftsqlserver.vm b/fastmybatis-core/src/main/resources/fastmybatis/tpl/microsoftsqlserver.vm index 2116eb0f93c7c765d00c9d942aa8854c8afa0aca..1dfc874f5e34eeba2605a7a9c224d1511cca4802 100644 --- a/fastmybatis-core/src/main/resources/fastmybatis/tpl/microsoftsqlserver.vm +++ b/fastmybatis-core/src/main/resources/fastmybatis/tpl/microsoftsqlserver.vm @@ -324,6 +324,32 @@ #end </update> + <!-- 根据指定条件更新 --> + <update id="updateByQuery"> + UPDATE ${table.tableName} + <set> + #foreach($column in $columns) + #if(${column.isUpdateColumn}) + #if(${column.hasTypeHandlerUpdate}) + ${column.columnName}=${column.mybatisUpdateValuePrefix}, + #else + <if test="entity.${column.javaFieldName} != null or query.forceUpdate"> + <if test="!@java.util.Arrays@asList(ignoreProperties).contains('${column.javaFieldName}'.toString())"> + ${column.columnName}=${column.mybatisUpdateValuePrefix}, + </if> + </if> + #end + #end + #end + </set> + <include refid="common.where"/> + #if(${table.hasLogicDeleteColumn}) + <if test="!query.forceQuery"> + AND ${table.logicDeleteColumn.columnName} = ${table.logicDeleteColumn.logicNotDeleteValueString} + </if> + #end + </update> + <!-- 根据指定条件更新 --> <update id="updateByMap"> 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 160a88bbbb5917386f05a300d498fe02c9326f59..b2dee1a2a92cac06431b3effd7e576d3d8d8bda5 100644 --- a/fastmybatis-core/src/main/resources/fastmybatis/tpl/mysql.vm +++ b/fastmybatis-core/src/main/resources/fastmybatis/tpl/mysql.vm @@ -317,6 +317,32 @@ #end </update> + <!-- 根据指定条件更新 --> + <update id="updateByQuery"> + UPDATE `${table.tableName}` + <set> + #foreach($column in $columns) + #if(${column.isUpdateColumn}) + #if(${column.hasTypeHandlerUpdate}) + `${column.columnName}`=${column.mybatisUpdateValuePrefix}, + #else + <if test="entity.${column.javaFieldName} != null or query.forceUpdate"> + <if test="!@java.util.Arrays@asList(ignoreProperties).contains('${column.javaFieldName}'.toString())"> + `${column.columnName}`=${column.mybatisUpdateValuePrefix}, + </if> + </if> + #end + #end + #end + </set> + <include refid="common.where"/> + #if(${table.hasLogicDeleteColumn}) + <if test="!query.forceQuery"> + AND ${table.logicDeleteColumn.columnName} = ${table.logicDeleteColumn.logicNotDeleteValueString} + </if> + #end + </update> + <!-- 根据指定条件更新 --> <update id="updateByMap"> 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 b40325b247d08310d04e45236041b698f8b58ba6..6c66e9bd5004a7e6b044b8dca7a592ae883ba712 100644 --- a/fastmybatis-core/src/main/resources/fastmybatis/tpl/oracle.vm +++ b/fastmybatis-core/src/main/resources/fastmybatis/tpl/oracle.vm @@ -114,19 +114,19 @@ <select id="getByQuery" resultMap="baseResultMap" parameterType="com.gitee.fastmybatis.core.query.Query"> SELECT * FROM ( - SELECT - <include refid="baseColumns"/> - FROM ${table.tableName} t - <include refid="common.join"/> - <where> - <include refid="common.condition"/> - #if(${table.hasLogicDeleteColumn}) - <if test="!query.forceQuery"> - AND t.${table.logicDeleteColumn.columnName} = ${table.logicDeleteColumn.logicNotDeleteValueString} - </if> - #end - </where> - <include refid="common.orderBy"/> + SELECT + <include refid="baseColumns"/> + FROM ${table.tableName} t + <include refid="common.join"/> + <where> + <include refid="common.condition"/> + #if(${table.hasLogicDeleteColumn}) + <if test="!query.forceQuery"> + AND t.${table.logicDeleteColumn.columnName} = ${table.logicDeleteColumn.logicNotDeleteValueString} + </if> + #end + </where> + <include refid="common.orderBy"/> ) WHERE ROWNUM = 1 </select> @@ -331,6 +331,32 @@ #end </update> + <!-- 根据指定条件更新 --> + <update id="updateByQuery"> + UPDATE ${table.tableName} + <set> + #foreach($column in $columns) + #if(${column.isUpdateColumn}) + #if(${column.hasTypeHandlerUpdate}) + ${column.columnName}=${column.mybatisUpdateValuePrefix}, + #else + <if test="entity.${column.javaFieldName} != null or query.forceUpdate"> + <if test="!@java.util.Arrays@asList(ignoreProperties).contains('${column.javaFieldName}'.toString())"> + ${column.columnName}=${column.mybatisUpdateValuePrefix}, + </if> + </if> + #end + #end + #end + </set> + <include refid="common.where"/> + #if(${table.hasLogicDeleteColumn}) + <if test="!query.forceQuery"> + AND ${table.logicDeleteColumn.columnName} = ${table.logicDeleteColumn.logicNotDeleteValueString} + </if> + #end + </update> + <!-- 根据指定条件更新 --> <update id="updateByMap"> 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 a91b0947d8391708923b51670c7d8298a4af8f14..6cd0a1b7cfcdce04e94919d907014a0fe4ffa719 100644 --- a/fastmybatis-core/src/main/resources/fastmybatis/tpl/postgresql.vm +++ b/fastmybatis-core/src/main/resources/fastmybatis/tpl/postgresql.vm @@ -296,6 +296,32 @@ #end </update> + <!-- 根据指定条件更新 --> + <update id="updateByQuery"> + UPDATE ${table.tableName} + <set> + #foreach($column in $columns) + #if(${column.isUpdateColumn}) + #if(${column.hasTypeHandlerUpdate}) + ${column.columnName}=${column.mybatisUpdateValuePrefix}, + #else + <if test="entity.${column.javaFieldName} != null or query.forceUpdate"> + <if test="!@java.util.Arrays@asList(ignoreProperties).contains('${column.javaFieldName}'.toString())"> + ${column.columnName}=${column.mybatisUpdateValuePrefix}, + </if> + </if> + #end + #end + #end + </set> + <include refid="common.where"/> + #if(${table.hasLogicDeleteColumn}) + <if test="!query.forceQuery"> + AND ${table.logicDeleteColumn.columnName} = ${table.logicDeleteColumn.logicNotDeleteValueString} + </if> + #end + </update> + <!-- 根据指定条件更新 --> <update id="updateByMap"> 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 6b5a0268e2cc65f98526f1c5d918224a21ba1ad0..83c4cb99d94d3c4a477dc0ffd8b5e407677ca1c8 100644 --- a/fastmybatis-core/src/main/resources/fastmybatis/tpl/sqlite.vm +++ b/fastmybatis-core/src/main/resources/fastmybatis/tpl/sqlite.vm @@ -279,6 +279,32 @@ #end </update> + <!-- 根据指定条件更新 --> + <update id="updateByQuery"> + UPDATE ${table.tableName} + <set> + #foreach($column in $columns) + #if(${column.isUpdateColumn}) + #if(${column.hasTypeHandlerUpdate}) + ${column.columnName}=${column.mybatisUpdateValuePrefix}, + #else + <if test="entity.${column.javaFieldName} != null or query.forceUpdate"> + <if test="!@java.util.Arrays@asList(ignoreProperties).contains('${column.javaFieldName}'.toString())"> + ${column.columnName}=${column.mybatisUpdateValuePrefix}, + </if> + </if> + #end + #end + #end + </set> + <include refid="common.where"/> + #if(${table.hasLogicDeleteColumn}) + <if test="!query.forceQuery"> + AND ${table.logicDeleteColumn.columnName} = ${table.logicDeleteColumn.logicNotDeleteValueString} + </if> + #end + </update> + <!-- 根据指定条件更新 --> <update id="updateByMap"> 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 2e1d70cef55024506a08e7467ad7fe2ba5a4b84d..c45f35e136ce4feadfa0df464353e19bba73153c 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 @@ -49,10 +49,10 @@ public class TUserMapperTest extends FastmybatisSpringbootApplicationTests { /** * 根据主键查询 - * + * * <pre> - * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` - * FROM `t_user` t + * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` + * FROM `t_user` t * WHERE `id` = ? LIMIT 1 * </pre> */ @@ -64,10 +64,10 @@ public class TUserMapperTest extends FastmybatisSpringbootApplicationTests { /** * 根据条件查询一条记录 - * + * * <pre> - * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` - * FROM `t_user` t + * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` + * FROM `t_user` t * WHERE id = ? AND money > ? LIMIT 1 * </pre> */ @@ -84,10 +84,10 @@ public class TUserMapperTest extends FastmybatisSpringbootApplicationTests { /** * 根据字段查询一条记录 - * + * * <pre> - * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` - * FROM `t_user` t + * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` + * FROM `t_user` t * WHERE t.`username` = ? LIMIT 1 * </pre> */ @@ -99,10 +99,10 @@ public class TUserMapperTest extends FastmybatisSpringbootApplicationTests { /** * 根据条件查询多条记录 - * + * * <pre> - * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` - * FROM `t_user` t + * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` + * FROM `t_user` t * WHERE state = ? AND money IN ( ? , ? , ? ) * </pre> */ @@ -235,7 +235,7 @@ public class TUserMapperTest extends FastmybatisSpringbootApplicationTests { /** * 返回自定义字段,并转换成自定义类集合 - * + * * <pre> * SELECT t.id , t.username as username FROM `t_user` t WHERE username = ? * </pre> @@ -316,7 +316,7 @@ public class TUserMapperTest extends FastmybatisSpringbootApplicationTests { /** * 获取记录数 - * + * * <pre> * SELECT count(*) FROM `t_user` t WHERE username = ? * </pre> @@ -362,7 +362,7 @@ public class TUserMapperTest extends FastmybatisSpringbootApplicationTests { UserVO userVo = mapper.getBySpecifiedColumns(Arrays.asList("id", "username"), query, UserVO.class); System.out.println(userVo); } - + @Test public void testCount() { Query query = new Query(); @@ -379,10 +379,10 @@ public class TUserMapperTest extends FastmybatisSpringbootApplicationTests { /** * 分页查询 - * + * * <pre> - * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` - * FROM `t_user` t + * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` + * FROM `t_user` t * WHERE username = ? LIMIT ?,? * </pre> */ @@ -481,10 +481,10 @@ public class TUserMapperTest extends FastmybatisSpringbootApplicationTests { /** * 排序 - * + * * <pre> - * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` - * FROM `t_user` t + * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` + * FROM `t_user` t * ORDER BY id ASC,state DESC * </pre> */ @@ -522,10 +522,10 @@ public class TUserMapperTest extends FastmybatisSpringbootApplicationTests { /** * 联表分页 - * + * * <pre> - * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` - * FROM `t_user` t LEFT JOIN user_info t2 ON t.id = t2.user_id + * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` + * FROM `t_user` t LEFT JOIN user_info t2 ON t.id = t2.user_id * WHERE t.isdel = 0 LIMIT ?,? * </pre> */ @@ -568,7 +568,7 @@ public class TUserMapperTest extends FastmybatisSpringbootApplicationTests { public void testSql() { Query query = new Query(); query.eq("state", 0) - .sql("username like '%?%' or isdel=?", "'--;\\'三", 1); + .sql("username like '%?%' or isdel=?", "'--;\\'三", 1); List<TUser> list = mapper.list(query); System.out.println("=============="); for (TUser user : list) { @@ -593,13 +593,13 @@ public class TUserMapperTest extends FastmybatisSpringbootApplicationTests { } System.out.println("=============="); } - + /** * 联表查询,并返回指定字段 * <pre> * SELECT t2.user_id userId , t.username , t2.city - * FROM `t_user` t - * LEFT JOIN user_info t2 ON t.id = t2.user_id WHERE t.isdel = 0 + * FROM `t_user` t + * LEFT JOIN user_info t2 ON t.id = t2.user_id WHERE t.isdel = 0 * </pre> */ @Test @@ -611,7 +611,7 @@ public class TUserMapperTest extends FastmybatisSpringbootApplicationTests { List<String> column = Arrays.asList("t2.user_id userId", "t.username", "t2.city"); // 再将map转换成实体bean List<UserInfoVo> list = mapper.listBySpecifiedColumns(column, query, UserInfoVo.class); - + this.print(list); } @@ -698,7 +698,7 @@ public class TUserMapperTest extends FastmybatisSpringbootApplicationTests { /** * 批量添加.支持mysql,sqlserver2008。如需支持其它数据库使用saveMulti方法 - * + * * <pre> * INSERT INTO person (id, name, age) VALUES (1, 'Kelvin', 22), (2, 'ini_always', 23); * </pre> @@ -814,7 +814,27 @@ public class TUserMapperTest extends FastmybatisSpringbootApplicationTests { int i = mapper.updateIgnoreNull(user); print("updateNotNull --> " + i); } - + + /** + * 根据条件更新。将状态为2的数据姓名更新为李四 + * UPDATE `t_user` SET `username`=?, `add_time`=? WHERE state = ? + */ + @Test + public void testUpdateByQuery() { + Query query = new Query().eq("state", 2); + // 方式1 + TUser user = new TUser(); + user.setUsername("李四"); + int i = mapper.updateByQuery(user, query); + print("updateByQuery --> " + i); + + /* // 方式2 + Map<String, Object> map = new HashMap<String, Object>(); + map.put("username", "李四2"); + i = mapper.updateByQuery(map, query); + print("updateByQuery --> " + i);*/ + } + @Test public void testUpdateByMap() { Query query = new Query().eq("id", 1); @@ -870,7 +890,7 @@ public class TUserMapperTest extends FastmybatisSpringbootApplicationTests { int i = mapper.deleteByQuery(query); print("deleteByQuery --> " + i); } - + /** * 强力查询,将无视逻辑删除字段 */ diff --git a/fastmybatis-demo/fastmybatis-demo-springmvc/src/test/java/com/myapp/TUserMapperTest.java b/fastmybatis-demo/fastmybatis-demo-springmvc/src/test/java/com/myapp/TUserMapperTest.java index f22ac8f0bb898612ec1ac2211e4571c416fd25c3..7a37e479564272520f664fa75b69bf05ae2c1b95 100644 --- a/fastmybatis-demo/fastmybatis-demo-springmvc/src/test/java/com/myapp/TUserMapperTest.java +++ b/fastmybatis-demo/fastmybatis-demo-springmvc/src/test/java/com/myapp/TUserMapperTest.java @@ -42,10 +42,10 @@ public class TUserMapperTest extends TestBase { /** * 根据主键查询 - * + * * <pre> - * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` - * FROM `t_user` t + * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` + * FROM `t_user` t * WHERE `id` = ? LIMIT 1 * </pre> */ @@ -57,10 +57,10 @@ public class TUserMapperTest extends TestBase { /** * 根据条件查询一条记录 - * + * * <pre> - * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` - * FROM `t_user` t + * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` + * FROM `t_user` t * WHERE id = ? AND money > ? LIMIT 1 * </pre> */ @@ -72,7 +72,7 @@ public class TUserMapperTest extends TestBase { TUser user = mapper.getByQuery(query); print(user); } - + @Test public void testGetQuery2() { TUser user = new TUser(); @@ -81,15 +81,15 @@ public class TUserMapperTest extends TestBase { Query query = Query.build(user); TUser user2 = mapper.getByQuery(query); print(user2); - + } /** * 根据字段查询一条记录 - * + * * <pre> - * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` - * FROM `t_user` t + * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` + * FROM `t_user` t * WHERE t.`username` = ? LIMIT 1 * </pre> */ @@ -101,10 +101,10 @@ public class TUserMapperTest extends TestBase { /** * 根据条件查询列表 - * + * * <pre> - * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` - * FROM `t_user` t + * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` + * FROM `t_user` t * WHERE state = ? AND money IN ( ? , ? , ? ) * </pre> */ @@ -182,7 +182,7 @@ public class TUserMapperTest extends TestBase { /** * 获取记录数 - * + * * <pre> * SELECT count(*) FROM `t_user` t WHERE username = ? * </pre> @@ -197,8 +197,8 @@ public class TUserMapperTest extends TestBase { print("total:" + total); } - - + + @Test public void testLike() { Query query = new Query(); @@ -214,8 +214,8 @@ public class TUserMapperTest extends TestBase { * 分页查询 * * <pre> - * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` - * FROM `t_user` t + * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` + * FROM `t_user` t * WHERE username = ? LIMIT ?,? * </pre> */ @@ -242,10 +242,10 @@ public class TUserMapperTest extends TestBase { /** * 排序 - * + * * <pre> - * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` - * FROM `t_user` t + * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` + * FROM `t_user` t * ORDER BY id ASC,state DESC * </pre> */ @@ -259,10 +259,10 @@ public class TUserMapperTest extends TestBase { /** * 联表分页 - * + * * <pre> - * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` - * FROM `t_user` t LEFT JOIN user_info t2 ON t.id = t2.user_id + * SELECT t.`id` , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` + * FROM `t_user` t LEFT JOIN user_info t2 ON t.id = t2.user_id * WHERE t.isdel = 0 LIMIT ?,? * </pre> */ @@ -280,11 +280,11 @@ public class TUserMapperTest extends TestBase { } System.out.println("=============="); } - + /** - * SELECT DISTINCT(t.`id`) , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` FROM `t_user` t - * WHERE username = ? AND t.isdel = 0 + * SELECT DISTINCT(t.`id`) , t.`username` , t.`state` , t.`isdel` , t.`remark` , t.`add_time` , t.`money` , t.`left_money` FROM `t_user` t + * WHERE username = ? AND t.isdel = 0 */ @Test public void testDistinct() { @@ -292,7 +292,7 @@ public class TUserMapperTest extends TestBase { // 添加查询条件 query.eq("username", "张三") .enableDistinct(); - + List<TUser> list = mapper.list(query); this.print(list); } @@ -358,7 +358,7 @@ public class TUserMapperTest extends TestBase { /** * 批量添加.支持mysql,sqlserver2008。如需支持其它数据库使用saveMulti方法 - * + * * <pre> * INSERT INTO person (id, name, age) VALUES (1, 'Kelvin', 22), (2, 'ini_always', 23); * </pre> @@ -386,13 +386,13 @@ public class TUserMapperTest extends TestBase { /** * 批量添加,兼容更多数据库版本,采用union all - * + * * <pre> - * INSERT INTO `t_user` ( `username` , `state` , `isdel` , `remark` , `add_time` , `money` , `left_money` ) - * SELECT ? , ? , ? , ? , ? , ? , ? - * UNION ALL - * SELECT ? , ? , ? , ? , ? , ? , ? - * UNION ALL + * INSERT INTO `t_user` ( `username` , `state` , `isdel` , `remark` , `add_time` , `money` , `left_money` ) + * SELECT ? , ? , ? , ? , ? , ? , ? + * UNION ALL + * SELECT ? , ? , ? , ? , ? , ? , ? + * UNION ALL * SELECT ? , ? , ? , ? , ? , ? , ? * </pre> */ @@ -475,7 +475,37 @@ public class TUserMapperTest extends TestBase { int i = mapper.updateIgnoreNull(user); print("updateNotNull --> " + i); } - + + /** + * 根据条件更新。将状态为2的数据姓名更新为李四 + * UPDATE `t_user` SET `username`=?, `add_time`=? WHERE state = ? AND isdel = 0 + * 注:逻辑删除字段在这里有效 + */ + @Test + public void testUpdateByQuery() { + Query query = new Query().eq("state", 2); + // 无视逻辑删除字段 + //query.ignoreLogicDeleteColumn(); + // 方式1 + TUser user = new TUser(); + user.setUsername("李四"); + user.setRemark(null); + int i = mapper.updateByQuery(user, query); + print("updateByQuery --> " + i); + + // 方式2 + // key为数据库字段名 + /* + * UPDATE `t_user` SET remark = ? , username = ? WHERE state = ? + * Parameters: null, 李四2(String), 2(Integer) + */ + /*Map<String, Object> map = new HashMap<String, Object>(); + map.put("username", "李四2"); + map.put("remark", null); + int i = mapper.updateByMap(map, query); + print("updateByQuery --> " + i);*/ + } + @Test public void testListByPojo() { UserDTO param = new UserDTO(); @@ -511,7 +541,7 @@ public class TUserMapperTest extends TestBase { print(tUser); } } - + public static class UserDTO { /** 用户名, 数据库字段:username */ @Condition(operator = Operator.likeRight, index = 1) @@ -589,7 +619,7 @@ public class TUserMapperTest extends TestBase { int i = mapper.deleteByQuery(query); print("deleteByQuery --> " + i); } - + @Test public void testDeleteByQuery2() { TUser user = new TUser(); @@ -598,7 +628,7 @@ public class TUserMapperTest extends TestBase { int i = mapper.deleteByQuery(query); print("deleteByQuery2 --> " + i); } - + /** * 强力查询,将无视逻辑删除字段 */