Implementation principles and specifications
Mapper interface Dynamic Proxy method requires the manual writing of the Mapper interface, the MyBatis framework creates an interface dynamic proxy object based on the interface definition, and the method body of the proxy object implements the method defined in the Mapper interface.
The following specifications are required to use the Mapper interface:
1. The namespace in the Mapper.xml file is the same as the classpath of the Mapper interface
2. The Mapper interface method name is the same as the ID of each statement defined in the Mapper.xml
3. The input parameter type of the Mapper interface method is the same as the type of parametertype for each SQL defined in Mapper.xml
4. The output parameter type of the Mapper interface method is the same as the type of resulttype for each SQL defined in Mapper.xml
Writing Mapper.xml mapping files
Define mapper mapping file Productmapper.xml, need to modify namespace value to Productmapper interface path
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd "><!--namespace: The format of the package name + file name is used here -<Mappernamespace= "Com.sl.mapper.ProductMapper"> <!--Returns a custom type note selectallproduct Returns a collection, in which case resulttype is the type that the collection contains, not the collection itself - <SelectID= "Selectallproduct"Resulttype= "Com.sl.po.Product">SELECT * from Products</Select> <SelectID= "Selectproductsbyvo"Resulttype= "Com.sl.po.Product">SELECT * from Products<where> <ifTest= "Product.citycode!=null">and Citycode = #{product.citycode}<!--citycode = #{citycode} - </if> <ifTest= "Product. Name!=null ">and name like #{product. Name}</if> <ifTest= "Product. Description!=null ">and description like #{product. Description}</if> </where> </Select> </Mapper>Writing the Mapper.java interface
Interface Definition Note points:
1. The Mapper interface method name is the same as the ID of the statement defined in the Mapper.xml
2. The input parameter type of the Mapper interface method is the same as the ParameterType type of the statement defined in the Mapper.xml
3. The output parameter type of the Mapper interface method is the same as the resulttype of the statement defined in the Mapper.xml
Package Com.sl.mapper; Import java.util.List; Import com.sl.po.Product; Import Com.sl.po.ProductVo; Public Interface productmapper { List<Product> selectallproduct (); List<Product> selectproductsbyvo (Productvo vo); }
View Code
PackageCom.sl.po; Public classProductvo {Private intcategory; Privateproduct product; Public intgetcategory () {returncategory; } Public voidSetcategory (intcategory) { This. Category =category; } PublicProduct getproduct () {returnproduct; } Public voidsetproduct (product product) { This. Product =product; } }View CodeRegistering the Mapper.xml configuration file (or Mapper.java interface)
To modify the Sqlmapconfig.xml file:
<mappers> <!--Registering productmapper.xml files - <MapperResource= "Mapper/productmapper.xml"></Mapper> <!--mapper.xml file and mapper interface may not be under one package - <!--registering the Mapper interface - <!--<mapper class= "Com.sl.mapper.ProductMapper" ></mapper> - <!--by registering Mapper interface mode: Mapper interface and Mapper.xml must be under the same package - </mappers>
Test method
//Mapper Interface Dynamic proxy mode Public classtestproductmapperclient {//Defining session Sqlsessionsqlsession session =NULL; @Before Public voidInit ()throwsIOException {//defining the Mabatis global configuration fileString resource = "Sqlmapconfig.xml"; //load MyBatis Global configuration file//InputStream InputStream =//TestClient.class.getClassLoader (). getResourceAsStream (Resource);InputStream InputStream=Resources.getresourceasstream (Resource); Sqlsessionfactorybuilder Builder=NewSqlsessionfactorybuilder (); Sqlsessionfactory Factory=Builder.build (InputStream); //generate session sqlsession based on SqlsessionfactorySession =factory.opensession (); } //Select by ID//@Test Public voidtestselectproduct () {//gets the proxy object for the Mapper interfaceProductmapper productmapper = Session.getmapper (productmapper.class); List<Product> listproduct =productmapper.selectallproduct (); for(product product:listproduct) {System.out.println (product); } //Close SessionSession.close (); } @Test Public voidtestwheretest () {Product Product=NewProduct (); Product.setcitycode ("A01"); Product.setname ("% international"); //product.setdescription ("%xx%"); //Product.setunitprice (new BigDecimal);Productvo vo =NewProductvo (); Vo.setproduct (product); Productmapper Productmapper= Session.getmapper (productmapper.class); List<Product> listproduct =Productmapper.selectproductsbyvo (VO); for(Product pro:listproduct) {System.out.println (pro); } //Close SessionSession.close (); }}View Code
The dynamic proxy object calls Sqlsession.selectone () and sqlsession.selectlist () to implement the database operation, which is determined based on the return value of the Mapper interface method. If a list is returned, the SelectList method is called, and if a single object is returned, the SelectOne method is called.
Because the parameter type is parametertype configured in the Mapper.xml configuration file, the interface method in Mapper.java has only one parameter, and the type is the same as configured in Mapper.xml (Mapper.xml can omit the parameter type configuration).
MyBatis Learning Series (iv) Mapper interface dynamic Agent