Java项目在使用MyBatis技术,编写mapper SQL时,如果在其中的ognl表达式或者sql中直接使用一些数字或者字符串的话,会造成难以维护的问题。在Java编码中,我们通常会把这些数字或者字符串定义在常量类或者接口中,可以直接在mapper中也可以使用这些常量就比较好。MybBatis@Select注解标签直接编写SQL,也是支持这样的需求的。
Java方法变量:
包名:com.yt.nbdt.adcenter.controller.vo
public static final String BELATE = "迟到";
public static final String LEAVE_EARLY ="早退";
public static final String LACK_OF_CARD = "缺卡";
mapper @Select注解使用
//根据用户姓名和月份统计用户上班缺卡次数
@Select({
"<script>",
"select count(1) from attendance_record where 1=1 AND clock_in_res ='${@com.yt.nbdt.adcenter.controller.vo.AttendanceRecordVO@LACK_OF_CARD}'",
"<if test='username != null'> and username = #{username}</if>",
"<if test='attendanceTime != null'>and attendance_time like '%${attendanceTime}%' </if>",
"</script>"
})
Integer getworkLackcardNum(@Param("username") String username, @Param("attendanceTime") String attendanceTime);