Mybatis 不确定条件查询(动态语句where和if)借助实体类封装参数
程序员文章站
2022-06-21 18:56:44
...
Mybatis 不确定条件查询(动态语句where和if)
在下面的案例本人将演示如何使用动态语句批量删除数据库数据,本人在数据库建了一张emp员工表(表的数据自己填充),表的结构如下:
核心代码为,在实体映射文件中配置如下的代码:
<!-- 多条件查询,条件个数不确定 -->
<!-- 方式一 -->
<select id="findEmpByConditionChanged" parameterType="cn.sz.hcq.pojo.EmpCondition"
resultType="cn.sz.hcq.pojo.Emp">
select empno,ename,job,mgr,sal,comm,hiredate
from emp where 1=1
<if test="kw!=null">and ename like '%${kw}%' </if>
<if test="job!=null">and job=#{job}</if>
<if test="losal!=null&&hisal!=null">
and sal between #{losal} and #{hisal}
</if>
</select>
<!--方式二 -->
<!-- where 标签能够自动把第一个条件的and去掉 (&转义符为&) -->
<select id="findEmpByConditionChanged" parameterType="cn.sz.hcq.pojo.EmpCondition"
resultType="cn.sz.hcq.pojo.Emp">
select empno,ename,job,mgr,sal,comm,hiredate
from emp
<where>
<if test="kw!=null">
ename like '%${kw}%' </if>
<if test="job!=null">and job=#{job}</if>
<if test="losal!=null&&hisal!=null">
and sal between #{losal} and #{hisal}
</if>
</where>
</select>
下面是项目的结构(本人使用的是maven搭建的web项目):
IEmpDAO.java为接口提供修改数据方法,EmpDAOImpl.java为接口的实现类,MybatisSqlSessionFactory.java为本人创建的获取sqlSession的工具类,EmpCondition.java为多条件查询将查询条件封装成的实体类,Emp.java为实体类,Emp.xml为映射文件,mybatis_cfg.xml为mybatis主配置文件,Test.java为测试类,pom.xml为maven引入依赖的文件。
1、IEmpDAO.java为接口提供查询方法
/**
* 多条件查询,但条件个数是不确定的情况
*
* @param ec 封装的添加实体类为参数
* @return
*/
public List<Emp> findEmpByConditionChanged(EmpCondition ec);
2、EmpDAOImpl.java为接口的实现类
public List<Emp> findEmpByConditionChanged(EmpCondition ec) {
SqlSession sqlSession = null;
try {
sqlSession = MybatisSqlSessionFactory.getMySqlSession();
return sqlSession.selectList(
"cn.sz.hcq.pojo.Emp.findEmpByConditionChanged", ec);
} catch (Exception e) {
e.printStackTrace();
} finally {
MybatisSqlSessionFactory.closeSqlSession();
}
return null;
}
3、MybatisSqlSessionFactory.java为本人创建的获取sqlSession的工具类
4、Emp.java为实体类
5、Emp.xml为映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.sz.hcq.pojo.Emp">
<!-- 多条件查询(将条件封装为一个实体类) -->
<!-- 多条件查询,条件个数不确定 -->
<!-- 方式一 -->
<!--<select id="findEmpByConditionChanged" parameterType="cn.sz.hcq.pojo.EmpCondition"
resultType="cn.sz.hcq.pojo.Emp">
select empno,ename,job,mgr,sal,comm,hiredate
from emp where 1=1
<if test="kw!=null">and ename like '%${kw}%' </if>
<if test="job!=null">and job=#{job}</if>
<if test="losal!=null&&hisal!=null">
and sal between #{losal} and #{hisal}
</if>
</select>-->
<!--方式二 -->
<!-- where 标签能够自动把第一个条件的and去掉 (&转义符为&) -->
<select id="findEmpByConditionChanged" parameterType="cn.sz.hcq.pojo.EmpCondition"
resultType="cn.sz.hcq.pojo.Emp">
select empno,ename,job,mgr,sal,comm,hiredate
from emp
<where>
<if test="kw!=null">
ename like '%${kw}%' </if>
<if test="job!=null">and job=#{job}</if>
<if test="losal!=null&&hisal!=null">
and sal between #{losal} and #{hisal}
</if>
</where>
</select>
</mapper>
6、mybatis_cfg.xml为mybatis主配置文件
7、pom.xml为maven引入依赖的文件
8、Test.java为测试类
public class Test {
public static void main(String[] args) {
// 多条件查询(将条件封装为一个实体类)
IEmpDAO empDAO = new EmpDAOImpl();
// 多条件查询,但条件个数是不确定的情况
System.out.println("*************多条件查询,但条件个数是不确定的情况****************");
// 将查询的条件信息封装为一个实体类
EmpCondition ec = new EmpCondition();
// 可以设置不确定的条件进行查询
ec.setKw("s");// 查询名字包括s
ec.setJob("clerk");// 查询职位为clerk
ec.setLosal(1000d);// 查询最低薪水为1000
ec.setHisal(5000d);// 查询最高薪水为5000
List<Emp> empList = empDAO.findEmpByConditionChanged(ec);
for (int i = 0; i < empList.size(); i++) {
Emp emp = empList.get(i);
System.out.println(emp.getEname() + "," + emp.getJob() + ","
+ emp.getSal());
}
}
}
完成代码后,运行测试类就可以完成不确定条件的查询。