Mybatis study summary (two) - Mapper proxy Development

I. Summary

1, the development of the original DAO problems:
(1) the presence of many processes to achieve DAO of the code of the method body.
(2) call SqlSession method (select / insert / update) need to specify the id Statement, there are hardcoded, is not conducive to code maintenance.

2, Mapper dynamic proxy method : the programmer only needs to write dao Interface (Mapper), without the need to write dao implementation classes, defined by the mybatis generated interface proxy object classes and interfaces according dao the mapping file statement.

3 goals: through a number of rules for mybatis generated interface proxy object definition dao interfaces and mapping files in the statement.

Second, the development of norms

1, in the namespace XXXmapper.xml equal mapper interface address (i.e., the same class path mapper.java namespace interface mapper.xml file).


2, consistent with the methods and interfaces XXXmapper.java mapper.xml, the statement of Id.
3. The method mapper.java input interface and the same parameters as in the statement of mapper.xml parameterType specified type.
4, the return value of the same type and mapper.xml resultType statement of the type specified method mapper.java interface.

Note: The above specification developed mainly for the following code to generate a unified

SqlSession sqlSession = sqlSessionFactory.openSession();
User user = sqlSession.selectOne("test.findUserById", id); 
......

 Three, UserMapper.java class code (the interface file)

Copy the code
com.mybatis.mapper Package; 

Import java.util.List; 

Import com.mybatis.entity.User; 

/ ** 
 * User Interface Manager mapper 
 * @author LXX 
 * 
 * / 
public interface UserMapper { 
    
    / ** query information based on user ID * / 
    public findUserById the user (the above mentioned id int); 

    / ** fuzzy query based on user name user information * / 
    public List <the user> findUserByName (String username); 

    / ** add user * / 
    public void insertUser (the user the user); 

    / * * delete The user ID * / 
    public void deleteUser (ID Integer); 

    / ** The updated user ID * / 
    public void the updateUser (the user user); 

}
Copy the code

Fourth, the original User.xml copy and change the name to UserMapper.xml, then UserMapper.xml file namespace instead mapper interface address

<-! Namespace namespace, sql role is to classify the management, understanding isolation for sql 
    Note: When using the mapper agent development, namespace have a special role 
 -> 
<mapper namespace = "com.mybatis.mapper.UserMapper" >

NOTE: namespace = mapper interface addresses
five, the loading UserMapper.xml in SqlMapConfig.xml

<!-- 加载映射文件 -->
    <mappers>
        <mapper resource="com/mybatis/mapping/User.xml"/>
        <mapper resource="com/mybatis/mapping/UserMapper.xml"/>
    </mappers>

Six, JUnit test UserMapperTest.java

Copy the code
package com.mybatis.test;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import com.mybatis.entity.User;
import com.mybatis.mapper.UserMapper;

public class UserMapperTest {

    private SqlSessionFactory sqlSessionFactory;

    // 此方法是在执行@Test标注的方法之前执行
    @Before
    public void setUp() throws Exception {
        String resource = "SqlMapConfig.xml";
        InputStream = Resources.getResourceAsStream the InputStream (Resource); 
        // Create SqlSessionFcatory 
        SqlSessionFactory the SqlSessionFactoryBuilder new new = () Build (inputStream);. 
    } 
    
    @Test 
    public void testFindUserById () { 
        the SqlSession SQLSESSION sqlSessionFactory.openSession = (); 
        // create Usermapper objects, automatically generating a proxy object mapper mybatis 
        UserMapper = sqlSession.getMapper mapper (UserMapper.class); 
        the User User mapper.findUserById = (. 1); 
        System.out.println (User); 
        sqlSession.close (); 
    } 

    @Test 
    public void testFindUserByName ( ) { 
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // create Usermapper objects, mybatis automatically generated proxy object mapper 
        UserMapper mapper = sqlSession.getMapper (UserMapper.class); 
        List <the User> List = mapper.findUserByName ( "small"); 
        System.out.println (List); 
        SQLSESSION. Close (); 
    } 
    
    @Test 
    public void testInsertUser () { 
        the SqlSession SQLSESSION sqlSessionFactory.openSession = (); 
        // create Usermapper objects, mybatis mapper proxy object automatically generating 
        the User User new new = the User (); 
        user.setUsername ( "Xiaodong" ); 
        user.setSex ( ". 1"); 
        user.setAddress ( "Tianjin"); 
        user.setBirthday (new new a Date ()); 
        UserMapper Mapper = sqlSession.getMapper (UserMapper.class);
        mapper.insertUser (User); 
        sqlSession.commit (); 
        sqlSession.close (); 
    } 
    } 
    @Test
    
    @Test
    void testUpdateUser public () { 
        the SqlSession SQLSESSION sqlSessionFactory.openSession = (); 
        // create Usermapper objects, mybatis mapper proxy object automatically generating 
        the User User new new = the User (); 
        user.setId (2); // set must Id 
        user.setUsername ( "Liu"); 
        user.setSex ( "1"); 
        user.setAddress ( "Beijing Haidian"); 
        user.setBirthday (new new a Date ()); 
        UserMapper Mapper = sqlSession.getMapper (UserMapper.class); 
        Mapper. the updateUser (User); 
        sqlSession.commit (); 
        sqlSession.close (); 
    
    public void testDeleteUser () { 
        the SqlSession SQLSESSION sqlSessionFactory.openSession = (); 
        // create Usermapper objects, mybatis automatically generated proxy object mapper 
        UserMapper = sqlSession.getMapper mapper (UserMapper.class); 
        mapper.deleteUser (. 3); 
        sqlSession.commit ( ); 
        sqlSession.close (); 
    } 

}
Copy the code

     So this sqlSession can automatically create a proxy object mapper interface! We just need to have just written bytecode object mapper pass getMapper interface class method, you can get a proxy object corresponding to this interface, then we can use this proxy interface object to manipulate a specific method.
  Here, the use of proxy mapper way to develop dao summed over, but there is little detail, because the parameters mapper interface method to specify the mapping file parameterType, and parameterType only one, so the mapper interface parameters of all methods only one! If we want to pass that two or more parameters of the Zezheng? This is no way, want to pass multiple parameters or give up on this heart of it, but you can solve this problem, is to enhance the incoming objects, so that objects contain parameters to pass in that we need. This is regarded as a small drawbacks it, but will not affect our development.

VII Summary

1, developed by the mapper proxy Just write two:

(1)mapper.xml

(2) mapper Interface

2, Mapper interface development need to follow the following specifications:

Same as (1) in the namespace mapper interface Mapper.xml file path.
The same for each statement (2) Mapper interface methods defined names and Mapper.xml id. 
ParameterType same type for each input parameter type and sql mapper.xml (3) Mapper interface methods defined.
ResultType same type for each type and output parameters sql mapper.xml (4) Mapper interface methods defined.

3, Acting internal object calls selectOne () and selectList ():

If the object returns internal mapper pojo single object (a collection of non-object) through a proxy object selectOne querying the database, if the method returns a collection object mapper, selectList internal proxy object database query.

4, method parameters mapper interface can have only one affect systems development, mapper interface method parameter can have only one system is not conducive to the maintenance?
Answer: system frame, the code dao layer is common to the business layer. mapper interface has only one parameter, you can use the package to meet the needs of different types of pojo business methods.

 Original Address: https://www.cnblogs.com/xiaoxi/p/6173534.html

Guess you like

Origin www.cnblogs.com/cdchencw/p/12303560.html