<set> 标签可以理解为 SQL update 语句的 set 语句,该语句没有任何属性。该标签可以被用于动态包含更新的列,而不包含不需更新的。比如:
<update id="updateAuthorIfNecessary" parameterType="domain.blog.Author"> update Author <set> <if test="username != null">username=#{username},</if> <if test="password != null">password=#{password},</if> <if test="email != null">email=#{email},</if> <if test="bio != null">bio=#{bio}</if> </set> where id=#{id} </update>
这里 <set> 标签会动态前置 SET 关键字,而且也会消除任意无关的逗号。
(1)MyBatis 配置文件 mybatis-cfg.xml 内容如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="database.properties"/> <environments default="MySqlDatabase" > <environment id="MySqlDatabase" > <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/hxstrive/mybatis/dynamic_sql/demo5/UserMapper.xml" /> </mappers> </configuration>
(2)实体 UserBean.java 代码如下:
package com.hxstrive.mybatis.dynamic_sql.demo5; public class UserBean { private Integer userId; private String name; private String sex; private Integer age; // 忽略 getter 和 setter 方法 @Override public String toString() { return "UserBean{" + "userId=" + userId + ", name='" + name + '\'' + ", sex='" + sex + '\'' + ", age=" + age + '}'; } }
(3)定义 Mapper
a、UserMapper.java
package com.hxstrive.mybatis.dynamic_sql.demo5; public interface UserMapper { void updateUser(UserBean userBean); void updateUser2(UserBean userBean); }
b、UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.hxstrive.mybatis.dynamic_sql.demo5.UserMapper"> <update id="updateUser"> update `user` <set> <if test="name != null and name != ''">`name`=#{name},</if> <if test="sex != null and sex != ''">`sex`=#{sex},</if> <if test="age != null and age > 0">`age`=#{age}</if> </set> where `user_id`=#{userId} </update> <!-- 利用 trim 实现 set 同等效果 --> <update id="updateUser2"> update `user` <trim prefix="set" suffixOverrides=","> <if test="name != null and name != ''">`name`=#{name},</if> <if test="sex != null and sex != ''">`sex`=#{sex},</if> <if test="age != null and age > 0">`age`=#{age}</if> </trim> where `user_id`=#{userId} </update> </mapper>
(4)客户端代码
package com.hxstrive.mybatis.dynamic_sql.demo5; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.InputStream; public class DynamicSqlDemo { public static void main(String[] args) throws Exception { String cfgName = "com/hxstrive/mybatis/dynamic_sql/demo5/mybatis-cfg.xml"; InputStream input = Resources.getResourceAsStream(cfgName); SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder(); SqlSessionFactory sqlFactory = factoryBuilder.build(input); System.out.println("\n\n示例1:"); updateUser(sqlFactory); System.out.println("\n\n示例2:"); updateUser2(sqlFactory); } private static void updateUser(SqlSessionFactory sqlFactory) { UserBean userBean = new UserBean(); userBean.setUserId(1); userBean.setName("爱丽丝"); userBean.setAge(27); SqlSession sqlSession = sqlFactory.openSession(true); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); userMapper.updateUser(userBean); } private static void updateUser2(SqlSessionFactory sqlFactory) { UserBean userBean = new UserBean(); userBean.setUserId(1); userBean.setName("赫仑"); userBean.setSex("男"); SqlSession sqlSession = sqlFactory.openSession(true); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); userMapper.updateUser2(userBean); } }