preface

When using Mybatis, we obtain SQL statements to be executed by calling XML files, for example: User user = (User) sqlSession.selectOne(“cn.ddnd.www.Entity.User.getUser”, “[email protected]”); . This method calls the SQL statement defined by the tag through a string. First, it is error prone, and second, when the ID in the XML is changed, you don’t know how many places in the program the ID is used, so you need to manually change it. Later, Mybatis introduced Mapper dynamic proxy mode, which only needs to write Mapper interface (equivalent to Dao layer), and Mybatis framework creates dynamic proxy object of interface according to interface definition.

Mapper interface specification

  1. The namespace in mapper. XML is the same as the classpath in the mapper. Java interface, that is<mapper namespace="cn.ddnd.www.Dao.User">The corresponding iscn.ddnd.www.DaoUnder the bagUserClass.
  2. Mapper. In XMLselectThe ID must be the same as the name of the class method in the Mapper. Java interface, i.e<select id="getUser" parameterType="String" resultType="User">thegetUserandpublic User getUser(String email);thegetUserMethod names correspond.
  3. Mapper. In XMLparameterTypeThe type of the Mapper interface methodIncoming parameter typeThe same.
  4. Mapper. In XMLresultTypeThe type of the Mapper interface methodReturn parameter typeThe same.

The implementation process

Configuration Mapper XML

IUser. XML:

<? 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">

<mapper namespace="cn.ddnd.www.Dao.IUser">
    <select id="getUser" parameterType="String" resultType="User">
        select * from user where email = #{email}
    </select>
</mapper>
Copy the code

Configure the Mapper. Java interface

IUser. Java:

package cn.ddnd.www.Dao;

import cn.ddnd.www.Entity.User;

public interface IUser {
    public User getUser(String email);
}


Copy the code

Write the Mybatis configuration file

Mybatis – config. XML:

<? xml version="1.0" encoding="UTF-8"? > <! DOCTYPE configuration PUBLIC"- / / mybatis.org//DTD Config / 3.0 / EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <typeAliases>
        <typeAlias type="cn.ddnd.www.Entity.User" alias="User"></typeAlias>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="JDBC: mysql: / / 127.0.0.1:3306 / spring? serverTimezone=GMT%2B8" />
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="cn/ddnd/www/Dao/IUser.xml"></mapper>
    </mappers>
</configuration>
Copy the code

The test class

Test. Java:

import cn.ddnd.www.Dao.IUser;
import cn.ddnd.www.Entity.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.apache.ibatis.io.Resources;

import java.io.Reader;
import java.io.IOException;


public class test {
    private static Reader reader;
    private static SqlSessionFactory sqlSessionFactory;

    static{
        try{
            reader = Resources.getResourceAsReader("Mybatis-config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    @Test
    public void a() throws IOException {
        SqlSession sqlSession = sqlSessionFactory.openSession();

        try{
            IUser IUser = (IUser) sqlSession.getMapper(IUser.class);
            User user = IUser.getUser("[email protected]");
            System.out.println("The user's email address is:" + user.getEmail() + ", the user name is: + user.getName() + ", the user password is:+ user.getPassword()); }finally { sqlSession.close(); }}}Copy the code

IUser IUser = (IUser) sqlSession.getMapper(IUser.class); SqlSession will help us generate an implementation class (for the IUser interface) that will fetch the proxy object for the IUser interface. User user = IUser.getUser(“[email protected]”); Proxy object methods.

From: DDND. Cn / 2018/11/30 /…