MyBatis——创建mapper、mybatis主配置文件模板、使用工具类或传统dao方式实现数据库的一些操作、MyBatis代理

简介: MyBatis——创建mapper、mybatis主配置文件模板、使用工具类或传统dao方式实现数据库的一些操作、MyBatis代理

文章目录:


1.创建相关xml文件的模板

1.1 mapper.xml文件模板 

1.2 mybatis.xml主配置文件模板

2.使用工具类MyBatisUtil来实现数据库的操作

2.1 工具类MyBatisUtil的编写 

2.2 工具类对应的StudentDao接口

2.3 工具类对应的测试类MyTest

3.使用传统dao方式来实现数据库的操作

3.1 创建一个存放接口实现类的包impl

3.2 编写接口的相关实现类 

3.3 编写dao方式下的测试类

4.什么是MyBatis代理?

4.1 工具类中的测试代码 

4.2 传统dao方式的测试代码

4.3 MyBatis代理

1.创建相关xml文件的模板


由于在项目开发中,我们会经常用到一些类似的代码块,所以在这里我们可以给那些经常使用的文件创建一个自己的模板。 

1.1 mapper.xml文件模板 

mapper.xml 文件模板中,我们需要修改的地方有: 1.     namespace 命名空间(使用对应接口的全限定名称)

2.    添加相应的 insertupdatedeleteselect标签来写sql语句(注意id为接口中对应的方法名,resultType只在select标签中存在,为需要返回的具体对象类型)


1.2 mybatis.xml主配置文件模板

mybatis.xml主配置文件模板,我们需要修改的地方只有一个(模板中的数据源一般不做修改),如下图:👇👇👇

这个resource表示读取到的 mapper 文件的具体位置。

2.使用工具类MyBatisUtil来实现数据库的操作


在我们的测试类中,可能会发现总是出现一些相同的代码,比如说,在selectinsert相关操作的方法中,都有下面这样几行代码 

首先呢,这几行代码非常重要,没有这几行代码的基础,是无法对数据库进行操作的。

但是,我们在测试类中,每写一个方法,都要加上这样的几行,让人觉得太麻烦了,而且会造成一定量的代码冗余,所以我们可以将这几行重要的代码封装到一个工具类MyBatisUtil中,见如下代码:👇👇👇

2.1 工具类MyBatisUtil的编写 

package com.bjpowernode.utils;
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 java.io.IOException;
import java.io.InputStream;
/**
 *  工具类:创建SqlSession对象
 */
public class MyBatisUtil {
    private static SqlSessionFactory factory=null;
    static {
        String config="mybatis.xml";
        try {
            InputStream inputStream=Resources.getResourceAsStream(config);
            factory=new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static SqlSession getSqlSession() {
        SqlSession session=null;
        if(factory!=null) {
            session=factory.openSession();
        }
        return session;
    }
}

可以看到,这样一来,我们的工具类就编写完成了,主要就是加载mybatis主配置文件,读取其中的内容,再创建相应的SqlSessionFactorySqlSession对象。

2.2 工具类对应的StudentDao接口

package com.bjpowernode.dao;
import com.bjpowernode.entity.Student;
import java.util.List;
/**
 *
 */
public interface StudentDao {
    Student selectById(Integer id);
    List<Student> selectStudents();
    int insertStudent(Student student);
}

2.3 工具类对应的测试类MyTest

    @Test
    public void testSelectById() {
        //1.获取SqlSession
        SqlSession session = MyBatisUtil.getSqlSession();
        //2.指定sqlId
        String sqlId="com.bjpowernode.dao.StudentDao.selectById";
        //3.执行SqlSession的方法,表示执行sql语句
        Student student=session.selectOne(sqlId,1001);
        System.out.println("查询的结果===" + student);
        //4.关闭SqlSession对象
        session.close();
    }

    @Test
    public void testSelectStudents() {
        //1.获取SqlSession
        SqlSession session = MyBatisUtil.getSqlSession();
        //2.指定sqlId
        String sqlId="com.bjpowernode.dao.StudentDao.selectStudents";
        //3.执行SqlSession的方法,表示执行sql语句
        List<Student> students=session.selectList(sqlId);
        for(Student stu : students) {
            System.out.println("student===" + stu);
        }
        //4.关闭SqlSession对象
        session.close();
    }

    @Test
    public void testInsertStudent() {
        //1.获取SqlSession
        SqlSession session = MyBatisUtil.getSqlSession();
        //2.指定sqlId
        String sqlId="com.bjpowernode.dao.StudentDao.insertStudent";
        //3.执行SqlSession的方法,表示执行sql语句
        Student student=new Student();
        student.setId(1008);
        student.setName("张起灵");
        student.setEmail("zhangqiling@qq.com");
        student.setAge(20);
        int rows=session.insert(sqlId,student);
        session.commit();
        System.out.println("insert的行数===" + rows);
        //4.关闭SqlSession对象
        session.close();
    }

在上面三个测试代码中,我们首先通过 SqlSession 对象来调用工具类中写好的方法 getSqlSession() (这个方法中已经加载过主配置文件,同时创建好了 SqlSessionFactory 对象)。之后再根据具体的 mapper 文件中的 namespaceid等相关信息,定义好要读取的 sqlId,最终执行就可以了。

3.使用传统dao方式来实现数据库的操作


在之前使用工具类来实现数据库的操作时,我们发现好像并没有用到 StudentDao 这个接口,这个接口呢,只是为我们提供了 mapper 文件中的namespaceidresultType这些内容。 

那么我们都知道,既然创建了接口,那为什么不再创建一个接口的实现类去实现这个接口呢?

下面,我就通过MyBatis框架使用传统的dao方式来实现数据库的操作。

首先,在我们的dao包下,创建一个 impl 包,用来存放接口的相关实现类。

3.1 创建一个存放接口实现类的包impl

3.2 编写接口的相关实现类 

之后,我们在这个实现类中,去编写实现接口方法的详细代码:👇👇👇

package com.bjpowernode.dao.impl;
import com.bjpowernode.dao.StudentDao;
import com.bjpowernode.entity.Student;
import com.bjpowernode.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
/**
 *
 */
public class StudentDaoImpl implements StudentDao {
    @Override
    public Student selectById(Integer id) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        String sqlId="com.bjpowernode.dao.StudentDao.selectById";
        Student student=sqlSession.selectOne(sqlId,id);
        sqlSession.close();
        return student;
    }
    @Override
    public List<Student> selectStudents() {
        SqlSession session = MyBatisUtil.getSqlSession();
        String sqlId="com.bjpowernode.dao.StudentDao.selectStudents";
        List<Student> students=session.selectList(sqlId);
        session.close();
        return students;
    }
    @Override
    public int insertStudent(Student student) {
        SqlSession session = MyBatisUtil.getSqlSession();
        String sqlId="com.bjpowernode.dao.StudentDao.insertStudent";
        int rows=session.insert(sqlId,student);
        session.commit();
        System.out.println("insert的行数===" + rows);
        session.close();
        return rows;
    }
}

3.3 编写dao方式下的测试类

