mybatis.5.动态SQL
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!=''">
AND dname 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!=''">
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>泛型类型即可。
mybatis.5.动态SQL的更多相关文章
- MyBatis的动态SQL详解
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...
- Mybatis解析动态sql原理分析
前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...
- mybatis 使用动态SQL
RoleMapper.java public interface RoleMapper { public void add(Role role); public void update(Role ro ...
- MyBatis框架——动态SQL、缓存机制、逆向工程
MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...
- 使用Mybatis实现动态SQL(一)
使用Mybatis实现动态SQL 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 写在前面: *本章节适合有Mybatis基础者观看* 前置讲解 我现在写一个查询全部的 ...
- MyBatis探究-----动态SQL详解
1.if标签 接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee); XML中:where 1=1必不 ...
- mybatis中的.xml文件总结——mybatis的动态sql
resultMap resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功. 如果sql查询字段名和pojo的属性名不一致,可以通过re ...
- MyBatis的动态SQL详解-各种标签使用
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) ...
- 利用MyBatis的动态SQL特性抽象统一SQL查询接口
1. SQL查询的统一抽象 MyBatis制动动态SQL的构造,利用动态SQL和自定义的参数Bean抽象,可以将绝大部分SQL查询抽象为一个统一接口,查询参数使用一个自定义bean继承Map,使用映射 ...
随机推荐
- mpstat命令详解
基础命令学习目录首页 原文链接:https://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858775.html 简介 mpstat是Multipro ...
- git push remote: User permission denied
这种错误因为本地保存了一个错误的账号密码,只需要重新编辑成正确的账号密码 直接上方法
- Node.js中exports,module.exports以及require方法
在Node.js中,使用module.exports.f = ...与使用exports.f = ...是一样的,此时exports就是module.exports的一种简写方式.但是,需要注意的是, ...
- 安卓端通过http对Mysql进行增删改查
各类it学习视频,大家都可以看看哦!我自己本人都是通过这些来学习it只知识的! 下面是视频链接转自:http://www.cnblogs.com/yzxk/p/4749440.html Android ...
- 新的Calculator的规范作业
附加作业题目 第三次作业 mygithub:sonnypp 这是开学来第一次写随笔,这一次的作业是对上一次作业的修改,对于上一次作业,在学长老师的帮助下,我重新修改了下代码,将.h文件分成了一个Sca ...
- EGener2四则运算出题器
项目源码: https://git.coding.net/beijl695/EGener2.git (代码纯属原创,设计细节不同,请思量) 项目发布后,由于期间各种事情,耽搁至最后一天交付.这次的项目 ...
- iOS- 多线程中如何去保证线程安全
一.前言 前段时间看了几个开源项目,发现他们保持线程同步的方式各不相同,有@synchronized.NSLock.dispatch_semaphore.NSCondition.pthread_mut ...
- 汇编语言段和RSEG用法
RSEG是段选择指令,要想明白它的意思就要了解段的意思.段是程序代码或数据对象的存储单位.程序代码放到代码段,数据对象放到数据段.段分两种,一是绝对段,一是再定位段.绝对段在汇编语言中指定,在用L51 ...
- ASP.NET MVC 4.0 参考源码索引
http://www.projky.com/asp.netmvc/4.0/Microsoft/AspNet/Mvc/Facebook/FacebookAppSettingKeys.cs.htmlhtt ...
- C++获取private的变量-偷走private
private提供了对数据的封装,使得private成员只能被类自身的成员函数以及类的友元访问,其他的函数或者类想要访问private成员只能通过该类所提供的set和get的方法进行访问, 或者返回其 ...