一、项目结构

二、代码实现

 import java.util.List;
import java.util.Map; import com.jmu.bean.Student; public interface IStudentDao {
void insertStudent(Student student);
void insertStudentCacheId(Student student);// 插入后获取
void deleteStudentById(int id);
void updateStudent(Student student);
List<Student> selectAllStudents();// 查询所有
/* Map<String,Object> selectAllStudentsMap(); */
Student selectStudentById(int id); // 根据id查询
List<Student> selectStudentsByName(String name);// 模糊查询
}

com.jmu.dao.IStudentDao

 import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.BasicConfigurator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.jmu.bean.Student;
import com.jmu.dao.IStudentDao;
import com.jmu.utils.MybatisUtils; public class MyTest {
private IStudentDao dao;
private SqlSession sqlSession; @Before
public void Before() {
sqlSession = MybatisUtils.getSqlSession();
dao = sqlSession.getMapper(IStudentDao.class);
BasicConfigurator.configure();
}
@After
public void after(){
if (sqlSession!=null) {
sqlSession.commit(); } }
@Test
public void test01() {
Student student = new Student("明明", 19, 87.9);
System.out.println("插入前:student=" + student);
dao.insertStudent(student);
System.out.println("插入后:student=" + student);
sqlSession.commit();
} // 插入后获取
@Test
public void test02() {
Student student = new Student("明明", 23, 99.5);
System.out.println("插入前:student=" + student);
dao.insertStudentCacheId(student);
System.out.println("插入后:student=" + student);
sqlSession.commit();
} @Test
public void test03() {
dao.deleteStudentById(25);
sqlSession.commit();
} @Test
public void test04() {
Student student = new Student("红酒", 23, 93.5);
student.setId(28);
dao.updateStudent(student);
sqlSession.commit();
} @Test
public void test05() {
List<Student> students = dao.selectAllStudents();
for (Student student : students) {
System.out.println(student);
}
} /*@Test
public void test06() {
Map<String, Object> map = dao.selectAllStudentsMap();
System.out.println(map.get("王维"));
}*/ @Test
public void test07() {
Student student = dao.selectStudentById(154);
System.out.println(student);
} @Test
public void test08() {
List<Student> students = dao.selectStudentsByName("明");
for (Student student : students) {
System.out.println(student);
} } }

com.jmu.test.MyTest

 ### Global logging configuration
log4j.logger.test=debug,console
##log4j.Logger.com.jmu.dao.IStudentDao=trace,console
### Uncomment for MyBatis logging
log4j.logger.org.apache.ibatis=ERROR log4j.logger.org.apache.ibatis.session.AutoMappingUnknownColumnBehavior=WARN ### Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n log4j.appender.lastEventSavedAppender=org.apache.ibatis.session.AutoMappingUnknownColumnBehaviorTest$LastEventSavedAppender

log4j.properties

 <?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.jmu.dao.IStudentDao">
