【Mybatis】多参数传递

时间:2021-09-01 21:26:31

1、单个参数

Dao层方法

   /**
* 根据学号查询所有考试信息 findAllExamByCode
* @param code
* @return
*/
List<MyExamModel> findAllExamByCode(String code);

Mapper对应内容

<select id="findAllExamByCode" parameterType="String" resultMap="MyExamModelMap">
SELECT
exam.course_id AS courseId,
exam.starting_date AS startTime ,
exam.ending_date AS endTime,
room.classroom_id AS roomId
FROM
t_examinee ee,
t_examination_room_arrangement room,
t_examination exam
WHERE
ee.examination_room_arrangement_id=room.id
ANDroom.examination_id=exam.id
ANDee.student_code=#{code}
</select>
<resultMap id="MyExamModelMap" type="com.dmsdbj.itoo.examinationEvaluation.entity.ext.MyExamModel">
<!--property="实体中属性名称(保持一致)",column="查询结果中对应的字段名称(见Select查询结果字段)"-->
<result property="courseId" column="course_id"/>
<result property="startTime" column="starting_date"/>
<result property="endTime" column="ending_date"/>
<result property="roomId" column="classroom_id"/>
</resultMap>

2、Map封装多参数

Dao层方法

    /**
* 根据学号查询本次考试详情 findExamByCode
* @param map (time 查询时间,code 学生学号)
* @return
*/
ExamInfoModel findExamByCode(Map map)throws Exception;


Mapper对应内容

    <select id="findExamByCode" parameterType="java.util.Map" resultMap="ExamineeExamInfoModelMap">
SELECT
ee.id AS examineeId , -- 考生安排ID
ee.student_code AS studentCode, -- 考生学号
ee.student_exam_state AS studentExamState, -- 考生考试状态
ee.student_ip AS studentIp,-- 考生IP
ee.examination_room_arrangement_id AS examRoomId, -- 考场ID
room.examination_room_name AS examRoomName, -- 考场名称
exam.course_id AS courseId, -- 课程ID
exam.examination_name AS examName, -- 考试名称
exam.starting_date AS examStartTime, -- 考试开始时间
exam.ending_date AS examEndTime, -- 考试结束时间
exam.examination_type_id AS examTypeId, -- 考试类型ID
exam.paper_or_template_id AS paperId, -- 试卷或模板ID
tp.is_paper_or_template AS isTemplatePaper -- 是试卷还是模板
FROM
t_examination exam,
t_examination_room_arrangement room,
t_examinee ee,
t_template_paper tp
WHERE
room.examination_id = exam.id
AND tp.id = exam.paper_or_template_id
AND ee.examination_room_arrangement_id=room.id
ANDCAST(#{time} AS TIME)>=CAST(exam.starting_date AS TIME)
AND CAST(exam.ending_date AS TIME) >= CAST(#{time} AS TIME)
AND exam.is_delete = "0"
AND exam.is_calculate = "0"
AND ee.is_delete="0"
and room.is_delete="0"
AND ee.student_code=#{code}
ORDER BY exam.starting_date ASC LIMIT 1
</select>
    <resultMap id="ExamineeExamInfoModelMap" type="com.dmsdbj.itoo.examinationEvaluation.entity.ext.ExamInfoModel">        <result property="examineeId" column="examineeId"/>        <result property="studentCode" column="studentCode"/>        <result property="studentExamState" column="studentExamState"/>        <result property="studentIp" column="studentIp"/>        <result property="examRoomId" column="examRoomId"/>        <result property="courseId" column="courseId"/>        <result property="examName" column="examName"/>        <result property="examStartTime" column="examStartTime"/>        <result property="examEndTime" column="examEndTime"/>        <result property="examTypeName" column="examTypeId"/>        <result property="paperId" column="paperId"/>        <result property="isTemplatePaper" column="isTemplatePaper"/>    </resultMap>


调用层代码

3、注解传多参数@Param("参数名")

Dao层方法


Mapper对应内容


        List<ExamHandleQuestionTypeModel> findTypeById(@Param("id") String id, @Param("code") String code);

4、List封装in(List为具体数据)

Dao层方法


Mapper对应内容


调用层代码



public List<XXXBean> getXXXBeanList(List<String> list);  

<select id="getXXXBeanList" resultType="XXBean">
  select 字段... from XXX where id in
  <foreach item="item" index="index" collection="list" open="(" separator="," close=")">  
    #{item}  
  </foreach>  
</select>  

foreach 最后的效果是select 字段... from XXX where id in ('1','2','3','4') 

5、索引获得参数值

Dao层方法


Mapper对应内容


IDEA 中是从右向左为索引增加方向

网上有人说myeclipes是从左向右为索引增加方向


6、内部传递多个参数
sql语句套sql语句,在colum中以map形式传参{参数名1=参数值1,参数名2=参数值2,…}