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方 ...
随机推荐
- APP兼容性测试
一.APP兼容性范围以及问题 1.硬件 各个硬件结构 2.软硬件之间 硬件dll库(C++) 软硬件之间的通信,各个厂商提供的ROM 3.软件 浏览器.操作系统.数据库.手机.功能兼容性(功能修改,二 ...
- (转)用python实现抓取网页、模拟登陆
涉及一系列内容,部分已在前面转载,仍转自crifan: http://www.crifan.com/how_to_use_some_language_python_csharp_to_implemen ...
- wait , notify 模拟 Queue
package com.itdoc.multi.sync009; import java.util.LinkedList; import java.util.concurrent.TimeUnit; ...
- sencha touch 模仿tabpanel导航栏TabBar的实现代码
这篇文章介绍了sencha touch 模仿tabpanel导航栏TabBar的实例代码,有需要的朋友可以参考一下 基于sencha touch 2.2所写 效果图: 代码: /* *模仿tabpan ...
- C#中file类的应用
现在就其中几个常用的进行介绍: Create:一般使用此重载方法,File.Create (String) ,String是一个路径名,表示文件的完整路径,返回值是一个FileStream实例: Co ...
- 【BZOJ1976】能量魔方 [最小割]
能量魔方 Time Limit: 10 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description 小C 有一个能量魔方,这个魔方可神奇 ...
- 【洛谷】P1648 看守 (数学)
题目链接 直接暴力搞\(O(n^2)\)显然是布星滴. 试想,若是一维,最远距离就是最大值减最小值. 现在推广到二维,因为有绝对值的存在,所以有四种情况 \((x1+y1) - (x2+y2), (x ...
- DotNet 学习笔记 MVC模型
Model Binding Below is a list of model binding attributes: •[BindRequired]: This attribute adds a mo ...
- 扑克牌(cards)
扑克牌 思路 这题也是二分!! 我们二分有几套牌,然后再去检验是否符合,至于怎么想到的,不要问我,我也不知道 那么我们主要解决的就是check函数 我们将二分的套数和每种牌的数量进行比较,如果该种牌的 ...
- iOS float小数四舍五入
http://blog.csdn.net/fanjunxi1990/article/details/21536189 直接贴代码了 #import "ViewController.h&quo ...