最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

Ibatis之3个不常用的Query方法

来源:懂视网 责编:小采 时间:2020-11-09 14:36:36
文档

Ibatis之3个不常用的Query方法

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
推荐度:
导读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. * 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里用起来可能更方便,如果没有这个方法还得自己进行转换,同样的一个