Suppose we have a requirement: A comprehensive query of user information that requires a complex query condition (which may include user information, other information, such as goods, orders)
Our idea is that parametertype, which is passed into select, is a wrapper class that can have a lot of things.
We already have a User.java PO class, but the conditions we're querying may be more complex than the user class. In order to facilitate the extension, we will extend a usercustom extend user such a class.
The cases are as follows:
Give the case structure first:
The above example illustrates the following: Sqlmapconfig.xml is the total configuration file. Mapper/usermapper.xml is the configuration file for this case. User.java is the entity class for the database. Usercustom.java is the implementation class of User.java. Userqueryvo.java is a wrapper class. Usermapper.java is the Mapper interface class. Mybatis_mappertest.java is the test code.
The first step:
The Sqlmapconfig.xml code is as follows:
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE configurationpublic "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" ><Configuration> <!--The configuration of the properties must be written on top of typealiases - <PropertiesResource= "Db.properties"></Properties><!--Configure aliases -<typealiases> <!--define type for alias: path to type alias: Alias - <Typealiastype= "Cn.itcast.mybatis.po.User"alias= "User"/> </typealiases> <!--environments configuration will be abolished after the integration with spring - <Environmentsdefault= "Development"> <EnvironmentID= "Development"> <!--using JDBC Transaction management - <TransactionManagertype= "JDBC" /> <!--Database Connection Pool - <DataSourcetype= "Pooled"> < Propertyname= "Driver"value= "${jdbc.driver}" /> < Propertyname= "url"value= "${jdbc.url}" /> < Propertyname= "username"value= "${jdbc.username}" /> < Propertyname= "Password"value= "${jdbc.password}" /> </DataSource> </Environment> </Environments> <!--Load the mapping file (sqlmap/user.xml) into the Sqlmapconfig.xml - <mappers> <!--load the mapper.xml into the Sqlmapconfig.xml - <!--Follow some specifications: You need to keep the Mapper interface class name and the Mapper.xml mapping file name consistent and in one directory. The premise of this specification is: Use the Mapper proxy method. - <MapperResource= "Mapper/usermapper.xml"/> </mappers> </Configuration>
Step Two:
<? xml version= "1.0" encoding= "UTF-8" ?> <! doctype mapperpublic "-//mybatis.org//dtd Mapper 3.0//en" "http://mybatis.org/dtd/ Mybatis-3-mapper.dtd " > <!-- Nanmespace: namespace. The function is to classify the SQL management, understand SAL separation Note: Using Mapper Proxy method, namespace has a special important role --> Span style= "color: #0000ff;" >< mapper namespace = "Cn.itcast.mybatis.mapper.userMapper" > <!--
use rqueryvo has a usercustom inside, #{usercustom.sex} is written in Userqueryvo the properties inside ( Usercustom ) Properties (sex )
- < ID= "Finduserlist" parametertype= "Cn.itcast.mybatis.po.UserQueryVo" Resulttype = "Cn.itcast.mybatis.po.UserCustom" > SELECT * from USER WHERE user.sex= #{usercustom.sex} and User.username=#{usercustom.username} </ Select > </ Mapper >
Step Three:
User.java Code and Usercustom.java:
PackageCn.itcast.mybatis.po;Importjava.util.Date; Public classUser {Private intId//PRIMARY KeyPrivateString username;//the name of the userPrivateDate birthday;//BirthdayPrivateString sex;//SexPrivateString address;//Address Public intgetId () {returnID;} Public voidSetId (intID) { This. ID =ID;} PublicString GetUserName () {returnusername;} Public voidSetusername (String username) { This. Username =username;} PublicDate Getbirthday () {returnbirthday;} Public voidsetbirthday (Date birthday) { This. Birthday =birthday;} PublicString Getsex () {returnsex;} Public voidsetsex (String sex) { This. Sex =sex;} PublicString getaddress () {returnaddress;} Public voidsetaddress (String address) { This. Address =address;}}
Package Cn.itcast.mybatis.po; Public class extends User {}
Fourth step: Write Userqueryvo.java Packaging class
Package Cn.itcast.mybatis.po; Public class Userqueryvo { // User's query conditions private usercustom usercustom; Public Usercustom Getusercustom () { return usercustom;} Public void Setusercustom (Usercustom usercustom) { this. usercustom = Usercustom;}}
Fifth step: Usermapper.java is the Mapper interface class
PackageCn.itcast.mybatis.mapper;Importjava.util.List;ImportCn.itcast.mybatis.po.User;ImportCn.itcast.mybatis.po.UserCustom;ImportCn.itcast.mybatis.po.UserQueryVo; Public InterfaceUsermapper {//the name (Finduserbyid) is identical to the ID inside the usermapper.xml.//Note that the returned list is written here. SelectList is called inside the code, but in Usermapper.xml it's written//resulttype= "Cn.itcast.mybatis.po.User"/** <select id= "finduserlist" parametertype= "Cn.itcast.mybatis.po.UserQueryVo" resulttype= " Cn.itcast.mybatis.po.UserCustom "> select * from USER WHERE user.sex= #{usercustom.sex} and User.username=#{userc Ustom.username} </select> * **/ PublicList<usercustom>finduserlist (Userqueryvo userqueryvo);}
Sixth step:
Write the Mybatis_mappertest.java test code.
PackageCn.itcast.mybatis.first;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.util.Date;Importjava.util.List;Importorg.apache.ibatis.io.Resources;Importorg.apache.ibatis.session.SqlSession;Importorg.apache.ibatis.session.SqlSessionFactory;ImportOrg.apache.ibatis.session.SqlSessionFactoryBuilder;ImportOrg.junit.Before;Importorg.junit.Test;ImportCn.itcast.mybatis.Dao.UserMapper;ImportCn.itcast.mybatis.mapper.userMapper;ImportCn.itcast.mybatis.po.User;ImportCn.itcast.mybatis.po.UserCustom;ImportCn.itcast.mybatis.po.UserQueryVo; Public classMybatis_mappertest {Privatesqlsessionfactory sqlsessionfactory; @Before Public voidSetup ()throwsIOException {String resource= "Sqlmapconfig.xml"; InputStream InputStream=Resources.getresourceasstream (Resource); //The main generation is sqlsessionfactory. This. sqlsessionfactory=NewSqlsessionfactorybuilder (). Build (InputStream); } @Test Public voidTestmaper () {sqlsession sqlsession=NULL; Sqlsession=sqlsessionfactory.opensession (); //Generate proxy classUsermapper Usermapper=sqlsession.getmapper (Usermapper.class); //Create wrapper objects, set query criteriaUserqueryvo userqueryvo=NewUserqueryvo (); Usercustom Usercustom=NewUsercustom (); Usercustom.setsex ("1"); Usercustom.setusername (King); //Set query criteria for a wrapper classUserqueryvo.setusercustom (Usercustom); Usermapper.finduserlist (USERQUERYVO); }}
Result: normal operation.
14mybatis_ Input mappings (passing Pojo wrapper objects)--very important