12_ Complex query 01_mapper proxy implementation

Source: Internet
Author: User

Engineering

"Code Implementation"

"User.java"

 Packagecom. Higgin.Mybatis.po;Importjava.util.Date; Public classUser {//property name corresponds to database table field        Private intID; PrivateString username; PrivateString sex; PrivateDate birthday; PrivateString address; //the corresponding get, set method}

"Usercustom.java"

 Package com. Higgin.Mybatis.po; /* information that can be used to extend a user */  Public class extends user{      // If you need to be able to increase the details              of the expansion here }

"Userqueryvo.java"

 Packagecom. Higgin.Mybatis.po;/*** Requirements: * Complete user information of the aggregate query, the need for incoming query conditions are complex (may also include user information, other information, such as goods, orders, etc.) * for the above requirements, we recommend that you use a custom wrapper type of Pojo * in the wrapper class to wrap the complex query conditions in*/ Public classUserqueryvo {//The required query conditions are packaged here//User Query Criteria    PrivateUsercustom Usercustom; This corresponds to the Usercustomer variable on the top. PublicUsercustom Getusercustom () {returnUsercustom; }     Public voidSetusercustom (Usercustom usercustom) { This. Usercustom =Usercustom; }}

"Usermapper.xml"//configuration is VERY important!!!!! , the namespace must correspond to the corresponding interface name:com. Higgin.Mybatis.mapper.UserMapper

 <Mappernamespace= "com. Higgin.Mybatis.mapper.UserMapper ">    <!--Comprehensive query for user information (complex query) #{usercustom.sex}: Remove the "gender" value from the Pojo wrapper object #{usercusotm.username}: Remove the user name from the Pojo wrapper class  -     <SelectID= "Finduserlist"ParameterType= "com. Higgin.Mybatis.po. Userqueryvo"Resulttype= "com. Higgin.Mybatis.po. usercustom">SELECT * from USER WHERE user.sex =#{usercustom.sex} and user.username like '%${usercustom.usern ame}% ' </Select> </Mapper>

"Usermapper.java" interface, methods in the Usermapper.java interface: method names in Finduserlist and Usermapper.xml are consistent

 Package com. Higgin.Mybatis.mapper; Import java.util.List; Import com. Higgin.Mybatis.po.User; Import com. Higgin.Mybatis.po.UserCustom; Import com. Higgin.Mybatis.po.UserQueryVo;  Public Interface usermapper {        // User Information Comprehensive query finduserlistpublic    throws Exception;
}

"Sqlmappercongfg.xml"//Add a load file, this file will be loaded in the test class

<!---    <mappers>        <  Resource = "Sqlmap/user.xml" />        <  resource= "Mapper/usermapper.xml"/>    </  Mappers>

"Usermappertest.java"//test class

 Public classUsermappertest {Privatesqlsessionfactory sqlsessionfactory; @Before Public voidSetUp ()throwsException {//Create Sqlsessionfactory//mybatis configuration fileString resource= "Sqlmapconfig.xml"; //get configuration file streamInputStream inputstream=Resources.getresourceasstream (Resource); //creating a session factory, passing in MyBatis profile informationsqlsessionfactory=NewSqlsessionfactorybuilder (). Build (InputStream); }/*** Comprehensive complex query of user information *@throwsException*/@Test Public voidTestfinduserlist ()throwsException {sqlsession sqlsession=sqlsessionfactory.opensession (); //creates a Usermapper object that MyBatis automatically generates mapper proxy objectsUsermapper Usermapper=sqlsession.getmapper (Usermapper.class); //Create wrapper objects, set query criteriaUserqueryvo userqueryvo=NewUserqueryvo (); Usercustom Usercustom=NewUsercustom (); Usercustom.setsex ("1"); Usercustom.setusername ("6");                Userqueryvo.setusercustom (Usercustom); //methods for calling UsermapperList<usercustom> list=usermapper.finduserlist (USERQUERYVO);    System.out.println (List.size ()); }}

"Run Results"

"Debug list data, all genders are" 1 ", Username contains" 6 "Data sets"

12_ Complex query 01_mapper proxy implementation

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.