1.批量添加
<insert id="insertBatch" parameterType="java.util.List">
insert into student(id,name,age)
values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.id}, #{item.name},#{item.age})
</foreach>
</insert>
2.批量修改
<update id="updateBatch" parameterType="java.util.List">
<foreach collection="list" item="item" index="index">
update student
<set>
sex = #{item.sex }
</set>
where id= #{item.id}</foreach>
</update>
如果拿到Navicat中可以执行但是mybatis中不行时需要在jdbc.url中配置allowMultiQueries=true
3.IFNULL(result1,result2):如果不为null则返回第一个的值,否则返回第二个的值
如:IFNULL(a.iccid,'') as iccid
4.对日期进行格式化
如:DATE_FORMAT(a.create_time,'%Y-%m-%d %H:%i:%s') as create_time_str
5.多字段进行检索数据:
(
g.vin like CONCAT('%',#{equalMap.queryTerminalParam},'%' )
or f.sim_no like CONCAT('%',#{equalMap.queryTerminalParam},'%' )
or g.vehicle_number like CONCAT('%',#{equalMap.queryTerminalParam},'%' )
or c.did like CONCAT('%',#{equalMap.queryTerminalParam},'%' )
)
6.IF(表达式,result1,result2):如果表达式结果为true,返回第一个的值,否则返回第二个的值
如:IF(b.vehicle_id is null,0,1) as STATUS效果类似于(CASE WHEN b.vehicle_id IS NULL THEN '0' ELSE '1' END) AS STATUS
7.更新某条记录时判断修改的字段是否已经被使用的判断方法:
SELECT COUNT(1) FROM tb_vehicle where enable_flag='1' and vehicle_number = #{vehicleNumber} AND ID != #{id}
8.时间段查询写法:
<if test="equalMap != null && equalMap.startTime != null &&
equalMap.startTime!=''" >
and a.operate_time >=#{equalMap.startTime}</if>
<if test="equalMap != null && equalMap.endTime != null &&
equalMap.endTime!=''" >
and a.operate_time <=#{equalMap.endTime}</if>
9.单个时间查询写法:
<if test="equalMap != null && equalMap.loseTime != null &&
equalMap.loseTime!=''" >
and a.lose_time>=#{equalMap.loseTime}and a.lose_time<=CONCAT(#{equalMap.loseTime},' ','23:59:59')
</if>
10. GROUP_CONCAT:分组后合并数据即将多行数据合并成一行数据