• 使用动态代理实现dao接口的实现类

MyBatis允许只声明一个dao接口,而无需写dao实现类的方式实现数据库操作。前提是必须保证Mapper文件中的<mapper>标签的namespace属性值必须要和dao接口的类路径一致,MyBatis容器会自动通过动态代理生成接口的实现类。

Mapper.java

 package cn.mybatis.dao;

 import cn.mybatis.domain.Student;

 public interface StudentMapper {
public void insertStudent(Student s);
public void updateStudent(Student s);
public void deleteStudent(String stuid);
public Student selectStudentById(String stuid);
}

Mapper.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="cn.mybatis.dao.StudentMapper">
<resultMap type="student" id="BaseResultMap">
<id column="stu_id" property="stuId" jdbcType="VARCHAR" javaType="java.lang.String" />
<result column="stu_name" property="stuName" jdbcType="VARCHAR" javaType="java.lang.String" />
<result column="stu_birthdate" property="stuBirthdate" jdbcType="DATE" javaType="java.util.Date" />
<result column="stu_phone" property="stuPhone" jdbcType="VARCHAR" javaType="java.lang.String" />
</resultMap> <!-- 插入数据 -->
<insert id="insertStudent" parameterType="student">
insert into student (stu_id,stu_name,stu_birthdate,stu_phone)
values(#{stuId},#{stuName},#{stuBirthdate},#{stuPhone})
</insert> <!-- 更新数据 -->
<update id="updateStudent" parameterType="student">
update student set stu_name=#{stuName}, stu_birthdate=#{stuBirthdate},
stu_phone=#{stuPhone} where stu_id=#{stuId}
</update>
<!-- 删除数据 -->
<delete id="deleteStudent" parameterType="string">
delete from student where stu_id=#{stuId}
</delete> <!-- 查询数据,返回的数据会根据resultMap设置封装到实体类对象中 -->
<select id="selectStudentById" resultType="cn.mybatis.domain.Student" parameterType="string" >
select stu_name as stuName from student where stu_id=#{stuId}
</select>
</mapper>

测试

 package cn.mybatis.demo;

 import java.io.InputStream;
import java.text.SimpleDateFormat; 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 org.apache.log4j.Logger; import cn.mybatis.dao.StudentMapper;
import cn.mybatis.domain.Student; public class Demo_01 {
private static SqlSessionFactory fac;
static{
InputStream is = null;
try{
//处理并根据config配置文件实例化SqlSessionFactory
is = Resources.getResourceAsStream("SqlMapperConfig.xml");
//获取session工厂类
fac = new SqlSessionFactoryBuilder().build(is);
}catch(Exception e){
e.printStackTrace();
Logger.getLogger(Demo_01.class).debug(e.getMessage());
}
}
public static void main(String[] args) throws Exception {
//创建要保存的学生信息
Student s = new Student();
s.setStuId("5");
s.setStuName("zhou");
s.setStuBirthdate(new SimpleDateFormat("yyyy-MM-dd").parse("1991-1-12"));
s.setStuPhone("341324123"); SqlSession session = fac.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
// mapper.insertStudent(s);
// mapper.updateStudent(s);
// mapper.deleteStudent("5");
Student s1 = mapper.selectStudentById("1");
System.out.println(s1.getStuName());
session.commit();
session.close();
}
}

使用动态代理实现dao接口的更多相关文章

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

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

  2. MyBatis使用mapper动态代理实现DAO接口

    工具: mysql 5.5.62   IDEA 参考自:https://www.cnblogs.com/best/p/5688040.html 遇到的问题: 无法读取src/main/java下配置文 ...

  3. MyBatis使用Mapper动态代理开发Dao层

    开发规范 Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同原始Dao接口实现类方法. Mappe ...

  4. 基于JDK动态代理实现的接口链式调用(Fluent Interface)工具

    什么是链式接口(Fluent Interface) 根据wikipedia上的定义,Fluent interface是一种通过链式调用方法来完成方法的调用,其操作分为终结与中间操作两种.[1] 下面是 ...

  5. SpringBoot 动态代理实现三方接口调用

    目录 一.定义注解 二.建立动态代理类 三.注入spring容器 四.编写拦截器 五.创建客户端调用类 六.main方法测试 七.启动项目 在某些业务场景中,我们只需要业务代码中定义相应的接口或者相应 ...

  6. Mybatis进阶学习笔记——动态代理方式开发Dao接口、Dao层(推荐第二种)

    1.原始方法开发Dao Dao接口 package cn.sm1234.dao; import java.util.List; import cn.sm1234.domain.Customer; pu ...

  7. JAVAEE——Mybatis第一天:入门、jdbc存在的问题、架构介绍、入门程序、Dao的开发方法、接口的动态代理方式、SqlMapConfig.xml文件说明

    1. 学习计划 第一天: 1.Mybatis的介绍 2.Mybatis的入门 a) 使用jdbc操作数据库存在的问题 b) Mybatis的架构 c) Mybatis的入门程序 3.Dao的开发方法 ...

  8. MyBatis开发Dao的原始Dao开发和Mapper动态代理开发

    目录 咳咳...初学者看文字(Mapper接口开发四个规范)属实有点费劲,博主我就废了点劲做了如下图,方便理解: 原始Dao开发方式 1. 编写映射文件 3.编写Dao实现类 4.编写Dao测试 Ma ...

  9. 最简单的动态代理实例(spring基于接口代理的AOP原理)

    JDK的动态代理是基于接口的 package com.open.aop; public interface BusinessInterface {     public void processBus ...

随机推荐

  1. WinForm使用CefSharp内嵌chrome浏览器

    先贴运行图:亲测可用!以图为证! 开始!1.创建winform程序,使用.NET 4.5.2或以上(vs2010最高支持.NET 4.0,我使用的是vs2017).这一步容易忽略,简单的说就是将项目. ...

  2. 设计模式C++实现——组合模式

    模式定义: 组合模式同意你将对象组合成树形结构来表现"总体/部分"层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 这个模式可以创建一个树形结构,在同一个结构中处理嵌套 ...

  3. Ylmf_Ghost_Win7_SP1_x64_2017_0113.iso虚拟机安装

    新建虚拟机,将iso镜像配置好,然后开启虚拟机 一开始选择PQ8.05: 找到“作业”菜单---“建立” ,新建一个“主分区”然后点击确定 新建主分区作业之后,如果需要新建其他分区继续进行即可,本例只 ...

  4. MYSQL工具之binlog2sql闪回操作

    文档结构: 在生产环境中如果遇到误删,改错数据的情况,利用mysql闪回工具binlog2sql,可以实现数据的快速回滚,从binlog中提取SQL,并能生成回滚SQL语句.Binlog以event作 ...

  5. Nginx实现负载均衡 + Keepalived实现Nginx的高可用

    前言 使用集群是大中型网站解决高并发.海量数据问题的常用手段.当一台服务器的处理能力.存储空间不足时,不要企图去换更强大的服务器,对大型网站而言,不管多么强大的服务器,都满足不了网站持续增长的业务需求 ...

  6. Polyfill 与 Shim

    Polyfill 与 Shim polyfill 的概念是 Remy Sharp 在2010年提出的. polyfill,或 polyfiller ,表示为开发人员提供旧浏览器没有原生支持的较新功能的 ...

  7. Ubuntu下安装sublime text3并汉化

    转载请注明出处:果冻栋吖 通过ppa安装,打开终端,输入以下命令: sudo add-apt-repository ppa:webupd8team/sublime-text- sudo apt-get ...

  8. BZOJ 4403 2982 Lucas定理模板

    思路: Lucas定理的模板题.. 4403 //By SiriusRen #include <cstdio> using namespace std; ; #define int lon ...

  9. Windows系统开发常用类-------------Environment类

    Windows系统开发常用类-------------Environment类:         SystemDirectory//显示系统目录         MachineName//计算机名称 ...

  10. cropper+pillow处理上传图片剪裁(一)

    在写新博客的时候,遇到需要用户上传自定义图片的处理,查了一番资料,决定用cropper和pillow来处理需要剪裁的图片上传,大致思路是:前端收集用户上传的图片和用户剪裁的尺寸数据,后台接收图片后按数 ...