<insert id="insertStudent" parameterType="Student">
insert into
student(name,age,score) values(#{name},#{age},#{score})
</insert> <insert id="insertStudentCacheId">
insert into student(name,age,score) values(#{name},#{age},#{score})
<selectKey resultType="int" keyProperty="id" order="AFTER">
select
@@identity
</selectKey>
</insert>
<delete id="deleteStudentById">
delete from student where id=#{XXX}<!--这里的#{} 仅仅是个占位符,里边放什么都行 -->
</delete>
<update id="updateStudent">
update student set
name=#{name},age=#{age},score=#{score} where id=#{id}
</update>
<select id="selectAllStudents" resultType="Student">
select
id,name,age,score from student
</select>
<select id="selectStudentById" resultType="Student">
select
id,name,age,score from student where id=#{JJJ}
</select>
<select id="selectStudentsByName" resultType="Student">
<!-- select id,name,age,score from student where name like CONCAT('%',#{XXX},'%') -->
<!-- select id,name,age,score from student where name like '%王二%' -->
<!-- select id,name,age,score from student where name like '%${value}%' --><!-- 不存在sql注入 风险,但是效率低 -->
select id,name,age,score from student where name like '%' #{XXX} '%'<!--常用 -->
</select>
</mapper>

/mybatis4-mapperDynamicProxy/src/com/jmu/dao/mapper.xml

输出:

插入前:student=Student [id=null, name=明明, score=87.9, age=19]
0 [main] DEBUG com.jmu.dao.IStudentDao.insertStudent - ==> Preparing: insert into student(name,age,score) values(?,?,?)
56 [main] DEBUG com.jmu.dao.IStudentDao.insertStudent - ==> Parameters: 明明(String), 19(Integer), 87.9(Double)
57 [main] DEBUG com.jmu.dao.IStudentDao.insertStudent - <== Updates: 1
插入后:student=Student [id=null, name=明明, score=87.9, age=19]
插入前:student=Student [id=null, name=明明, score=99.5, age=23]
82 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId - ==> Preparing: insert into student(name,age,score) values(?,?,?)
82 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId - ==> Preparing: insert into student(name,age,score) values(?,?,?)
83 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId - ==> Parameters: 明明(String), 23(Integer), 99.5(Double)
83 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId - ==> Parameters: 明明(String), 23(Integer), 99.5(Double)
85 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId - <== Updates: 1
85 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId - <== Updates: 1
88 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId!selectKey - ==> Preparing: select @@identity
88 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId!selectKey - ==> Preparing: select @@identity
88 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId!selectKey - ==> Parameters:
88 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId!selectKey - ==> Parameters:
126 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId!selectKey - <== Total: 1
126 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId!selectKey - <== Total: 1
插入后:student=Student [id=165, name=明明, score=99.5, age=23]
145 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById - ==> Preparing: delete from student where id=?
145 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById - ==> Preparing: delete from student where id=?
145 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById - ==> Preparing: delete from student where id=?
145 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById - ==> Parameters: 25(Integer)
145 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById - ==> Parameters: 25(Integer)
145 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById - ==> Parameters: 25(Integer)
146 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById - <== Updates: 0
146 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById - <== Updates: 0
146 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById - <== Updates: 0
162 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - ==> Preparing: update student set name=?,age=?,score=? where id=?
162 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - ==> Preparing: update student set name=?,age=?,score=? where id=?
162 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - ==> Preparing: update student set name=?,age=?,score=? where id=?
162 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - ==> Preparing: update student set name=?,age=?,score=? where id=?
163 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - ==> Parameters: 红酒(String), 23(Integer), 93.5(Double), 28(Integer)
163 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - ==> Parameters: 红酒(String), 23(Integer), 93.5(Double), 28(Integer)
163 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - ==> Parameters: 红酒(String), 23(Integer), 93.5(Double), 28(Integer)
163 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - ==> Parameters: 红酒(String), 23(Integer), 93.5(Double), 28(Integer)
163 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - <== Updates: 0
163 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - <== Updates: 0
163 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - <== Updates: 0
163 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - <== Updates: 0
181 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - ==> Preparing: select id,name,age,score from student
181 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - ==> Preparing: select id,name,age,score from student
181 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - ==> Preparing: select id,name,age,score from student
181 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - ==> Preparing: select id,name,age,score from student
181 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - ==> Preparing: select id,name,age,score from student
182 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - ==> Parameters:
182 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - ==> Parameters:
182 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - ==> Parameters:
182 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - ==> Parameters:
182 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - ==> Parameters:
188 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - <== Total: 8
188 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - <== Total: 8
188 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - <== Total: 8
188 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - <== Total: 8
188 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - <== Total: 8
Student [id=157, name=明明, score=87.9, age=19]
Student [id=158, name=明明, score=87.9, age=19]
Student [id=159, name=明明, score=87.9, age=19]
Student [id=160, name=明明, score=87.9, age=19]
Student [id=162, name=明明, score=87.9, age=19]
Student [id=163, name=明明, score=99.5, age=23]
Student [id=164, name=明明, score=87.9, age=19]
Student [id=165, name=明明, score=99.5, age=23]
206 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Preparing: select id,name,age,score from student where id=?
206 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Preparing: select id,name,age,score from student where id=?
206 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Preparing: select id,name,age,score from student where id=?
206 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Preparing: select id,name,age,score from student where id=?
206 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Preparing: select id,name,age,score from student where id=?
206 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Preparing: select id,name,age,score from student where id=?
207 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Parameters: 157(Integer)
207 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Parameters: 157(Integer)
207 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Parameters: 157(Integer)
207 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Parameters: 157(Integer)
207 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Parameters: 157(Integer)
207 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Parameters: 157(Integer)
211 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - <== Total: 1
211 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - <== Total: 1
211 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - <== Total: 1
211 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - <== Total: 1
211 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - <== Total: 1
211 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - <== Total: 1
Student [id=157, name=明明, score=87.9, age=19]
247 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Preparing: select id,name,age,score from student where name like '%' ? '%'
247 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Preparing: select id,name,age,score from student where name like '%' ? '%'
247 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Preparing: select id,name,age,score from student where name like '%' ? '%'
247 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Preparing: select id,name,age,score from student where name like '%' ? '%'
247 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Preparing: select id,name,age,score from student where name like '%' ? '%'
247 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Preparing: select id,name,age,score from student where name like '%' ? '%'
247 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Preparing: select id,name,age,score from student where name like '%' ? '%'
253 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Parameters: 明(String)
253 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Parameters: 明(String)
253 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Parameters: 明(String)
253 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Parameters: 明(String)
253 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Parameters: 明(String)
253 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Parameters: 明(String)
253 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Parameters: 明(String)
262 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - <== Total: 8
262 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - <== Total: 8
262 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - <== Total: 8
262 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - <== Total: 8
262 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - <== Total: 8
262 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - <== Total: 8
262 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - <== Total: 8
Student [id=157, name=明明, score=87.9, age=19]
Student [id=158, name=明明, score=87.9, age=19]
Student [id=159, name=明明, score=87.9, age=19]
Student [id=160, name=明明, score=87.9, age=19]
Student [id=162, name=明明, score=87.9, age=19]
Student [id=163, name=明明, score=99.5, age=23]
Student [id=164, name=明明, score=87.9, age=19]
Student [id=165, name=明明, score=99.5, age=23]

output

MyBatis动态代理的更多相关文章

  1. 通过模拟Mybatis动态代理生成Mapper代理类,讲解Mybatis核心原理

    本文将通过模拟Mybatis动态代理生成Mapper代理类,讲解Mybatis原理 1.平常我们是如何使用Mapper的 先写一个简单的UserMapper,它包含一个全表查询的方法,代码如下 pub ...

  2. MyBatis 动态代理开发

    MyBatis 动态代理开发 §  Mapper.xml文件中的namespace与mapper接口的类路径相同. §  Mapper接口方法名和Mapper.xml中定义的每个statement的i ...

  3. Java EE开发平台随手记5——Mybatis动态代理接口方式的原生用法

    为了说明后续的Mybatis扩展,插播一篇广告,先来简要说明一下Mybatis的一种原生用法,不过先声明:下面说的只是Mybatis的其中一种用法,如需要更深入了解Mybatis,请参考官方文档,或者 ...

  4. MyBatis动态代理执行原理

    前言 大家使用MyBatis都知道,不管是单独使用还是和Spring集成,我们都是使用接口定义的方式声明数据库的增删改查方法.那么我们只声明一个接口,MyBatis是如何帮我们来实现SQL呢,对吗,我 ...

  5. Mybatis动态代理实现函数调用

    如果我们要使用MyBatis进行数据库操作的话,大致要做两件事情: 1. 定义DAO接口 在DAO接口中定义需要进行的数据库操作. 2. 创建映射文件 当有了DAO接口后,还需要为该接口创建映射文件. ...

  6. Spring系列之Mybatis动态代理实现全过程?回答正确率不到1%

    面试中,可能会问到Spring怎么绑定Mapper接口和SQL语句的.一般的答案是Spring会为Mapper生成一个代理类,调用的时候实际调用的是代理类的实现.但是如果被追问代理类实现的细节,很多同 ...

  7. MyBatis动态代理查询出错

     org.apache.ibatis.exceptions.PersistenceException: ### Error querying database.  Cause: org.apache. ...

  8. MyBatis总结三:使用动态代理实现dao接口

    由于我们上一篇实现MyBatis的增删改查的接口实现类的方法都是通过sqlsession调用方法,参数也都类似,所以我们使用动态代理的方式来完善这一点 MyBatis动态代理生成dao的步骤: 编写数 ...

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

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

随机推荐

  1. LNMP架构部署

    第1章 部署LNMP架构步骤 1.1 ①部署Linux系统(OK) 基本优化完成(ip地址设置 yum源更新 字符集设置) 安全优化完成(iptables关闭 selinux关闭 /tmp/ 1777 ...

  2. Git(1)----Eclipse安装Git插件

    一.从官网选择系统版本下载Git并安装 地址:https://git-scm.com/downloads/ 二.打开Eclipse 1. 第一种安装方法: help-->Install New ...

  3. HTTPS 建立连接的详细过程

    HTTPS是在HTTP的基础上和ssl/tls证书结合起来的一种协议,保证了传输过程中的安全性,减少了被恶意劫持的可能.很好的解决了解决了http的三个缺点(被监听.被篡改.被伪装) 对称加密和非对称 ...

  4. C#泛型集合之List

    1.命名空间:System.Collections.Generic(程序集:mscorlib)2.描述: 1).表示可通过索引访问的对象的强类型列表:提供用于对列表进行搜索.排序和操作的方法. 2). ...

  5. ASP.NET没有魔法——ASP.NET MVC 过滤器(Filter)

    上一篇文章介绍了使用Authorize特性实现了ASP.NET MVC中针对Controller或者Action的授权功能,实际上这个特性是MVC功能的一部分,被称为过滤器(Filter),它是一种面 ...

  6. linux操作系统基础篇(七)

    Linux服务篇(二) 1.nfs服务的搭建 安装: yum install rpcbind nfs-utils -y 配置: NFS服务的配置文件为 /etc/exports,这个文件是NFS的主要 ...

  7. 分布式网络文件系统--MooseFS

    一.介绍 1.简介 MooseFS是一个具备冗余容错功能的分布式网络文件系统,它将数据分别存放在多个物理服务器或单独磁盘或分区上,确保一份数据有多个备份副本.对于访问的客户端或者用户来说,整个分布式网 ...

  8. 线性回归,附tensorflow实现

    本文同步自:https://zhuanlan.zhihu.com/p/30738405 本文旨在通过介绍线性回归来引出一些基本概念:h(x),J(θ),梯度下降法 有一组数据: x=[1,2,3,4, ...

  9. tungsten-replicator安装

    需要环境 ruby1.8.5 or laterJava1.6 or later 应用程序对外开放接口 3306 (MySQL database)2112 (Tungsten THL)10000 (Tu ...

  10. 浅谈IM(InstantMessaging) 即时通讯/实时传讯

        一.IM简要概述 IM InstantMessaging(即时通讯,实时传讯)的缩写是IM,互动百科大致解释是一种可以让使用者在网络上建立某种私人聊天(chatroom)的实时通讯服务. 大部 ...