Mapper动态代理开发
- 在开发的过程中只需要写Dao层的借口,无需写其实现类,实现类有框架自己补充。
- 框架是根据mapper文件自动补充的,因此需要满足下面四个条件
- Mapper接口开发需要遵循以下规范:
- Mapper.xml文件中的namespace与mapper接口的类路径相同。
- Mapper接口方法名和Mapper.xml中定义的每个statement的id相同
- Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql 的parameterType的类型相同
- Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同
1,数据库
create table STUDENT
(
STU_ID VARCHAR2(11) not null,
STU_NAME VARCHAR2(50),
STU_BIRTHDATE DATE,
STU_PHONE VARCHAR2(50)
)
2,pojo
package com.songyan.pojo; import java.util.Date;
import java.lang.String;
public class Student {
private String stuId;
private String stuName;
private Date stuBirthdate;
private String stuPhone; public String getStuId() {
return stuId;
} public void setStuId(String stuId) {
this.stuId = stuId;
} public String getStuName() {
return stuName;
} public void setStuName(String stuName) {
this.stuName = stuName;
} public Date getStuBirthdate() {
return stuBirthdate;
} public void setStuBirthdate(Date stuBirthdate) {
this.stuBirthdate = stuBirthdate;
} public String getStuPhone() {
return stuPhone;
} public void setStuPhone(String stuPhone) {
this.stuPhone = stuPhone;
}
@Override
public String toString() { return "student:"+this.stuId+" "+this.stuName;
} }
3,dao
package com.songyan.dao;
import java.util.List;
import com.songyan.pojo.Student;
public interface StudentMapper {
public void insertStudent(Student student);
public void deteteStudent(String id);
public void updateStudent(Student student);
public List<Student> selectStudent(String id);
}
4,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.songyan.dao.StudentMapper">
<resultMap type="student" id="BaseResultMap">
<id column="STU_ID" property="stuId" javaType="java.lang.String"
jdbcType="VARCHAR" />
<result column="STU_NAME" property="stuName" javaType="java.lang.String"
jdbcType="VARCHAR" />
<result column="STU_BIRTHDATE" property="stuBirthdate"
javaType="java.util.Date" jdbcType="DATE" />
<result column="STU_PHONE" property="stuPhone" javaType="java.lang.String"
jdbcType="VARCHAR" />
</resultMap>
<select id="selectStudent" parameterType="String"
resultType="com.songyan.pojo.Student">
select * from student where STU_ID= #{stuId}
</select>
<insert id="insertStudent" parameterType="student" >
insert into student(STU_ID,STU_NAME,STU_PHONE)
values(#{stuId},#{stuName},#{stuPhone})
</insert> <delete id="deteteStudent" parameterType="String">
delete from student where STU_ID= #{stuId}
</delete> <update id="updateStudent" parameterType="String">
update student set STU_NAME=#{stuName} where STU_ID = #{stuId}
</update>
</mapper>
5,核心配置文件
<?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>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<typeAlias type="com.songyan.pojo.Student" alias="student" />
</typeAliases>
<!--配置环境,默认的环境id为oracle -->
<environments default="oracle">
<!-- 配置环境为oracle的环境 -->
<environment id="oracle">
<!--使用JDBC的事务处理 -->
<transactionManager type="JDBC" />
<!--数据库连接池 -->
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:inspur"></property>
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
</dataSource>
</environment>
</environments>
<!--配置mapper的位置 -->
<mappers>
<mapper resource="com/songyan/dao/studentMapper.xml" />
</mappers>
</configuration>
6,log4j
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p [%t] %c{1}:%L - %m%n
#\u5728\u5F00\u53D1\u73AF\u5883\u4E0B\u65E5\u5FD7\u7EA7\u522B\u8981\u8BBE\u7F6E\u6210DEBUG\uFF0C\u751F\u4EA7\u73AF\u5883\u8BBE\u7F6E\u6210info\u6216error
log4j.rootLogger=DEBUG, stdout
#log4j.rootLogger=stdout
7,测试类
package com.songyan.client; import java.io.IOException;
import java.io.InputStream;
import java.util.List; 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.junit.Test; import com.songyan.dao.StudentMapper;
import com.songyan.pojo.Student; public class Client {
@Test
public void testInsert() throws IOException
{
//读取配置信息
String resource="applicationContext.xml";
//根据配置文件构建sqlsessionFactory
InputStream in=Resources.getResourceAsStream(resource);
//通过sqlsessionFactory创建sqlsession
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession =sqlSessionFactory.openSession();
//创建一个学生对象
Student student =new Student();
student.setStuId("1");
student.setStuName("tom");
student.setStuPhone("111");
//获取接口类
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
studentMapper.insertStudent(student);
//输出结果
sqlSession.commit();
sqlSession.close();
} @Test
public void testDelete() throws IOException
{
//读取配置信息
String resource="applicationContext.xml";
//根据配置文件构建sqlsessionFactory
InputStream in=Resources.getResourceAsStream(resource);
//通过sqlsessionFactory创建sqlsession
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession =sqlSessionFactory.openSession();
//获取接口类
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
studentMapper.deteteStudent("1");
sqlSession.commit();
sqlSession.close();
} @Test
public void testUpdate() throws IOException
{
//读取配置信息
String resource="applicationContext.xml";
//根据配置文件构建sqlsessionFactory
InputStream in=Resources.getResourceAsStream(resource);
//通过sqlsessionFactory创建sqlsession
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession =sqlSessionFactory.openSession();
//创建一个学生对象
Student student =new Student();
student.setStuId("2");
student.setStuName("tofm");
student.setStuPhone("111");
//获取接口类
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
studentMapper.updateStudent(student); sqlSession.commit();
sqlSession.close();
} @Test
public void testSelect() throws IOException
{
//读取配置信息
String resource="applicationContext.xml";
//根据配置文件构建sqlsessionFactory
InputStream in=Resources.getResourceAsStream(resource);
//通过sqlsessionFactory创建sqlsession
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession =sqlSessionFactory.openSession();
//获取接口类
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class); List<Student> relstu=studentMapper.selectStudent("2");
System.out.println(relstu.toString());
//输出结果
for(Student stu : relstu)
{
System.out.println(stu);
}
sqlSession.close();
}
}
目录

