使用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. Zookeeper(三)-- JAVA原生API

    一.前提 jar包:zookeeper-3.4.9.jar,slf4j-api-1.6.1.jar,slf4j-log4j12-1.6.1.jar,log4j-1.2.15.jar 二.Demo pa ...

  2. UE4射线的碰撞与绘制

    http://blog.csdn.net/qq992817263/article/details/51800657 //起点 终点 FHitResult RayGetHitResult(FVector ...

  3. 更改嵌入式Linux中开机画面----左上角小企鹅图标

    一直想给嵌入式仪表加个开机LOGO,但是没有找到更换的方法.最近在网上收集了一些文章,整理一下一共自己参考.目前也还没有试过这种方法究竟是否可以.但察看Kernel源代码可以知道,Linux-2.6的 ...

  4. pvm虚拟机基本原理

    零.绪论:特别鸣谢下文博客,自己博客是对这篇博客的学习笔记: 大佬webber博客:https://www.cnblogs.com/webber1992/p/6597166.html 一.三种文件: ...

  5. fabric入门

    author: headsen  chen date: 2018-08-12  23:13:16 1,安装 yum -y install epel-releaseyum -y install fabr ...

  6. Egret Wing4.0.3 合并资源图片问题

    一 发布项目时,选择合并图片资源 选择合图大小 发布后,图片合并.随机了图片名字.  二  随机名的问题 当资源不变更的情况下,多次发布,每次发布后资源的图片随机名是不变的. 现在改变preload组 ...

  7. windows 下,go语言 交叉编译

    http://bbs.csdn.net/topics/390601048 参考上面的操作

  8. iOS - 开发中调试小技巧

    对于软件开发而言,调试是必须学会的技能,重要性不言而喻.对于调试的技能,基本上是可以迁移的,也就是说你以前在其他平台上掌握的很多调试技巧,很多也是可以用在iOS开发中.不同语言.不同IDE.不同平台的 ...

  9. 使用SSH工具连接到MySQL

    在SSH中查看MySQL数据信息 格式为:mysql -h主机地址 -u用户名 -p用户密码 -P端口号 -D数据库名称 例如: mysql -uroot -p123456 -h192.168.1.1 ...

  10. thinkphp---手机访问切换模板!

    手机访问切换模板:一般用在手机在做自适应的情况. 第一步:需要添加判断是否是手机访问的方法: http://www.cnblogs.com/e0yu/p/7561811.html 第二步:Home / ...