[mybatis]接口式编程

Posted 唐火

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[mybatis]接口式编程相关的知识,希望对你有一定的参考价值。

While this approach works, and is familiar to users of previous versions of MyBatis, there is now a
cleaner approach. Using an interface (e.g. BlogMapper.class) that properly describes the parameter
and return value for a given statement, you can now execute cleaner and more type safe code, without
error prone string literals and casting.

小结

  • 1.推荐使用接口式编程

原生:Dao->DaoImpl

mybatis:Mapper->xxMapper.xml

  • 2.SqlSession代表和数据库的一次会话;用完必须关闭;

  • 3.SqlSession和connection一样,它都是非线程安全,每次使用都应该去获取新的对象

  • 4.mapper接口没有实现类,但是mybatis会为这个接口生成一个代理对象

(将接口和xml进行绑定)

EmployeeMapper empMapper = sqlSession.getMapper(EmployeeMapper.class);
  • 5.两个重要的配置文件

mybatis的全局配置文件;包含数据库连接池信息,事务管理器信息等…系统运行环境信息

sql映射文件;保存了每一个sql语句的映射信息;将sql抽取出来

process

项目结构:

pom.xml引入依赖:

    <dependencies>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>


        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.22</version>
        </dependency>



    </dependencies>

Employee实体类:

package com.atguigu.mybatis.bean;



public class Employee 

    private Integer id;
    private String lastName;
    private String email;
    private String gender;

    @Override
    public String toString() 
        return "Employee" +
                "id=" + id +
                ", lastName='" + lastName + '\\'' +
                ", email='" + email + '\\'' +
                ", gender='" + gender + '\\'' +
                '';
    

    public Employee() 
    

    public Employee(Integer id, String lastName, String email, String gender) 
        this.id = id;
        this.lastName = lastName;
        this.email = email;
        this.gender = gender;
    

    public Integer getId() 
        return id;
    

    public void setId(Integer id) 
        this.id = id;
    

    public String getLastName() 
        return lastName;
    

    public void setLastName(String lastName) 
        this.lastName = lastName;
    

    public String getEmail() 
        return email;
    

    public void setEmail(String email) 
        this.email = email;
    

    public String getGender() 
        return gender;
    

    public void setGender(String gender) 
        this.gender = gender;
    

EmployeeMapper:

package com.atguigu.mybatis.dao;

import com.atguigu.mybatis.bean.Employee;

public interface EmployeeMapper 


    public Employee getEmpById(Integer id);

    


配置文件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>
    <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://localhost:3306/mybatis?serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mybatis/mapper/EmployeeMapper.xml"/>
    </mappers>
</configuration>

映射文件EmployeeMapper:

<?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="com.atguigu.mybatis.dao.EmployeeMapper">



    <select id="getEmpById" resultType="com.atguigu.mybatis.bean.Employee">

    select id,last_name lastName,email,gender from tb1_employee where id = #id

</select>
</mapper>

测试类:

package com.atguigu.mybatis.test;

import com.atguigu.mybatis.bean.Employee;
import com.atguigu.mybatis.dao.EmployeeMapper;
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.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;

public class MybatisTest 

    public SqlSessionFactory getSqlSessionFactory() throws IOException 
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(inputStream);

    


    @Test
    public void test01() throws IOException 
        //1.获取sqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();

        //2.获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();


        try 
            //3.获取接口的实现类对象
            //会为接口自动的创建一个代理对象,代理对象执行增删改查
            EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);

            Employee empById = mapper.getEmpById(1);

            System.out.println(mapper.getClass());

            System.out.println(empById);
        finally 
            sqlSession.close();
        

    



以上是关于[mybatis]接口式编程的主要内容,如果未能解决你的问题,请参考以下文章

[mybatis]接口式编程

Mybatis面向接口式编程

MyBatis接口式编程

mybatis学习——接口式编程

MyBatis接口式编程(SqlSession直接调用API向接口转变)

接口式编程小结