Explain in simple terms Mybatis (4) Use mapper to develop dao interface

There are some problems when developing dao in the original way in the previous section.

1. There is a lot of duplicate code.

2. The id of the incoming statement is hard-coded when calling the sqlsession method.

3. The variable passed in by calling the method of sqlsession is not conducive to development because sqlsession uses generics, even if an error is passed in, it will not report an error in the compilation stage.



mapper agent develops dao.

Just write mapper interface and mapper.xml mapping file.

The programmer only needs to write the specifications that the mapper interface needs to follow, and the mapper can automatically generate the proxy object of the mapper interface implementation class.

Development Specifications:

1. In mapper.xml, the namespace is equal to the address of the mapper interface.

2. The method name of the mapper interface is the same as the id of the statement in mapper.xml.

3. The input parameter type of the method in the mapper interface is the same as the type specified by parameterType in mapper.xml.

4. The return value type of the method in the mapper interface is the same as that of the resultType in mapper.xml.

Develop our own mapper according to this specification.

Create a mapper folder under the resource folder and create a userMapper.xml file under it.

Create a mapper interface.


Copy the contents of the userMapper under the original sqlmap folder, and change the namespace to the path of the mapper interface.


Then introduce this file in SqlMapConfig.xml


Create a new method for querying by id in the mapper interface.

public interface UserMapper {
    /**
    *   @author:kevin
    * @Description: Query users by id
    *   @Date:21:18 2018/3/28
    */
     User selectUser(int id) throws Exception;
}
<select id="selectUser" parameterType="int" resultType="com.beyond.mybatis.po.User">
        select * from user WHERE id = #{value}
    </select>

It can be seen by the above method. The method name and the id of sql are both selectUser, the input parameters of the method and the input parameters of sql are of type int, and the return value of the method and the return value of sql are both User.


Let's test this excuse.

public class UserMapperTest {
    private SqlSessionFactory factory;
    @Before
    public void loadFactory() throws Exception{
        String resource = "SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //create session factory
        factory = new SqlSessionFactoryBuilder().build(inputStream);
    }
    @Test
    public void testfindUserById() throws Exception{
        SqlSession sqlSession = factory.openSession();
        //Get the proxy object of UserMapper through reflection
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.selectUser(1);
        System.out.println(user);

    }
}

The rest of the methods can be written according to this method.

public interface UserMapper {
    /**
    *   @author:kevin
    * @Description: Query users by id
    *   @Date:21:18 2018/3/28
    */
     User selectUser(int id) throws Exception;

     /**
     *   @author:kevin
     * @Description: Query users based on user name fuzzy query
     *   @Date:21:18 2018/3/28
     */
    List<User> selectUserByName(String name) throws Exception;

    /**
     *   @author:kevin
     * @Description: insert user
     *   @Date:21:18 2018/3/28
     */
    void insertUser(User user) throws Exception;
    /**
     *   @author:kevin
     * @Description: delete user
     *   @Date:21:18 2018/3/28
     */
    void deleteUserById(int id) throws Exception;

    /**
     *   @author:kevin
     * @Description: update user
     *   @Date:21:18 2018/3/28
     */
    void updateUser(User user) throws Exception;

}

The test method is no longer demonstrated here, you can imitate the test of the query.

When there are multiple input parameters, you can wrap it into a pojo class, and then encapsulate the input parameters in this class.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325474757&siteId=291194637