Ibatis: The ibatisquery method, which is not commonly used.
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;
When the query object is a heavyweight Object and the creation process is complex, or the query object does not have a default constructor method, you can use this method to create a query object externally and then send it to Ibatis, ibatis does not create a new object at this time, but calls the set Method of the input object to assign values.
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;
This method can be used to implement the paging function. For example, (skip = 0, max = 10), the first 10 data records are returned (skip = 10, max = 10), and 10-20 data records are returned, however, the paging efficiency of this method is very low, because Ibatis performs the filtering operation only after all the query results are queried. When the data volume is small, it can still be used. Therefore, this method is quite helpful.
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;
There are many posts on the internet saying that this method can only return one record. It is also wrong to put all the attributes of resultClass into a map and return it. This method is a supplement to queryForList. In most cases, queryForList is used to return the list of objects, but sometimes it is more convenient to put it in Map, if you do not have this method, You have to convert it yourself. The same <select...> configuration, which can be accessed using queryForList or queryForMap without any changes.
Ibatis data insertion
1. Query Method
The optional QueryForObject () method is used to obtain a single row of data from the database and convert it to a javajan object. "SelectUser" corresponds to the id of the <select> node defined in the ing file.
UserInfo user = new UserInfoVo ();
User. setUserName ("Andy ");
UserInfo user = (UserInfo) sqlMapClient. queryForObject ("selectUser", userInfo );
In the result object user of this query, the username in the database is "Andy.
2. The QueryForList () method is used to obtain multiple rows of data from the database and return the data in the form of a List object. The same method as the QueryForMap () method is used, the returned data is encapsulated in the HashMap object. They also have two parameters. The string value corresponds to the id of the <select> node defined in the ing file.
The usage is as follows:
UserInfo user = new UserInfo ();
List list = new ArrayList ();
List = sqlMapClient. queryForList ("selectAllUserInfo", user );
3. QueryForPaginatedList () is used for querying by page. It has three parameters: the matching of the string and the <selelct> node id, the object of the query condition, and the number of results of each query. The method is as follows:
UserInfo user = new UserInfo ();
PaginatedList list = sqlMap. queryForPaginatedList ("selectAllUserInfo", user, 10 );
You must know how to configure the iBATIS configuration file and ing file!
Reference: developer assault on integrated development of Struts2 core technology and JavaEE framework
What are the common methods of ibatis (mybatis) underlying encapsulation?
Commonly used: updateByExample, selectByPrimaryKey, selectByExample, insert, insertSelective,
DeleteByPrimaryKey, deleteByExample... basically all DAOImpl is used;
For example:
Public List selectByExample (TUserExample example ){
List list = getSqlMapClientTemplate (). queryForList ("t_user.ibatorgenerated_selectByExample", example );
Return list;
}
This method queries data from a table based on specified conditions. The method parameter type is TUserExample. to encapsulate the query condition class, the query condition is passed to ** sqlMap. in the SQL statement in xml, the queried records are encapsulated into objects and then returned to you in a List.