一、各種實體類的簡介及作用
pojo:不按mvc分層,只是javabean有一些屬性,還有getset方法
domain:不按mvc分層,只是javabean有一些屬性,還有getset方法
po:用在持久層,還可以再增加或者修改的時候,從頁面直接傳入action中,它裏面的javabean 類名等於表名,
屬性名等於表的字段名,還有對應的getset方法
vo:view object表現層對象,主要用於在高級查詢中從頁面接收傳過來的各種參數.好處是擴展性強
bo:用在servie層,現在企業基本不用.
這些po,vo,bo,pojo可以用在各種層面嗎
可以,也就是po用在表現層,vo用在持久層不報錯,因爲都是普通的javabean沒有語法錯誤.但是在企業最好不要混着用,因爲這些都是設計的原則,混着用比較亂.不利於代碼維護.
二、輸入映射paramterType
#{id}-佔位符
${value}拼接符字符串原樣拼接。
<!--
id:sql語句的唯一標識符
parameterType:指定傳入的參數的類型
resultType:指定返回的參數的類型
#{}:如果是基本數據類型(Integer、String、double...),那麼#{}花括號中的變量可隨意填寫,如果是pojo那麼必須保證和pojo的變量名一致
-->
<select id="findById" parameterType="int" resultType="org.lier.zz.User">
select * from user where id = #{id}
</select>
三、輸出映射
resultType:返回單條是指定是接收數據的類型pojo,返回多條結果的時候,得到的結果是是集合的泛型的類型。
四、動態sql
1、where和if搭配使用
select id="findUsersByNameAndSex" parameterType="org.lier.zz.User" resultType="org.lier.zz.User">
select *from user where 1=1
<-- 這種方式拼接完成的sql有問題 -->
<if test="username != null and username != ''">
and username like '%${username}%'
</if>
<if test="sex != null and sex != ''">
and sex = #{sex}
</if>
</select>
拼接完成 的sql是這樣的:
select *from user where 1=1 and username like '%李%' and sex = ?
這樣的話,即便不傳入值也能獲取相應的結果。
更改:
<select id="findUsersByNameAndSex" parameterType="org.lier.zz.User"
resultType="org.lier.zz.User">
select *from user
<where>
<!-- where標籤作用:
1、自動添加where關鍵字
2、去掉第一個條件的and關鍵字 -->
<if test="username != null and username != ''">
and username like '%${username}%'
</if>
<if test="sex != null and sex != ''">
and sex = #{sex}
</if>
</where>
</select>
這種方式生成的sql語句:
select *from user WHERE username like '%李%' and sex = ?
非常的人性化,特別的棒棒!!
2、sql的重用
sql標籤和include標籤搭配使用
<sql id="user_where">
<where>
<!-- where標籤作用: 1、自動添加where關鍵字 2、去掉第一個條件的and關鍵字 -->
<if test="username != null and username != ''">
and username like '%${username}%'
</if>
<if test="sex != null and sex != ''">
and sex = #{sex}
</if>
</where>
</sql>
<select id="findUsersByNameAndSex" parameterType="org.lier.zz.User"
resultType="org.lier.zz.User">
select *from user
//調用sql條件
<include refid="user_where"></include>
</select>
3、foreach語句
應用場景:select* fromuser where id in (?,?,?) 不要使用or語句使用or語句查詢速度會非常的慢
<select id="findUsersByIds" parameterType="org.lier.zz.QueryVo" resultType="org.lier.zz.User">
select * from user
<where>
<if test="ids != null">
<!--
foreach:循環傳入集合參數
collection:傳入集合的變量名稱
item:每次循環將循環中的數據放入這個變量中
open:循環開始拼接的字符串
close:循環結束拼接的字符串
separater:循環中的分隔符
-->
<foreach collection="ids" item="id" open="id in (" close=")" separator=",">
#{id}
</foreach>
</if>
</where>
</select>
4、單個對象關係映射
場景:多個表進行映射,單個的實體無法容納全部的結果。
方式1:將兩個表對應的實體類進行平鋪,如下
public class CustomOrders extends Orders{
private int uid;
private String username;
private String sex;
private Date birthday;
private String address;
…geter and seter method
}
< – 一對一:自動映射 – >
<select id="findOrderToUser" resultType="org.lier.zz.CustomOrders">
SELECT
o.*, u.id uid, username, birthday, sex, address
FROM
orders o,
user u
WHERE
o.user_id = u.id;
</select>
這是一種偷懶的做法,將相關表單的實體類進行平鋪,Mybatis中是不提倡的。~_~
< – 一對一手動映射 – >
<!-- 一對一:手動映射
id:resultMap的唯一標識符
type:將查詢的數據放入的對象
-->
<resultMap type="org.lier.zz.Orders" id="Order_user">
<!-- id:id標籤指定主鍵對應關係
column:數據庫字段名
property:屬性,pojo中的屬性名
result:指定非主鍵列的對應關係
-->
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="createtime" property="createtime"/>
<result column="numeber" property="number"/>
<result column="note" property="note"/>
<!--
這個標籤指定單個對象的映射關係
-->
<association property="user" javaType="org.lier.zz.User">
<id column="uid" property="id"/>
<result column="username" property="username"/>
<result column="birthday" property="birthday"/>
<result column="sex" property="sex"/>
<result column="address" property="address"/>
</association>
</resultMap>
<select id="findOrderToUser2" resultMap="Order_user">
SELECT
o.*, u.id uid, username, birthday, sex, address
FROM
orders o,
user u
WHERE
o.user_id = u.id;
</select>
class文件中:
public class Orders {
private Integer id;
private Integer userId;
private String number;
private Date createtime;
private String note;
private User user;
…….geter and seter methods
}
這是Mybatis提倡的方式。
<!-- 一對多映射 -->
<resultMap type="org.lier.zz.User" id="user_orders">
<id column="id" property="id" />
<result column="username" property="username" />
<result column="birthday" property="birthday" />
<result column="sex" property="sex" />
<result column="address" property="address" />
<!--
指定集合對象關係映射
property:將數據放入User中的orders集合中
ofType:指定orders集合的泛型類型
-->
<collection property="orders" ofType="org.lier.zz.Orders">
<id column="oid" property="id" />
<result column="user_id" property="userId" />
<result column="createtime" property="createtime" />
<result column="numeber" property="number" />
<result column="note" property="note" />
</collection>
</resultMap>
<select id="findUserAndOrders" resultMap="user_orders">
select u.*,o.id
oid,user_id,number,createtime,note from user u, orders o where u.id =
o.user_id;
</select>
public class User {
private int id;
private String username;
private String sex;
private Date birthday;
private String address;
private List<Orders> orders;
…….
}