这个对象是iBatis操作数据库的接口(执行CRUD等操作),它也可以执行事务管理等操作。这个类是我们使用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; //在静态区块中初试化返回 static { try { //声明配置文件的名称(映射文件被定义在其中) String resource = "sql_map_config.xml"; //利用工具类Resources来读取到配置文件 Reader reader = Resources.getResourceAsReader(resource); //创建SqlMapClient接口的变量实例 sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException( "Error initializing MyAppSqlConfig class. Cause: " + e); } } public static SqlMapClient getSqlMapInstance() { //提供静态方法返回静态区块中得到的SqlMapClient return sqlMap; } }
<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>标签来设置 //如果不通过<selectKey>标签来设置,则返回值为空! //<selectKey >中的keyProperty,是指定User中的id属性,当调用结束之后, //user对象的id值和insert方法的返回值都是这条记录的ID值! Object obj = sqlMapper.insert("insertUser", 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> <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("张三密码"); //传递进去的对象,必须是User类型 Object obj = sqlMapper.insert("insertUser", user);insertUser2 没有使用 parameterClass ,所以可以传入任意具有相应属性值的对象
JustAnObject anobj = new JustAnObject(); anobj.setUsername("用户名"); anobj.setPassword("用户密码"); //如果没有指定parameterClass属性,则任何一个具有相应属性值 //的对象都可以被传递进去 Object obj = sqlMapper.insert("insertUser2", anobj);
<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>
User user = new User(); user.setUsername("张三dd"); user.setPassword("张三密码dd");
Object obj = sqlMapper.insert("insertUser", user);
利用parameterMap,可以定义参数对象的属性如何映射到SQL查询语句的动态参数上,注意parameterMap中<parameter/>标签的先后顺序不能颠倒!
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表,将其结果映射到一个属性名不同的对象中! OtherObject obj = (OtherObject)sqlMapper.queryForObject("selectUserForOtherObject", 1); System.out.println(obj.getProp1()+","+obj.getProp2());
<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表,将其结果映射到一个属性名不同的对象中! OtherObject obj = (OtherObject)sqlMapper.queryForObject("selectUserForOtherObject2", 17); System.out.println(obj.getProp1()+","+obj.getProp2());
<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表,将其结果映射到xml! //返回值是xml形式的字符串 Object obj = (Object)sqlMapper.queryForObject("selectXmlData", 1); System.out.println(obj); //查找t_user表,将其结果映射到xml! List list = (List)sqlMapper.queryForList("selectXmlDatas"); System.out.println(list);
<!-- 这里,可以使用全路径类名,如: 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);
<!-- resultClass可以定义为java.util.HashMap类型, 将能自动转换 --> <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的数据 }