Mapper动态代理开发
Posted song.yan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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动态代理开发的主要内容,如果未能解决你的问题,请参考以下文章