1. First define an entity User type that receives the data, and a userquery type for the input parameter.
1 Public classUser {2 Public intID;3 PublicString username;4 PublicDate birthday;5 PublicString sex;6 PublicString address;7 }8 Public classUserQuery {9 PrivateString Orderbyclause;Ten PrivateList<criteria>Oredcriteria; One Public voidAddusernamelike (String username) { AOredcriteria.add (NewCriteria ("username like", username)); - } - Public voidAddsexgreaterthan (intvalue) { theOredcriteria.add (NewCriteria ("Sex >", value)); - } - Public classCriteria { - PrivateString Conditia; + PrivateObject value; - PublicCriteria (String Conditia, Object value) { + This. Conditia =Conditia; A This. Value =value; at } - } -}
2, now need to configure a more complex mapper mapping file
- Defines Resultmap, which is used to map entity properties to SQL fields. The final Pojo returned here is the User type.
- The ID represents the mapping of the primary key attribute and the field, if multiple primary keys are required to define multiple IDs;
- Result represents the mapping of other properties and fields;
- The SQL fragment is defined, such as: User_columns, User_where, so it can be referenced in any SQL script, and is easy to maintain;
- The ${alias} placeholder is defined in User_colums , and the value of the placeholder is defined when the fragment is referenced;
- User_where the SQL fragment used to define the query criteria, foreach is used to loop a collection that repeats the SQL statements within the section.
- The Oredcriteria value is the member name of UserQuery,
- The criteria is the private variable name in the statement.
- Separator= "and" means that the stitching statement is separated by an and.
- When you define select, UserQuery is used as an incoming parameter, and the return type uses the Resultmap type UserMap;
- Use include to introduce a SQL fragment, and you can set property values for others;
- Use if test to determine, satisfy the condition to use the IF section of the SQL statement;
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <!DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.3//en"3 "Http://mybatis.org/dtd/mybatis-3-mapper.dtd">4 <Mappernamespace= "Test">5 <Resultmaptype= "Cn.xleos.mybatis.po.User"ID= "UserMap">6 <IDcolumn= "id" Property= "id" />7 <resultcolumn= "username" Property= "username" />8 <resultcolumn= "Birthday" Property= "Birthday" />9 <resultcolumn= "Sex" Property= "Sex" />Ten <resultcolumn= "Address" Property= "Address" /> One </Resultmap> A <SQLID= "User_columns"> - ${alias}id, ${alias}username, ${alias}birthday, - ${alias}sex, ${alias}address, ${alias}attrs the </SQL> - <SQLID= "User_where"> - <where> - <foreachCollection= "Oredcriteria"Item= "Criteria"Separator= "and"> + (${criteria.condition} #{criteria.value}) - </foreach> + </where> A </SQL> at <SelectID= "Getuserbyquery"ParameterType= "Cn.xleos.mybatis.po.UserQuery" - Resultmap= "UserMap"> - SELECT - <includerefID= "User_columns"> - < Propertyname= "Alias"value="" /> - </include> in From user - <ifTest= "_parameter! = null"> to <includerefID= "User_where" /> + </if> - <ifTest= "Oredcriteria! = null"> the ORDER by ${orderbyclause} * </if> $ </Select>Panax Notoginseng </Mapper>
3, through the unit test to see how the results
The first thing to do is to create a userquery query Pojo object, and then add conditions and sort fields;
Since the return type is already specified in the Mapper mapping file, multiple records are returned as list<user> types;
1 Public classMybatismappertest {2 @Test3 Public voidqueryusertest () {4Sqlsession sqlsession =sqlsessionfactory.opensession ();5 Try {6UserQuery UserQuery =Newuserquery ();7Userquery.setorderbyclause ("Birthday");8Userquery.addusernamelike ("Zhang%");9 Tenlist<user> users = sqlsession.selectlist ("Test.getuserbyquery", userquery); One System.out.println (users); A}finally { - sqlsession.close (); - } the } -}
The resulting SQL script can be seen through the trace log as follows, resulting in the same as expected:
DEBUG [main]-==> preparing:select ID, username, birthday, sex, address from user WHERE (username like?) Order BY Birthday
DEBUG [main]-==> Parameters: Zhang% (String)
Third, MyBatis series: Mapper mapping uses POJO entities to receive data and incoming parameters