1. 程式人生 > 其它 >Mybatis學習——動態sql

Mybatis學習——動態sql

在mapper.xml檔案中使用<where> <if test> <foreach>標籤實現動態sql

 

  <where>和<if>標籤

    <where>

      <if test="(物件值)!=null">

        ....

      </if>

    <where>

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://www.mybatis.org/dtd/mybatis-3-mapper.dtd"
> <mapper namespace="xyz.javaswing.mapper.UserMapper"> <select id="findUserById" parameterType="int" resultType="user"> select * from user where id = #{id} </select> <select id="findUser" parameterType="user" resultType="user"> select * from user
<where> <if test="id!=0"> id = #{id} </if> <if test="username!=null"> username = #{username} </if> <if test="password!=null"> password = #{password}
</if> </where> </select> </mapper>