使用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. 利用html实现类似于word自动生成的目录的效果

    在word中的自动生成目录当中,我们会看到是这样的目录结构: 嗯,自动生成固然是简单,但是在html当中,却没有一个合适的标签来去做.今天后台导出PDF的时候告诉我,他需要用html做一个这样的结构, ...

  2. C、C++编程入口,常见的编程题

    1.设计一个从5个数中取最小数和最大数的程序. 2.#include<stdio.h> 3.int min(int a[],int i); 4.int max(int a[],int i) ...

  3. web基础----->readonly与disabled的区别

    readonly和Disabled是用在表单中的两个属性,它们都能够做到使用户不能够更改表单域中的内容.但是它们之间有着微小的差别,今天我们通过案例来学习一下. readonly和Disabled的区 ...

  4. Linux Netfilter注册钩子点

    注册钩子点首先要包含响应的头文件,因为这应该已经属于对kernel的编程了. #include <linux/module.h> #include <linux/kernel.h&g ...

  5. thinkphp --- 写入日志

    在开发过程中,对于一些参数,不好直接输入或者打印调试,特别是在微信开发过程中,这个时候,通过日志来查看信息就显得格外重要. 下面是在TP3.2.3框架中,写入日志的方法: public functio ...

  6. 170620、springboot编程之页面版Hello World

    书接上回,把Hello World 在页面上显示! 1.在pom文件中加入web支持 <dependency> <groupId>org.springframework.boo ...

  7. jq和axios的取消请求

    场景: 分页: 每次点击分页会发送请求,如果上一次请求还未获取到,下一次请求已经开始且先一步获取到,那么数据上会出现问题. 快速点击会发送多次请求,多次点击的时候一般的做法我们会定义一个flag,此时 ...

  8. 删除Excel中的复选框等控件

  9. .net配置404错误页面

    如果你的网站出现一堆让人看不懂的报错,那么你就不是一个合格的程序员,也不是一个合格的站长. 下面的方面可以帮助你的网站远离让人头大的页面. 第一步:配置web.config 打开web.config, ...

  10. .NET获取Html字符串中指定标签的指定属性的值

    using System.Text; using System.Text.RegularExpressions; //以上为要用到的命名空间 /// <summary> /// 获取Htm ...