MyBatis傳多個引數的常用方法
MyBatis傳多個引數的時候有很多種方法,這裡記錄一下自己常用的方法。
1、@Param註解。
Mapper.java介面:
int get***ByContentIdAndClassId(@Param("contentId")String contentId, @Param("classId")String classId);
Mapper.xml檔案:
<select id="get***ByContentIdAndClassId" resultType="int"> SELECT count(flag) FROM t_* n WHERE n.content_id = #{contentId,jdbcType=VARCHAR} AND n.class_id = #{classId,jdbcType=VARCHAR} </select>
2、索引【從0開始】。
Mapper.java介面:
int get***ByContentIdAndClassId(String contentId, String classId);
Mapper.xml檔案:
<select id="get***ByContentIdAndClassId" resultType="int">
SELECT
count(flag)
FROM
t_* n
WHERE
n.content_id = #{0}
AND n.class_id = #{1}
</select>
3、List【封裝in 】。
Mapper.java介面:
List<userBean> getUserBeanList(List<String> list);
Mapper.xml檔案:
<select id="getUserBeanList" resultType="userBean"> select * from t_user where id in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select>
4、引數是包裝類,返回List。
Mapper.java介面【使用@Param註解後xml檔案不需使用parameterType標記】:
List<CslExercise> selectByExerciseQueryVo(@Param("vo")ExerciseQueryVo exerciseQueryVo);
Mapper.xml檔案【使用別名進行對映】:
<select id="selectByExerciseQueryVo" resultType="...server.model.CslExercise">
select
exercise_id exerciseId,
exercise_name exerciseName,
exercise_type exerciseType,
task_type taskType,
pager_count pagerCount,
period_id periodId,
grade_id gradeId,
course_id courseId,
term_id termId,
sort,
create_time createTime,
end_time endTime,
state,
update_time updateTime
from
csl_exercise e
<trim prefix="WHERE" prefixOverrides="AND | OR">
<if test="vo.exerciseName!=null and vo.exerciseName!='' ">
and e.exercise_name like concat('%',#{vo.exerciseName},'%')
</if>
<if test="vo.periodId!=null and vo.periodId!='' ">
and e.period_id = #{vo.periodId}
</if>
<if test="vo.gradeId!=null and vo.gradeId!='' ">
and e.grade_id = #{vo.gradeId}
</if>
<if test="vo.courseId!=null and vo.courseId!='' ">
and e.course_id = #{vo.courseId}
</if>
<if test="vo.termId!=null and vo.termId!='' ">
and e.term_id = #{vo.termId}
</if>
<if test="vo.createTime!=null and vo.createTime!='' ">
and e.create_time >= #{vo.createTime}
</if>
<if test="vo.endTime!=null and vo.endTime!='' ">
and e.end_time <= #{vo.endTime}
</if>
</trim>
</select>
注意:
① 在對標題進行模糊搜尋時,需要用concat關鍵字連線%。在進行時間戳比較時,大於號需要用 > 表示,小於號用 < 表示。
② trim的作用:
| 作為一個格式化標記,在此處完成where標記的功能,也可以完成set標記的功能。
| prefixOverrides去掉拼接SQL中的第一個and或or關鍵字。
③ 對釋出時間進行降序排列:
List<CslExercise> list = cslExerciseService.selectByExerciseQueryVo(exerciseQueryVo);
Collections.sort(list,new Comparator<CslExercise>(){
@Override
public int compare(CslExercise o1, CslExercise o2) {
return o2.getCreateTime().compareTo(o1.getCreateTime());
}
});
5、如果包裝類裡面有List物件,則可以在resultMap中關聯collection標籤。
① 新增lombok依賴取代set和get方法。
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.8</version>
</dependency>
② 使用lombok.data註解的包裝類。
@Data
public class HomeworkDetail {
private String id;
private String publicAccountId;
private String publicUserName;
private Date publicTime;
private String title;
private String content;
private List<String> classIds;
private List<HomeworkAttachment> attachments;
private String isNeedCheck;
private String isFinish;
}
③ 包裝類中有集合的例項。
<resultMap id="HomeworkDetailMap" type="***.vo.HomeworkDetail">
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="public_account_id" jdbcType="VARCHAR" property="publicAccountId" />
<result column="public_user_name" jdbcType="VARCHAR" property="publicUserName" />
<result column="public_time" jdbcType="TIMESTAMP" property="publicTime" />
<result column="title" jdbcType="LONGVARCHAR" property="title" />
<result column="content" jdbcType="LONGVARCHAR" property="content" />
<result column="is_need_check" jdbcType="VARCHAR" property="isNeedCheck" />
<result column="is_finish" jdbcType="VARCHAR" property="isFinish" />
<collection property="classIds" resultMap="ClassInfoResultMap" />
<collection property="attachments" resultMap="AttachmentResultMap" />
</resultMap>
<resultMap id="ClassInfoResultMap" type="java.lang.String">
<result column="class_id" jdbcType="VARCHAR" property="classId" />
</resultMap>
<resultMap id="AttachmentResultMap" type="***.model.vo.HomeworkAttachment">
<id column="attachment_id" jdbcType="VARCHAR" property="id" />
<result column="path" jdbcType="VARCHAR" property="path" />
<result column="original_name" jdbcType="VARCHAR" property="originalName" />
<result column="homework_id" jdbcType="VARCHAR" property="homeworkId" />
<result column="type" jdbcType="VARCHAR" property="type" />
</resultMap>
----- END -----
相關推薦
MyBatis傳多個引數的常用方法
MyBatis傳多個引數的時候有很多種方法,這裡記錄一下自己常用的方法。 1、@Param註解。 Mapper.java介面: int get***ByContentIdAndClassId(@Param("contentId")String contentId, @P
mybatis傳多個引數(不使用@param註解情況下),3.4.2版本之後出現#{0}-#{n}引數繫結異常
解決方案: 在mybatis配置檔案中宣告setting屬性的useActualParamName 引數值為false **
Mybatis 傳入多個引數的方法
1.由於是多引數那麼就不能使用parameterType, 改用#{index}是第幾個就用第幾個的索引,索引從0開始 <update id="modifyPwd">
mybatis封裝多個引數的方法總結
1 單個引數 <select id="get" parameterType="long" resultType="string"> select name from test where id=#{id}
SpringMVC頁面向Controller提交陣列+Mybatis傳多個引數+MySQL一次寫入多條記錄
DAO層定義:int insertRole2Authorities(@Param("roleId")int roleId, @Param("authorities")List<Integer&g
mybatis傳多個引數(不使用@param註解情況下),3.4.2版本之後使用#{0}-#{n}引起的引數繫結異常,以及settings屬性中useActualParamName的作用。
解決方案: mybatis的xml配置檔案中宣告settings屬性的useActualParamName引數值為false <setting name="useActualParamName" value="false" /> 程式碼展示
Mybatis傳多個引數
三種方法 第一種: DAO層方法 Public User selectUser(String name,String area); 對應的Mapper.xml <select id="selectUser" resultMap="Bas
mybatis collection和association 傳多個引數
mybatis3.0版本以上,才可以 <resultMap type="CaseVO" id="processInfoList"> <id column="TEMPLATE_ID" property="taskTemplate.templ
mybatis多個引數處理方法
一,普通方法 多個引數時: 方法傳兩個引數 : 獲取方法: 二,使用@Param標註介面方法的傳入引數 註解 獲取: 三,如果引數太多,標註也不方便 1,可以直接傳入寫好的pojo類,也可以直接用 #{屬性名
Dao向mapper傳多個引數(Mybatis)
第一種方案 DAO層的函式方法 : Public User selectUser(String name,String age); 對應的Mapper.xml <select id="selectUser" resultMap="BaseResultMap"
Mybatis 傳入多個引數查詢資料 (3種方法)
據我目前接觸到的傳多個引數的方案有三種。 第一種方案 DAO層的函式方法 public User selectUser(String name,String area); 對應的Mapper.xml <select id="selectUser" result
java傳多個引數到mybatis
一、單個引數方法1:public List<XXBean> getXXBeanList(String id); <select id="getXXXBeanList" parameterType="java.lang.String" re
【轉】Mybatis傳多個參數(三種解決方案)
三種 方案 var nbsp myba rom name bsp 什麽 轉自: http://www.2cto.com/database/201409/338155.html 據我目前接觸到的傳多個參數的方案有三種。 第一種方案: DAO層的函數方法 Public
mybatis傳入多個引數
方案一: public List<XXXBean> getXXXBeanList(String xxId, String xxCode); <select id="getXXXBeanList" resultType="XXBean"> select t.*
mybatis 傳入多個引數
List<BoReview> check(@Param("flows") String[] flows, @Param("id") long id); <select id="check" resultMap="BaseResultMap" > select *
從onclick到function到ajax的url問號傳多個引數(更多的話以此類推)問題
廢話不多直接上程式碼 onclick <a class="blue" onclick="changeUse('${use.device_code}','${use.device_use }');" href="javascript:void(0)" data-rel="tooltip
關於超連結傳多個引數其中URL 中的帶 {xxx}的問題,
window.location.href=basePath+"/weixin/car/paysuccess/"+wid+"?returnMSG="+returnMSG; @RequestMapping(value = { "/paysuccess/{wid}" },
關於Ajax傳多個引數的問題
最近都要自學程式碼了,上學不好好上,現在只能自己動手豐衣足食了 最近碰到的是關於頁面區域性更新,直接貼程式碼吧! 首先講一下傳單個引數吧 單個比較簡單,直接如下data寫法就好 //區域性更新 function up(ind){ var u
django URL的補充 預設值 傳多個引數
url 後面還可以加上預設值 預設值 url(r'^index/', views.index, {'name': 'root'}), urls.py url對應關係 from django.conf.urls import url
iview Select元件傳多個引數
問題描述:on-change 事件預設返回的是value值,想要多傳一個引數 解決辦法: <Select v-model="model1" @on-change="handleChange($event, 'params')"></Select