MyBatic与Spring的整合,Mapper接口方式的开发

本文摘自:Java EE企业级应用开发教程,有部分修改

在MyBatis+Spring项目中,虽然使用传统DAO的开发可以实现所需功能,但是采用这种方式在实现类中会出现大量重复代码,在方法中也需要指定映射文件中执行语句的Id,并且不能保证编写时的Id的正确性(运行时才能知道)。为此,我们可以使用MyBatis提供的另一种编程方式,Mapper接口编程。

本文假定你的MyBatis与Spring环境已经搭建完毕,数据结构及测试数据也已经导入数据库,实体类(Customer.java)也已经创建,参考:MyBatic与Spring的整合,传统DAO方式的开发

三、Mapper接口方式的开发

1、创建接口类

package com.itheima.mapper;
import com.itheima.po.Customer;
public interface CustomerMapper {
    // 通过id查询客户
    public Customer findCustomerById(Integer id);
    
    // 添加客户
    public void addCustomer(Customer customer);
}

2、创建映射文件

<?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.itheima.mapper.CustomerMapper">
    <!--根据id查询客户信息 -->
    <select id="findCustomerById" parameterType="Integer"
             resultType="customer">
        select * from t_customer where id = #{id}
    </select>
    
    <!--添加客户信息 -->
    <insert id="addCustomer" parameterType="customer">
        insert into t_customer(username,jobs,phone)
        values(#{username},#{jobs},#{phone})
    </insert>
</mapper>

注意:此文件与接口文件位于同一个目录。

3、在mybatis-config-spring.xml配置文件中,增加如下代码:

    <mappers> 
       <mapper resource="com/itheima/mapper/CustomerMapper.xml" />
    </mappers>

4、在applicationContext-mybatis.xml配置文件中,增加如下代码:

    <!-- Mapper代理开发(基于MapperFactoryBean) -->
    <bean id="customerMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.itheima.mapper.CustomerMapper" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
    </bean>

上述配置代码为MapperFactoryBean指定了接口以及SqlSessionFactory。

5、创建测试类

package com.itheima.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import 
     org.springframework.context.support.ClassPathXmlApplicationContext;
import com.itheima.mapper.CustomerMapper;
import com.itheima.po.Customer;

/**
 * DAO测试类
 */
public class DaoTest {
//    @Test
    public void findCustomerByIdMapperTest(){    
        ApplicationContext act = 
                new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
        CustomerMapper customerMapper = act.getBean(CustomerMapper.class);   
        Customer customer = customerMapper.findCustomerById(1);
        System.out.println(customer);
    }
    
    @Test
    public void addCustomerTest(){    
        ApplicationContext act = 
                new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
        CustomerMapper customerMapper = act.getBean(CustomerMapper.class);   
        Customer customer = new Customer();
        customer.setUsername("李咏");
        customer.setJobs("测试哦");
        customerMapper.addCustomer(customer);
    }
}

addCustomerTest方法输出:

DEBUG [main] - ==>  Preparing: insert into t_customer(username,jobs,phone) values(?,?,?) 
DEBUG [main] - ==> Parameters: 李咏(String), 测试哦(String), null
DEBUG [main] - <==    Updates: 1

findCustomerByIdMapperTest输出:

DEBUG [main] - ==>  Preparing: select * from t_customer where id = ? 
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <==      Total: 1
Customer(id=1, username=张三, jobs=测试工程师, phone=13099992222)

 

上一篇:一个简单的银行bank系统(运用了对象数组和链式编程)


下一篇:Spring Data JPA 通过不同的方式实现查询操作,Java大厂面试真题