Mybatis3如何实现接口式编程

2026-02-21 20:27:23

1、新建一个接口类EmployeeMapper:

package com.gwolf.dao;

import com.gwolf.bean.Employee;

public interface EmployeeMapper {

        public Employee queryEmpById(Integer id);

}

Mybatis3如何实现接口式编程

2、我们可以实现接口与xml文件绑定,在EmployeeMapper.xml的namespace中指定为EmployeeMapper 的全类名就可以实现接口与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="com.gwolf.dao.EmployeeMapper">

        <!-- 

                namespace:名称空间

                id:唯一标识

                resultType:返回值类型

                #{empId}:从传递过来的参数取出empId值

         -->

  <select id="selectEmployee" resultType="com.gwolf.bean.Employee">

    select emp_id empId,gender,emp_name empName,email,gender 

        from tbl_emp where emp_id = #{empId}

  </select>

</mapper>

Mybatis3如何实现接口式编程

3、然后把Mapper中的方法与xml的sql语句进行绑定,只需要把方法名称和sql语句的id名称一样即可:

<?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.gwolf.dao.EmployeeMapper">

        <!-- 

                namespace:名称空间

                id:唯一标识

                resultType:返回值类型

                #{empId}:从传递过来的参数取出empId值

         -->

  <select id="queryEmpById" resultType="com.gwolf.bean.Employee">

    select emp_id empId,gender,emp_name empName,email,gender 

        from tbl_emp where emp_id = #{empId}

  </select>

</mapper>

Mybatis3如何实现接口式编程

Mybatis3如何实现接口式编程

4、创建一个测试方法,首先获取接口的实现类对象:

        @Test

        public void test1() throws Exception {

                String resource = "mybatis-config.xml";

                InputStream inputStream = Resources.getResourceAsStream(resource);

                SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

                

                SqlSession sqlSession = sqlSessionFactory.openSession();

                

                try {

                        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);

                        

                }finally {

                        sqlSession.close();

                }

        }

Mybatis3如何实现接口式编程

5、调用接口的方法:

@Test

        public void test1() throws Exception {

                String resource = "mybatis-config.xml";

                InputStream inputStream = Resources.getResourceAsStream(resource);

                SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

                

                SqlSession sqlSession = sqlSessionFactory.openSession();

                

                try {

                        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);

                        

                        Employee employee = employeeMapper.getEmpById(1);

                        System.out.println(employee);

                }finally {

                        sqlSession.close();

                }

        }

Mybatis3如何实现接口式编程

6、我们虽然没有没有接口的实现类,但是mybatis会自动创建一个代理对象,代理对象会去执行增改查方法。

Mybatis3如何实现接口式编程

相关推荐
  • 阅读量:37
  • 阅读量:116
  • 阅读量:196
  • 阅读量:46
  • 阅读量:108
  • 猜你喜欢