題目
在EmpMapper接口中增加如下方法:
//根據動態條件查詢符合條件的員工
List<Emp> getEmpByCondition(條件類);
測試如下功能:
1)請查詢 員工所在部門為2,工資在3000-5000的員工。
2)請查詢 員工入職時間超過5年以上,工資在3000-5000的員工。
代碼示例
EmpMapper.xml:
這里僅列出主要方法代碼。
<!--這里用了兩種大于、小于、大于等于、小于等于的寫法,僅為練習-->
<select id="getEmpByCondition" parameterType="Map" resultMap="EmpMap">
select * from emp
<trim prefix="where" suffixOverrides="and">
<!--入職時間區間-->
<if test="beginHireDate!=null or endHireDate!=null">
<choose>
<!--開始入職時間和結束入職時間相同-->
<when test="beginHireDate==endHireDate">
hireDate=#{beginHireDate} and
</when>
<when test="beginHireDate!=endHireDate">
<![CDATA[ hireDate>=#{beginHireDate} and hireDate<=#{endHireDate} and]]>
</when>
<when test="beginHireDate!=null and endHireDate==null">
<![CDATA[ hireDate>=#{beginHireDate} and]]>
</when>
<when test="beginHireDate==null and endHireDate!=null">
<![CDATA[hireDate<=#{endHireDate} and]]>
</when>
</choose>
</if>
<!--工資區間-->
<if test="beginSalary!=null or endSalary!=null">
<!--開始工資和初始工資相同-->
<choose>
<when test="beginSalary==endSalary">
salary =#{beginSalary} and
</when>
<when test="beginSalary!=endSalary">
salary >=#{beginSalary} and salary <=#{endSalary} and
</when>
<when test="beginSalary!=null and endSalary==null">
salary >=#{beginSalary} and
</when>
<when test="beginSalary==null and endSalary!=null">
salary <=#{endSalary} and
</when>
</choose>
</if>
<!--入職時間年長-->
<if test="hirePerTime!=null and hirePerTimeCon!=null">
<choose>
<when test="hirePerTimeCon=='gt'">
TIMESTAMPDIFF(YEAR,hireDate,curdate())>#{hirePerTime} and
</when>
<when test="hirePerTimeCon=='ge'">
TIMESTAMPDIFF(YEAR,hireDate,curdate())>=#{hirePerTime} and
</when>
<when test="hirePerTimeCon=='lt'">
TIMESTAMPDIFF(YEAR,hireDate,curdate())<#{hirePerTime} and
</when>
<when test="hirePerTimeCon=='le'">
TIMESTAMPDIFF(YEAR,hireDate,curdate())<=#{hirePerTime} and
</when>
</choose>
</if>
<!--根據部門編號查詢員工-->
<if test="deptnoList!=null">
deptno in
<foreach collection="list" item="deptno" open="(" separator="," close=")">
#{deptno}
</foreach>
</if>
</trim>
</select>
EmpController.java:
@RequestMapping(value="/getEmpByCondition", method= RequestMethod.POST)
@ResponseBody
public Map<String,Object> getEmpByCondition(@RequestParam(value="deptno",required = false) List deptno,
@RequestParam(value="beginHireDate",required = false) String beginHireDate,
@RequestParam(value="endHireDate",required = false) String endHireDate,
@RequestParam(value="hirePerTime",required = false) Integer hirePerTime,
@RequestParam(value="hirePerTimeCon",required = false) String hirePerTimeCon,
@RequestParam(value="beginSalary",required = false) Double beginSalary,
@RequestParam(value="endSalary",required = false) Double endSalary){
Map<String,Object> conditions = new HashMap<>();
Map<String,Object> returnMap = new HashMap<>();
try {
if(deptno!=null)
conditions.put("deptno",deptno);
if(beginHireDate!=null){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
conditions.put("beginHireDate",format.parse(beginHireDate));
}
if(endHireDate!=null){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
conditions.put("endHireDate",format.parse(endHireDate));
}
if(beginSalary!=null)
conditions.put("beginSalary",beginSalary);
if(endSalary!=null)
conditions.put("endSalary",endSalary);
//入職時間年長
if(hirePerTime!=null&&hirePerTimeCon!=null){
conditions.put("hirePerTime",hirePerTime);
conditions.put("hirePerTimeCon",hirePerTimeCon);
}
} catch (ParseException e) {
e.printStackTrace();
}
List<Employee> elist = employeeService.getEmpByCondition(conditions);
if(elist.size()!=0){
returnMap.put("message","findUserByDeptno successfully!");
returnMap.put("employeeList",elist);
}else{
returnMap.put("message","can not find user");
}
return returnMap;
}