Two Mapper agent development

Two Mapper agent development

This article is the second section, and this column records the whole process of MyBatis learning
column directory

1 process

  1. Define the Mapper interface with the same name as the SQL mapping file, and place the Mapper interface and the SQL mapping file in the same directory

  2. Set the namespace attribute of the SQL mapping file to the fully qualified name of the Mapper interface

  3. Define the method in the Mapper interface, the method name is the id of the sql statement in the SQL mapping file, and keep the parameter type and return value type consistent

  4. coding

    • Obtain the proxy object of the Mapper interface through the getMapper method of SqlSession
    • Call the corresponding method to complete the execution of sql

detail: If the Mapper interface name and the SQL mapping file name are the same and are in the same directory, you can use package scanning to simplify the loading of the SQL mapping file

2 Concrete operation

  1. Define the interface and place the Mapper interface and the SQL mapping file in the same directory. ==Attention,==Do not use . to create a new folder in the resource folder here, but use /, although the display is the same, but the effect is different after compilation

image-20220814005527502

  1. Set the namespace attribute of the SQL mapping file to the fully qualified name of the Mapper interface

image-20220814005652768

  1. Define the method in the Mapper interface, the method name is the id of the sql statement in the SQL mapping file, and keep the parameter type and return value type consistent
package org.example.mapper;


import org.example.pojo.User;

import java.util.List;

public interface UserMapper {
    
    
    /**
     * 查询所有的用户信息
     *
     * @return 用户列表
     */
    List<User> selectAll();

}

Pay attention to changing the address information of the mapping file

image-20220814010020198

  1. coding
package org.example;


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.example.mapper.UserMapper;
import org.example.pojo.User;

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

//Mapper代理开发
public class MyBatisDemo2 {
    
    
    public static void main(String[] args) throws IOException {
    
    
//        1. 加载MyBatis的核心配置文件 获取SQLSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

//        2. 获取SQLSession对象 用它来执行SQL
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        3. 执行sql
//        List<User> users = sqlSession.selectList("test.selectAll");
//        3.1 获取UserMapper接口的代理对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.selectAll();
        System.out.println(users);
//        4. 释放资源
        sqlSession.close();
    }
}

Guess you like

Origin blog.csdn.net/qq_45842943/article/details/126356715