记录一个简单的mybatis动态sql例子
新增
<?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="org.ruiskey.mapper.UserMapper">
<insert id="save">
insert into user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null and id != ''">
id,
</if>
<if test="username != null and username != ''">
username,
</if>
<if test="password != null and password != ''">
password,
</if>
<if test="nickname != null and nickname != ''">
nickname,
</if>
<if test="email != null and email != ''">
email,
</if>
<if test="phone != null and phone != ''">
phone,
</if>
<if test="address != null and address != ''">
address
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null and id != ''">
#{id},
</if>
<if test="username != null and username != ''">
#{username},
</if>
<if test="password != null and password != ''">
#{password},
</if>
<if test="nickname != null and nickname != ''">
#{nickname},
</if>
<if test="email != null and email != ''">
#{email},
</if>
<if test="phone != null and phone != ''">
#{phone},
</if>
<if test="address != null and address != ''">
#{address}
</if>
</trim>
</insert>
</mapper>
更新
<update id="update">
update user
<set>
<if test="username != null and username != ''">
username = #{username}
</if>
<if test="password != null and password != ''">
password = #{password}
</if>
<if test="nickname != null and nickname != ''">
nickname = #{nickname}
</if>
<if test="email != null and email != ''">
email = #{email}
</if>
<if test="phone != null and phone != ''">
phone = #{phone}
</if>
<if test="address != null and address != ''">
address = #{address}
</if>
</set>
<where>
id = #{id}
</where>
</update>