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方 ...
随机推荐
- Web自适应
随着移动设备的普及,移动web在前端工程师们的工作中占有越来越重要的位置.移动设备更新速度频繁,手机厂商繁多,导致的问题是每一台机器的屏幕宽度和分辨率不一样.这给我们在编写前端界面时增加了困难,适配问 ...
- tomcat内存配置(二)
Tomcat本身不能直接在计算机上运行,需要依赖于硬件基础之上的操作系统和一个Java虚拟机.Tomcat的内存溢出本质就是JVM内存溢出,所以在本文开始时,应该先对JavaJVM有关内存方面的知识进 ...
- JavaBean定义、JSP中使用以及内省操作
Apache commons 一系列的开源工具室非常值得学习的实现. 一 JavaBean定义 JavaBean是一种可重复使用.且跨平台的软件组件.JavaBean可分为两种:一种是 ...
- mybatis基本流程、jdbc连接、ps:附mybatis(乐观锁)实现
一.前言 Mybatis和Hibernate一样,是一个优秀的持久层框架.已经说过很多次了,原生的jdbc操作存在大量的重复性代码(如注册驱动,创建连接,创建statement,结果集检测等).框架的 ...
- 【BZOJ2820】YY的GCD [莫比乌斯反演]
YY的GCD Time Limit: 10 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 求1<=x<=N, ...
- js三层引号嵌套
··· 参考:https://blog.csdn.net/feiyangbaxia/article/details/49681131 第一层用双引号,第二层转义双引号,第三层单引号
- mysql索引作用的简单理解
转自:http://blog.csdn.net/pengsidong/article/details/62104703,有添加 索引好比书的目录,好比新华字典的拼音.偏旁部首查字,可以帮助人快速查找到 ...
- mybatis获取表信息,以及遍历ResultSet
@RunWith(SpringRunner.class) @SpringBootTest public class BravolinksCrmServerApplicationTests { @Aut ...
- 【 浅谈Linux路由机制 】
以下均为个人实验验证结果,如有问题,恳请指正. 现在服务器一般都有四张网卡,给了我们更多网络模型的选择.为了业务的需求,有时我们需要如下网络架构 系统:centos 7.2 x64 两张网卡不在同一个 ...
- MySQL 多列索引优化小记
MySQL 5.6.30 问题背景 由于爬虫抓取的数据不断增多,这两天在不断对数据库以及查询语句进行优化,其中一个表结构如下: CREATE TABLE `newspaper_article` ( ` ...