Mybatis 之動態代理


使用Mybatis 開發Web 工程時,通過Mapper 動態代理機制,可以只編寫接口以及方法的定義。

 

  如下:

定義db.properties

driver=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
username=scott 
password=tiger

定義SqlMapConfig.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>
    <!--引入外部 db.properties-->
    <properties resource="db.properties"/>

    <!--配置Oracle 數據庫信息-->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/mapper/UserInfo.xml"/>
        <mapper resource="com/mapper/BatchCustomerOneToOne.xml"/>
        <mapper resource="com/mapper/BatchCustomerOneToMany.xml"/>
        <mapper resource="com/mapper/BatchCustomerManyToMany.xml"/>
        <mapper resource="com/mapper/DelayedLoading.xml"/>
        <mapper resource="com/service/impl/BatchCustomerMapper.xml"/>
    </mappers>
</configuration>

 

 

定義一個Mapper 接口:

package com.service.impl;

import com.entity.onetoonebyresultMap.Customer;

/**
 * @author 王立朝
 * @version 1.0
 * @description Mapper 動態代理類
 * * @date 2019/1/24
 **/
public interface BatchCustomerMapper {
    Customer findOneCustomerById(Integer integer);
}

 定義Customer 實體類

package com.entity.onetoonebyresultMap;

/**
 * @author 王立朝
 * @version 1.0
 * @description com.entity.onetoonebyresultMap
 * @date 2019/1/19
 **/
public class Customer {
    //用戶id
    private Integer cusId;
    //用戶名
    private String username ;
    //卡號
    private String acno ;
    //性別
    private String gender ;
    //聯系方式
    private String phone ;

    @Override
    public String toString() {
        return "Customer{" +
                "cusId=" + cusId +
                ", username='" + username + '\'' +
                ", acno='" + acno + '\'' +
                ", gender='" + gender + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }

    public Customer() {
    }

    public Integer getCusId() {
    
        return cusId;
    }

    public void setCusId(Integer cusId) {
        this.cusId = cusId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getAcno() {
        return acno;
    }

    public void setAcno(String acno) {
        this.acno = acno;
    }

    public String getGender() {
        return gender;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Customer(Integer cusId, String username, String acno, String gender, String phone) {
    
        this.cusId = cusId;
        this.username = username;
        this.acno = acno;
        this.gender = gender;
        this.phone = phone;
    }
}

定義BatchCustomerMapper.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.service.impl.BatchCustomerMapper">


    <select id="findOneCustomerById" parameterType="java.lang.Integer"
            resultType="com.entity.onetoonebyresultMap.Customer">
       select * from customer where cus_id = 4
    </select>

</mapper>

 

 編寫獲取SqlSession 會話的工具類  DataConnection.java

package com.util;

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 java.io.IOException;
import java.io.InputStream;

/**
 * @author 王立朝
 * @version 1.0
 * @description 獲取 SqlSession 會話對象
 * @date 2019/1/13
 **/
public class DataConnection {

    //mybatis 配置文件
    private String resources = "SqlMapConfig.xml";
    private SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession;

    public SqlSession getSqlSession() {
        try {
            InputStream inputStream = Resources.getResourceAsStream(resources);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            sqlSession = sqlSessionFactory.openSession();
            System.out.println("獲得連接");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sqlSession;
    }

    public static void main(String[] args) {
        DataConnection dataConnection = new DataConnection();
        dataConnection.getSqlSession();
    }
}

 

 

編寫單元測試 testBatchCustomerMapper.java

import com.entity.onetoonebyresultMap.Customer;
import com.service.impl.BatchCustomerMapper;
import com.util.DataConnection;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

/**
 * @author 王立朝
 * @version 1.0
 * @description PACKAGE_NAME
 * @date 2019/1/24
 **/
public class testBatchCustomerMapper {

    private static DataConnection dataConnection = new DataConnection();


    //測試Mapper 動態代理
    @Test
    public void testMapper(){

        SqlSession sqlSession = dataConnection.getSqlSession();

        BatchCustomerMapper batchCustomerMapper = sqlSession.getMapper(BatchCustomerMapper.class);
        Customer customer = batchCustomerMapper.findOneCustomerById(4);
        System.out.println("用戶信息為:"+ customer.getUsername()
                +" 性別為:"+ customer.getGender());
    }

}

 測試結果為:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM