之前對Mybatis有了初步的學習與使用,最近學習ssh發現ssh太重,我個人覺得,像hibernate事務,很多情況下沒有使用,而緩存,大部分時候也用不上,需要用到的場景,完全可以自已開發,更輕,而數據關系變得復雜后,hibernate越難駕馭,相比而言mybatis更靈活,必要時,直接寫SQL。所以再次對Mybatis進一步的進行學習。
返回值
Mybatis 中在查詢進行select映射的時候,返回類型可以用resultType,也可以用resultMap,resultType是直接表示返回類型的,而resultMap則是對外部resultMap的引用,但是resultMap和resultType不能同時存在。
在Mybatis進行查詢映射時,其實查詢出的每一個屬性都是放在一個對應的Map里面的,其中key是屬性名,values則是其對應的值。
- 當提供的返回值類型屬性是resultType時,Mybatis會將Map里面的key的value取出給resultType所指定對應的屬性,其實Mybatis的每一個查詢映射的返回值類型都是resultMap,只是當提供的返回值類型屬性resultType的時候,Mybatis對其自動的給把對應的值獻給resultType所指定對象的屬性。
- 當提供的返回值類型是resultMap時,因為Map不能很好表示,就需要自己再進行一步把它轉換為對應的對象,這常常在復雜的查詢中很有作用如:assoiation、collection。
使用resultType類型
<!-- resultType對應JavaBean類型 -->
<select id="selectAll" resultType="cn.softjx.modle.User">
select t_id id,t_username username,t_password password from t_user;
</select>
可以看到在select語句中都進行了字段名的替換,因為在javabean中屬性的命名與在數據庫中真實的字段名是不一樣的,所以需要在sql中進行手動的代換,這是字段比較少的情況如果數據多就會使sql語句變得復雜,不便于維護修改。
使用resultMap類型
<resultMap type="cn.softjx.modle.User" id="UserMap">
<result column="t_id" property="id"/>
<result column="t_username" property="username"/>
<result column="t_password" property="password"/>
</resultMap>
<select id="selectAll2" resultMap="UserMap" >
select t_id,t_username,t_password from t_user;
</select>
使用resultMap在進行sql語句查詢時不用在進行手動的替換,交給Mybatis自動映射完成,得到的結果是一致的。
- column 字段對應的應當是數據庫查詢結果字段,而不是數據庫中的字段
- property 與Javabean中屬性值一致
查詢標簽中的resultMap要與表映射的resultMap標簽中的id一致。
表查詢
- 多對一
多對一在實際項目中經常用到,比如多個學生對應一個學校多個工人對應一個工廠。可以通過在數據庫中進行外鍵的關聯物理上的實現也可以通過sql語句實現邏輯上的關聯,在此不再贅述。無論通過那種關聯在Mybatis的映射文件配置基本上是一置的。
本例舉多個學生對一個學校的例子:
- User.java
public class User {
private Integer id;
private String username;
private String password;
private Integer sid;
private School school;
}
當然還要提供相應的set、get方法,在UserBean里面聲明了一個School對象,要取出學校名稱時可以通過這個schoo對象的get方法將名字取出。
實現方式一
- UserMapping.xml
<resultMap type="cn.softjx.modle.User" id="UserMap">
<result column="t_id" property="id"/>
<result column="t_username" property="username"/>
<result column="t_password" property="password"/>
<result column="t_sid" property="sid"/>
<result column="s_name" property="school.name"/>
</resultMap>
<select id="getSchoolName" resultMap="UserMap" >
select t_id,t_username,t_password,s_name FROM t_user,school where t_sid=s_id AND s_id=1;
</select>
可以看到在resultMap中不配置了User自身的屬性還多了<result column="s_name" property="school.name"/>,即我們需要哪個附表的值就要在resultMap中配置相應的附表映射。
實現方式二
<resultMap type="cn.softjx.modle.User" id="UserMap">
<result column="t_id" property="id"/>
<result column="t_username" property="username"/>
<result column="t_password" property="password"/>
<result column="t_sid" property="sid"/>
<association property="school" javaType="cn.softjx.modle.School">
<result column="s_name" property="name" />
</association>
</resultMap>
<select id="getSchoolName" resultMap="UserMap" >
select t_id,t_username,t_password,s_name FROM t_user,school where t_sid=s_id AND s_id=1;
</select>
方式二引入了association標簽,不再在原有user映射關系上進行補充,此標簽中的property屬性值為UserBean中附表對象的引用名,javaType即為Schooljava類的的全路徑。sql語句不變
可以看到通過mybatis查詢出的結果與在數據庫中查詢的結果是一致的。
- 一對多
一對多與多對一的配置有許多相似點,一對多返回的類型是一個集合所以要在SchoolBean中加入學生的集合
School.java
public class School {
private Integer id;
private String name;
private List<User> user;
}
學校映射文件配置
<resultMap type="cn.softjx.modle.School" id="SchoolMap">
<result column="s_id" property="id"/>
<result column="s_name" property="name"/>
<collection property="user" ofType="cn.softjx.modle.User">
<result column="t_id" property="id"/>
<result column="t_username" property="username"/>
<result column="t_password" property="password"/>
<result column="t_sid" property="sid"/>
</collection>
</resultMap>
<select id="getSchool" resultMap="SchoolMap">
select s_id,s_name,t_id,t_username,t_password
FROM school,t_user WHERE s_id=t_sid AND s_id=1;
</select>
標簽屬性含義與多對一配置差不多不再詳說