mybatis.5.动态SQL

来源:互联网 发布:手机测分贝软件 编辑:程序博客网 时间:2024/05/05 17:16

1.动态SQL,解决关联sql字符串的问题,mybatis的动态sql基于OGNL表达式

   if语句,在DeptMapper.xml增加如下语句;


<select id="selectByLikeName" resultType="org.mybatis.example.dao.Dept"parameterType="org.mybatis.example.dao.Dept">select d.deptno,d.dname,d.loc from dept d where 1=1<if test="dname!=null and dname!=''">ANDdname like #{dname}</if></select>
DeptMapper.java接口中增加如下代码

public Dept selectByLikeName(Dept dept);

测试类如下:


public class Test23 {public static void main(String[] args) {SqlSession session=SqlSessionFactoryUtil.getSqlSession();DeptMapper mapper=session.getMapper(DeptMapper.class);Dept d=new Dept();d.setDname("开发部");Dept dept=mapper.selectByLikeName(d);System.out.println(dept.getDname());}}

如果查询雇员的姓名,查询出雇员并且找到一些部门呢?


EmpMapper.xml增加如下代码

<select id="selectByLike" resultMap="getEmpresultMap"parameterType="org.mybatis.example.dao.Emp">select d.deptno,d.dname,d.loc,e.empno,e.ename  from Dept d  join emp e  on d.deptno=e.deptno where 1=1<if test="ename!=null and ename!=''">AND ename like #{ename}</if><if test="dept!=null and dept.dname!=null">AND dname like #{dept.dname}</if></select>

EmpMapper.java增加如下代码:

public List<Emp> selectByLike(Emp e);

测试类:

public class Test24 {public static void main(String[] args) {SqlSession session=SqlSessionFactoryUtil.getSqlSession();EmpMapper mapper=session.getMapper(EmpMapper.class);Emp e=new Emp();e.setEname("张%");List<Emp>empList=mapper.selectByLike(e);for(Emp emp:empList){System.out.println("所有名字首字母为张的员工是:"+emp.getEname());}}}

3 choose when  otherwise语句

有时候针对,有多重选择的情况,可以使用choose语句

<select id="selectByLikeTwo" parameterType="org.mybatis.example.dao.Emp"resultMap="getEmpresultMap">select d.deptno,d.dname,d.loc,e.empno,e.ename,e.sal  from Dept d  join emp e  on d.deptno=e.deptno where 1=1 <choose> <when test="ename!=null">AND ename like #{ename}</when> <when test="dept!=null and dept.dname!=null">AND dname=#{dept.dname}</when> <otherwise>AND sal >5000</otherwise> </choose></select>
EmpMapper.java增加接口

public List<Emp> selectByLikeTwo(Emp e);

public class Test25 {public static void main(String[] args) {SqlSession session=SqlSessionFactoryUtil.getSqlSession();EmpMapper mapper=session.getMapper(EmpMapper.class);Emp e=new Emp();Dept d=new Dept();d.setDname("开发部");e.setDept(d);List<Emp>empList=mapper.selectByLikeTwo(e);for(Emp emp:empList){System.out.println("所有开发部的员工:"+emp.getEname());}}}


4.

<select id="selectByLikeName" resultType="org.mybatis.example.dao.Dept"parameterType="org.mybatis.example.dao.Dept">select d.deptno,d.dname,d.loc from dept d where<if test="dname!=null">1=1</if><if test="dname!=null and dname!=''">ANDdname like #{dname}</if></select>

测试类

public static void main(String[] args) {SqlSession session=SqlSessionFactoryUtil.getSqlSession();DeptMapper mapper=session.getMapper(DeptMapper.class);Dept d=new Dept(); //或者设置为nulld.setDname("开发部");//设置为null的时候注释List<Dept>deptList=mapper.selectByLikeName(d);System.out.println(deptList.get(0).getDname());}

5. foreach

    动态SQL迭代一个集合,通常放在In条件语句中,foreach允许指定一个集合,声明集合项和索引变量,他们可以用在元素体内,也允许指定开放和关闭的字符串,在迭代之间放置分隔符。这个元素是智能的,不会偶然地附加多余的分隔符。

   在EmpMapper.xml中增加代码段

<select id="selectDeptIn" resultType="org.mybatis.example.dao.Dept">select * from dept d where deptno in<foreach item="item" index="index" collection="list"open="(" separator="," close=")">#{item}</foreach></select>

在接口EmpMapper.java中增加代码

public List<Dept> selectDeptIn(List<Integer>list);

测试类代码段

public class Test26 {public static void main(String[] args) {SqlSession session=SqlSessionFactoryUtil.getSqlSession();DeptMapper mapper=session.getMapper(DeptMapper.class);List<Integer>idList=new ArrayList<Integer>();idList.add(5);idList.add(6);List<Dept>deptList=mapper.selectDeptIn(idList);System.out.println(deptList.get(0).getDname());System.out.println(deptList.get(1).getDname());}}

对于在Mybatis中出现的TooManyResultsException异常,需要将接口的方法返回类型修改为List<T>泛型类型即可。

foreach元素的属性主要有 item,index,collection,open,separator,close。

item表示集合中每一个元素进行迭代时的别名.

index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置.

open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符.

close表示以什么结束.

 

<select id="selectTestForEach" parameterType="News" resultMap="NewsResultMapper">
  select * from t_news n where 
  <foreach collection="listTag" index="index" item="tag" open=""
    separator="or" close="">
   #{tag} in n.tags
  </foreach>
 </select>
所以 去除左右括号 和 把,改成 or 进行 。 就可以转化为这种形式。
select * from t_news n where ? in n.tags or ? in n.tags   这样可以用List<String> 来查。

 

但是查不到数据
需要使用如下方式:
<select id="selectTestForEach" parameterType="News" resultMap="NewsResultMapper">
  select * from t_news n where 
  <foreach collection="listTag" index="index" item="tag" open=""
    separator="or" close="">
    n.tags like  '%'||#{tag}||'%'
  </foreach>
 <lect>

生成的SQL为

select * from t_news n where n.tags like ? or n.tags like ?    

 

foreach : 用的更多的地方为: 根据Id批量删除    /    判断什么 in 集合中(此时需要生成(**,***,)的形式)


0 0
原创粉丝点击