MyBatis之动态sql
Posted ruanjiancainiao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis之动态sql相关的知识,希望对你有一定的参考价值。
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); } }
(2)foreach传入集合----------->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的主要内容,如果未能解决你的问题,请参考以下文章