1. 程式人生 > 其它 >MyBatic與Spring的整合,Mapper介面方式的開發

MyBatic與Spring的整合,Mapper介面方式的開發

本文摘自:JavaEE企業級應用開發教程,有部分修改

在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)