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

