<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace 命名空間矿咕,作用就是對SQL進行分類化管理乒裆,理解SQL隔離
注意:使用Mapper代理開發(fā)升薯,namespace有特殊的作用,等于mapper接口的地址
-->
<mapper namespace="com.mybatis.mapper.UserMapper">
<!-- SQL標簽:
定義SQL片段
id:sql片段的唯一標識
經(jīng)驗:一般定義SQL片段基于單表來定義
這樣的話SQL片段的可重用性更高
在SQL片段中不要包括where
-->
<sql id="query_user_where">
<if test="userCustom!=null">
<if test="userCustom.sex!=null and userCustom.sex!=''">
and user.sex like '${userCustom.sex}'
</if>
<if test="userCustom.username!=null and userCustom.username!=''">
and user.username like '%${userCustom.username}%'
</if>
<!-- 如果ids不等于null刨晴,下面就要使用foreach -->
<if test="ids!=null">
<!-- 使用foreach來遍歷傳入的ids的多個id
collection:指定輸入對象中的集合屬性
item:每次遍歷生成的對象名
open:開始遍歷時拼接串
close:結束遍歷時拼接的串
separator:每遍歷的兩個對象中間所需要拼接的串
-->
<!-- 實現(xiàn)下面SQL的拼接
and (id=1 or id=2 or id=3)
-->
<foreach collection="ids" item="user_id" open="and (" close=")" separator="or">
<!-- 每次遍歷所需要拼接的串 -->
id=#{user_id}
</foreach>
<!-- 實現(xiàn)下面SQL的拼接
and id in(1,2,3)
-->
<!-- <foreach collection="ids" item="user_id" open="and id in(" close=")" separator=",">
每次遍歷所需要拼接的串
#{user_id}
</foreach> -->
</if>
</if>
</sql>
<!-- 定義resultMap
將select id id_,username username_ from user和user類中的屬性做一個映射關系
type是最終映射的java對象類型
id是resultMap的唯一標識
-->
<resultMap type="user" id="userResultMap">
<!-- id表示查詢結果集中唯一標識
column:查詢出來的列名
property:pojo中的屬性名,type所指定的pojo的屬性名
最終resultMap對column和property做一個映射關系(對應關系)
-->
<id column="id_" property="id"/>
<!-- result對普通列的映射關系
column:查詢出來的列名
property:type所指定的pojo的屬性名
最終resultMap對column和property做一個映射關系(對應關系)
-->
<result column="username_" property="username"/>
</resultMap>
<!-- 用戶信息的綜合查詢
${userCustom.sex}:取出包裝對象的性別的值
${userCustom.username}:取出包裝對象的用戶名稱
-->
<select id="findUserList" parameterType="userQueryVo" resultType="UserCustom">
select * from user
<where>
<!-- 引用SQL片段的id,如果refid指向的id不在本mapper文件中杏糙,需要前邊加上namespace -->
<include refid="query_user_where"></include>
<!-- 在這里還要引用其他的SQL片段 -->
</where>
</select>
<!-- 用戶信息的綜合查詢總數(shù)
輸入類型和findUserList一致
輸出類型是一個簡單類型
-->
<select id="findUserCount" parameterType="userQueryVo" resultType="int">
select count(*) from user
<!-- where標簽可以自動完成拼接并且去掉拼接條件中的第一個and -->
<where>
<if test="userCustom!=null">
<if test="userCustom.sex!=null and userCustom.sex!=''">
and user.sex like '${userCustom.sex}'
</if>
<if test="userCustom.username!=null and userCustom.username!=''">
and user.username like '%${userCustom.username}%'
</if>
</if>
</where>
</select>
<!-- 使用resultMap進行輸出關系映射
resultMap:指定定義的resultMap的id玫芦,如果這個resultMap在其他的mapper文件浆熔,前面需要加namespace
-->
<select id="findUserByIdResultMap" parameterType="int" resultMap="userResultMap">
select id id_,username username_ from user where id=#{value}
</select>
<!-- 在映射文件中配置多個SQL語句 -->
<!-- 通過select執(zhí)行SQL的查詢
id:用于標識映射文件中的SQL
將來SQL語句會封裝到mappedstatement對象中,所以稱為statement的id
#{}表示一個占位符
parameterType:輸入?yún)?shù)的類型
#{id}其中的id表示接收輸入的參數(shù)桥帆,參數(shù)名稱就是id医增,如果輸入?yún)?shù)是簡單類型,那么#{}中的參數(shù)名可以任意老虫,可以是value或者其他的名稱
resultType:指定SQL輸出結果所映射的java對象類型叶骨,select指定resultType表示將單條記錄映射成的java對象
-->
<select id="findUserById" parameterType="int" resultType="user">
select * from user where id=#{id}<!-- id接收輸入的參數(shù) -->
</select>
<!-- 根據(jù)用戶名稱模糊查詢用戶信息
resultType表示將單條記錄映射成的java對象
${}:拼接SQL串,將接收到的參數(shù)的內容不加任何修飾拼接在SQL中,只能使用${value} 拼接符號祈匙,沒有使用占位符
使用${}拼接可能引起SQL注入select * from user where username like '%' or 1=1 or'%'
-->
<select id="findUserByName" parameterType="java.lang.String" resultType="com.mybatis.po.User">
<!-- 把%固定到SQL語句中 -->
select * from user where username like '%${value}%'
</select>
<!-- 添加用戶
parameterType:輸入?yún)?shù)類型忽刽,是pojo(包括用戶信息)
#{}中指定pojo的屬性名,接收到pojo的屬性值夺欲,mybatis通過OGNL獲取對象的屬性值
#{id}:id是自增的
通過LAST_INSERT_ID()獲取自增的主鍵,只適用于自增主鍵
keyProperty:將查詢到的主鍵值設置到parameterType指定對象的id屬性
order:執(zhí)行順序跪帝,想對于insert語句來說的執(zhí)行順序,只有在insert語句之后才會執(zhí)行獲取自增主鍵
resultType:指定結果的類型
將插入數(shù)據(jù)的主鍵返回
-->
<insert id="insertUser" parameterType="com.mybatis.po.User" >
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
select LAST_INSERT_ID()
</selectKey>
insert into user(username,birthday,sex,address) value(#{username},#{birthday},#{sex},#{address})
</insert>
<!-- 使用MySQL的UUID()生成主鍵
UUID() 非自增主鍵的返回
需要將表中id的字段類型改成String洁闰,長度設置成35位
首先通過UUID()得到主鍵歉甚,將主鍵設置到user對象的id屬性中
其次在insert執(zhí)行時,在user對象中取出id屬性值
-->
<!-- <insert id="insertUser" parameterType="com.mybatis.po.User" >
<selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">
select UUID()
</selectKey>
insert into user(id,username,birthday,sex,address) value(#{id},#{username},#{birthday},#{sex},#{address})
</insert> -->
<!-- 刪除用戶
根據(jù)用戶id刪除用戶扑眉,需要輸入id值
-->
<delete id="deleteUser" parameterType="java.lang.Integer">
delete from user where id=#{id}
</delete>
<!-- 更新用戶
傳入用戶的更新信息纸泄,傳入user對象,id必須存在
#{id}:從輸入的user中獲取id的屬性值
-->
<update id="updateUser" parameterType="com.mybatis.po.User">
update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}
</update>
</mapper>
Mybatis關于xml文件的基本配置
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門姻政,熙熙樓的掌柜王于貴愁眉苦臉地迎上來呆抑,“玉大人,你說我怎么就攤上這事汁展∪蛋” “怎么了厌殉?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長侈咕。 經(jīng)常有香客問我公罕,道長,這世上最難降的妖魔是什么耀销? 我笑而不...
- 正文 為了忘掉前任楼眷,我火速辦了婚禮,結果婚禮上树姨,老公的妹妹穿的比我還像新娘摩桶。我一直安慰自己,他們只是感情好帽揪,可當我...
- 文/花漫 我一把揭開白布硝清。 她就那樣靜靜地躺著,像睡著了一般转晰。 火紅的嫁衣襯著肌膚如雪芦拿。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼未桥,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了芥备?” 一聲冷哼從身側響起冬耿,我...
- 正文 年R本政府宣布瘫里,位于F島的核電站实蔽,受9級特大地震影響,放射性物質發(fā)生泄漏谨读。R本人自食惡果不足惜局装,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望劳殖。 院中可真熱鬧铐尚,春花似錦、人聲如沸哆姻。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽矛缨。三九已至爹脾,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間箕昭,已是汗流浹背灵妨。 一陣腳步聲響...
推薦閱讀更多精彩內容
- 在使用mybatis的時候我們經(jīng)常要用到聯(lián)合查詢桨武,現(xiàn)在就對一表對多表和一對一表的xml文件配置進行說明Studen...
- MyBatis 的配置文件包含了影響 MyBatis 行為甚深的設置(settings)和屬性(propertie...
- 罷了呀酸,誰還沒有這樣的時候凉蜂,愛一個人愛得像條狗。 大半夜我被電話吵醒性誉,掙扎著把眼睛扯出一條縫窿吩,看到來電顯示又是嬌嬌。...