mybatis学习(五)(动态mybatis(多条件查询))
有时候要查询条件是多条件的,尤其是使用mybatis的时候如何创建sql语句呢?
这里mybatis有自己的办法,如下:
案例:通过传入map,根据map里面的数据来查询
mapper配置如下:
<select id="select04" parameterType="Emp1" resultType="Emp1">
select * from emp
<where>
<if test="empno != 0">
and empno = #{empno}
</if>
<if test="ename != null">
and ename = #{ename}
</if>
<if test = "deptno != null ">
and deptno = #{deptno}
</if>
</where>
</select>
测试类如下:
package com.yc.mybatis; import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test; public class TestTest01 {
InputStream is = null;
SqlSessionFactory factory = null;
SqlSession session = null; {
try {
is = Resources.getResourceAsStream("mybatis-config.xml");
factory = new SqlSessionFactoryBuilder().build(is);
session = factory.openSession();
} catch (IOException e) {
e.printStackTrace();
}
} @Test
public void TTest04(){
Emp1 emp = new Emp1();
emp.setDeptno(20);
List<Emp1> list = session.selectList("TTest.select04", emp);//TTest为mapper的命名空间
for(Emp1 e : list){
System.out.println(e);
}
}
}
实体类如下:
package com.yc.mybatis;
public class Emp1 {
private int empno;
private int deptno;
private String ename;
@Override
public String toString() {
return "Emp1 [empno=" + empno + ", deptno=" + deptno + ", ename=" + ename + "]";
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + deptno;
result = prime * result + empno;
result = prime * result + ((ename == null) ? 0 : ename.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Emp1 other = (Emp1) obj;
if (deptno != other.deptno)
return false;
if (empno != other.empno)
return false;
if (ename == null) {
if (other.ename != null)
return false;
} else if (!ename.equals(other.ename))
return false;
return true;
}
public Emp1(int empno, int deptno, String ename) {
super();
this.empno = empno;
this.deptno = deptno;
this.ename = ename;
}
public Emp1() {
super();
}
}
Emp1.java
结果截图如下:

多条件查询到此结束,但是相关的思想却非常重要,比如有多条件更新,多个值的赋值再插入数据等等,这里我就将配置项发一下,相关的测试类和实体类就不一 一列举了。
多数据更新:
<update id="update01" parameterType="Emp1">
update emp
<set>
<if test="ename != null ">
ename = #{ename},
</if>
<if test = "deptno != 0">
deptno = #{deptno},
</if>
</set>
where empno = #{empno}
</update>
多条件删除:
<delete id="delete01" parameterType="Emp">
delete emp
<where>
<if test="empno != 0">
and empno = #{empno}
</if>
<if test="ename != null">
and ename = #{ename}
</if>
<if test="deptno != 0">
and deptno = #{deptno}
</if>
</where>
</delete>
注意:关于添加、删除和修改必须要手动提交一次才行,要session.commit(),因为mybatis不会自动帮你提交。
基本的mybatis到此。
mybatis学习(五)(动态mybatis(多条件查询))的更多相关文章
- mybatis 学习五 动态SQL语句
3.1 selectKey 标签 在insert语句中,在Oracle经常使用序列.在MySQL中使用函数来自动生成插入表的主键,而且需要方法能返回这个生成主键.使用myBatis的selectKey ...
- Mybatis中动态SQL多条件查询
Mybatis中动态SQL多条件查询 mybatis中用于实现动态SQL的元素有: if:用if实现条件的选择,用于定义where的字句的条件. choose(when otherwise)相当于Ja ...
- MyBatis学习总结(七)——Mybatis缓存(转载)
孤傲苍狼 只为成功找方法,不为失败找借口! MyBatis学习总结(七)--Mybatis缓存 一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的 ...
- 【转】MyBatis学习总结(七)——Mybatis缓存
[转]MyBatis学习总结(七)——Mybatis缓存 一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualC ...
- 转:MyBatis学习总结(Mybatis总结精华文章)
http://www.cnblogs.com/xdp-gacl/tag/MyBatis%E5%AD%A6%E4%B9%A0%E6%80%BB%E7%BB%93/ 当前标签: MyBatis学习总结 ...
- 【转】MyBatis学习总结(一)——MyBatis快速入门
[转]MyBatis学习总结(一)——MyBatis快速入门 一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC ...
- 【JAVAWEB学习笔记】21_多条件查询、attr和prop的区别和分页的实现
今天主要学习了数据库的多条件查询.attr和prop的区别和分页的实现 一.实现多条件查询 public List<Product> findProductListByCondition( ...
- spring boot 学习(五)SpringBoot+MyBatis(XML)+Druid
SpringBoot+MyBatis(xml)+Druid 前言 springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成. 主要是 ...
- Mybatis plus中一个框多条件查询 SQL拼接
遇到多条件查询时,只用框架自带的方法搞不定,只能自己写方法拼接 EntityWrapper<YcejShopEntity> wrapper = new EntityWrapper<& ...
- Mybatis学习笔记(一) —— mybatis介绍
一.Mybatis介绍 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名 ...
随机推荐
- python chrome selenium
#coding=utf-8 from selenium import webdriver options = webdriver.ChromeOptions() options.add_argumen ...
- 超详细SQL SERVER 2016跨网段和局域网发布订阅配置图解和常见问题
原文:超详细SQL SERVER 2016跨网段和局域网发布订阅配置图解和常见问题 转载标明出处:http://blog.csdn.net/u012861467 前方高能,要有点耐心,图片较多,注意在 ...
- 编译时MSIL注入--实践Mono Cecil(1)
原文:编译时MSIL注入--实践Mono Cecil(1) 紧接上两篇浅谈.NET编译时注入(C#-->IL)和浅谈VS编译自定义编译任务—MSBuild Task(csproject),在第一 ...
- MySql如何将一个表字段更新到另一个表字段
今天遇到这样一个需求: 有两张表,一张是专辑表,另一张是专辑下的图片表, 专辑表中有拍摄年\月,两个字段; 图片表中有实际拍摄日期的字段; 因为专辑表中这两个字段是后加入的,因此只能用一条sql语句将 ...
- Codility---Dominator
Task description A zero-indexed array A consisting of N integers is given. The dominator of array A ...
- Oracle PL/SQL编程
一.PL/SQL简介 1.概念:PL/SQL是Oracle在标准SQL语言上的过程性扩展. 2.优点和特性 提高应用程序的运行性能 提供模块化的程序设计功能 允许定义标示符 具有过程语言控制结构 具备 ...
- uni-app中vue组件父子值传递
一.父组件向子组件传递数据(props) <template> <view class="container" style="background: # ...
- Centos 7 防火墙 firewalld 简单使用说明
1.firewalld简介 firewalld是centos7的一大特性,最大的好处有两个:支持动态更新,不用重启服务:第二个就是加入了防火墙的“zone”概念 2.firewalld命令行界面管 ...
- 错误处理之try、catch、finally中的return、throw执行顺序。
今天遇到一个让人无语的代码块 try { bilSheetService.syncUser(bilWebseviceLog, userId, optType); }catch (Exception e ...
- 曹工说Tomcat2:自己撸一个简易Tomcat Digester
一.前言 框架代码其实也没那么难,大家不要看着源码就害怕,现在去看 Tomcat 3.0的代码,保证还是看得懂一半,照着撸一遍基本上很多问题都能搞定了.这次我们就模拟 Tomcat 中的 Digest ...