您好,欢迎访问一九零五行业门户网

Ibatis之3个不常用的Query方法

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 sel
1.queryforobject
/** * executes a mapped sql select statement that returns data to populate * the supplied result object. * * 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. * * 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. * * 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里用起来可能更方便,如果没有这个方法还得自己进行转换,同样的一个配置,不用做任何更改即可以用queryforlist访问也可以用queryformap访问。
其它类似信息

推荐信息