Mybatis 之动态代理

时间:2023-03-09 19:32:03
Mybatis 之动态代理

使用Mybatis 开发Web 工程时,通过Mapper 动态代理机制,可以只编写接口以及方法的定义。

Mybatis 之动态代理

如下:

定义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>

Mybatis 之动态代理

编写获取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());
} }

测试结果为:

Mybatis 之动态代理