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方 ...
随机推荐
- 使用T4模板生成MySql数据库实体类
注:本文系作者原创,但可随意转载. 现在呆的公司使用的数据库几乎都是MySQL.编程方式DatabaseFirst.即先写数据库设计,表设计按照规范好的文档写进EXCEL里,然后用公司的宏,生成建表脚 ...
- 开发中常遇到的Python陷阱和注意点
最近使用Python的过程中遇到了一些坑,例如用datetime.datetime.now()这个可变对象作为函数的默认参数,模块循环依赖等等. 在此记录一下,方便以后查询和补充. 避免可变对象作为默 ...
- 关于JAVA正则匹配空白字符的问题(全角空格与半角空格)
今天遇到一个字符串,怎么匹配空格都不成功!!! 我把空格复制到test.properties文件 显示“\u3000” ,这是什么? 这是全角空格!!! 查了一下 \s 不支持全角 1.& ...
- SQL性能分析
MySQL常见瓶颈: CPU:CPU在饱和的时候一般发生在数据装入内存或从磁盘上读取数据的时候. IO:磁盘I/O瓶颈发生在装入数据远大于内存容量的时候. 服务器硬件的性能瓶颈:top.free.io ...
- struts学习笔记(四)
一. 文件的上传: 1). 表单需要注意的 3 点 2). Struts2 的文件上传实际上使用的是 Commons FileUpload 组件, 所以需要导入 commons-fileupload- ...
- LOJ 6057 - [HNOI2016]序列 加强版再加强版
Description 给定一个长度为 \(n\le 3*10^6\) 的序列 \(q\le 10^7\) 次询问每次求区间 \([l,r]\) 的所有子区间的最小值的和 询问随机 Solution ...
- 51Nod-1586-约数和
#include <cstdio> using namespace std; typedef long long ll; ; int n, q; int cnt[MAXN]; ll a[M ...
- 【poj3415-Common Substrings】sam子串计数
题意: 给出两个串,问这两个串的所有的子串中(重复出现的,只要是位置不同就算两个子串),长度大于等于k的公共子串有多少个. 题解: 这题好像大神们都用后缀数组做..然而我在sam的题表上看到这题,做 ...
- 【洛谷 P4211】[LNOI2014]LCA(树链剖分,差分)
题目链接 看到题目肯定首先想到要求LCA(其实是我菜),可乍一看,n与q的规模为5W, 求LCA的复杂度为\(O(logN)\),那么总时间复杂度为\(O(nq\ log\ n)\). 怎么搞呢? 会 ...
- Python爬虫学习 - day2 - 站点登陆
利用Python完成简单的站点登陆 最近学习到了爬虫,瞬时觉得很高大上,想取什么就取什么,感觉要上天.这里分享一个简单的登陆抽屉新热榜的教程(因为它不需要验证码,目前还没有学会图像识别.哈哈),供大家 ...