Mybatis的动态Sql

 mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。###

 1、动态sql

  映射文件:

  <!--
        会自动的去判断传入的id和username是否为空,
        如果id为空则SQL语句中不会拼接id = ?查询语句
        如果username为空则SQL语句中不会拼接username = ?查询语句
    -->
    <delete id="deleteUser" parameterType="User">
        DELETE FROM user
        <where>
			<!--
				test:里面写判断条件,如果该条件不满足,则不会拼接该if标签内的语句
			-->
            <if test="id != null and id != ''">
                and id=#{id}
            </if>
            <if test="username != null and username !=''">
                and username=#{username}
            </if>

        </where>
    </delete>

 2、sql片段

  在Mapper文件中可以定义sql片段,定义后sql语句可以引用该片段,达到重复使用的效果。
  映射文件:

  <!--
         代码片段
         id:该代码片段的唯一标识
    -->
    <sql id="sql_1" >
	<!--
        会自动的去判断传入的id和username是否为空,
        如果id为空则SQL语句中不会拼接id = ?查询语句
        如果username为空则SQL语句中不会拼接username = ?查询语句
    -->
        <if test="id != null and id != ''">
            and id=#{id}
        </if>
        <if test="username != null and username !=''">
            and username=#{username}
        </if>
    </sql>

  引用片段:

    <delete id="deleteUser" parameterType="User">
        DELETE FROM user
        <where>
            <!--
                引用SQL片段
                refid:要引用的sql片段id
            -->
            <include refid="sql_1" />
        </where>
    </delete>

 3、foreach

  向sql传递数组或List,mybatis使用foreach解析.

  POJO文件:

public class UserPlus extends User{

    private List<Integer> list;

    public List<Integer> getList() {
        return list;
    }

    public void setList(List<Integer> list) {
        this.list = list;
    }
}

  映射文件:

   <!--foreach-->
    <select id="selectUser" parameterType="UserPlus" resultType="User">
        SELECT * FROM user
        <where>
            <if test="list != null">
                <!--
                    select * from user where (id=? or id=? or id=?)
                    collection:传入的list和数组的名称
                    item:遍历list或数组时的变量名称
                    open:开始遍历时拼接的sql语句部分
                    close:结束遍历时拼接的sql语句部分
                    separator:遍历的两个对象中间需要拼接的sql语句部分
                -->
                <foreach collection="list" item="user_id" open="and (" close=")" separator="or">
                  <!--每次遍历需要拼接的SQL语句部分-->
                    id=#{user_id}
                </foreach>
            </if>
        </where>
    </select>

  接口文件:

//根据多个id查询多个用户
    public List<User> selectUser(UserPlus userPlus) throws Exception;

  测试文件:

 public void selectUser(){
        try {
            sqlSession = sessionFactory.openSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            UserPlus userPlus = new UserPlus();
            List<Integer> list = new ArrayList<Integer>();
            list.add(1);
            list.add(3);
            list.add(5);
            list.add(7);
            userPlus.setList(list);
           List<User> list1 =  userMapper.selectUser(userPlus);
            System.out.println("查询成功");
            for (User user:list1) {
                System.out.println(user);
            }
        }catch (Exception e){
            System.out.println("查询失败");
        }finally {
            sqlSession.close();
        }
    }
原文地址:https://www.cnblogs.com/jack1995/p/7242860.html