The Mapper.xml mapping file is the core of MyBatis, which defines the SQL that operates the database, and each SQL is a statement.
ParameterType (input type), input type includes: base type, Pojo object type, HashMap,
A. #{} and ${}
#{} is to set the parameter value to a pre-processing statement in Preparestatement, which represents a placeholder, equivalent to?. Using placeholder #{} can effectively prevent SQL injection, and when used without caring about the type of the parameter value, MyBatis will invoke different statement to set the parameter value based on the type of the parameter value. Note: The parameter names in #{} are usually the same as the parameter names of the Mapper interface, and can be set to any value .
${} is different from #{}, ${} is a concatenation of parameter values in SQL, equivalent to JDBC statement splicing SQL, does not prevent SQL injection. However, there are times when you use the comparison method:
<!--query user information by user name--><select id= "Finduserbyusername" parametertype= "string" resulttype= "User" >select * From the users where username like '%${value}% ' </select><!--Comprehensive query user information--><select id= "Finduserlist" Parametertype= "User" resulttype= "user" >select * from the users where username like '%${username}% ' and sex = #{sex}</sel Ect>Note: If ParameterType is a base type, then the variable name in the ${variable name} must be value, and if it is a Pojo object type, the variable name is the property name of the Pojo object .
Basic type--no introduction
Pojo Object Type: The above code the second <select> ParameterType is the User object type, you can directly invoke the object's properties
Public list<user> finduserlist () throws Exception {sqlsession sqlsession = sqlsessionfactory.opensession (); Usermapper usermapper = Sqlsession.getmapper (Usermapper.class); User user = new user (), User.setusername ("Anna"), User.setsex ("0"); list<user> users = usermapper.finduserlist (User); System.out.println (Users.size ());iterator<user> Iterator = Users.iterator (); while (Iterator.hasnext ()) {User User2 = Iterator.next (); System.out.println (user2);} return users;}
HASHMAP type: The key in the HASHMAP in the code needs to be hardcoded, so HashMap is not used much.
<!--by HashMap query--><select id= "Finduserlistbyhashmap" parametertype= "HashMap" resulttype= "user" >select * From the users where username like '%${username}% ' and sex = #{sex}</select>
Public list<user> Finduserlistbyhashmap () throws Exception {sqlsession sqlsession = Sqlsessionfactory.opensession (); Usermapper usermapper = Sqlsession.getmapper (Usermapper.class); hashmap<string, object> map = new hashmap<string, object> (), Map.put ("username", "Anna"), Map.put ("Sex", "0" ); list<user> users = usermapper.finduserlistbyhashmap (map); System.out.println (Users.size ()); return users;}
Resulttype (output Type): base type, Pojo object Type (single), Pojo object list
Basic type--not much introduction
Pojo Object Types (single and list), resulttype= "User" is the same in Mapper.xml, the difference is in the Write Mapper interface, as follows: MyBatis will decide to call Mapper based on the return type of the Session.se interface. Lectone () or the Session.selectlist () method. Returns a single Pojo object to ensure that the result set of the SQL query is individual, using the Session.selectone method call, the Mapper interface uses the Pojo object as the method return value.
Public User Finduserbyid (int userId) throws Exception;public list<user> Finduserbyusername (String username) Throws Exception;
Resultmap (Output Type): Resultmap is also an output type that is used to resolve fields where the output Pojo object does not correspond to the field name of the SQL query. Commonly used in one-to-one related queries, one-to-many related queries.
-
public class person {private int id;private string name;private string addr;public int getid () {return id;} Public void setid (Int id) {this.id = id;} Public string getname () {return name;} Public void setname (String name) {this.name = name;} PUBLIC&NBSP;STRING&NBSP;GETADDR () {return addr;} PUBLIC&NBSP;VOID&NBSP;SETADDR (STRING&NBSP;ADDR) {this.addr = addr;} @Overridepublic string tostring () {return "person [id=" + id + ", name= " + name + ", addr= " + addr + "] ";}}
-
public void Finduserlistreturnresultmap () throws Exception {sqlsession sqlsession = sqlsessionfactory.opensession ( ); Usermapper usermapper = Sqlsession.getmapper (Usermapper.class); User user = new user (), User.setusername ("Anna"), User.setsex ("0"); list<person> list = usermapper.finduserlistretrunresultmap (user); System.out.println (List.size ());iterator<person> Iterator = List.iterator (); while (Iterator.hasnext ()) { Person person = iterator.next (); SYSTEM.OUT.PRINTLN (person);}}
Use of MyBatis Four (a) (Mapper.xml)