    @Test
    public void testSelectOne() {
        StudentDao studentDao=new StudentDaoImpl();
        Student student=studentDao.selectById(1001);
        System.out.println("通过dao执行方法得到的对象==" + student);
    }

    @Test
    public void testSelectStudents() {
        StudentDao studentDao=new StudentDaoImpl();
        List<Student> students=studentDao.selectStudents();
        for(Student stu : students) {
            System.out.println("stu===" + stu);
        }
    }

    @Test
    public void testInsert() {
        StudentDao studentDao=new StudentDaoImpl();
        Student student=new Student();
        student.setId(1009);
        student.setName("王芳");
        student.setEmail("wangfang@qq.com");
        student.setAge(33);
        studentDao.insertStudent(student);
    }


4.什么是MyBatis代理?


在说MyBatis代理之前,我们先来看下面两段代码:👇👇👇 

4.1 工具类中的测试代码 

    @Test
    public void testSelectById() {
        SqlSession session = MyBatisUtil.getSqlSession();
        String sqlId="com.bjpowernode.dao.StudentDao.selectById";
        Student student=session.selectOne(sqlId,1001);
        System.out.println("查询的结果===" + student);
        session.close();
    }

4.2 传统dao方式的测试代码

    @Test
    public void testSelectOne() {
        StudentDao studentDao=new StudentDaoImpl();
        Student student=studentDao.selectById(1001);
        System.out.println("通过dao执行方法得到的对象==" + student);
    }

大家看看这两段代码是不是有很多相似的地方,第一段代码是通过MyBatis框架来进行数据库操作的,第二段代码是通过我们手动创建来进行数据库操作的。

那么在这两段代码的差异中,就隐藏了MyBatis代理!!!

4.3 MyBatis代理


相关文章
|
XML Oracle Java
mybatis反向生成实体类、dao层以及映射文件
mybatis反向生成实体类、dao层以及映射文件
Mybatis+mysql动态分页查询数据案例——分页工具类(Page.java)
Mybatis+mysql动态分页查询数据案例——分页工具类(Page.java)
Mybatis+mysql动态分页查询数据案例——工具类(MybatisUtil.java)
Mybatis+mysql动态分页查询数据案例——工具类(MybatisUtil.java)
|
12月前
|
SQL Java 数据库连接
mybatis使用四:dao接口参数与mapper 接口中SQL的对应和对应方式的总结,MyBatis的parameterType传入参数类型
这篇文章是关于MyBatis中DAO接口参数与Mapper接口中SQL的对应关系,以及如何使用parameterType传入参数类型的详细总结。
325 10
|
Java 数据库连接 mybatis
SpringBoot配置Mybatis注意事项,mappers层下的name命名空间,要落实到Dao的video类,resultType要落到bean,配置好mybatis的对应依赖。
SpringBoot配置Mybatis注意事项,mappers层下的name命名空间,要落实到Dao的video类,resultType要落到bean,配置好mybatis的对应依赖。
|
SQL Java 数据库连接
Mybatis如何使用mapper代理开发
Mybatis如何使用mapper代理开发
|
XML 关系型数据库 数据库
使用mybatis-generator插件生成postgresql数据库model、mapper、xml
使用mybatis-generator插件生成postgresql数据库model、mapper、xml
1096 0
|
XML Java 数据库连接
探秘MyBatis:手写Mapper代理的源码解析与实现
探秘MyBatis:手写Mapper代理的源码解析与实现
142 1
|
SQL Java 数据库连接
MyBatis精髓揭秘:Mapper代理实现的黑盒探索
MyBatis精髓揭秘:Mapper代理实现的黑盒探索
221 1
|
Java 数据库连接 mybatis
MyBatis中Mapper接口和dao区别是什么?
MyBatis中Mapper接口和dao区别是什么?
342 0