• 使用动态代理实现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. bzoj4247: 挂饰(背包)

    4247: 挂饰 题目:传送门 题解: 看完题目很明显的一道二维背包(一开始还推错了) 设f[i][j]表示前i个挂饰选完(可以有不选)之后还剩下j个挂钩的最大值(j最多贡献为n) 那么f[i][j] ...

  2. Maven打包编译错误工作区间设置编码格式gbk可以utf-8不可以

    转自:https://blog.csdn.net/wolf_love666/article/details/52593483 问题:Maven打包编译错误工作区间设置编码格式gbk可以utf-8不可以 ...

  3. web 端即时通讯

    1. 前言 Web端即时通讯技术因受限于浏览器的设计限制,一直以来实现起来并不容易,主流的Web端即时通讯方案大致有4种:传统Ajax短轮询.Comet技术.WebSocket技术.SSE(Serve ...

  4. CSS Flexbox 弹性盒子模型

    CSS Flexbox 弹性盒子模型 设置元素样式为 display: flex 或 display: inline-flex, 让元素变成flex容器, 从而可以通过flex模式布局它的子元素. f ...

  5. 常用GC算法

    在C/C++中是由程序员自己去申请.管理和释放内存的,因此没有GC的概念.而在Java中,专门有一个用于垃圾回收的后台线程来进行监控.扫描,自动将一些无用的内存进行释放.下面介绍几种常见的GC算法. ...

  6. No control record for Activity type 1000/4220/1442 in version 000 / 2017 activity planning/qty planning

    No control record for Activity type 1000/4220/1442 in version 000 / 2017 activity planning/qty plann ...

  7. Java单例模式解析(收藏)

    在GoF的23种设计模式中,单例模式是比较简单的一种.然而,有时候越是简单的东西越容易出现问题.下面就单例设计模式详细的探讨一下. 所谓单例模式,简单来说,就是在整个应用中保证只有一个类的实例存在.就 ...

  8. JavaScript中的数组创建

    JavaScript中的数组创建 数组是一个包含了对象或原始类型的有序集合.很难想象一个不使用数组的程序会是什么样. 以下是几种操作数组的方式: 初始化数组并设置初始值 通过索引访问数组元素 添加新元 ...

  9. 使用Custom scrollbar(彩色滚动条)插件实现WordPress滚动条变色的方法

    1.在插件中心关键词搜索Custom scrollbar 2.按照说明操作就行 查看演示:sheji.xinlvtian.com

  10. Linux 安装MySQL5.7.18

    https://dev.mysql.com/downloads/mysql/Linux-Generic md5sum mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz ...