mybatis mapper.xml常用写法

本文详细介绍了MyBatis中resultMap的使用,包括id和result标签的配置,以及如何处理if和foreach语句进行条件判断和循环插入、更新操作。此外,还展示了choose语句的使用以及如何处理日期类型的数据。这些内容对于理解和优化MyBatis的SQL映射文件非常有帮助。
摘要由CSDN通过智能技术生成

resultMap写法

<resultMap id="BaseResultVoMap" type="*.*.*.Entity" >
    <id column="id" property="id" jdbcType="VARCHAR" />
    <result column="value" property="value" jdbcType="VARCHAR" />
    <result column="date" property="date" jdbcType="DATE" />
    <result column="time" property="time" jdbcType="TIMESTAMP" />
    <result column="status" property="status" jdbcType="INTEGER" />
    <result column="bool" property="bool" jdbcType="BOOLEAN" />
</resultMap>

if 书写

<if test=' value != null and value!= ""'>
     value = #{value}
</if>

foreach 书写

<foreach collection="ids" item="item" open="(" separator=" , " close=")" index="index">
     #{item}
</foreach>

批量插入 

<insert id="insert" parameterType="java.util.Map">
    insert into table(id, value, date, time, status)
    values
    <foreach collection="list" item="entity" separator=",">
        (
        #{entity.id}, 
        #{entity.value},
        #{entity.date},
        #{entity.time},
        #{entity.status}
        )
    </foreach>
</insert>

批量更新 

<update id="batchUpdate" parameterType="java.util.List">
	<foreach collection="lists" item="item" index="index" open="" close="" separator=";">
		UPDATE table_name
		<set>
			create_time = #{item.createTime}
		</set>
		WHERE id = #{item.id}
	</foreach>
</update>

 

choose 书写

<choose>
    <when test=' time != null and time == "1" '>
         table_${time}
    </when>
    <otherwise>
         table_${date}
    </otherwise>
</choose>

大于小于

&lt;=<=
&gt;=>=

sql 书写

<sql id="BaseColumn">
    id, value, date, time, status
</sql>

<select id="selectByPidsAndQids" parameterType="java.util.Map" resultMap="BaseResultVoMap">
    SELECT <include refid="BaseColumn"/>
    FROM table
</select>

resultType中接受Date数据类型

<select id="queryMaxDate" resultType="java.util.Date">
  	SELECT MAX(date)  as maxDate from dual
</select>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值