㈠ 加载iBatis 配置文件与映射文件:
加载配置文件:
① String resource = "sqlMapConfig.xml";
Reader reader = Resources.getResourceAsReader(reader);
SqlMapClient sqlMapClient = SqlMapClientBuilder.buildSqlMap(reader);
② String resource = "sqlMapConfig.properties";
Properties props = Resources.getResourceAsProperties(resource);
SqlMapClient sqlMapClient = SqlMapClientBuilder.buildSqlMap(props);
③ String resource = "sqlMapConfig.xml";
Stream stream = Resources.getResourceAsStream(resource);
SqlMapClient sqlMapClient = SqlMapClientBuilder.buildSqlMap(stream);
④ String resource = "sqlMapConfig.xml";
File file = Resources.getResourceAsFile(resource);
SqlMapClient sqlMapClient = SqlMapClientBuilder.buildSqlMap(file);
㈡ 数据更新:
插入:public int insert(String statementName,Object parameterObject) throws SQLException.
更新:public int update(String statementName,Object parameterObject) throws SQLException.
删除:public int delete(String statementName,Object parameterObject) throws SQLException.
例:sqlMapClient.startTransaction();
User user = new User();
user.setId(100);
user.setUsername("joe");
user.setPassword("123");
user.setEmail("[email protected]");
int rows = sqlMapClient.insert("users.insertUser",user);//users映射文件namespace值,insertUser映射文件insert的id值
sqlMapClient.commitTransaction();
㈢ 数据查询:
① 查询结果返回一个对象
public Object queryForObject(String statementName,Object parameterObject) throws SQLException.
public Object queryForObject(String statementName,Object parameterObject,Object resultObject) throws SQLException
例:sqlMapClient.startTransaction();
Integer id = new Integer(100);
User user = (User)sqlMapClient.queryForObject("users.getUser",id);
sqlMapClient.commitTransaction();
② 查询结果放入List中
public List queryForList(String statementName,Object parameterObject) throws SQLException.
public List queryForList(String statementName,Object parameterObject,int skipResults,int maxResults) throws SQLException.
public List queryForList(String statementName,Object parameterObject,RowHandler rowHandler) throws SQLException.
例:sqlMapClient.startTransaction();
List list = sqlMapClient.queryForList("users.getUserList",null);
sqlMapList.commitTransaction();
例:sqlMapClient.startTransaction();
List list = sqlMapClient.queryForList("users.getUserList",null,0,40);
sqlMapList.commitTransaction();
例:sqlMapClient.startTransaction();
Rowhandler rowhandler = new MyRowHandler();
List list = sqlMapClient.queryForList("users.getUserList",null,rowhandler );
sqlMapList.commitTransaction();
public class MyRowHandler implements RowHandler{
public void handlerRow(Object obj,List list) throws SQLException{
User user = (User)obj;
user.setPassword("123");
sqlMapClient.update("users.updateUser",user);
}
}
③ 分页查询
public PaginatedList queryForPaginatedList(String statementName,Object parameterObject,int pageSize) throws SQLException.
例:PaginatedList list = sqlMapClient.queryForPaginatedList("users.getUserList",null,10);
list.nextPage(); //下一页
list.previous(); //前一页
list.isFirstPage(); //是否是首页
list.isMiddlePage(); //是否是中间页
list.isLastPage(); //是否是最后一页
list.isPreviousPageAvaliable(); //是否有前一页
list.getPageIndex(); //获得当前页码
list.getPageSize(); //获得页面大小。
④ 查询结果放入Map中
public Map queryForMap(String statementName,Object parameterObject,String keyProperty) thorws SQLException.
public Map queryForMap(String statementName,Object parameterObject,String keyProperty,String valueProperty) thorwsSQLException.
例:sqlMapClient.startTransaction();
Map map = sqlMapClient.queryForMap("users.getUserList",null,"id");
sqlMapClient.commitTransaction();
User user = (User)map.get(100);
⑤ 批量处理(insert/update/delete)
sqlMapClient.startBatch();
//...execute statements
sqlMapClient.executeBatch();
事务管理
public void startTransaction() throws SQLException //开始事务
public void commitTransaction() throws SQLException //提交事务
public void rollbackTransaction() throws SQLException //回滚事务
public void endTransaction() throws SQLException //结束事务
注:对每个SqlMapClient实例,一个线程最多只能打开一个事务。
使用自动事务
如果没有使用startTransaction()、commitTransaction()、rollbackTransaction()方法明确地划分事务范围,事务将自动执行。不建议使用。
使用全局事务
全局事务也叫分布式事务,它可以允许在同一事务中更新多个数据库,即同时成功或失败。全局事务即可以手工管理,也可以有框架来管理。
使用手工来管理全局事务,必须在配置文件中设定<transactionManager>的type的属性值设为EXTERNAL。
使用框架来管理全局事务,必须在配置文件中设定<transactionManager>的type的属性值设为JTA。
例:try{
sqlMapClient.startTransaction();
sqlMapClient0.startTransaction();
sqlMapClient.insert(...);
sqlMapClient0.update(...);
sqlMapClient0.commitTransaction();
sqlMapClient.commitTransaction();
}finally{
try{
sqlMapClient0.endTransaction();
}finally{
sqlMapClient.endTransaction();
}
}