mybatis mapper.xml
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper
3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5
6
7 <!-- 1. namespace必需是介面的全路徑名 -->
8 <!-- 2. 介面的方法名必需與對映檔案的sql id一致 -->
9 <!-- 3. 介面的輸入引數必需與對映檔案的parameterType型別一致 -->
10 <!-- 4. 介面的返回型別必須與對映檔案的resultType型別一致 -->
11
12
13 <!-- 1. 建立UserMapper.xml對映檔案(把原來的user.xml複製按開發規則要求修改一下) -->
14 <!-- 2. 建立UserMapper介面(把原來的UserDao.java複製按開發規則要求修改一下) -->
15 <!-- 3. 載入UserMapper.xml -->
16
17
18 <mapper namespace="mybatis.studentMapper">
19 <select id="getUserById" parameterType="int"
20 resultType="cn.edu.ahtcm.student">
21 SELECT * FROM STUDENT WHERE SID = #{id1}
22
23 </select>
24
25 <!-- 程式沒有報錯,但是後臺資料庫的記錄也沒有加進去 -->
26 <!-- 沒有加進去的原因是沒有提交事務 -->
27 <!-- 返回值被放在傳入引數的屬性裡,而不是返回值,返回值返回的是影響的行數 -->
28 <insert id="insertStudent" parameterType="cn.edu.ahtcm.student"
29 useGeneratedKeys="false" keyProperty="id">
30
31 <selectKey keyProperty="sid" resultType="String"
32 order="AFTER">
33 <!-- SELECT LAST_INSERT_ID() -->
34 <!-- SELECT UUID() -->
35 </selectKey>
36 INSERT INTO `S`(SNAME,GENDER,CLASS_ID)
37 VALUES(#{sname},#{gender},#{class_id})
38 </insert>
39
40 <!-- 修改 -->
41 <update id="updateBySID" parameterType="cn.edu.ahtcm.student">
42
43 UPDATE STUDENT SET
44 SNAME=#{sname} WHERE SID=#{sid}
45 </update>
46
47 <!-- 刪除 -->
48
49 <delete id="deleteBySID" parameterType="cn.edu.ahtcm.student">
50 DELETE FROM STUDENT WHERE
51 SID =#{sid}
52 </delete>
53 <!-- 查詢結果為列表,返回型別定位單個元素型別即可 -->
54 <select id="getAllStudent" parameterType="cn.edu.ahtcm.student"
55 resultType="cn.edu.ahtcm.student">
56 SELECT * FROM STUDENT
57 <include refid="sql"></include>
58
59 </select>
60
61
62 <!-- 定義sql片段,可以通過include的id使用SQL片段 -->
63 <sql id="sql">
64 WHERE SID BETWEEN 0 AND 10
65 </sql>
66
67
68 <!-- foreach可以讀取陣列或列表自動生成 in ()的sql語法 -->
69 <select id="getUserBySIds" parameterType="queryvo"
70 resultType="cn.edu.ahtcm.student">
71
72 SELECT
73 *
74 FROM USER
75 <!-- where會自動加上where同處理多餘的and -->
76 <where>
77 <!-- id IN(1,10,25,30,34) -->
78 <!-- foreach迴圈標籤 collection:要遍歷的集合,來源入參 open:迴圈開始前的sql separator:分隔符 close:迴圈結束拼接的sql -->
79 <foreach item="uid" collection="ids" open="id IN("
80 separator="," close=")">
81 #{uid}
82 </foreach>
83 </where>
84 </select>
85
86
87 <!-- 一對一的關係對映 -->
88 <!-- 1.resultType的返回對映,對映至設計好的類(在繼承原來的類,並增加需要的屬性),在SQL語句是是用聯接 -->
89 <!-- 2.使用resultMap在xml內設計對映規則(繼承原來的類,新增關係對映的引用) -->
90
91 <!-- resultMap -->
92 <!-- type:對映成的pojo型別 -->
93 <!-- id:resultMap唯一標識 -->
94
95 <resultMap type="cn.edu.ahtcm.student" id="studentMap">
96 <!-- id標籤用於繫結主鍵 -->
97 <!-- <id property="id" column="id"/> -->
98
99 <!-- 使用result繫結普通欄位 -->
100 <result property="userId" column="user_id" />
101 <result property="number" column="number" />
102 <result property="createtime" column="createtime" />
103 <result property="note" column="note" />
104
105 <!-- association:配置一對一關聯 -->
106 <!-- property:繫結的使用者屬性 -->
107 <!-- javaType:屬性資料型別,支援別名 -->
108 <!-- 使用時需要在resultMap 的type指定的類中包含下列association中JavaType指定的類的引用 -->
109 <association property="user"
110 javaType="cn.edu.ahtcm.student">
111 <id property="id" column="user_id" />
112
113 <result property="username" column="username" />
114 <result property="address" column="address" />
115 <result property="sex" column="sex" />
116 </association>
117
118 <!-- collection:配置一對多關係 -->
119 <!-- property:列表的屬性名 -->
120 <!-- ofType:property的資料型別,支援別名 -->
121 <!-- 使用時需要在resultMap 的type指定的類中包含下列collection中ofType指定的類的列表的引用 -->
122 <collection property="orders" ofType="order">
123 <!-- id標籤用於繫結主鍵 -->
124 <id property="id" column="oid" />
125 <!-- 使用result繫結普通欄位 -->
126 <result property="userId" column="id" />
127 <result property="number" column="number" />
128 <result property="createtime" column="createtime" />
129 </collection>
130
131
132 </resultMap>
133
134 <!-- 使用resultMap -->
135 <select id="getOrderListResultMap" resultMap="orderMap">
136 SELECT * FROM
137 `student`
138 </select>
139
140
141 </mapper>
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 1. namespace必需是介面的全路徑名 --><!-- 2. 介面的方法名必需與對映檔案的sql id一致 --><!-- 3. 介面的輸入引數必需與對映檔案的parameterType型別一致 --><!-- 4. 介面的返回型別必須與對映檔案的resultType型別一致 -->
<!-- 1. 建立UserMapper.xml對映檔案(把原來的user.xml複製按開發規則要求修改一下) --><!-- 2. 建立UserMapper介面(把原來的UserDao.java複製按開發規則要求修改一下) --><!-- 3. 載入UserMapper.xml -->
<mapper namespace="mybatis.studentMapper"><select id="getUserById" parameterType="int"resultType="cn.edu.ahtcm.student">SELECT * FROM STUDENT WHERE SID = #{id1}
</select>
<!-- 程式沒有報錯,但是後臺資料庫的記錄也沒有加進去 --><!-- 沒有加進去的原因是沒有提交事務 --><!-- 返回值被放在傳入引數的屬性裡,而不是返回值,返回值返回的是影響的行數 --><insert id="insertStudent" parameterType="cn.edu.ahtcm.student"useGeneratedKeys="false" keyProperty="id">
<selectKey keyProperty="sid" resultType="String"order="AFTER"><!-- SELECT LAST_INSERT_ID() --><!-- SELECT UUID() --></selectKey>INSERT INTO `S`(SNAME,GENDER,CLASS_ID)VALUES(#{sname},#{gender},#{class_id})</insert>
<!-- 修改 --><update id="updateBySID" parameterType="cn.edu.ahtcm.student">
UPDATE STUDENT SETSNAME=#{sname} WHERE SID=#{sid}</update>
<!-- 刪除 -->
<delete id="deleteBySID" parameterType="cn.edu.ahtcm.student">DELETE FROM STUDENT WHERESID =#{sid}</delete><!-- 查詢結果為列表,返回型別定位單個元素型別即可 --><select id="getAllStudent" parameterType="cn.edu.ahtcm.student"resultType="cn.edu.ahtcm.student">SELECT * FROM STUDENT<include refid="sql"></include>
</select>
<!-- 定義sql片段,可以通過include的id使用SQL片段 --><sql id="sql">WHERE SID BETWEEN 0 AND 10</sql>
<!-- foreach可以讀取陣列或列表自動生成 in ()的sql語法 --><select id="getUserBySIds" parameterType="queryvo"resultType="cn.edu.ahtcm.student">
SELECT*FROM USER<!-- where會自動加上where同處理多餘的and --><where><!-- id IN(1,10,25,30,34) --><!-- foreach迴圈標籤 collection:要遍歷的集合,來源入參 open:迴圈開始前的sql separator:分隔符 close:迴圈結束拼接的sql --><foreach item="uid" collection="ids" open="id IN("separator="," close=")">#{uid}</foreach></where></select>
<!-- 一對一的關係對映 --><!-- 1.resultType的返回對映,對映至設計好的類(在繼承原來的類,並增加需要的屬性),在SQL語句是是用聯接 --><!-- 2.使用resultMap在xml內設計對映規則(繼承原來的類,新增關係對映的引用) -->
<!-- resultMap --><!-- type:對映成的pojo型別 --><!-- id:resultMap唯一標識 -->
<resultMap type="cn.edu.ahtcm.student" id="studentMap"><!-- id標籤用於繫結主鍵 --><!-- <id property="id" column="id"/> -->
<!-- 使用result繫結普通欄位 --><result property="userId" column="user_id" /><result property="number" column="number" /><result property="createtime" column="createtime" /><result property="note" column="note" />
<!-- association:配置一對一關聯 --><!-- property:繫結的使用者屬性 --><!-- javaType:屬性資料型別,支援別名 --><!-- 使用時需要在resultMap 的type指定的類中包含下列association中JavaType指定的類的引用 --><association property="user"javaType="cn.edu.ahtcm.student"><id property="id" column="user_id" />
<result property="username" column="username" /><result property="address" column="address" /><result property="sex" column="sex" /></association>
<!-- collection:配置一對多關係 --><!-- property:列表的屬性名 --><!-- ofType:property的資料型別,支援別名 --><!-- 使用時需要在resultMap 的type指定的類中包含下列collection中ofType指定的類的列表的引用 --><collection property="orders" ofType="order"><!-- id標籤用於繫結主鍵 --><id property="id" column="oid" /><!-- 使用result繫結普通欄位 --><result property="userId" column="id" /><result property="number" column="number" /><result property="createtime" column="createtime" /></collection>
</resultMap>
<!-- 使用resultMap --><select id="getOrderListResultMap" resultMap="orderMap">SELECT * FROM`student`</select>
</mapper>