OA项目之mybatis动态查询

类似于三个条件,可以全部选择,也可以选择几个条件进行查询
Mapper.xml文件:
<resultMap type="Employee" id="selAll">
<id property="empId" column="empid"/>
<id property="hireDate" column="hiredate"/>
<id property="sex" column="sex"/>
<id property="phone" column="phone"/>
<result property="realName" column="realname"/>
<collection property="mgr" ofType="employee">
<id property="empId" column="mgrid"/>
<result property="realName" column="name"/>
</collection>
<collection property="position" ofType="position">
<id property="posid" column="posid"/>
<result property="pname" column="pname"/>
</collection>
<collection property="dept" ofType="Department">
<id property="deptno" column="deptno"/>
<result property="deptname" column="deptname"/>
</collection>
</resultMap>
<select id="selbyd" resultMap="selAll">
select e.empid,e.realname,e.sex,e.hiredate,e.phone,d.deptname,p.pname,e2.realname
from employee e left join dept d on e.deptno=d.deptno
left join position p on e.posid=p.posid
left join employee e2 on e.mgrid=e2.empid
<where>
and e.onduty=#{onduty}
<if test="deptno!=0">
and d.deptno=#{deptno}
</if>
<if test="empid!=null and empid!=''">
<bind name="empid" value="'%'+ empid +'%'"/>
and e.empid like #{empid}
</if>
<if test="hiredate!=null and hiredate!=''">
and e.hiredate >=#{hiredate}
</if>
</where>
</select>
中间用到了模糊查询。
Mapper接口:
List<Employee> selbyd(@Param("empid") String empid,@Param("deptno") int deptno,@Param("hiredate") Date hiredate,@Param("onduty") int onduty);
Service方法:
List<Employee> selbyd(String empid, int deptno, Date hiredate, int onduty);
ServiceImpl:
@Override
public List<Employee> selbyd(String empid,int deptno, Date hiredate,
int onduty) {
List<Employee> selbyd = mapper.selbyd(empid, deptno, hiredate, onduty);
session.close();
return selbyd;
}
Servlet:
public void selinfo(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String empid = request.getParameter("empId");
int deptno =Integer.parseInt(request.getParameter("deptno"));
String shireDate = request.getParameter("hireDate");
int onduty =Integer.parseInt(request.getParameter("onDuty"));
DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
Date hiredate =null;
try {
hiredate = df.parse(shireDate);
} catch (ParseException e) {
System.out.println("");
}
//获取所有的部门信息
DepartmentService ds=new DepartmentServiceImpl();
List<Department> deptList = ds.seAll();
request.setAttribute("deptList", deptList);
EmployeeService es = new EmployeeServiceImpl();
List<Employee> selAll = es.selbyd(empid, deptno, hiredate, onduty);
request.setAttribute("empid", empid);
request.setAttribute("deptno", deptno);
request.setAttribute("onduty", onduty);
request.setAttribute("hireDate", shireDate);
request.setAttribute("selAll", selAll);
request.getRequestDispatcher("/system/empList.jsp").forward(request, response);
}
查询所有:

条件查询:

