MyBatis中XML 映射配置文件的简单介绍

官网写的比较具体,可以查看以下的网站:

http://www.mybatis.org/mybatis-3/zh/configuration.html

 

另外,实际用到标准的CRUD的操作和查询列表,

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.iteye.tom.dao.AuthItemRecordDao">
    
    <sql id="requestAllFields">
        <![CDATA[
            id,
            record_time AS recordTime,
            source_code AS sourceCode,
            userid,
            auth_item_id AS authItemId,
            isvalid,
            last_status_time AS lastStatusTime
        ]]>
    </sql>
    
    <sql id="whereClause">
        <where>
            <if test="null!=id">AND id = #{id}</if>
            <if test="null!=sourceCode and ''!=sourceCode">AND source_code = #{sourceCode}</if>
            <if test="null!=userid and ''!=userid">AND userid = #{userid}</if>
            <if test="null!=authItemId and ''!=authItemId">AND auth_item_id = #{authItemId}</if>
            <if test="null!=isvalid and ''!=isvalid">AND isvalid = #{isvalid}</if>
            <include refid="extendedWhereClause" />
        </where>
    </sql>
    
    <insert id="add" useGeneratedKeys="true" parameterType="com.iteye.tom.bean.AuthItemRecord" keyProperty="id">
        INSERT INTO tab_auth_item_record
        <trim prefix="(" suffix=")" prefixOverrides=",">
            <if test="null!=recordTime">,record_time</if>
            <if test="null!=sourceCode and ''!=sourceCode">,source_code</if>
            <if test="null!=userid and ''!=userid">,userid</if>
            <if test="null!=authItemId and ''!=authItemId">,auth_item_id</if>
            <if test="null!=isvalid and ''!=isvalid">,isvalid</if>
            <if test="null!=lastStatusTime">,last_status_time</if>
        </trim>
        VALUES
        <trim prefix="(" suffix=")" prefixOverrides=",">
            <if test="null!=recordTime">,#{recordTime}</if>
            <if test="null!=sourceCode and ''!=sourceCode">,#{sourceCode}</if>
            <if test="null!=userid and ''!=userid">,#{userid}</if>
            <if test="null!=authItemId and ''!=authItemId">,#{authItemId}</if>
            <if test="null!=isvalid and ''!=isvalid">,#{isvalid}</if>
            <if test="null!=lastStatusTime">,#{lastStatusTime}</if>
        </trim>
    </insert>
    
    <update id="update" parameterType="com.iteye.tom.bean.AuthItemRecord">
        UPDATE tab_auth_item_record
        <trim prefix="SET" prefixOverrides=",">
            <if test="null!=recordTime">,record_time = #{recordTime}</if>
            <if test="null!=sourceCode and ''!=sourceCode">,source_code = #{sourceCode}</if>
            <if test="null!=userid and ''!=userid">,userid = #{userid}</if>
            <if test="null!=authItemId and ''!=authItemId">,auth_item_id = #{authItemId}</if>
            <if test="null!=isvalid and ''!=isvalid">,isvalid = #{isvalid}</if>
            <if test="null!=lastStatusTime">,last_status_time = #{lastStatusTime}</if>
            <include refid="extendedUpdateSql" />
        </trim>
        WHERE id = #{id}
    </update>
    
    <select id="query" parameterType="com.iteye.tom.bean.AuthItemRecord" resultType="com.iteye.tom.bean.AuthItemRecord">
        SELECT <include refid="requestAllFields"/> FROM tab_auth_item_record
        <include refid="whereClause" />
        <include refid="extendedOrderByClause" />
        <if test="null!=requestOffset">
            LIMIT #{requestOffset}, #{requestCount}
        </if>
    </select>
    
    <select id="get" parameterType="com.iteye.tom.bean.AuthItemRecord" resultType="com.iteye.tom.bean.AuthItemRecord">
        SELECT <include refid="requestAllFields"/> FROM tab_auth_item_record
        <include refid="whereClause" />
        LIMIT 1
    </select>
    
    <select id="getById" parameterType="long" resultType="com.iteye.tom.bean.AuthItemRecord">
        SELECT <include refid="requestAllFields"/> FROM tab_auth_item_record WHERE id = #{id}
    </select>
    
    <select id="count" parameterType="com.iteye.tom.bean.AuthItemRecord" resultType="int">
        SELECT COUNT(1) FROM tab_auth_item_record <include refid="whereClause" />
    </select>

    <delete id="deleteByUserid" parameterType="com.iteye.tom.bean.AuthItemRecord" >
        DELETE FROM tab_auth_item_record WHERE userid = #{userid}    </delete>
    
    <!-- 扩展的更新等语句(自定义)-->
    <sql id="extendedUpdateSql">
        <if test="null!=extendedParameter">
            
        </if>
    </sql>
    
    <!-- 扩展的条件过滤语句(自定义)-->
    <sql id="extendedWhereClause">
        <if test="null!=extendedParameter">
            
        </if>
    </sql>
    
    <!-- 扩展的排序等语句(自定义)-->
    <sql id="extendedOrderByClause">
        <if test="null!=extendedParameter">
            
        </if>
    </sql>
</mapper>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值