使用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());
} }

测试结果为:

Mybatis 之动态代理的更多相关文章

  1. (十二)mybatis之动态代理

    mybatis之动态代理的应用 在前文(https://www.cnblogs.com/NYfor2018/p/9093472.html)我们知道了,Mybatis的使用需要用到Mapper映射文件, ...

  2. Mybatis mapper动态代理的原理详解

    在开始动态代理的原理讲解以前,我们先看一下集成mybatis以后dao层不使用动态代理以及使用动态代理的两种实现方式,通过对比我们自己实现dao层接口以及mybatis动态代理可以更加直观的展现出my ...

  3. MyBatis学习(三)MyBatis基于动态代理方式的增删改查

    1.前言 上一期讲到MyBatis-Statement版本的增删改查.可以发现.这种代码写下来冗余的地方特别多.写一套没啥.如果涉及到多表多查询的时候就容易出现问题.故.官方推荐了一种方法.即MyBa ...

  4. 【转载】由浅入深分析mybatis通过动态代理实现拦截器(插件)的原理

    转自:http://zhangbo-peipei-163-com.iteye.com/blog/2033832?utm_source=tuicool&utm_medium=referral 我 ...

  5. Mybatis使用动态代理实现拦截器功能

    1.背景介绍 拦截器顾名思义为拦截某个功能的一个武器,在众多框架中均有“拦截器”.这个Plugin有什么用呢?或者说拦截器有什么用呢?可以想想拦截器是怎么实现的.Plugin用到了Java中很重要的一 ...

  6. JAVA框架 Spring 和Mybatis整合(动态代理)

    一.使用传统方式的dao的书写方式,不建议.目前采用的是动态代理的方式交给mybatis进行处理. 首先回顾下动态代理要求: 1)子配置文件的中,namespace需要是接口的全路径,id是接口的方法 ...

  7. spring如何管理mybatis(一) ----- 动态代理接口

    问题来源 最近在集成spring和mybatis时遇到了很多问题,从网上查了也解决了,但是就是心里有点别扭,想看看到底怎么回事,所以跟了下源码,终于发现了其中的奥妙. 问题分析 首先我们来看看基本的配 ...

  8. Spring 整合Mybatis Mapper动态代理方法

    先看项目目录结构 很清爽了 最重要的Spring的核心配置文件,看一下 <?xml version="1.0" encoding="UTF-8"?> ...

  9. Mybatis Mapper动态代理方式 typeAliases 别名的使用

    目录结构及配置文件与原始dao方法相比更简便 只需一个UserMapper的接口,放在一起的配置文件,配置文件中namespace的地址确定jdk动态代理的对象 <?xml version=&q ...

随机推荐

  1. divmod()

    divmod() 接收两个数值,然后以元组的形式返回这两个数值的商和余数 In [1]: divmod(5, 2) Out[1]: (2, 1) In [2]: divmod(10, 7) Out[2 ...

  2. docker rmi 详解

    docker rmi 用于删除指定的镜像,常见用法如下: [root@localhost ~]$ docker rmi centos # 根据 REPOSITORY 来删除镜像 [root@local ...

  3. Centos安装Memcache

    Memcache概述 官方 Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据.简单的说就是将数据调用到内存中,然后从内存 ...

  4. XML 和 JSON 的使用场景

    我们都知道 JSON 和 XML 设计的初衷里都包含一点是对人类友好, 所以两者在这方面属于竞争关系. 而在 UI 描述上, 我觉得 XML 是比 JSON 要优异很多. 我们先来看一组简单的数据排版 ...

  5. poj_3436 网络最大流

    题目大意 生产电脑的工厂将一台电脑分成P个部件来进行流水线生产组装,有N个生产车间,每个车间可以将一个半成品电脑添加某些部件,使之成为另一个半成品电脑或者成为一台完好的电脑,且每个车间有一个效率,即在 ...

  6. LeetCode——Search a 2D Matrix II

    Description: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix ...

  7. 关于PreparedStatement.addBatch()方法 (转)

    Statement和PreparedStatement的区别就不多废话了,直接说PreparedStatement最重要的addbatch()结构的使用. 1.建立链接,(打电话拨号 ) Connec ...

  8. Linux系统内核main函数执行之前

    1.linux是一个操作系统在机器加电后,需要从硬件通过一个引导程序加载os kernel,那么在os kernel的main函数运行之前,都发生了什么呢? (1)引导BIOS(存储在ROM芯片中,R ...

  9. 【BZOJ3939】[Usaco2015 Feb]Cow Hopscotch 动态规划+线段树

    [BZOJ3939][Usaco2015 Feb]Cow Hopscotch Description Just like humans enjoy playing the game of Hopsco ...

  10. 微信小程序 --- 模板的使用

    由于微信小程序文件大小的限制,可以把一些公用的文件 单离出来形成模板,从而被各个模板引用: 定义模板第一种方式: 新建一个目录: 写入: <text>hello world</tex ...