Mybatis中Mapper文件模板

MyBatis:java

<?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.hmbb.nwvisitor.dao.StudentDAO">
    <resultMap id="BaseResultMap" type="com.hmbb.nwvisitor.dataobject.StudentDO">
        <result column="id" property="id" jdbcType="BIGINT"/>
        <result column="gmt_create" property="gmtCreate" jdbcType="TIMESTAMP"/>
        <result column="gmt_modified" property="gmtModified" jdbcType="TIMESTAMP"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        	...
    </resultMap>
    <sql id="Base_Column_List">
         id,
		gmt_create,
		gmt_modified,
		...
    </sql>

    <insert id="insert">
        insert into visitor_apply(
        <include refid="Base_Column_List"/>
        ) VALUES
        (#{id},now(),now(),
         ...
        )
        <selectKey keyProperty="id" resultType="Long" order="AFTER">
            SELECT
            LAST_INSERT_ID()
        </selectKey>
    </insert>

    <select id="getCount" resultType="int">
        select count(*) from
        student
        <where>
            <if test="屬性 != null">
                AND 字段 = #{屬性}
            </if>
            <if test="屬性 != null">
                AND 字段  LIKE CONCAT('%', #{屬性},'%')
            </if>
            <if test="屬性">
                <![CDATA[
                AND 字段 <> 0
                ]]>
            </if>
            <if test="searchKey != null">
                AND (字段 like CONCAT('%',#{屬性},'%' ) or
                                        字段 like CONCAT('%',#{屬性},'%' )
                        ...
                                       字段 like CONCAT('%',#{屬性},'%' ))
            </if>
        </where>
    </select>

    <select id="getList" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from student
        <where>
             <if test="屬性 != null">
                AND 字段 = #{屬性}
            </if>
            <if test="屬性 != null">
                AND 字段  LIKE CONCAT('%', #{屬性},'%')
            </if>
            <if test="屬性 != null">
                <![CDATA[
                AND 字段 <> 0
                ]]>
            </if>
            <if test="searchKey != null">
                AND (字段 like CONCAT('%',#{屬性},'%' ) or
                                        字段 like CONCAT('%',#{屬性},'%' )
                        ...
                                       字段 like CONCAT('%',#{屬性},'%' ))
            </if>
        </where>
        order by id DESC
        <if test="startRow != null">
            limit #{startRow},#{limit}
        </if>
    </select>
    <select id="getById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM 字段 where id = #{id}
    </select>

    <update id="update">
        update student set
        gmt_modified = now(),
       	 字段 = #{屬性},
        ...
    </update>

    
</mapper>

Hello:sql

    

public class Hello {

}
相關文章
相關標籤/搜索