MYBATIS问题汇总(自用)
一、mybatis SQL的批量操作
1>,批量插入
简单语法:
insert into table (字段一,字段二,字段三) values(xx,xx,xx),(oo,oo,oo)
示例:
<insert id="insertBatch" parameterType="java.util.List" useGeneratedKeys="true">
<selectKey resultType="long" keyProperty="id" order="AFTER">
SELECT
LAST_INSERT_ID()
</selectKey>
insert into wd_solr
(fayu_id, tablename,name,logo,description,section_no,look_count,favorite_count,create_uid,create_time,update_time,timestamp)
values
<foreach collection="list" item="wdSolr" index="index"
separator=",">
(
#{wdSolr.fayuId},#{wdSolr.tablename},#{wdSolr.name},#{wdSolr.logo},
#{wdSolr.description},#{wdSolr.sectionNo},#{wdSolr.lookCount},#{wdSolr.favoriteCount},
#{wdSolr.createUid},#{wdSolr.createTime},#{wdSolr.updateTime},#{wdSolr.timestamp}
)
</foreach>
</insert>
SELECT LAST_INSERT_ID() :
将插入数据的主键返回到 user 对象中。
SELECT LAST_INSERT_ID():得到刚 insert 进去记录的主键值,只适用与自增主键
keyProperty: 将查询到主键值设置到 parameterType 指定的对象的那个属性
order: SELECT LAST_INSERT_ID() 执行顺序,相对于 insert 语句来说它的执行顺序
resultType: 指定 SELECTLAST_INSERT_ID() 的结果类型
2>,批量更新
<update id="updPlanConferenceDetails" parameterType="java.util.List" > <foreach collection="list" item="item" index="index" open="" close="" separator=";"> update t_propose_meeting_commit_del <set > <if test="item.reason != null" > not_arrange_meeting_reason = #{item.reason,jdbcType=VARCHAR}, </if> <if test="item.expectMeetingTime != null" > expect_meeting_time = #{item.expectMeetingTime,jdbcType=VARCHAR}, </if> <if test="item.expectMeetingAddress != null" > expect_meeting_address = #{item.expectMeetingAddress,jdbcType=VARCHAR}, </if> </set> where uuid = #{item.id,jdbcType=VARCHAR} </foreach> </update>
二、MyBatis 中拼接字符串:
CONCAT 函数:SELECT * FROM user WHERE name LIKE CONCAT(CONCAT('%', #{name}), '%')
三、动态标签:
1> <choose>
<when>
</when>
</choose>
<select id="getEmpByChoose" resultType="Emp" parameterType="Emp">
select * from emp where 1 = 1
<choose>
<when test="job != null">
and job = #{job}
</when>
<when test="deptno != null">
and deptno = #{deptno}
</when>
<otherwise>
and mgr = #{mgr}
</otherwise>
</choose>
</select>
2> if 或者 when 动态标签的额时候,test里判断字符串是否相等时,需要
<choose>
<when test="prsnType == '0'.toString()">
AND rel.prsn_type = 0 AND base.fin_row_meeting_stat = 0
</when>
<when test="prsnType == '1'.toString()">
AND rel.prsn_type = 1 AND base.not_fin_row_meeting_stat = 0
</when>
</choose>
为什么呢?因为mybatis会把'0'解析为字符,java是强类型语言,所以不能这样写。
四、传参
1>,常规写法
DAO层常规写法: int insertMulti(@Param("menuId")int menuId, @Param("authorities")List<Integer> authorities); Mapper取参: <insert id="insertMulti"> insert into auth_menu (menu_id,authority_id) values <foreach collection="authorities" item="authId" index="index" separator="," > (#{authId,jdbcType=INTEGER},#{menuId}) </foreach> </insert>
2>,参数:Object obj,属性List
Mapper接口定义:
int batchInsert(@Param("st")Station st);
Bean定义:
public class Station{ private String stationId; private List<Direction> directions; setter,getter...... }
XML实现:
<insert id="batchInsert"> insert into DIRECTION(ID,STATION_ID,DIRECTION_ID,DIRECTION_NAME,DIRECTION_DESC) select CONCAT(#{st.stationId,jdbcType=VARCHAR},DIRECTION_ID) ID,
#{st.stationId,jdbcType=VARCHAR} STATION_ID,
DIRECTION_ID,
CODEDESC DIRECTION_NAME,
CODEDESC DIRECTION_DESC
from( <foreach collection="st.directions" item="item" index="index" separator=" union " > select #{item.directionId,jdbcType=VARCHAR} DIRECTION_ID </foreach> )a,t_sys_code b where a.DIRECTION_ID = b.CODE and a.DIRECTION_ID not in(select DIRECTION_ID from DIRECTION where STATION_ID = #{st.stationId,jdbcType=VARCHAR}) </insert>
<select id="queryDelayMeetingPlanState" parameterType="list" resultType="java.lang.Integer">
SELECT
count(*)
FROM meet_mag_postpone_proj_list m
LEFT JOIN cens_iss_app_piec_info base on base.iss_app_piec_id = m.iss_app_piec_id
WHERE m.is_smit_row_meeting_app = 0 AND m.is_tect = 1
<if test="list != null and list.size()>0" >
AND m.iss_app_piec_id in
<foreach item="item" collection="list" separator="," open="(" close=")" index="">
#{item.issAppPiecId}
</foreach>
</if>
AND base.proj_stat_cd in (103,303,307,309,404)
</select>
未完待续~

浙公网安备 33010602011771号