First of all, the basic content, Ibatis is not the true meaning of the ORM, the official document called the Datamapper, is a data mapper, which is a mapping query tool. Ibatis is not omnipotent, in some of the problems it can not handle, can not abandon the use of the JDBC API, that is the root of the fundamental.
In Ibatis, we recommend the use of JavaBean, because we are object-oriented design, then in the system design will certainly create a lot of concrete object-specific classes, using JavaBean can directly manipulate getter method to get the content. It's like a PO in hibernate. Here's a way to get the property names and property types in a bean, which you might use at development time.
First define a JavaBean, describe the user model, as follows:
Java code package ibatis.model; public class user implements java.io.serializable { private Integer userId; private String userName; private string password; private String mobile; private String email; public user () { super (); } public user (integer userid, string username, string password, String mobile, string email) { Super (); this.userid = userid; this.userName = userName; this.password = password; this.mobile = mobile; this.email = email; } // Omit getter and setter methods @Override public string tostring () { return "User [email=" + email + ", mobile=" + mobile + ", password=" + password + ", userid=" + userid + ", username=" + userName + "]"; } }
Write a method to test, as follows:
Java code Public static void main (String[] args) { try { PropertyDescriptor[] pd = introspector.getbeaninfo (User.class). Getpropertydescriptors (); for (int i = 0; i < pd.length; i++) { system.out.println (Pd[i]. GetName () + " (" + pd[i].getpropertytype (). GetName () + ")" ); } } catch (introspectionexception e) { E.printstacktraCE (); } }
In the console, we get the following output:
Java code Class (Java.lang.Class) email (java.lang.String) mobile (java.lang.String) password (java.lang.String) US Erid (Java.lang.Integer) userName (java.lang.String)
This is a good way to locate a bug.
Next, let's take a look at three common query methods, whose names are similar to spring's jdbctemplate/sqlmapclienttemplate, but separate them.
The first is the queryForObject () method, which returns a result of the database query and puts it into a Java object, where a record can be either a JavaBean or a collection type of java. It can be determined according to the ResultClass property configured in the <select> tag, if you do not specify the ResultClass property, then the query result is NULL, because Ibatis does not know how to handle the result, And we don't have a result map configured (RESULTMAP).
First we set the ResultClass to user according to the user type above, the code is as follows:
XML code <sqlmap namespace= "User" > <typealias alias= "User" type= "ibatis.model.User"/> <select i D= "Getuserbyname" parameterclass= "java.lang.String" resultclass= "User" > select * from Use RS where username= #VARCHAR # </select> </sqlMap>
It is necessary to have a default constructor in the user class, otherwise it will not be possible to instantiate the object and throw an exception, which cannot be forgotten (if the construction method is overloaded). We write a program:
Java code System.out. println (Sqlmap.queryforobject ("User.getuserbyname", "Sarin"). GetClass (). GetName ());
At this point, the output is: Ibatis.model.User, it is clear to see that the result type of the query is determined by the <select> ResultClass.
Another overloaded method for queryForObject () is an object queryForObject (String ID, object parameter, Object Resultobject) throws Exception, This method is used for situations where an object cannot be easily created (such as an object without a default constructor method), then an exception is thrown using the preceding format, which is required to look at the following code: (this removes the default constructor in the user class)
Java Code User User=new user (null, NULL, NULL, NULL, NULL); user = (user) Sqlmap.queryforobject ("User.getuserbyname", "Sarin", user); SYSTEM.OUT.PRINTLN (user);
This will get the user object.
The second method is the queryForMap () method, which can return either one or more of the results. Its method signature has two forms: the first is the map queryForMap (string ID, Object parameter, string key) throws SQLException, and the second is one more parameter, string value. The first two parameters are good to understand, that is, the ID of the select tag and the parameters passed in, and what the following key and value mean. KEY specifies the key stored in the map, and value determines the stored value, and an object that stores the query when no value is set, such as the following code (the ResultClass of select is now set to HashMap):
Java code Map map = Sqlmap.queryformap ("User.getallusers", NULL, "userId"); SYSTEM.OUT.PRINTLN (map);
As you can imagine, the output of this code is:
Java code {1={email=gmail@gmail.com, userid=1, Username=sarin, password=123, mobile=15940912345}, 2={email= Gmail@gmail.com, userid=2, Username=sarin, password=123, mobile=15940912345}}
The 1 and 2 here are key, the keys, then what type are they? We use the following code to see:
Java Code System.out.println (Map.keyset (). iterator (). Next (). GetClass ());
Get the result: Class Java.lang.Integer, which indicates that this is the corresponding field, because we have not associated the query results with JavaBean. So what type of value is stored in HashMap? Let's see, the code looks like this:
Java Code System.out.println (Map.get (1). GetClass ());
Print: Class Java.util.HashMap, indicating whether it is stored or HashMap. The second overloaded method of queryForMap () specifies the contents of value, which we look at:
Java code Map map = Sqlmap.queryformap ("User.getallusers", NULL, "UserId", "mobile"); SYSTEM.OUT.PRINTLN (map);
This will print: {1=15940912345, 2=15940912345}, this time it's clear, and the type of mobile you get is a string and it's easy to understand. Remember, the queryForMap () method can return one or more records. However, in practice it is often used to obtain a complete record, so it is very convenient to use the Get () method of map to get the value.
Here's The queryForList () method, as well, there are two types of method signatures for the method: The first class is queryForList (String ID, Object parameter) throws SQLException, or no arguments are required. That's a good understanding. See an example: (ResultClass in Sqlmap is set to HashMap)
Java code List users = sqlmap.queryforlist ("User.getallusers"); System.out.println (users);
The result of the printing is:
Java code [{email=gmail@gmail.com, userid=1, Username=sarin, password=123, mobile=15940912345}, {email=gmail@gmail.com, userid=2, Username=sarin, password=123, mobile=15940912345}]
That is, the list is loaded with the HashMap object, in Sqlmap will hashmap for the user, then get:
Java code [user [Email=gmail@gmail.com, mobile=15940912345, password=123, Userid=1, Username=nanlei], user [email= Gmail@gmail.com, mobile=15940912345, password=123, userid=2, Username=sarin]
The second method of queryForList () is queryforlist (String ID, Object parameter, int skip, int max) throws SQLException, You can see that there are more than two parameters of type int, so what can be done with two parameters of type int in SQL. Paging, yes, this is the main application. Ibatis provides a way to provide support for paging in queryForList (). Remember that skip is calculated from 0, and Max is the number of bars taken out, then take the first 10 is (0,10), take 11~20 bar is (10,10), and so on.
Opinion, for reference only. Welcome to the Exchange.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.