mybatis一些簡單配置 原

<?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">
<!-- namespace 標識唯一的映射 -->
<!-- user爲model.User的別名,其中別名的定義在sqlMapConfig中定義的,命名空間model.User不能用別名代替 -->
<mapper namespace="model.User">
    <!-- id:sql語句的唯一標識 parameterType:傳入數據的參數類型 resultType:
    返回值得數據類型 #{id}:接收參數的語法,如果是一個參數,參數名任意 -->
    <resultMap type="model.User"  id="baseResultMap">
        <id column="user_id" property="userId" />
        <result column="user_name" property="userName" />
        <result column="birthday" property="birthday" />
        <result column="user_addr" property="userAddr" />
    </resultMap>
    <select id="selectUserById" parameterType="java.lang.Integer"
        resultType="model.User">
        select * from user t where t.user_id = #{id}
    </select>
    <select id="selectUserById1" parameterType="java.lang.Integer"
        resultMap="baseResultMap">
        select * from user t where t.user_id = #{id}
    </select>
    <select id="selectUserCount" parameterType="java.lang.Integer"
        resultType="java.lang.Integer">
        select count(*) from user
    </select>
    <!--resultType:返回系統定義的數據類型時使用 resultMap:返回自定義實體類型使用 -->
    <select id="selectUserByIdWithMap" parameterType="java.lang.Integer"
        resultType="java.util.Map">
        select * from user t where t.user_id = #{id}
    </select>
    <select id="selectUserAll" resultMap="baseResultMap">
        select * from user
    </select>
    <!-- 多個參數查詢,使用map方式#{userName},userName爲Map中的key,parameterType要設置成 java.util.Map -->
    <select id="selectUserById2" parameterType="java.util.Map"
        resultMap="baseResultMap">
        select * from user t where t.user_name=#{userName} and
        t.user_addr=#{userAddr}
    </select>
    <select id="selectUserById3" parameterType="model.Condition"
        resultMap="baseResultMap">
        select * from user t where t.user_name=#{userName} and
        t.user_addr=#{userAddr}
    </select>
    <select id="selectUserByLike" parameterType="java.util.Map"
        resultMap="baseResultMap">
        select *from user t where t.user_name like '%${userName}%'
    </select>
    
    <!-- 容易有輸入輸出問題 -->
    <select id="selectUserByIn" parameterType="java.util.Map" resultMap="baseResultMap">
        select *from user t where t.user_id in ${ids}
    </select>
    
    <!-- (1,2,3)
        collection:集合,用於接收map中的集合,ids必須是map中的key
        open:以某種字符開始
        close:以某種字符結束
        item:循環的每一項
        separator:用什麼來分割
    -->
    <select id="selectUserByInWithForEach"  resultMap="baseResultMap">
            select *from user t where t.user_id in
            <foreach collection="ids" open="(" close=")" item="id" separator=",">
                #{id}
            </foreach>
    </select>
    
    <insert id="insert" parameterType="model.User" >
        <!-- keyProperty:實體類裏的主鍵 order:是主鍵生成的順序,在MySQL中是AFTER resultType:是主鍵在實體類中的類型
            生成主鍵的sql:在MySQL中select LAST_INSERT_ID() -->
        <selectKey keyProperty="userId" order="AFTER" resultType="int">
            select LAST_INSERT_ID()
        </selectKey>
        insert into user(user_id,user_name,birthday,user_addr)
        value (#{userId},#{userName},#{birthday},#{userAddr})
    </insert>

    <update id="update" parameterType="model.User">
        update user t set t.user_name=#{userName},
        t.birthday=#{birthday},
        t.user_addr=#{userAddr}
        where t.user_id=#{userId}
    </update>
    <!-- delete在MySQL中不支持別名 -->
    <delete id="deleteUserById" parameterType="java.lang.Integer">
        delete from user where user_id=#{userId}
    </delete>
    <!-- 注意,引用上面的baseResultMap 是resultMap類型而不是resultType類型 -->
    <select id="selectUserIdByCondition" parameterType="java.util.Map" resultMap="baseResultMap">
    select * from user t
    <where>
        <if test ="userName!=null">
            t.user_name like '%${userName}%'
        </if>
        <if test="birthday !=null">
            <![CDATA[
            and t.birthday<  #{birthday}
            ]]>
                
        </if>
        <if test="userAddr!=null">
            and t.user_addr like '%${userAddr}%'
        </if>
    </where>
    </select>
    <!-- 動態更新 <set>可以自動處理最後一個人逗號 -->
    <update id="dynamicUpdate" parameterType="user">
        update user t
        <set>
            <if test="userName!=null">
                t.user_name=#{userName},
            </if>
            <if test="userAddr!=null">
                t.user_addr=#{userAddr},
            </if>
            <if test="birthday!=null">
                t.birthday=#{birthday}
            </if>
            where t.user_id=#{userId}
        </set>
    </update>
    
    
</mapper>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章