SqlMapClient對象
這個對象是iBatis操作數(shù)據(jù)庫的接口(執(zhí)行CRUD等操作),它也可以執(zhí)行事務(wù)管理等操作。這個類是我們使用iBATIS的最主要的類。它是線程安全的。通常,將它定義為單例。(與hibernate中sessionFactory的定義類似)。如:
import java.io.Reader; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; public class IbatisSQLMapConfig { private static final SqlMapClient sqlMap; //在靜態(tài)區(qū)塊中初試化返回 static { try { //聲明配置文件的名稱(映射文件被定義在其中) String resource = "sql_map_config.xml"; //利用工具類Resources來讀取到配置文件 Reader reader = Resources.getResourceAsReader(resource); //創(chuàng)建SqlMapClient接口的變量實(shí)例 sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException( "Error initializing MyAppSqlConfig class. Cause: " + e); } } public static SqlMapClient getSqlMapInstance() { //提供靜態(tài)方法返回靜態(tài)區(qū)塊中得到的SqlMapClient return sqlMap; } } |
主要用法:
如何獲得剛插入記錄的自增長ID值?
以下所有虛線上面代表User.xml中的內(nèi)容,虛線下方是測試類中的內(nèi)容:User類沿用上一篇中的User類
<insert id="insertUser" parameterClass="User"> insert into t_user values ( null,#username#,#password# ) <selectKey resultClass="int" keyProperty="id"> SELECT @@IDENTITY AS ID </selectKey> </insert> |
User user = new User(); user.setUsername("張三"); user.setPassword("張三密碼");
//如果主鍵是自動生成的,則其返回值可以通過<selectKey>標(biāo)簽來設(shè)置 //如果不通過<selectKey>標(biāo)簽來設(shè)置,則返回值為空! //<selectKey >中的keyProperty,是指定User中的id屬性,當(dāng)調(diào)用結(jié)束之后, //user對象的id值和insert方法的返回值都是這條記錄的ID值! Object obj = sqlMapper.insert("insertUser", user); |
parameterClass的使用
<insert id="insertUser" parameterClass="User"> insert into t_user values ( null,#username#,#password# ) <selectKey resultClass="int" keyProperty="id"> SELECT @@IDENTITY AS ID </selectKey> </insert>
<insert id="insertUser2"> insert into t_user values ( null,#username#,#password# ) <selectKey resultClass="int" keyProperty="id"> SELECT @@IDENTITY AS ID </selectKey> </insert> |
insertUser使用了parameterClass,所以必需傳入User類型的對象
User user = new User(); user.setUsername("張三"); user.setPassword("張三密碼");
//傳遞進(jìn)去的對象,必須是User類型 Object obj = sqlMapper.insert("insertUser", user); |
insertUser2沒有使用parameterClass,所以可以傳入任意具有相應(yīng)屬性值的對象
JustAnObject anobj = new JustAnObject(); anobj.setUsername("用戶名"); anobj.setPassword("用戶密碼");
//如果沒有指定parameterClass屬性,則任何一個具有相應(yīng)屬性值 //的對象都可以被傳遞進(jìn)去 Object obj = sqlMapper.insert("insertUser2", anobj); |
parameterMap的使用
<parameterMap class="User" id="insertUser-param"> <parameter property="username"/> <parameter property="password"/> </parameterMap> <insert id="insertUser" parameterMap="insertUser-param"> insert into t_user values ( null,?,? ) <selectKey resultClass="int" keyProperty="id"> SELECT @@IDENTITY AS ID </selectKey> </insert> |
parameterMap用于傳入?yún)?shù),以便匹配SQL語句中的?號
User user = new User(); user.setUsername("張三dd"); user.setPassword("張三密碼dd");
Object obj = sqlMapper.insert("insertUser", user); |
利用parameterMap,可以定義參數(shù)對象的屬性如何映射到SQL查詢語句的動態(tài)參數(shù)上,注意parameterMap中<parameter/>標(biāo)簽的先后順序不能顛倒!
如何將查詢結(jié)果映射到不同的對象?(resultClass的使用)
package com.ibatis.model;
publicclassOtherObject {
privateintid;
private String prop1;
private String prop2;
publicint getId() {
returnid;
}
publicvoid setId(int id) {
this.id = id;
}
public String getProp1() {
return Prop1;
}
publicvoid set Prop1 (String Prop1) {
this. Prop1 = Prop1;
}
public String getProp2() {
returnusername;
}
publicvoid setProp2 (String Prop2) {
this.Prop2 = Prop2;
}
}
<select id="selectUserForOtherObject" resultClass="com. ibatis.OtherObject" parameterClass="int"> select username as prop1, password as prop2 from t_user where id=#value# </select> |
//查找t_user表,將其結(jié)果映射到一個屬性名不同的對象中! OtherObject obj = (OtherObject)sqlMapper.queryForObject("selectUserForOtherObject", 1); System.out.println(obj.getProp1()+","+obj.getProp2()); |
如何將查詢結(jié)果集映射到不同的對象?(resultMap的基本使用)
<resultMap class="com.ibatis.model.OtherObject" id="ooResult"> <result property="prop1" column="username"/> <result property="prop2" column="password"/> </resultMap> <!-- 如果使用resultMap來定義如何映射,則如下語句不可寫成: select username as prop1,password as prop2 .... --> <select id="selectUserForOtherObject2" parameterClass="int" resultMap="ooResult"> select username, password from t_user where id=#value# </select> |
//查找t_user表,將其結(jié)果映射到一個屬性名不同的對象中! OtherObject obj = (OtherObject)sqlMapper.queryForObject("selectUserForOtherObject2", 17); System.out.println(obj.getProp1()+","+obj.getProp2()); |
如何將查詢結(jié)果集映射為xml格式的數(shù)據(jù)?
<select id="selectXmlData" resultClass="xml" xmlResultName="User" parameterClass="int"> select * from t_user where id=#value# </select> <select id="selectXmlDatas" resultClass="xml" xmlResultName="User"> select * from t_user </select> |
//查找t_user表,將其結(jié)果映射到xml! //返回值是xml形式的字符串 Object obj = (Object)sqlMapper.queryForObject("selectXmlData", 1); System.out.println(obj); //查找t_user表,將其結(jié)果映射到xml! List list = (List)sqlMapper.queryForList("selectXmlDatas"); System.out.println(list); |
如何用Map類型的對象作為傳入?yún)?shù)?
<!-- 這里,可以使用全路徑類名,如: java.util.Map java.util.HashMap java.util.TreeMap 或 map --> <insert id="insertUser" parameterClass="map"> insert into t_user values ( null,#username#,#password# ) </insert> |
Map user = new TreeMap(); user.put("username", "Map用戶"); user.put("password", "Map用戶密碼"); sqlMapper.insert("insertUser",user); |
如何將查詢結(jié)果集的元素轉(zhuǎn)換為Map類型的對象?
<!-- resultClass可以定義為java.util.HashMap類型, 將能自動轉(zhuǎn)換 --> <select id="selectMapUsers" resultClass="java.util.HashMap"> select * from t_user </select> |
List list = (List)sqlMapper.queryForList("selectMapUsers"); System.out.println(list); for (Iterator iter = list.iterator(); iter.hasNext();) { Map map = (Map) iter.next(); //可在此輸出map的數(shù)據(jù) } |
事務(wù)處理
可以使用sqlMapClient的startTransaction/commitTransaction/endTransaction等方法來控制事務(wù)的邊界。
如果與spring整合(這是iBatis推薦的方式),則我們需要在spring配置文件中指定其事務(wù)特性。