1. Dynamic SQL, to solve the problem of associated SQL strings, mybatis dynamic SQL is based on OGNL expressions
If statement, add the following statement in 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!=''"> AND dname like #{dname} </if> </select>
Add the following code to the DeptMapper.java interface
public Dept selectByLikeName(Dept dept);
The test classes are as follows:
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()); }}
What if you look up the names of employees, look up employees and find some departments?
EmpMapper.xml adds the following code
<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 adds the following code:
public List<Emp> selectByLike(Emp e);
Test category:
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("Zhang %"); List<Emp>empList=mapper.selectByLike(e); for(Emp emp:empList){ System.out.println("All employees whose names start with Zhang are:"+emp.getEname()) ;} }}
3 choose when otherwise语句
Sometimes for multiple choices, you can use the choose statement
<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 add interface
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!=''"> AND dname like #{dname} </if> </select>
测试类
public static void main(String[] args) { SqlSession session=SqlSessionFactoryUtil.getSqlSession(); DeptMapper mapper=session.getMapper(DeptMapper.class); Dept d=new Dept(); //或者设置为null d.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 集合中(此时需要生成(**,***,)的形式)