码迷,mamicode.com
首页 > 数据库 > 详细

MyBatis之动态sql

时间:2017-09-22 22:35:26      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:.com   user   tis   print   blog   except   文件   final   mybatis   

1.Mybatis动态sql是基于OGNL的表达式,可以使我们方便的在sql语句中实现某些逻辑。

2.使用if+where实现多条件查询

1〉dao层方法

 //if动态sql查询
    public List<User> findByIf(User user);

2〉xml配置文件的sql语句

    <!-- if动态sql查询 -->
    <select id="findByIf" resultType="User">
        SELECT  * FROM  user 
        <where>
            <if test="userName != null">
                and userName like concat(‘%‘,#{userName},‘%‘)
            </if>
            
            <if test="userPassword != null">
                and userPassword=#{userPassword}
            </if>
        </where>
    </select>

3〉测试方法

  //if动态sql查询
    @Test
    public  void  findByIf(){
        SqlSession sqlSession = null;
        User user = new User();
        user.setUserName("测");
        //]user.setUserPassword("");
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            List<User> list = sqlSession.getMapper(IUserDao.class).findByIf(user);
            sqlSession.commit();
            for (User user1 : list) {
                System.out.println("user-------->"+user1.getUserCode()+"\t" +user1.getUserName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

    }

 3.choose,相当于java中的switch,通常与otherwise搭配

1〉dao层方法

//智能标签choose查询
    public List<User> findByChoose(User user);

2〉xml配置文件中的sql语句

    <!--choose动态sql查询-->
    <select id="findByChoose" resultType="User">
        SELECT  * FROM  user
        <where>
            <choose>
                <when test="userName!=null">
                    and userName like concat(‘%‘,#{userName},‘%‘)
                </when>
                <when test="userPassword !=null">
                    and userPassword=#{userPassword}
                </when>
                <otherwise></otherwise>
            </choose>
        </where>
    </select>

3〉测试方法

   //choose动态sql查询
    @Test
    public  void  findByChoose(){
        SqlSession sqlSession = null;
        User user = new User();
        user.setUserName("测");
        //]user.setUserPassword("");
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            List<User> list = sqlSession.getMapper(IUserDao.class).findByChoose(user);
            sqlSession.commit();
            for (User user1 : list) {
                System.out.println("user-------->"+user1.getUserCode()+"\t" +user1.getUserName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

    }

4. (1)foreach:迭代一个集合,通常用于in条件------------->传入数组  array

1〉dao层方法

//智能标签foreach查询 String [] array
    public List<User> findByForeach(String [] depIds);

2〉xml配置文件的sql语句

    <!--foreach动态sql查询 String [] array-->
    <select id="findByForeach" resultType="User">
        SELECT  * FROM  user WHERE  depId in
        <foreach collection="array" open="(" close=")" separator="," item="depIds">
            #{depIds}
        </foreach>
    </select>

3〉测试方法

 //foreach动态sql查询   array
    @Test
    public  void  findByForeach(){
        SqlSession sqlSession = null;
        String [] depIds = {"1","2"};
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            List<User> list = sqlSession.getMapper(IUserDao.class).findByForeach(depIds);
            sqlSession.commit();
            for (User user1 : list) {
                System.out.println("user-------->"+user1.getUserCode()+"\t" +user1.getUserName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

    }

2foreach传入集合----------->list

1〉dao方法

//智能标签foreach查询 List<User> list
    public  List<User> findByForeachList(List<Integer> list);

2〉xml文件的配置

    <!--foreach动态sql查询 Liat<User> list-->
    <select id="findByForeachList" resultType="User">
        SELECT  * FROM  user WHERE  depId in
        <foreach collection="list" open="(" close=")" separator="," item="depIds">
            #{depIds}
        </foreach>
    </select>

3〉测试方法

  //foreach动态sql查询   list
    @Test
    public  void  findByForeachList(){
        SqlSession sqlSession = null;
       List<Integer> list = new ArrayList<Integer>();
       list.add(1);
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            List<User> list1 = sqlSession.getMapper(IUserDao.class).findByForeachList(list);
            sqlSession.commit();
            for (User user3 : list1) {
                System.out.println("user-------->"+user3.getUserCode()+"\t" +user3.getUserName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

    }

 

MyBatis之动态sql

标签:.com   user   tis   print   blog   except   文件   final   mybatis   

原文地址:http://www.cnblogs.com/cn-930621/p/7577170.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!