mybatis3中@SelectProvider傳遞參數(shù)方式
mybatis3 @SelectProvider傳遞參數(shù)
一、通常情況下我喜歡使用實(shí)體或者vo去傳參數(shù)
這樣在Provide的方法中可以直接通過(guò)#{param}(param為你實(shí)體中的字段)來(lái)獲取你要的參數(shù)。
二、使用map傳參數(shù)
在超過(guò)一個(gè)參數(shù)的情況下,@SelectProvide方法必須接受Map<String, Object>做為參數(shù),
如果參數(shù)使用了@Param注解,那么參數(shù)在Map中以@Param的值為key,如下例中的userId;
如果參數(shù)沒(méi)有使用@Param注解,那么參數(shù)在Map中以參數(shù)的順序?yàn)閗ey,如下例中的password:
UserMapper.java:
@SelectProvider(type = SqlProvider.class, method = "selectUserCheck") @ResultMap("userMap") public User getUserCheck(@Param("userId") long userId, String password);
SqlProvider.java:
public String selectUserCheck(Map<String, Object> para) { return "select * from user where userId=" + para.get("userId") + " and password=' " + para.get("1") + "'"; }
@SelectProvider,@Select和xml用法的一點(diǎn)理解
1.@Select
同@select功能類(lèi)似的還有@insert,@delete,@update,對(duì)應(yīng)于數(shù)據(jù)庫(kù)語(yǔ)句的CRUD。使用@select很方便,不用寫(xiě)配置文件,一般是寫(xiě)在mapper的interface類(lèi)中,用法如下:
public interface AdmainMapper{ @Select("SELECT * FROM userinfo WHERE username = #{username} AND password = #{password}") @Results(value = { //@Result這項(xiàng)可以不寫(xiě) @Result(id = true, column = "id", property = "id"), @Result(column = "username", property = "username"), @Result(column = "password", property = "password") }) public Admin selectAdmin(@Param("username") String username, @Param("password") String password);//參數(shù)可以是一個(gè)對(duì)象,sql語(yǔ)句會(huì)自動(dòng)匹配 }
使用注解的方式很簡(jiǎn)單但是不是很靈活,對(duì)于動(dòng)態(tài)條件查詢(xún)是無(wú)法實(shí)現(xiàn)的,這時(shí),我們可以使用xml注解的方式。
2.xml方式
public interface AdminMapper { public List<Admin> getAdminByConditions(@Param("username")String username, @Param("password")String password, @Param("start")int start, @Param("limit")int limit);//參數(shù)可以直接傳入一個(gè)類(lèi),在xml中可以自動(dòng)匹配 }
注解的方式和xml的方式可以寫(xiě)在同一個(gè)類(lèi)中,就如上所示,這兩個(gè)方法可以在同一個(gè)類(lèi)中。這里寫(xiě)上@Param,在xml中就不用使用parameterType屬性了。
<?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"> <!-- 上面類(lèi)的路徑--> <mapper namespace="com.xxx. ... .AdminMapper"> <!-- 指定字段映射 --> <resultMap type="com. ... .Student" id="studentResultMap"> //下面方法返回結(jié)果存放類(lèi)型通過(guò)id識(shí)別 <id property="id" column="id" /> <result property="name" column="name" /> <result property="age" column="age" /> <result property="classId" column="classId" /> </resultMap> <sql id="Base_Column_List"> //使用這種方法可以使重復(fù)使用的字段通過(guò)id直接調(diào)用即可,很方便 id, name ,age,classId </sql> <!-- 若不需要自動(dòng)返回主鍵,將useGeneratedKeys="true" keyProperty="id"去掉即可 --> <select id="getAdminByConditions" parameterType="com. ... .StudentVo" resultMap="studentResultMap"> <![CDATA[ SELECT ]]> //此id要和mapper中的方法名對(duì)應(yīng) <include refid="Base_Column_List" /> <![CDATA[ FROM student WHERE 1=1]]> <if test="id != null"><![CDATA[ AND id = #{id} ]]></if> <if test="name != null"><![CDATA[ AND name like CONCAT('%',#{name},'%') ]]></if> <if test="age != null"><![CDATA[ AND age = #{age} ]]></if> <![CDATA[ ORDER BY id DESC limit #{startRow} , #{pageSize} ]]> </select> </mapper>
xml的文件名最好與mapper的名字對(duì)應(yīng)下來(lái)。
此外我們還需要對(duì)配置文件進(jìn)行配置,這里我就不多說(shuō),大家自行百度
3.@SelectProvider方式
我覺(jué)得這種方式集成了前兩種方法的優(yōu)點(diǎn),用法如下:
public interface AdmainMapper{ @SelectProvider(type = SqlBuilder.class, method = "queryList")//指定所用sql語(yǔ)句 @ResultMap("student") List<student> getList(@Param("start") int start, @Param("pageSize") int pageSize, @Param("apIds") String apIds, @Param("model") String model); }
public class SqlBuilder {//與type對(duì)應(yīng) public String queryList(@Param("start") int start, @Param("pageSize") int pageSize, //于method對(duì)應(yīng) @Param("apIds") String apIds, @Param("model") String model) { String sql = " SELECT ap.* FROM ap WHERE ap.id NOT in ( SELECT apid FROM ap_activity_mapping WHERE del = 0 "; if (StringUtils.isNotEmpty(apIds)) { sql += " and apid not in (" + apIds + ") "; } sql += " ) "; if (StringUtils.isNotEmpty(model)) { sql += " and model = '" + model + "'"; } sql += " GROUP BY ap.id limit " + start + "," + pageSize; return sql; }//返回類(lèi)型必須是String }
用這種方法要注意的是不要重載,此外還接受Map<String, Object>作為參數(shù)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
比較java中Future與FutureTask之間的關(guān)系
在本篇文章里我們給大家分享了java中Future與FutureTask之間的關(guān)系的內(nèi)容,有需要的朋友們可以跟著學(xué)習(xí)下。2018-10-10SpringMvc配置靜態(tài)資源訪問(wèn)路徑的實(shí)現(xiàn)
本文主要介紹了SpringMvc配置靜態(tài)資源訪問(wèn)路徑的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Springboot項(xiàng)目中運(yùn)用vue+ElementUI+echarts前后端交互實(shí)現(xiàn)動(dòng)態(tài)圓環(huán)圖(推薦)
今天給大家?guī)?lái)一篇教程關(guān)于Springboot項(xiàng)目中運(yùn)用vue+ElementUI+echarts前后端交互實(shí)現(xiàn)動(dòng)態(tài)圓環(huán)圖的技能,包括環(huán)境配置及圓環(huán)圖前端后端實(shí)現(xiàn)代碼,感興趣的朋友一起看看吧2021-06-06JAVA 獲取系統(tǒng)當(dāng)前時(shí)間實(shí)例代碼
這篇文章主要介紹了JAVA 獲取系統(tǒng)當(dāng)前時(shí)間實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-10-10