OA项目之mybatis动态查询的更多相关文章
- Mybatis动态查询语句
MyBatis中动态SQL语句完成多条件查询 标签: mybatis动态SQL多条件查询java.sql.SQLSyntaxEr 2015-06-29 19:00 22380人阅读 评论(0) 收藏 ...
- Mybatis动态查询
需要导入的jar包: 实体类User: package com.bjsxt.pojo; import java.io.Serializable; public class User implement ...
- SQL mybatis动态查询小结
动态返回mysql某张表指定列的名字 <select id="queryColumns" resultType="map" parameterType=& ...
- OA项目之Mybatis多表链接查询
xml文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC & ...
- Java高级架构师(一)第16节:Mybatis动态查询和Json自动拼装
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...
- 在MyBatis中查询数据、涉及多参数的数据访问操作、插入数据时获取数据自增长的id、关联表查询操作、动态SQL、关于配置MyBatis映射没有代码提示的解决方案
1. 单元测试 在单元测试中,每个测试方法都需要执行相同的前置代码和后置代码,则可以自定义2个方法,分别在这2个方法中执行前置代码和后置代码,并为这2个方法添加@Before和@After注解,然后, ...
- mybatis 动态SQL查询总结
背景 ××项目需要提供系统部分函数第三方调用接口,基于安全性和避免暴露数据库表信息的基础上进行函数接口的设计,根据第三方调用身份的权限提供某张表的自定义集合.本项目基于mybatis的持久层框架,支持 ...
- mybatis深入之动态查询和连接池介绍
mybatis深入之动态查询和连接池介绍 一.mybatis条件查询 在mybatis前述案例中,我们的查询条件都是确定的.但在实际使用的时候,我们的查询条件有可能是动态变化的.例如,查询参数为一个u ...
- 项目一:第十三天 1、菜单数据管理 2、权限数据管理 3、角色数据管理 4、用户数据管理 5、在realm中动态查询用户权限,角色 6、Shiro中整合ehcache缓存权限数据
1 课程计划 菜单数据管理 权限数据管理 角色数据管理 用户数据管理 在realm中动态查询用户权限,角色 Shiro中整合ehcache缓存权限数据 2 菜单数据添加 2.1 使用c ...
随机推荐
- Linux之ant安装部署
接下来呢,就开始ant的部署,具体分为如下几个步骤: 1. 获取介质: 在apache的官网中直接下载,下载地址为:http://ant.apache.org/ 下载需要的版本即可: 2. 复制到us ...
- PHP中高级面试题 一个高频面试题:怎么保证缓存与数据库的双写一致性?
分布式缓存是现在很多分布式应用中必不可少的组件,但是用到了分布式缓存,就可能会涉及到缓存与数据库双存储双写,你只要是双写,就一定会有数据一致性的问题,那么你如何解决一致性问题? Cache Aside ...
- JS如何在不给新空间的情况下给数组去重?
1.先排序,在让相邻元素对比去重 const nums = [3, 1, 1, 5, 2, 3, 4, 3, 5, 5, 6, 4, 6, 6, 6]; Array.prototype.arrayNo ...
- C语言|博客作业07
这个作业属于哪个课程 C语言程序设计II 这个作业的要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-1/homework/9935 我在这个课程的 ...
- shell脚本3——调试
bash -x file.sh 这样会把执行到的语句全部打印出来 #!/bin/bash 不会打印的程序块 set -v 需要打印的程序块 set -v 不会打印的程序块
- 万恶之源-python的部分内容
1.字符串格式化输出 %占位符: 声明占位的类型%s--字符串 %d%i--整型 %%转义 成为普通的% %s ,%d, %% msg = '%s,学习进度5%%' print(msg%(in ...
- 2019-9-17:渗透测试,基础学习,apache初识,mysql初识等笔记
python -m SimpleHTTPServer gedit 文本编辑器 apache2 默认配置文件目录:/etc/apache2/apache2默认首页源码: /var/www/html my ...
- 对 /langversion 无效;必须是 ISO-1、ISO-2、3、4、5 或 Default
反编译或者.net用更高版本打开时会出现这个问题,解决办法如下: 1.网页版程序,将解决方案中的Web.config中的 /langversion 的值改为指定的值,既可以解决,我这里采用的是默认值, ...
- 实战webpack系列01
01. 采坑webpack 一.webpack初章 // 一个常见的`webpack`配置文件 const webpack = require('webpack'); const HtmlWebpac ...
- 【JavaEE】之MyBatis输出映射
MyBatis中的输出映射有两种:resultType和resultMap. 1.resultType 使用resultType进行结果映射时,只有当查询结果中有至少一列的名称和resultType指 ...