Java_myBatis_XML代理_动态SQL
主要是设计到映射文件的编写:
SELECT:
<sql id="query_user_where"> <!-- test里面可以编写OGNL表达式 --> <!-- 判断字符串不为空的标准写法 --> <if test=" user.username != null and user.username !='' "> AND username like '%${user.username}%' </if> </sql> <!-- 分页查询之查询记录 --> <select id="findUserList" parameterType="UserQueryVO" resultType="user"> SELECT * FROM user <where> <include refid="query_user_where" /> </where> </select>
if语句前面必须带AND,where会自己判断什么时候该去除
UPDATE:
<update id="testDSQL" parameterType="User"> UPDATE user <set> <if test="username != null and username != ''"> username = #{username}, </if> <if test="address != null and address != ''"> address = #{address}, </if> </set> WERE id = 36 </update>
if最后的逗号是必须写的,set会自己判断什么时候去除
INSERT:
<insert id="insertSelective" parameterType="com.kkb.mybatis.pojo.Account" > <selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER" > SELECT LAST_INSERT_ID() </selectKey> INSERT INTO account <trim prefix="(" suffix=")" suffixOverrides="," > <if test="name != null" > name, </if> <if test="money != null" > money, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="name != null" > #{name,jdbcType=VARCHAR}, </if> <if test="money != null" > #{money,jdbcType=DOUBLE}, </if> </trim> </insert>
if最后的逗号是必须写的,trim会自己判断什么时候去除