MyBatis Mapper中 @Select注解調(diào)用靜態(tài)常量的問題分析
MyBatis Mapper中 @Select注解調(diào)用靜態(tài)常量
Java項目在使用MyBatis技術,編寫mapper SQL時,如果在其中的ognl表達式或者sql中直接使用一些數(shù)字或者字符串的話,會造成難以維護的問題。在Java編碼中,我們通常會把這些數(shù)字或者字符串定義在常量類或者接口中,可以直接在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注解使用
//根據(jù)用戶姓名和月份統(tǒng)計用戶上班缺卡次數(shù) @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);
mybatis mapper.xml中使用靜態(tài)方法或者靜態(tài)常量
靜態(tài)方法
語法
${@全限定類名@方法名}
示例
- 例1
<insert id="saveArticleLabel"> INSERT INTO dcy_article_label(id, article_id, label_id) VALUES <foreach collection="labelIds" item="item" separator=","> ('${@com.baomidou.mybatisplus.core.toolkit.IdWorker@getId()}', #{articleId}, #{item}) </foreach> </insert>
- 例2
<choose> // 需要靜態(tài)方法返回true還是false <when test="@staticClass@staticMethod(args)"> // 需要拼接的SQL </when> </choose>
- 例3
select <include refid="Base_Column"/> from xxx where valid = 1 <if test="record.code != null and record.code.trim().length() > 0"> and code ${@com.common.util.MapperHelp@getQuery("record.code",record.code)} </if>
public class MapperHelp { public static String getQuery(String propertyField, String value) { return "= 'xxx'"; } }
靜態(tài)常量
語法
${@全限定類名@常量名}
示例
select <include refid="Base_Column"/> from xxx where valid = ${@com.common.util.MapperHelp@VALID}
public class MapperHelp { public static final String VALID = "1"; }
到此這篇關于MyBatis Mapper中 @Select注解調(diào)用靜態(tài)常量的問題分析的文章就介紹到這了,更多相關MyBatis Mapper靜態(tài)常量內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
RabbitMQ中的Connection和Channel信道詳解
這篇文章主要介紹了RabbitMQ中的Connection和Channel信道詳解,信道是建立在 Connection 之上的虛擬連接,RabbitMQ 處理的每條 AMQP 指令都是通過信道完成的,需要的朋友可以參考下2023-08-08Spring?Data?JPA系列QueryByExampleExecutor使用詳解
這篇文章主要為大家介紹了Spring?Data?JPA系列QueryByExampleExecutor使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09