后台(37)——MyBatis的Mapper开发方式

探索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–滑动冲突的产生及其处理


版权声明


使用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,再调用它定义的增删改查方法

最后,按照惯例还是附上项目的结构图:

后台(37)——MyBatis的Mapper开发方式

上一篇:【Java开发者专场】阿里专家杜万:Java响应式编程,一文全面解读


下一篇:【工具】screen 的使用