Ibatis之3个不常用的Query方法,ibatisquery方法
1.queryForObject
/** * Executes a mapped SQL SELECT statement that returns data to populate * the supplied result object. * <p/> * The parameter object is generally used to supply the input * data for the WHERE clause parameter(s) of the SELECT statement. * * @param id The name of the statement to execute. * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.). * @param resultObject The result object instance that should be populated with result data. * @return The single result object as supplied by the resultObject parameter, populated with the result set data, * or null if no result was found * @throws java.sql.SQLException If more than one result was found, or if any other error occurs. */ Object queryForObject(String id, Object parameterObject, Object resultObject) throws SQLException;
当查询对象是一个重量级对象、创建过程比较复杂时或者查询对象没有默认的构造方法时,通过该方法,可以在外部先构建好查询对象,然后传给Ibatis,Ibatis此时不会创建新对象,而是调用传入对象的set方法进行赋值。
2.queryForList
/** * Executes a mapped SQL SELECT statement that returns data to populate * a number of result objects within a certain range. * <p/> * This overload assumes no parameter is needed. * * @param id The name of the statement to execute. * @param skip The number of results to ignore. * @param max The maximum number of results to return. * @return A List of result objects. * @throws java.sql.SQLException If an error occurs. */ List queryForList(String id, int skip, int max) throws SQLException;
利用这个方法可以实现分页功能,如(skip=0,max=10)返回前10条数据,(skip=10,max=10)返回第10-20条数据,但这个方法的分页效率非常低,因为Ibatis是把所有的查询结果查询出来之后才进行筛选操作。数据量小的时候用用还可以,所以这个方法比较鸡肋。
3.queryForMap
/** * Executes a mapped SQL SELECT statement that returns data to populate * a number of result objects that will be keyed into a Map. * <p/> * The parameter object is generally used to supply the input * data for the WHERE clause parameter(s) of the SELECT statement. * * @param id The name of the statement to execute. * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.). * @param keyProp The property to be used as the key in the Map. * @return A Map keyed by keyProp with values being the result object instance. * @throws java.sql.SQLException If an error occurs. */ Map queryForMap(String id, Object parameterObject, String keyProp) throws SQLException;
网上有不少帖子说这个方法只能返回一条记录是不对的,还有说是把resultClass的所有属性放到一个map中返回来也是不对的。这个方法是对queryForList的一个补充,大部分情况下我们用的都是queryForList返回对象的列表,但有时候放到Map里用起来可能更方便,如果没有这个方法还得自己进行转换,同样的一个<select ...>配置,不用做任何更改即可以用queryForList访问也可以用queryForMap访问。
1.查询型方法
QueryForObject()方法用于从数据库中获取单行数据,并将其转换为JavaBean对象。"selectUser"对应于定义在映射文件中<select>节点的id。
UserInfo user = new UserInfoVo();
user.setUserName("Andy");
UserInfo user=(UserInfo)sqlMapClient.queryForObject("selectUser", userInfo);
本次查询的结果对象user中就得到数据库中用户名称是“Andy”的用户信息。
2。QueryForList()方法用于从数据库中获取多行数据以List对象的形式返回,与它使用方法相同的还有QueryForMap(),返回的数据封装在HashMap的对象中。它们也都有两个参数,字符串值对应与定义在映射文件中<select>节点的id。
使用方法如下:
UserInfo user = new UserInfo();
List list=new ArrayList();
list=sqlMapClient .queryForList("selectAllUserInfo", user);
3。queryForPaginatedList()用来进行分页查询,它有3个参数,分别是字符串与<selelct>节点id对应、查询条件的对象、每次查询结果的数量。使用的方法如下:
UserInfo user=new UserInfo();
PaginatedList list= sqlMap.queryForPaginatedList("selectAllUserInfo", user, 10);
至于iBATIS的配置文件和映射文件你一定知道如何配置吧!
参考资料:《开发者突击 Struts2核心技术与JavaEE框架整合开发实战》
常用的:updateByExample、selectByPrimaryKey、selectByExample、insert、insertSelective、
deleteByPrimaryKey、deleteByExample ...基本上DAOImpl里的都用得上;
举个例子说明:
public List selectByExample(TUserExample example) {
List list = getSqlMapClientTemplate().queryForList("t_user.ibatorgenerated_selectByExample",example);
return list;
}
这个方法,是按指定的条件从表中查询数据。方法参数类型TUserExample,为封装了查询条件的类,查询条件会被传到**sqlMap.xml中的sql中,查询的一条条记录都会被封装成一个个的对象,然后放在一个List中返回给你。