Mapper动态代理开发的更多相关文章
- Mybaits之Mapper动态代理开发
Mybaits之Mapper动态代理开发 开发规范: Mapper接口开发方法只需要程序员与Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法 ...
- Mybatis框架三:DAO层开发、Mapper动态代理开发
这里是最基本的搭建:http://www.cnblogs.com/xuyiqing/p/8600888.html 接下来做到了简单的增删改查:http://www.cnblogs.com/xuyiqi ...
- MyBatis使用Mapper动态代理开发Dao层
开发规范 Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同原始Dao接口实现类方法. Mappe ...
- MyBatis开发Dao的原始Dao开发和Mapper动态代理开发
目录 咳咳...初学者看文字(Mapper接口开发四个规范)属实有点费劲,博主我就废了点劲做了如下图,方便理解: 原始Dao开发方式 1. 编写映射文件 3.编写Dao实现类 4.编写Dao测试 Ma ...
- JavaWeb_(Mybatis框架)Mapper动态代理开发_三
系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...
- MyBatis - Mapper动态代理开发
Mapper接口开发方法编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象. Mapper接口开发方式是基于入门程序的基础上,对 控制程序 进行分层开发, ...
- Mybatis(五)Spring整合Mybatis之mapper动态代理开发
要操作的数据库: IDEA创建的Java工程,目录结构如下: 一.导包 1.spring的jar包 2.Mybatis的jar包 3.Spring+mybatis的整合包. 4.Mysql的数据库驱动 ...
- 【Mybatis】-- Mapper动态代理开发注意事项
1.1. Mapper动态代理方式 1.1.1. 开发规范 Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对 ...
- Mybatis框架基础入门(三)--Mapper动态代理方式开发
使用MyBatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper动态代理开发方法. 原始Dao开发方法需要程序员编写Dao接口和Dao实现类,此方式开发Dao,存在以下问题: Dao方 ...
随机推荐
- Google File System中文版
英文原文地址: Google File system 译文原文地址: The Google File System中文版 Google File System中文版 摘要 我们设计并实现了Google ...
- auto login vpnclient bat
@echo offstart "" /b "C:\Program Files (x86)\Cisco Systems\VPN Client\vpngui.exe" ...
- 模拟实现jdk动态代理
实现步骤 1.生成代理类的源代码 2.将源代码保存到磁盘 3.使用JavaCompiler编译源代码生成.class字节码文件 4.使用JavaCompiler编译源代码生成.class字节码文件 5 ...
- 调用webservice接口
这里是cxf服务器,采用myeclipse6.5,把wsdl放到本地的方式. 新建一个包, 把解析到的类放在这个包下面. 生成的代码结构: 调用: public static String callI ...
- DecimalFormat中格式化问题
一:前言 每天自己斗会看到新的东西,每天自己都会学到东西,但是觉得自己老是想一口吃一个胖子.每天看到一个知识点都把其收藏了,但是自己也没有时间去看,不知道自己到底想感谢什么.真是自己无语,本来说是把自 ...
- HDU 5881--Tea 思维规律
感谢http://blog.csdn.net/black_miracle/article/details/52567718 题意:有一壶水, 体积在 L和 R之间, 有两个杯子, 你要把水倒到两个杯子 ...
- ACM-ICPC 2018 南京赛区网络预赛 Sum
A square-free integer is an integer which is indivisible by any square number except 11. For example ...
- bzoj2811 [Apio2012]Guard
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2811 [题解] 首先我们先把没看到忍者的段去掉,可以用线段树做. 如果剩下的就是K,那么特判 ...
- 如何打开小米,oppo,华为等手机的系统应用的指定页面
如题,拿Oppo 手机做个示例,小米 华为也是如此. 在编写Android应用的时候,我们经常会有这样的需求,我们想直接打开系统应用的某个页面.比如在Oppo R9 手机上我们想打开某个应用的通知管理 ...
- 在DirectX11下用Stencil Buffer绘制可视化Depth Complexity
这是一道在<Introduction to 3D Game Programming with DirectX 11>上的练习题. 要求把某个像素点上的Depth Complexity(深度 ...