Mapper代理的開發方式:
-
只需要編寫mapper接口,Mybatis會根據映射文件自動生成動態代理實現類。
開發規範:
- mapper接口的全名對應 mapper映射文件的namespace的值相同
- mapper接口的方法名對應 mapper映射文件中的statement(select,insert,delete)的id相同;
- mapper接口的方法參數只能有一個,且類型要和mapper映射文件中statement的parameter的type相同。
- mapper接口的返回值類型要和mapper映射文件中statement的resultType值或resultMap中的type相同;
動態SQL
1. if和where
- If標籤:用來判斷傳入參數,如果符合條件,則把if標籤體內的SQL拼接上
- 用if進行判斷是否爲空時,不僅要判斷null,也要判斷空字符串''
- Where標籤:會去掉條件中的第一個and符號
- 案例:模糊查詢性別是user.sex(入參),名字是user.name(入參),地址是user.address(入參),判斷三者是否爲空或null
-
<!-- 3.if和where的使用--> <sql id="select_user_where"> <if test="user != null"> <if test="user.sex != null and user.sex != ''"> sex = #{user.sex} </if> <if test="user.username != null and user.username != ''"> and username LIKE '%${user.username}%' </if> <if test="user.address != null and user.address != ''"> and address LIKE '%${user.address}%' </if> </if> </sql> <select id="findUserList" parameterType="userQueryVO" resultType="user"> /*性別和名字*/ SELECT * FROM user <where> <include refid="select_user_where"/> </where> </select>
- 實質:判斷條件,然後拼接,注意拼接規則
2.SQL片斷
3.foreach 遍歷
- SELECT * FROM user WHERE id in (1,2,3) 參數傳入一個關於 id的list集合
<foreach collection="ids" item="id" open="id in(" close=")" separator="," >
${id}
</foreach>
拼接格式
- collection:集合,寫集合屬性
- item:遍歷接收的變量
- open:遍歷開始
- close:遍歷結束
- 注意open和close的拼接,id in ( )
- separator:拼接格式,分隔符號
- ${id}:獲取( )內部的參數
-
<select id="findUserByIds" parameterType="userQueryVO" resultType="user"> SELECT * FROM user <where> <if test="ids != null and ids.size > 0"> <foreach collection="ids" item="id" open="id in(" close=")" separator="," > ${id} </foreach> </if> </where> </select>
關聯查詢 :
-
個人總結:關聯查詢 肯定就不是一個簡單的模型了,如下代碼,在模型1中有模型2的集合,集合中有模型3,查詢關聯多個表,必定要採用resyltMap來進行和數據庫表字段名的對應,在映射過程中集合用collection 模型用association,對應每個表(每個model)的主鍵要用<id column="xxx".....>來說明,其餘字段用result,注意嵌套關係,model包中的VO類(來拿各種方式:model嵌套成複雜model;model繼承(tostring:super.tostring)
代碼案例
<!-- ==============查詢用戶信息及用戶購買的商品信息============-->
<resultMap id="userRslMap" type="user">
<!-- 1.匹配user屬性 -->
<id column="id" property="id"></id>
<result column="username" property="username"/>
<result column="password" property="password"/>
<!--2.匹配user的orderList-->
<collection property="orderList" ofType="orders">
<id column="order_id" property="id"></id>
<result column="number" property="number"/>
<result column="createtime" property="createtime"/>
<result column="note" property="note"/>
<!-- 3.匹配Orders裏有orderDetails-->
<collection property="orderDetails" ofType="orderDetail">
<id column="detail_id" property="id"></id>
<result column="items_id" property="itemsId"/>
<result column="items_num" property="itemsNum"/>
<!-- 4.配置定單詳情的商品信息-->
<association property="items" javaType="items">
<id column="items_id" property="id"/>
<result column="name" property="name"/>
<result column="price" property="price"/>
<result column="detail" property="detail"/>
</association>
</collection>
</collection>
</resultMap>
<select id="findUserAndOrderInfo" resultMap="userRslMap">
SELECT
u.id,
u.username,
u.address,
o.id order_id,
o.number,
o.createtime,
o.note,
od.id detail_id,
od.items_id,
od.items_num,
it.name,
it.price,
it.detail
FROM
user u,
orders o,
orderdetail od,
items it
WHERE
o.user_id = u.id
AND o.id = od.orders_id
AND od.items_id = it.id
</select>