mybatis
Posted code home
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis相关的知识,希望对你有一定的参考价值。
批量插入,主键冲突就更新
<insert id="insertFpxxBatch" parameterType="java.util.List"> <![CDATA[ insert into BM_FPXX ( fpzl,fphm,gfmc,gfsh.... ) values ]]> <foreach collection="list" item="item" separator=","> <![CDATA[ (#{item.fpzl,jdbcType=VARCHAR},#{item.fphm,jdbcType=VARCHAR},SYSDATE(),SYSDATE()。。。。) ]]> </foreach> on duplicate key update fpzl=VALUES(fpzl),fphm=VALUES(fphm),gfmc=VALUES(gfmc), update_time=current_timestamp </insert>
自增主键插入
<insert id="insertRole" parameterType="com.holytax.common.data.role.entity.Role"> <selectKey resultType="java.lang.Long" keyProperty="id"> SELECT LAST_INSERT_ID() as id </selectKey> INSERT INTO bm_role <trim prefix="(" suffix=")" suffixOverrides=","> <if test="roleName != null"> ROLE_NAME, </if> <if test="description != null"> DESCRIPTION, </if> <if test="status != null"> STATUS, </if> <if test="createTime != null"> CREATE_TIME, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="roleName != null"> #{roleName,jdbcType=VARCHAR}, </if> <if test="description != null"> #{description,jdbcType=VARCHAR}, </if> <if test="status != null"> #{status,jdbcType=CHAR}, </if> <if test="createTime != null"> #{createTime,jdbcType=TIMESTAMP}, </if> </trim> </insert>
<!-- 分页查询数据头文件 把头和尾加到正常sql语句上-->
<sql id="queryHeader">
select * from (
</sql>
<!-- 分页查询数据尾文件 -->
<sql id="queryFooter">
<![CDATA[) innerresult limit #{start}, #{end}]]>
</sql>
日期操作
<if test="kprqBegin != null">
and DATE_FORMAT(kprq, ‘%Y-%m-%d‘) >= DATE_FORMAT(#{kprqBegin}, ‘%Y-%m-%d‘)
</if>
<if test="kprqEnd != null">
and DATE_FORMAT(kprq, ‘%Y-%m-%d‘) <= DATE_FORMAT(#{kprqEnd}, ‘%Y-%m-%d‘)
</if>
order by str_to_date(kprq, ‘%Y-%m-%d‘) desc
以上是关于mybatis的主要内容,如果未能解决你的问题,请参考以下文章
SSM-MyBatis-05:Mybatis中别名,sql片段和模糊查询加getMapper
MYBATIS05_ifwherechoosewhentrimsetforEach标签sql片段