<?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命名空間,為了對sql語句進行隔離,方便管理 ,mapper開發dao方式,使用namespace有特殊作用
mapper代理開發時將namespace指定為mapper接口的全限定名-->
<mapper namespace="com.sihai.mybatis.mapper.UserMapper">
<!-- 在mapper.xml文件中配置很多的sql語句,執行每個sql語句時,封裝為MappedStatement對象
mapper.xml以statement為單位管理sql語句--><!-- 將用戶查詢條件定義為sql片段建議對單表的查詢條件單獨抽取sql片段,提高公用性注意:不要將where標簽放在sql片段--><sql id="query_user_where"><!-- 如果 userQueryVo中傳入查詢條件,再進行sql拼接--><!-- test中userCustom.username表示從userQueryVo讀取屬性值--><if test="userCustom!=null"><if test="userCustom.username!=null and userCustom.username!=''">and username like '%${userCustom.username}%'</if><if test="userCustom.sex!=null and userCustom.sex!=''">and sex = #{userCustom.sex}</if><!-- 根據id集合查詢用戶信息 --><!-- 最終拼接的效果:SELECT id ,username ,birthday FROM USER WHERE username LIKE '%小明%' AND id IN (16,22,25)collection:集合的屬性open:開始循環拼接的串close:結束循環拼接的串item:每次循環取到的對象separator:每兩次循環中間拼接的串--><foreach collection="ids" open=" AND id IN ( " close=")" item="id" separator=",">#{id}</foreach><!-- SELECT id ,username ,birthday FROM USER WHERE username LIKE '%小明%' AND (id = 16 OR id = 22 OR id = 25) <foreach collection="ids" open=" AND ( " close=")" item="id" separator="OR">id = #{id}</foreach>--><!-- 還有很的查詢條件 --></if></sql><!-- 定義resultMap,列名和屬性名映射配置id:mapper.xml中的唯一標識 type:最終要映射的pojo類型--><resultMap id="userListResultMap" type="user" ><!-- 列名id_,username_,birthday_id:要映射結果集的唯 一標識 ,稱為主鍵column:結果集的列名property:type指定的哪個屬性中--><id column="id_" property="id"/><!-- result就是普通列的映射配置 --><result column="username_" property="username"/><result column="birthday_" property="birthday"/></resultMap><!-- 根據id查詢用戶信息 --><!-- id:唯一標識 一個statement#{}:表示 一個占位符,如果#{}中傳入簡單類型的參數,#{}中的名稱隨意parameterType:輸入 參數的類型,通過#{}接收parameterType輸入 的參數resultType:輸出結果 類型,不管返回是多條還是單條,指定單條記錄映射的pojo類型--><select id="findUserById" parameterType="int" resultType="user">SELECT * FROM USER WHERE id= #{id}</select><!-- 根據用戶名稱查詢用戶信息,可能返回多條${}:表示sql的拼接,通過${}接收參數,將參數的內容不加任何修飾拼接在sql中。--><select id="findUserByName" parameterType="java.lang.String" resultType="com.sihai.mybatis.po.User">select * from user where username like '%${value}%'</select><!-- 自定義查詢條件查詢用戶的信息parameterType:指定包裝類型%${userCustom.username}%:userCustom是userQueryVo中的屬性,通過OGNL獲取屬性的值--><select id="findUserList" parameterType="userQueryVo" resultType="user">select id,username,birthday from user<!-- where標簽相當 于where關鍵字,可以自動去除第一個and --><where><!-- 引用sql片段,如果sql片段和引用處不在同一個mapper必須前邊加namespace --><include refid="query_user_where"></include><!-- 下邊還有很其它的條件 --><!-- <include refid="其它的sql片段"></include> --></where></select><!-- 使用resultMap作結果映射resultMap:如果引用resultMap的位置和resultMap的定義在同一個mapper.xml,直接使用resultMap的id,如果不在同一個mapper.xml要在resultMap的id前邊加namespace--><select id="findUserListResultMap" parameterType="userQueryVo" resultMap="userListResultMap">select id id_,username username_,birthday birthday_ from user where username like '%${userCustom.username}%'</select><!-- 輸出簡單類型功能:自定義查詢條件,返回查詢記錄個數,通常用于實現 查詢分頁--><select id="findUserCount" parameterType="userQueryVo" resultType="int">select count(*) from user <!-- where標簽相當 于where關鍵字,可以自動去除第一個and --><where><!-- 引用sql片段,如果sql片段和引用處不在同一個mapper必須前邊加namespace --><include refid="query_user_where"></include><!-- 下邊還有很其它的條件 --><!-- <include refid="其它的sql片段"></include> --></where></select><!-- 添加用戶parameterType:輸入 參數的類型,User對象 包括 username,birthday,sex,address#{}接收pojo數據,可以使用OGNL解析出pojo的屬性值#{username}表示從parameterType中獲取pojo的屬性值selectKey:用于進行主鍵返回,定義了獲取主鍵值的sqlorder:設置selectKey中sql執行的順序,相對于insert語句來說keyProperty:將主鍵值設置到哪個屬性resultType:select LAST_INSERT_ID()的結果 類型--><insert id="insertUser" parameterType="com.sihai.mybatis.po.User"><selectKey keyProperty="id" order="AFTER" resultType="int">select LAST_INSERT_ID()</selectKey>INSERT INTO USER(username,birthday,sex,address) VALUES(#{username},#{birthday},#{sex},#{address})</insert><!-- mysql的uuid生成主鍵 --><!-- <insert id="insertUser" parameterType="com.sihai.mybatis.po.User"><selectKey keyProperty="id" order="BEFORE" resultType="string">select uuid()</selectKey>INSERT INTO USER(id,username,birthday,sex,address) VALUES(#{id},#{username},#{birthday},#{sex},#{address})</insert> --><!-- oracle在執行insert之前執行select 序列.nextval() from dual取出序列最大值,將值設置到user對象 的id屬性--><!-- <insert id="insertUser" parameterType="com.sihai.mybatis.po.User"><selectKey keyProperty="id" order="BEFORE" resultType="int">select 序列.nextval() from dual</selectKey>INSERT INTO USER(id,username,birthday,sex,address) VALUES(#{id},#{username},#{birthday},#{sex},#{address})</insert> --><!-- 用戶刪除 --><delete id="deleteUser" parameterType="int">delete from user where id=#{id}</delete><!-- 用戶更新 要求:傳入的user對象中包括 id屬性值--><update id="updateUser" parameterType="com.sihai.mybatis.po.User">update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}</update></mapper>com.sihai.mybatis.mapper.UserMapper">
<!-- 在mapper.xml文件中配置很多的sql語句,執行每個sql語句時,封裝為MappedStatement對象
mapper.xml以statement為單位管理sql語句--><!-- 將用戶查詢條件定義為sql片段建議對單表的查詢條件單獨抽取sql片段,提高公用性注意:不要將where標簽放在sql片段--><sql id="query_user_where"><!-- 如果 userQueryVo中傳入查詢條件,再進行sql拼接--><!-- test中userCustom.username表示從userQueryVo讀取屬性值--><if test="userCustom!=null"><if test="userCustom.username!=null and userCustom.username!=''">and username like '%${userCustom.username}%'</if><if test="userCustom.sex!=null and userCustom.sex!=''">and sex = #{userCustom.sex}</if><!-- 根據id集合查詢用戶信息 --><!-- 最終拼接的效果:SELECT id ,username ,birthday FROM USER WHERE username LIKE '%小明%' AND id IN (16,22,25)collection:集合的屬性open:開始循環拼接的串close:結束循環拼接的串item:每次循環取到的對象separator:每兩次循環中間拼接的串--><foreach collection="ids" open=" AND id IN ( " close=")" item="id" separator=",">#{id}</foreach><!-- SELECT id ,username ,birthday FROM USER WHERE username LIKE '%小明%' AND (id = 16 OR id = 22 OR id = 25) <foreach collection="ids" open=" AND ( " close=")" item="id" separator="OR">id = #{id}</foreach>--><!-- 還有很的查詢條件 --></if></sql><!-- 定義resultMap,列名和屬性名映射配置id:mapper.xml中的唯一標識 type:最終要映射的pojo類型--><resultMap id="userListResultMap" type="user" ><!-- 列名id_,username_,birthday_id:要映射結果集的唯 一標識 ,稱為主鍵column:結果集的列名property:type指定的哪個屬性中--><id column="id_" property="id"/><!-- result就是普通列的映射配置 --><result column="username_" property="username"/><result column="birthday_" property="birthday"/></resultMap><!-- 根據id查詢用戶信息 --><!-- id:唯一標識 一個statement#{}:表示 一個占位符,如果#{}中傳入簡單類型的參數,#{}中的名稱隨意parameterType:輸入 參數的類型,通過#{}接收parameterType輸入 的參數resultType:輸出結果 類型,不管返回是多條還是單條,指定單條記錄映射的pojo類型--><select id="findUserById" parameterType="int" resultType="user">SELECT * FROM USER WHERE id= #{id}</select><!-- 根據用戶名稱查詢用戶信息,可能返回多條${}:表示sql的拼接,通過${}接收參數,將參數的內容不加任何修飾拼接在sql中。--><select id="findUserByName" parameterType="java.lang.String" resultType="com.sihai.mybatis.po.User">select * from user where username like '%${value}%'</select><!-- 自定義查詢條件查詢用戶的信息parameterType:指定包裝類型%${userCustom.username}%:userCustom是userQueryVo中的屬性,通過OGNL獲取屬性的值--><select id="findUserList" parameterType="userQueryVo" resultType="user">select id,username,birthday from user<!-- where標簽相當 于where關鍵字,可以自動去除第一個and --><where><!-- 引用sql片段,如果sql片段和引用處不在同一個mapper必須前邊加namespace --><include refid="query_user_where"></include><!-- 下邊還有很其它的條件 --><!-- <include refid="其它的sql片段"></include> --></where></select><!-- 使用resultMap作結果映射resultMap:如果引用resultMap的位置和resultMap的定義在同一個mapper.xml,直接使用resultMap的id,如果不在同一個mapper.xml要在resultMap的id前邊加namespace--><select id="findUserListResultMap" parameterType="userQueryVo" resultMap="userListResultMap">select id id_,username username_,birthday birthday_ from user where username like '%${userCustom.username}%'</select><!-- 輸出簡單類型功能:自定義查詢條件,返回查詢記錄個數,通常用于實現 查詢分頁--><select id="findUserCount" parameterType="userQueryVo" resultType="int">select count(*) from user <!-- where標簽相當 于where關鍵字,可以自動去除第一個and --><where><!-- 引用sql片段,如果sql片段和引用處不在同一個mapper必須前邊加namespace --><include refid="query_user_where"></include><!-- 下邊還有很其它的條件 --><!-- <include refid="其它的sql片段"></include> --></where></select><!-- 添加用戶parameterType:輸入 參數的類型,User對象 包括 username,birthday,sex,address#{}接收pojo數據,可以使用OGNL解析出pojo的屬性值#{username}表示從parameterType中獲取pojo的屬性值selectKey:用于進行主鍵返回,定義了獲取主鍵值的sqlorder:設置selectKey中sql執行的順序,相對于insert語句來說keyProperty:將主鍵值設置到哪個屬性resultType:select LAST_INSERT_ID()的結果 類型--><insert id="insertUser" parameterType="com.sihai.mybatis.po.User"><selectKey keyProperty="id" order="AFTER" resultType="int">select LAST_INSERT_ID()</selectKey>INSERT INTO USER(username,birthday,sex,address) VALUES(#{username},#{birthday},#{sex},#{address})</insert><!-- mysql的uuid生成主鍵 --><!-- <insert id="insertUser" parameterType="com.sihai.mybatis.po.User"><selectKey keyProperty="id" order="BEFORE" resultType="string">select uuid()</selectKey>INSERT INTO USER(id,username,birthday,sex,address) VALUES(#{id},#{username},#{birthday},#{sex},#{address})</insert> --><!-- oracle在執行insert之前執行select 序列.nextval() from dual取出序列最大值,將值設置到user對象 的id屬性--><!-- <insert id="insertUser" parameterType="com.sihai.mybatis.po.User"><selectKey keyProperty="id" order="BEFORE" resultType="int">select 序列.nextval() from dual</selectKey>INSERT INTO USER(id,username,birthday,sex,address) VALUES(#{id},#{username},#{birthday},#{sex},#{address})</insert> --><!-- 用戶刪除 --><delete id="deleteUser" parameterType="int">delete from user where id=#{id}</delete><!-- 用戶更新 要求:傳入的user對象中包括 id屬性值--><update id="updateUser" parameterType="com.sihai.mybatis.po.User">update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}</update></mapper>