后台(37)——MyBatis的Mapper开发方式
2017-03-12 22:12
253 查看
探索Android软键盘的疑难杂症
深入探讨Android异步精髓Handler
详解Android主流框架不可或缺的基石
站在源码的肩膀上全解Scroller工作机制
Android多分辨率适配框架(1)— 核心基础
Android多分辨率适配框架(2)— 原理剖析
Android多分辨率适配框架(3)— 使用指南
自定义View系列教程00–推翻自己和过往,重学自定义View
自定义View系列教程01–常用工具介绍
自定义View系列教程02–onMeasure源码详尽分析
自定义View系列教程03–onLayout源码详尽分析
自定义View系列教程04–Draw源码分析及其实践
自定义View系列教程05–示例分析
自定义View系列教程06–详解View的Touch事件处理
自定义View系列教程07–详解ViewGroup分发Touch事件
自定义View系列教程08–滑动冲突的产生及其处理
作者博客地址:http://blog.csdn.net/lfdfhl
使用Mybatis开发Dao,通常有两个方法:原始Dao开发方式和Mapper接口开发方式。
在本篇文章中,我们在前两篇博客的基础上来一起完成Mapper接口开发方式。
在Mapper接口开发方式中有两个核心的东西:mapper.xml和mapper.java
mapper接口开发需要遵循以下规范:
1、mapper.xml文件中的namespace与mapper.java接口的类的全路径相同。
2、mapper.java接口中的方法名和mapper.xml中定义的每个sql的id相同
3、mapper.java接口中的方法的输入参数类型和mapper.xml中定义的每个sql的parameterType的类型保持一致
4、mapper.java接口中方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型保持一致
好了,我们现在就按照此规范来改造之前的例子
嗯哼,对照着这两个文件看就会发现:我们在书写的过程中严格遵守了开发规范。
这些测试用例中,最重要的就是:
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
得到Mapper,再调用它定义的增删改查方法
最后,按照惯例还是附上项目的结构图:
深入探讨Android异步精髓Handler
详解Android主流框架不可或缺的基石
站在源码的肩膀上全解Scroller工作机制
Android多分辨率适配框架(1)— 核心基础
Android多分辨率适配框架(2)— 原理剖析
Android多分辨率适配框架(3)— 使用指南
自定义View系列教程00–推翻自己和过往,重学自定义View
自定义View系列教程01–常用工具介绍
自定义View系列教程02–onMeasure源码详尽分析
自定义View系列教程03–onLayout源码详尽分析
自定义View系列教程04–Draw源码分析及其实践
自定义View系列教程05–示例分析
自定义View系列教程06–详解View的Touch事件处理
自定义View系列教程07–详解ViewGroup分发Touch事件
自定义View系列教程08–滑动冲突的产生及其处理
版权声明
本文原创作者:谷哥的小弟作者博客地址:http://blog.csdn.net/lfdfhl
使用Mybatis开发Dao,通常有两个方法:原始Dao开发方式和Mapper接口开发方式。
在本篇文章中,我们在前两篇博客的基础上来一起完成Mapper接口开发方式。
开发规范
Mapper接口开发方式比原始的DAO的方式要简便许多,但是这种简便是建立在规范之上的,所以在采用该方式时务必严格遵守开发规范.在Mapper接口开发方式中有两个核心的东西:mapper.xml和mapper.java
mapper接口开发需要遵循以下规范:
1、mapper.xml文件中的namespace与mapper.java接口的类的全路径相同。
2、mapper.java接口中的方法名和mapper.xml中定义的每个sql的id相同
3、mapper.java接口中的方法的输入参数类型和mapper.xml中定义的每个sql的parameterType的类型保持一致
4、mapper.java接口中方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型保持一致
好了,我们现在就按照此规范来改造之前的例子
StudentMapper.java
/** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ package cn.com; import java.util.List; public interface StudentMapper { public Student findStudentById(int id); public List<Student> findStudentByName(String name); public void insertStudent(Student student); public void deleteStudent(int id); public void updateStudent(Student student); }
StudentMapper.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="cn.com.StudentMapper"> <select id="findStudentById" parameterType="int" resultType="cn.com.Student"> SELECT * FROM student WHERE id=#{value} </select> <select id="findStudentByName" parameterType="java.lang.String" resultType="cn.com.Student"> SELECT * FROM student WHERE name LIKE '%${value}%' </select> <insert id="insertStudent" parameterType="cn.com.Student"> <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> SELECT LAST_INSERT_ID() </selectKey> INSERT INTO student (name,gender,birthday) value (#{name},#{gender},#{birthday}) </insert> <delete id="deleteStudent" parameterType="java.lang.Integer"> DELETE FROM student where id=#{id} </delete> <update id="updateStudent" parameterType="cn.com.Student"> UPDATE student set name=#{name},gender=#{gender},birthday=#{birthday} where id=#{id} </update> </mapper>
嗯哼,对照着这两个文件看就会发现:我们在书写的过程中严格遵守了开发规范。
TestCRUD.java
/** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ package cn.com; import java.io.IOException; import java.io.InputStream; import java.util.Date; 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.Before; import org.junit.Test; public class TestCRUD { private SqlSessionFactory sqlSessionFactory; @Before public void intiSqlSessionFactory() throws Exception { String resource = "SqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } @Test public void findStudentById() throws IOException{ SqlSession sqlSession=sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); Student student = studentMapper.findStudentById(5); System.out.println(student); } @Test public void findStudentByName() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); List<Student> list = studentMapper.findStudentByName("木"); for (Student student : list) { System.out.println(student); } } @Test public void insertStudent() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); Student student=new Student(); student.setName("小小木希"); student.setGender("female"); student.setBirthday(new Date()); studentMapper.insertStudent(student); sqlSession.commit(); sqlSession.close(); System.out.println(student.getId()); } @Test public void deleteStudent() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); studentMapper.deleteStudent(5); sqlSession.commit(); sqlSession.close(); } @Test public void updateStudent() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); Student student=new Student(); student.setId(5); student.setName("空空姐姐"); student.setGender("female"); student.setBirthday(new Date()); studentMapper.updateStudent(student); sqlSession.commit(); sqlSession.close(); } }
这些测试用例中,最重要的就是:
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
得到Mapper,再调用它定义的增删改查方法
最后,按照惯例还是附上项目的结构图:
相关文章推荐
- MyBatis的Mapper代理开发方式的简单使用
- mybatis入门--mapper代理方式开发
- MyBatis中Mapper代理开发方式
- mybatis3整合spring4-采用mapper动态代理的方式开发Dao
- mybatis开发Mapper代理方式
- myBatis开发Mapper代理方式
- mybatis之入门到开发(四)之 Mapper动态代理方式
- mybatis--开发Dao--原始dao方式和Mapper代理方式
- mybatis——使用mapper代理开发方式
- 后台(36)——MyBatis的原始Dao开发方式
- JavaEE_Mybatis_SpringMVC_Mybatis_lesson3_ Mapper代理的开发方式
- MyBatis学习记录(3):Mapper动态代理方式开发DAO
- mybatis入门--mapper代理方式开发
- myBatis中原始dao开发方式与Mapper代理方式
- JavaEE_Mybatis_SpringMVC_整合开发_lesson2_Mapper代理开发方式
- 基于mapper的动态代理的开发方式——MyBatis的第二种开发方式
- 【MyBatis-05】mybatis开发mapper的方式
- Mybatis(二)— 使用Mapper动态代理方式进行开发
- Mybatis Mapper代理的开发方式
- Java SSM框架之MyBatis3(二)——Mapper代理的开发方式