mybatis-动态sql

1.where和if标签

  (1)where标签能消除第一个条件的前and 和or关键字,避免了sql语句语法错误

  (2).if标签表示判断

    <!-- 以性别和名字条件查询用户 -->
    <select id="findBySexAndName" parameterType="User" resultType="User">
        <!-- select * from user where username = #{username} and sex = #{sex} --> select * from user <where>
            <if test="username != null and username !=‘‘ "> username = #{username} </if>
            <if test="sex != null and sex != ‘‘ "> and sex = #{sex} </if>
        </where>
    </select>

值得一提的是,我们传入的对象的属性,会被逐个提取出来放到一个map中,键为对象属性名,所以在if标签中直接写属性名即可,不需写#{属性名}

 

2.sql片段

  (1)sql标签作用很简单,将sql语句中存在重复性较高的部分提取出来;

    <sql id="userTable"> select * from user </sql>
    <!-- 以性别和名字条件查询用户 -->
    <select id="findBySexAndName" parameterType="User" resultType="User">
        <!-- select * from user where username = #{username} and sex = #{sex} -->
        
        <!-- 引用sql片段 -->
        <include refid="userTable"></include>
        <where>
            <if test="username != null and username !=‘‘ "> username = #{username} </if>
            <if test="sex != null and sex != ‘‘ "> and sex = #{sex} </if>
        </where>
    </select>

 

3.foreach标签

  foreach标签用来遍历数组或者集合,下面看下数组和集合出现的三种情况如何使用foreach

  (1)方法参数是含有集合和数组的对象

 

   下面只演示集合,数组跟集合差不读:

    <!-- 传入拥有集合和数组的对象 -->
    <select id="findByListAndArray" parameterType="QueryVo" resultType="User">
        <!-- select * from user where username = #{username} and sex = #{sex} -->
        
        <!-- 引用sql片段 -->
        <include refid="userTable"></include>
        <where>
            <!-- id in (1,2,3) -->
            <foreach collection="list" item="id" open="id in (" close=")" separator=","> #{id} </foreach>
        </where>
    </select>    

  collection属性值代表传入对象的集合或者数组,item值为集合或数组中每个元素的临时变量,open为遍历拼接前的语句的前缀,close为遍历拼接后的后缀,separator为每遍历一次后追加字符串内容,#{id}取得当前元素的值;foreach有个好处,它能知道有效元素的最后一个元素,在之后空的索引位置将不会遍历

  junit测试:

 @Test public void test5() { //加载主配置文件
        String resource = "sqlMapConfig.xml"; try { //获取指向该配置的流
            InputStream in = Resources.getResourceAsStream(resource); //获取工厂
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); //获取session
            SqlSession session = factory.openSession(); //-------------------------------------------------------------------- //得到代理对象
            UserMapper mapper = session.getMapper(UserMapper.class); QueryVo vo = new QueryVo(); // Integer[] ids = new Integer[5]; // ids[0] = 1; // ids[1] = 10; // ids[2] = 15; // vo.setIds(ids);
 List<Integer> idList = new ArrayList<Integer>(); idList.add(10); idList.add(1); idList.add(15); vo.setList(idList); List<User> list = mapper.findByListAndArray(vo); System.out.println(list); } catch (IOException e) { e.printStackTrace(); } }

  (2)方法参数是数组

 

   junit测试:

 @Test public void test6() { //加载主配置文件
        String resource = "sqlMapConfig.xml"; try { //获取指向该配置的流
            InputStream in = Resources.getResourceAsStream(resource); //获取工厂
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); //获取session
            SqlSession session = factory.openSession(); //-------------------------------------------------------------------- //得到代理对象
            UserMapper mapper = session.getMapper(UserMapper.class); Integer[] ids = new Integer[5]; ids[0] = 1; ids[1] = 10; ids[2] = 15; List<User> list = mapper.findByArray(ids); System.out.println(list); } catch (IOException e) { e.printStackTrace(); } }

 (3)方法参数是集合 (不演示,参照数组)