码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatis在不适用MyBatis-generator代码自动生成时的map文件的书写规范(总结)

时间:2015-08-30 12:37:52      阅读:826      评论:0      收藏:0      [点我收藏+]

标签:

首先我们参考一段通过MyBatis-generator的自动生成的代码

<?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.eliteams.quick4j.web.dao.UserMapper">
    <resultMap id="BaseResultMap" type="com.eliteams.quick4j.web.model.User">
        <id column="id" property="id" jdbcType="BIGINT" />
        <result column="username" property="username" jdbcType="VARCHAR" />
        <result column="password" property="password" jdbcType="CHAR" />
        <result column="state" property="state" jdbcType="VARCHAR" />
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    </resultMap>
    <sql id="Example_Where_Clause">
        <where>
            <foreach collection="oredCriteria" item="criteria" separator="or">
                <if test="criteria.valid">
                    <trim prefix="(" suffix=")" prefixOverrides="and">
                        <foreach collection="criteria.criteria" item="criterion">
                            <choose>
                                <when test="criterion.noValue">
                                    and ${criterion.condition}
                                </when>
                                <when test="criterion.singleValue">
                                    and ${criterion.condition} #{criterion.value}
                                </when>
                                <when test="criterion.betweenValue">
                                    and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                                </when>
                                <when test="criterion.listValue">
                                    and ${criterion.condition}
                                    <foreach collection="criterion.value" item="listItem" open="(" close=")" separator=",">
                                        #{listItem}
                                    </foreach>
                                </when>
                            </choose>
                        </foreach>
                    </trim>
                </if>
            </foreach>
        </where>
    </sql>
    <sql id="Update_By_Example_Where_Clause">
        <where>
            <foreach collection="example.oredCriteria" item="criteria" separator="or">
                <if test="criteria.valid">
                    <trim prefix="(" suffix=")" prefixOverrides="and">
                        <foreach collection="criteria.criteria" item="criterion">
                            <choose>
                                <when test="criterion.noValue">
                                    and ${criterion.condition}
                                </when>
                                <when test="criterion.singleValue">
                                    and ${criterion.condition} #{criterion.value}
                                </when>
                                <when test="criterion.betweenValue">
                                    and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                                </when>
                                <when test="criterion.listValue">
                                    and ${criterion.condition}
                                    <foreach collection="criterion.value" item="listItem" open="(" close=")" separator=",">
                                        #{listItem}
                                    </foreach>
                                </when>
                            </choose>
                        </foreach>
                    </trim>
                </if>
            </foreach>
        </where>
    </sql>
    <sql id="Base_Column_List">
        id, username, password, state, create_time
    </sql>
    <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.eliteams.quick4j.web.model.UserExample">
        select
        <if test="distinct">
            distinct
        </if>
        <include refid="Base_Column_List" />
        from user
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
        <if test="orderByClause != null">
            order by ${orderByClause}
        </if>
    </select>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List" />
        from user
        where id = #{id,jdbcType=BIGINT}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
        delete from user
        where id = #{id,jdbcType=BIGINT}
    </delete>
    <delete id="deleteByExample" parameterType="com.eliteams.quick4j.web.model.UserExample">
        delete from user
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
    </delete>
    <insert id="insert" parameterType="com.eliteams.quick4j.web.model.User">
        insert into user (id, username, password,
        state, create_time)
        values (#{id,jdbcType=BIGINT}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=CHAR},
        #{state,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP})
    </insert>
    <insert id="insertSelective" parameterType="com.eliteams.quick4j.web.model.User">
        insert into user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="username != null">
                username,
            </if>
            <if test="password != null">
                password,
            </if>
            <if test="state != null">
                state,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=BIGINT},
            </if>
            <if test="username != null">
                #{username,jdbcType=VARCHAR},
            </if>
            <if test="password != null">
                #{password,jdbcType=CHAR},
            </if>
            <if test="state != null">
                #{state,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
        </trim>
    </insert>
    <select id="countByExample" parameterType="com.eliteams.quick4j.web.model.UserExample" resultType="java.lang.Integer">
        select count(*) from user
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
    </select>
    <update id="updateByExampleSelective" parameterType="map">
        update user
        <set>
            <if test="record.id != null">
                id = #{record.id,jdbcType=BIGINT},
            </if>
            <if test="record.username != null">
                username = #{record.username,jdbcType=VARCHAR},
            </if>
            <if test="record.password != null">
                password = #{record.password,jdbcType=CHAR},
            </if>
            <if test="record.state != null">
                state = #{record.state,jdbcType=VARCHAR},
            </if>
            <if test="record.createTime != null">
                create_time = #{record.createTime,jdbcType=TIMESTAMP},
            </if>
        </set>
        <if test="_parameter != null">
            <include refid="Update_By_Example_Where_Clause" />
        </if>
    </update>
    <update id="updateByExample" parameterType="map">
        update user
        set id = #{record.id,jdbcType=BIGINT},
        username = #{record.username,jdbcType=VARCHAR},
        password = #{record.password,jdbcType=CHAR},
        state = #{record.state,jdbcType=VARCHAR},
        create_time = #{record.createTime,jdbcType=TIMESTAMP}
        <if test="_parameter != null">
            <include refid="Update_By_Example_Where_Clause" />
        </if>
    </update>
    <update id="updateByPrimaryKeySelective" parameterType="com.eliteams.quick4j.web.model.User">
        update user
        <set>
            <if test="username != null">
                username = #{username,jdbcType=VARCHAR},
            </if>
            <if test="password != null">
                password = #{password,jdbcType=CHAR},
            </if>
            <if test="state != null">
                state = #{state,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.eliteams.quick4j.web.model.User">
        update user
        set username = #{username,jdbcType=VARCHAR},
        password = #{password,jdbcType=CHAR},
        state = #{state,jdbcType=VARCHAR},
        create_time =
        #{createTime,jdbcType=TIMESTAMP}
        where id = #{id,jdbcType=BIGINT}
    </update>

    <!-- 用户登录验证查询 -->
    <select id="authentication" resultMap="BaseResultMap" parameterType="map">
        select
        <include refid="Base_Column_List" />
        from user
        where username = #{record.username,jdbcType=VARCHAR} and password = #{record.password,jdbcType=CHAR}
    </select>

    <!-- 分页条件查询 -->
    <select id="selectByExampleAndPage" resultMap="BaseResultMap" parameterType="com.eliteams.quick4j.web.model.UserExample">
        select
        <if test="distinct">
            distinct
        </if>
        <include refid="Base_Column_List" />
        from user
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
        <if test="orderByClause != null">
            order by ${orderByClause}
        </if>
    </select>
</mapper>

这一段代码是quick4j架构框架模板中UserMapper.xml的代码(MyBatis-generator代码自动生成无须插件原博文地址:http://blog.csdn.net/zhshulin/article/details/23912615)。

 

先把代码贴到这里

MyBatis在不适用MyBatis-generator代码自动生成时的map文件的书写规范(总结)

标签:

原文地址:http://www.cnblogs.com/JCYH/p/4770499.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!