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()); } }
测试结果为:
阳光总在风雨后!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了