Implementation of Mybatis through Mapper Agent Custom Interface in java Application Development

  • 2021-11-14 05:33:54
  • OfStack

How to achieve it? It is mainly divided into the following two steps

1. Implement interface definition through Mapper agent 2. Write Mapper. xml corresponding to the method

1. Custom interface AccountRepository


package repository;
import entity.Account; 
import java.util.List;
public interface AccountRepository {
    public int save(Account account);
    public int update(Account account);
    public int deleteById(long id);
    public List<Account> findAll();
    public Account findById(long id);
}

2. Create the corresponding Mapper. xml, and define the SQL statement corresponding to the interface method.

The statement tag can select insert, delete, update and select according to the business performed by SQL. The MyBatis framework dynamically creates proxy objects following implementation classes according to rules

Rules:

In Mapper. xml, namespace is the general class name followed by. id of statement in Mapper. xml is the corresponding normal name in. Mapper. Parameter types of parameterType of statement in xml and corresponding method in connection. Mapper. The return value type of resultType of statement in xml and the corresponding method in statement.

<?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="repository.AccountRepository">
    <insert id="save" parameterType="entity.Account">
        insert into t_account(username,password,age) values (#{username},#{password},#{age});
    </insert>
    <update id="update" parameterType="entity.Account">
        update t_account set username=#{username},password=#{password},age=#{age} where id=#{id};
    </update>
    <delete id="deleteById" parameterType="long">
        delete from t_account where id=#{id};
    </delete>
    <select id="findAll" resultType="entity.Account">
        select * from t_account;
    </select>
    <select id="findById" parameterType="long" resultType="entity.Account">
        select * from t_account where id =#{id};
    </select>
</mapper>

3. Register AccountRepository. xml in config. xml


<mappers>
        <mapper resource="mapper/AccountMapper.xml"></mapper>
        <mapper resource="repository/AccountRepository.xml"></mapper>
    </mappers>

4. Call the next proxy object to complete the related business operation


package Test;
import entity.Account;
import org.apache.ibatis.io.ResolverUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import repository.AccountRepository;
import java.io.InputStream;
import java.util.List;
public class Test1 {
    public static void main(String[] args) {
        InputStream inputStream= ResolverUtil.Test.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AccountRepository accountRepository=sqlSession.getMapper(AccountRepository.class);
//        List<Account> list=accountRepository.findAll();
//        for(Account account:list){
//            System.out.println(account);
//        }
//        sqlSession.close();
        // Add 
//        Account account=new Account(3L,"wangwu","555555",122);
//        accountRepository.save(account);
//        sqlSession.commit();
        // Pass id Find an object 
//        Account account=accountRepository.findById(3L);
//        System.out.println(account);
//        sqlSession.close();
        // Pass id Change object 
//        Account account=accountRepository.findById(2L);
//        account.setUsername("alibaba");
//        account.setPassword("12345678");
//        account.setAge(11);
//        int result=accountRepository.update(account);
//        sqlSession.commit();
//        System.out.println(result);
//        sqlSession.close();
        // Delete 
        int result=accountRepository.deleteById(3L);
        sqlSession.commit();
        System.out.println(result);
        sqlSession.close();
     }
}

The above is the java application development of Mybatis through Mapper agent custom interface implementation details, more about Mybatis through Mapper agent custom interface information please pay attention to other related articles on this site!


Related articles: