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

简介: 探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 详解Android主流框架不可或缺的基石 站在源码的肩膀上全解Scroller工作机制Android多分...

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

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

这里写图片描述

相关文章
|
1月前
|
SQL XML Java
mybatis Mapper的概念与实战
MyBatis 是一个流行的 Java 持久层框架,它提供了对象关系映射(ORM)的功能,使得Java对象和数据库中的表之间的映射变得简单。在MyBatis中,Mapper是一个核心的概念,它定义了映射到数据库操作的接口。简而言之,Mapper 是一个接口,MyBatis 通过这个接口与XML映射文件或者注解绑定,以实现对数据库的操作。
33 1
|
2月前
|
Java 数据库连接 Maven
使用mybatis插件generator生成实体类,dao层和mapper映射
使用mybatis插件generator生成实体类,dao层和mapper映射
47 0
|
5月前
|
SQL XML Java
mybatis的注解开发之三种动态sql
mybatis的注解开发之三种动态sql
|
3月前
|
SQL Java 数据库连接
Mybatis之Mybatis简介、搭建Mybatis相关步骤(开发环境、maven、核心配置文件、mapper接口、映射文件、junit测试、log4j日志)
【1月更文挑战第2天】 MyBatis最初是Apache的一个开源项目iBatis, 2010年6月这个项目由Apache Software Foundation迁移到了Google Code。随着开发团队转投Google Code旗下,iBatis3.x正式更名为MyBatis。代码于2013年11月迁移到Github iBatis一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBatis提供的持久层框架包括SQL Maps和Data Access Objects(DAO)
197 3
Mybatis之Mybatis简介、搭建Mybatis相关步骤(开发环境、maven、核心配置文件、mapper接口、映射文件、junit测试、log4j日志)
|
7天前
|
SQL Java 数据库连接
MyBatis精髓揭秘:Mapper代理实现的黑盒探索
MyBatis精髓揭秘:Mapper代理实现的黑盒探索
16 1
|
1月前
|
JavaScript Java 数据库连接
Spring Boot 2.0+Mybatis+Vue的轻量级后台管理系统
Spring Boot 2.0+Mybatis+Vue的轻量级后台管理系统
|
3月前
|
Java 数据库连接 数据库
JAVAEE框架技术之10-myBatis注解式开发
JAVAEE框架技术之10-myBatis注解式开发
55 0
JAVAEE框架技术之10-myBatis注解式开发
|
3月前
|
SQL Java 数据库连接
快速上手MyBatis Plus:简化CRUD操作,提高开发效率!
快速上手MyBatis Plus:简化CRUD操作,提高开发效率!
|
3月前
|
SQL Java 数据库连接
Mybatis 开发神器 Fast MyBatis
Mybatis 开发神器 Fast MyBatis
31 1