使用动态代理实现dao接口
- 使用动态代理实现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接口的更多相关文章
- MyBatis总结三:使用动态代理实现dao接口
由于我们上一篇实现MyBatis的增删改查的接口实现类的方法都是通过sqlsession调用方法,参数也都类似,所以我们使用动态代理的方式来完善这一点 MyBatis动态代理生成dao的步骤: 编写数 ...
- MyBatis使用mapper动态代理实现DAO接口
工具: mysql 5.5.62 IDEA 参考自:https://www.cnblogs.com/best/p/5688040.html 遇到的问题: 无法读取src/main/java下配置文 ...
- MyBatis使用Mapper动态代理开发Dao层
开发规范 Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同原始Dao接口实现类方法. Mappe ...
- 基于JDK动态代理实现的接口链式调用(Fluent Interface)工具
什么是链式接口(Fluent Interface) 根据wikipedia上的定义,Fluent interface是一种通过链式调用方法来完成方法的调用,其操作分为终结与中间操作两种.[1] 下面是 ...
- SpringBoot 动态代理实现三方接口调用
目录 一.定义注解 二.建立动态代理类 三.注入spring容器 四.编写拦截器 五.创建客户端调用类 六.main方法测试 七.启动项目 在某些业务场景中,我们只需要业务代码中定义相应的接口或者相应 ...
- Mybatis进阶学习笔记——动态代理方式开发Dao接口、Dao层(推荐第二种)
1.原始方法开发Dao Dao接口 package cn.sm1234.dao; import java.util.List; import cn.sm1234.domain.Customer; pu ...
- JAVAEE——Mybatis第一天:入门、jdbc存在的问题、架构介绍、入门程序、Dao的开发方法、接口的动态代理方式、SqlMapConfig.xml文件说明
1. 学习计划 第一天: 1.Mybatis的介绍 2.Mybatis的入门 a) 使用jdbc操作数据库存在的问题 b) Mybatis的架构 c) Mybatis的入门程序 3.Dao的开发方法 ...
- MyBatis开发Dao的原始Dao开发和Mapper动态代理开发
目录 咳咳...初学者看文字(Mapper接口开发四个规范)属实有点费劲,博主我就废了点劲做了如下图,方便理解: 原始Dao开发方式 1. 编写映射文件 3.编写Dao实现类 4.编写Dao测试 Ma ...
- 最简单的动态代理实例(spring基于接口代理的AOP原理)
JDK的动态代理是基于接口的 package com.open.aop; public interface BusinessInterface { public void processBus ...
随机推荐
- Empower Developers
 Empower Developers Timothy High THingS ARE uSuAlly EASiER SAid THAn donE, and software architects ...
- ScalaChina: Scala中文社区
给大家推荐一个很常使用心的Scala中文社区 ScalaChina地址:http://scalachina.org/ 来自社区创建者的<我为什么想做ScalaChina>: http:// ...
- 关于ShapeDrawable应用的一些介绍(下)
我们今天接着来看一下Shape中的stroke属性,stroke其实就是边缘的意思,当我们在定义画笔的时候,有很多时候会用到 FILL 和 STROKE,前者能够画出一个实心的形状,而后者就画出一个空 ...
- identity in sql server 批量插入history
https://stackoverflow.com/questions/1920558/what-is-the-difference-between-scope-identity-identity-i ...
- Linux - 配置php-fpm 以及 配置nginx支持php
配置php-fpm [root@localhost php7]# which php-fpm /usr/local/php7/sbin/php-fpm [root@localhost php7]# p ...
- Dark roads--hdoj
Dark roads Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Su ...
- 自定义数据类型 C++ 结构体类型 共同体类型 枚举类型 类类型{}
一.结构体类型 结构体类型,共用体类型,枚举类型,类类型等统称为自定义类型(user-defined-type,UDT). 结构体相当于其他高级语言中的记录(record);例如: struct St ...
- 解决 dotnet core 1.x 命令行(cli) 下运行路径错误
环境: Windows 10,Visual Studio 2017 centos 7,nginx,supervisor,dotnet core 1.1 问题: 在 Linux 配置 superviso ...
- php 判断过去离现在几年的函数
function gettime($worktime){ $time=time(); $amount=date("Y",$time)-date("Y",strt ...
- POJ 2406 KMP next数组的应用
题意:让你找最小重复串的个数 加深KMP中对next数组的理解 #include <cstdio> #include <cstring> using namespace std ...