Mybatis的一些使用积累
基础
通过 映射数据库与实体类的字段。
<!-- 映射对应的Mapper接口 -->
<mapper namespace="com.xxx.xx.mapper.xxxMapper">
<!-- 通用查询映射结果 -->
<resultMap id="ResultMap" type="com.ydmh.api.entity.BusinessBaseEntity">
<result column="DM" property="fwsdm"/>
<result column="MC" property="fwsmc"/>
<result column="FR" property="fddbr"/>
<result column="ZRSJ" property="zrsj"/>
<result column="sfsc" property="sfsc"/>
<result column="dzs" property="dzs"/>
<result column="FY" property="sywlf"/>
<result column="sfdz" property="sfdz"/>
<result column="PJMC" property="pjmc"/>
<result column="GZSJ" property="gzsj"/>
<result column="DZSJ" property="dzsj"/>
<result column="LLSJ" property="llsj"/>
<result column="LXMC" property="lxmc"/>
<result column="LOGO" property="logo"/>
<result column="YHID" property="yhid"/>
</resultMap>
Where标签
和 if test 标签还是有区别的,只匹配单个成立之后结束匹配,比如,下面的查询当 selectType = 0 的时候就执行 where DATE_FORMAT(t5.YF,’%Y%m’) = DATE_FORMAT(CURDATE(),’%Y%m’),后面的不再进行判断。
<where>
<choose>
<when test="selectType == 0">
DATE_FORMAT(t5.YF,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m')
</when>
<when test="selectType == 1">
PERIOD_DIFF( date_format( now( ), '%Y%m' ), date_format( t5.YF, '%Y%m' ) ) = 1
</when>
<when test="selectType == 2">
QUARTER(t5.YF) = QUARTER(NOW())
</when>
<when test="selectType == 3">
QUARTER(t5.YF) = QUARTER(DATE_SUB(NOW(),INTERVAL 1 QUARTER))
</when>
<when test="selectType == 4">
YEAR(t5.YF)=YEAR(NOW())
</when>
<when test="selectType == 5">
YEAR(t5.YF) = YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR))
</when>
<when test="selectType == 6">
date_format(t5.YF,'%Y-%m-%d') between #{startTime} AND #{endTime}
</when>
</choose>
</where>
if标签
如果,就。。。
<if test="lxmc !='不限' and lxmc !=null and lxmc !=''">
where t14.LXMC = #{lxmc}
</if>
foreach 标签的使用
可以在Mapper层通过@Param(“xxx”)指定一个List的别名。
Boolean updateStatusByMeterIds(@Param("meterIds") List<Long> meterIds);
<update id="updateStatusByMeterIds">
update test_table set STATUS = '2'
<if test="meterIds!=null and meterIds.size>0">
WHERE test_id IN
<foreach collection="meterIds" item="ids" index="index" open="(" close=")" separator=",">
#{ids}
</foreach>
</if>
</update>