百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

从 0 开始手写一个 Mybatis 框架,三步搞定

ccwgpt 2024-09-18 12:24 34 浏览 0 评论

继上一篇手写SpringMVC之后,我最近趁热打铁,研究了一下Mybatis。MyBatis框架的核心功能其实不难,无非就是动态代理和jdbc的操作,难的是写出来可扩展,高内聚,低耦合的规范的代码。

本文完成的Mybatis功能比较简单,代码还有许多需要改进的地方,大家可以结合Mybatis源码去动手完善。

1

Mybatis框架流程简介



在手写自己的Mybatis框架之前,我们先来了解一下Mybatis,它的源码中使用了大量的设计模式,阅读源码并观察设计模式在其中的应用,才能够更深入的理解源码(ref:Mybatis源码解读-设计模式总结)。

我们对上图进行分析总结:

1、mybatis的配置文件有2类

  • mybatisconfig.xml,配置文件的名称不是固定的,配置了全局的参数的配置,全局只能有一个配置文件。
  • Mapper.xml 配置多个statemement,也就是多个sql,整个mybatis框架中可以有多个Mappe.xml配置文件。

2、通过mybatis配置文件得到SqlSessionFactory

3、通过SqlSessionFactory得到SqlSession,用SqlSession就可以操作数据了。

4、SqlSession通过底层的Executor(执行器),执行器有2类实现:



  • 基本实现
  • 带有缓存功能的实现

5、MappedStatement是通过Mapper.xml中定义statement生成的对象。

6、参数输入执行并输出结果集,无需手动判断参数类型和参数下标位置,且自动将结果集映射为Java对象

  • HashMap,KV格式的数据类型
  • Java的基本数据类型
  • POJO,java的对象


2

梳理自己的Mybatis的设计思路


根据上文Mybatis流程,我简化了下,分为以下步骤:



1.读取xml文件,建立连接

从图中可以看出,MyConfiguration负责与人交互。待读取xml后,将属性和连接数据库的操作封装在MyConfiguration对象中供后面的组件调用。本文将使用dom4j来读取xml文件,它具有性能优异和非常方便使用的特点。

2.创建SqlSession,搭建Configuration和Executor之间的桥梁

我们经常在使用框架时看到Session,Session到底是什么呢?一个Session仅拥有一个对应的数据库连接。类似于一个前段请求Request,它可以直接调用exec(SQL)来执行SQL语句。

从流程图中的箭头可以看出,MySqlSession的成员变量中必须得有MyExecutor和MyConfiguration去集中做调配,箭头就像是一种关联关系。我们自己的MySqlSession将有一个getMapper方法,然后使用动态代理生成对象后,就可以做数据库的操作了。

3.创建Executor,封装JDBC操作数据库

Executor是一个执行器,负责SQL语句的生成和查询缓存(缓存还没完成)的维护,也就是jdbc的代码将在这里完成,不过本文只实现了单表,有兴趣的同学可以尝试完成多表。

4.创建MapperProxy,使用动态代理生成Mapper对象

我们只是希望对指定的接口生成一个对象,使得执行它的时候能运行一句sql罢了,而接口无法直接调用方法,所以这里使用动态代理生成对象,在执行时还是回到MySqlSession中调用查询,最终由MyExecutor做JDBC查询。这样设计是为了单一职责,可扩展性更强。推荐阅读:<em>手</em><em>写</em>实现一个迷你版的Tomcat

3

实现自己的Mybatis


工程文件及目录:



首先,新建一个maven项目,在pom.xml中导入以下依赖:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.liugh</groupId>
 <artifactId>liugh-mybatis</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>
 
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <maven.compiler.source>1.8</maven.compiler.source>
 <maven.compiler.target>1.8</maven.compiler.target>
 <java.version>1.8</java.version>
 </properties>
 
 <dependencies>
 <!-- 读取xml文件 -->
 <dependency>
 <groupId>dom4j</groupId>
 <artifactId>dom4j</artifactId>
 <version>1.6.1</version>
 </dependency>
 
 <!-- MySQL -->
 <dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>5.1.29</version>
 </dependency>
 </dependencies>
</project>

创建我们的数据库xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<database>
 <property name="driverClassName">com.mysql.jdbc.Driver</property>
 <property name="url">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8</property>
 <property name="username">root</property>
 <property name="password">123456</property>
</database>

然后在数据库创建test库,执行如下SQL语句:

CREATE TABLE `user` (
 `id` varchar(64) NOT NULL,
 `password` varchar(255) DEFAULT NULL,
 `username` varchar(255) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
INSERT INTO `test`.`user` (`id`, `password`, `username`) VALUES ('1', '123456', 'liugh');

创建User实体类,和UserMapper接口和对应的xml文件:

package com.liugh.bean;
public class User {
 private String id;
 private String username;
 private String password;
 //省略get set toString方法...
}
package com.liugh.mapper;
import com.liugh.bean.User;
public interface UserMapper {
 
 public User getUserById(String id); 
}
<?xml version="1.0" encoding="UTF-8"?>
<mapper nameSpace="com.liugh.mapper.UserMapper">
 <select id="getUserById" resultType ="com.liugh.bean.User">
 select * from user where id = ?
 </select>
</mapper>

基本操作配置完成,接下来我们开始实现MyConfiguration:

package com.liugh.sqlSession;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.liugh.config.Function;
import com.liugh.config.MapperBean;
/**
* 读取与解析配置信息,并返回处理后的Environment
*/
public class MyConfiguration {
 private static ClassLoader loader = ClassLoader.getSystemClassLoader();
 /**
 * 读取xml信息并处理
 */
 public Connection build(String resource){
 try {
 InputStream stream = loader.getResourceAsStream(resource);
 SAXReader reader = new SAXReader();
 Document document = reader.read(stream);
 Element root = document.getRootElement();
 return evalDataSource(root);
 } catch (Exception e) {
 throw new RuntimeException("error occured while evaling xml " + resource);
 }
 }
 
 private Connection evalDataSource(Element node) throws ClassNotFoundException {
 if (!node.getName().equals("database")) {
 throw new RuntimeException("root should be <database>");
 }
 String driverClassName = null;
 String url = null;
 String username = null;
 String password = null;
 //获取属性节点
 for (Object item : node.elements("property")) {
 Element i = (Element) item; 
 String value = getValue(i);
 String name = i.attributeValue("name");
 if (name == null || value == null) {
 throw new RuntimeException("[database]: <property> should contain name and value");
 }
 //赋值
 switch (name) {
 case "url" : url = value; break;
 case "username" : username = value; break;
 case "password" : password = value; break;
 case "driverClassName" : driverClassName = value; break; 
 default : throw new RuntimeException("[database]: <property> unknown name"); 
 }
 }
 
 Class.forName(driverClassName); 
 Connection connection = null;
 try {
 //建立数据库链接
 connection = DriverManager.getConnection(url, username, password);
 } catch (SQLException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 return connection;
 }
 
 //获取property属性的值,如果有value值,则读取 没有设置value,则读取内容
 private String getValue(Element node) {
 return node.hasContent() ? node.getText() : node.attributeValue("value");
 }
 
 
 
 @SuppressWarnings("rawtypes")
 public MapperBean readMapper(String path){
 MapperBean mapper = new MapperBean();
 try{
 InputStream stream = loader.getResourceAsStream(path);
 SAXReader reader = new SAXReader();
 Document document = reader.read(stream);
 Element root = document.getRootElement();
 mapper.setInterfaceName(root.attributeValue("nameSpace").trim()); //把mapper节点的nameSpace值存为接口名
 List<Function> list = new ArrayList<Function>(); //用来存储方法的List
 for(Iterator rootIter = root.elementIterator();rootIter.hasNext();) {//遍历根节点下所有子节点
 Function fun = new Function(); //用来存储一条方法的信息
 Element e = (Element) rootIter.next(); 
 String sqltype = e.getName().trim();
 String funcName = e.attributeValue("id").trim();
 String sql = e.getText().trim();
 String resultType = e.attributeValue("resultType").trim();
 fun.setSqltype(sqltype);
 fun.setFuncName(funcName);
 Object newInstance=null;
 try {
 newInstance = Class.forName(resultType).newInstance();
 } catch (InstantiationException e1) {
 e1.printStackTrace();
 } catch (IllegalAccessException e1) {
 e1.printStackTrace();
 } catch (ClassNotFoundException e1) {
 e1.printStackTrace();
 }
 fun.setResultType(newInstance);
 fun.setSql(sql);
 list.add(fun);
 }
 mapper.setList(list);
 
 } catch (DocumentException e) {
 e.printStackTrace();
 }
 return mapper;
 }
}

用面向对象的思想设计读取xml配置后:

package com.liugh.config;
import java.util.List;
public class MapperBean {
 private String interfaceName; //接口名
 private List<Function> list; //接口下所有方法
 //省略 get set方法...
}

Function对象包括sql的类型、方法名、sql语句、返回类型和参数类型。

package com.liugh.config;
public class Function {
 private String sqltype; 
 private String funcName; 
 private String sql; 
 private Object resultType; 
 private String parameterType; 
 //省略 get set方法
}

接下来实现我们的MySqlSession,首先的成员变量里得有Excutor和MyConfiguration,代码的精髓就在getMapper的方法里。

package com.liugh.sqlSession;
import java.lang.reflect.Proxy;
public class MySqlsession {
 
 private Excutor excutor= new MyExcutor(); 
 
 private MyConfiguration myConfiguration = new MyConfiguration();
 
 public <T> T selectOne(String statement,Object parameter){ 
 return excutor.query(statement, parameter); 
 } 
 
 @SuppressWarnings("unchecked")
 public <T> T getMapper(Class<T> clas){ 
 //动态代理调用
 return (T)Proxy.newProxyInstance(clas.getClassLoader(),new Class[]{clas},
 new MyMapperProxy(myConfiguration,this)); 
 } 
}

紧接着创建Excutor和实现类:

package com.liugh.sqlSession;
public interface Excutor {
 public <T> T query(String statement,Object parameter); 
}

MyExcutor中封装了JDBC的操作:

package com.liugh.sqlSession;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.liugh.bean.User;
public class MyExcutor implements Excutor{
 
 private MyConfiguration xmlConfiguration = new MyConfiguration();
 
 @Override 
 public <T> T query(String sql, Object parameter) { 
 Connection connection=getConnection(); 
 ResultSet set =null;
 PreparedStatement pre =null;
 try { 
 pre = connection.prepareStatement(sql); 
 //设置参数
 pre.setString(1, parameter.toString());
 set = pre.executeQuery(); 
 User u=new User(); 
 //遍历结果集
 while(set.next()){ 
 u.setId(set.getString(1));
 u.setUsername(set.getString(2)); 
 u.setPassword(set.getString(3));
 } 
 return (T) u; 
 } catch (SQLException e) { 
 e.printStackTrace(); 
 } finally{
 try{ 
 if(set!=null){ 
 set.close(); 
 }if(pre!=null){ 
 pre.close(); 
 }if(connection!=null){ 
 connection.close(); 
 } 
 }catch(Exception e2){ 
 e2.printStackTrace(); 
 } 
 } 
 return null; 
 } 
 
 private Connection getConnection() { 
 try { 
 Connection connection =xmlConfiguration.build("config.xml");
 return connection; 
 } catch (Exception e) { 
 e.printStackTrace(); 
 } 
 return null; 
 } 
}

MyMapperProxy代理类完成xml方法和真实方法对应,执行查询:

package com.liugh.sqlSession;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.List;
import com.liugh.config.Function;
import com.liugh.config.MapperBean;
public class MyMapperProxy implements InvocationHandler{
 
 private MySqlsession mySqlsession; 
 
 private MyConfiguration myConfiguration;
 
 public MyMapperProxy(MyConfiguration myConfiguration,MySqlsession mySqlsession) { 
 this.myConfiguration=myConfiguration; 
 this.mySqlsession=mySqlsession; 
 } 
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 MapperBean readMapper = myConfiguration.readMapper("UserMapper.xml");
 //是否是xml文件对应的接口
 if(!method.getDeclaringClass().getName().equals(readMapper.getInterfaceName())){
 return null; 
 }
 List<Function> list = readMapper.getList();
 if(null != list || 0 != list.size()){
 for (Function function : list) {
 //id是否和接口方法名一样
 if(method.getName().equals(function.getFuncName())){ 
 return mySqlsession.selectOne(function.getSql(), String.valueOf(args[0])); 
 } 
 }
 }
 return null; 
 }
}

到这里,就完成了自己的Mybatis框架,我们测试一下:

package com.liugh;
import com.liugh.bean.User;
import com.liugh.mapper.UserMapper;
import com.liugh.sqlSession.MySqlsession;
public class TestMybatis {
 
 public static void main(String[] args) { 
 MySqlsession sqlsession=new MySqlsession(); 
 UserMapper mapper = sqlsession.getMapper(UserMapper.class); 
 User user = mapper.getUserById("1"); 
 System.out.println(user);
 } 
}

执行结果:



查询一个不存在的用户试试:



到这里我们就大功告成了!


现阶段私信我可以免费获得从入门到深入的Java编程学习资料一份,大数据只有入门的!!

相关推荐

十分钟让你学会LNMP架构负载均衡(impala负载均衡)

业务架构、应用架构、数据架构和技术架构一、几个基本概念1、pv值pv值(pageviews):页面的浏览量概念:一个网站的所有页面,在一天内,被浏览的总次数。(大型网站通常是上千万的级别)2、u...

AGV仓储机器人调度系统架构(agv物流机器人)

系统架构层次划分采用分层模块化设计,分为以下五层:1.1用户接口层功能:提供人机交互界面(Web/桌面端),支持任务下发、实时监控、数据可视化和报警管理。模块:任务管理面板:接收订单(如拣货、...

远程热部署在美团的落地实践(远程热点是什么意思)

Sonic是美团内部研发设计的一款用于热部署的IDEA插件,本文其实现原理及落地的一些技术细节。在阅读本文之前,建议大家先熟悉一下Spring源码、SpringMVC源码、SpringBoot...

springboot搭建xxl-job(分布式任务调度系统)

一、部署xxl-job服务端下载xxl-job源码:https://gitee.com/xuxueli0323/xxl-job二、导入项目、创建xxl_job数据库、修改配置文件为自己的数据库三、启动...

大模型:使用vLLM和Ray分布式部署推理应用

一、vLLM:面向大模型的高效推理框架1.核心特点专为推理优化:专注于大模型(如GPT-3、LLaMA)的高吞吐量、低延迟推理。关键技术:PagedAttention:类似操作系统内存分页管理,将K...

国产开源之光【分布式工作流调度系统】:DolphinScheduler

DolphinScheduler是一个开源的分布式工作流调度系统,旨在帮助用户以可靠、高效和可扩展的方式管理和调度大规模的数据处理工作流。它支持以图形化方式定义和管理工作流,提供了丰富的调度功能和监控...

简单可靠高效的分布式任务队列系统

#记录我的2024#大家好,又见面了,我是GitHub精选君!背景介绍在系统访问量逐渐增大,高并发、分布式系统成为了企业技术架构升级的必由之路。在这样的背景下,异步任务队列扮演着至关重要的角色,...

虚拟服务器之间如何分布式运行?(虚拟服务器部署)

  在云计算和虚拟化技术快速发展的今天,传统“单机单任务”的服务器架构早已难以满足现代业务对高并发、高可用、弹性伸缩和容错容灾的严苛要求。分布式系统应运而生,并成为支撑各类互联网平台、企业信息系统和A...

一文掌握 XXL-Job 的 6 大核心组件

XXL-Job是一个分布式任务调度平台,其核心组件主要包括以下部分,各组件相互协作实现高效的任务调度与管理:1.调度注册中心(RegistryCenter)作用:负责管理调度器(Schedule...

京东大佬问我,SpringBoot中如何做延迟队列?单机与分布式如何做?

京东大佬问我,SpringBoot中如何做延迟队列?单机如何做?分布式如何做呢?并给出案例与代码分析。嗯,用户问的是在SpringBoot中如何实现延迟队列,单机和分布式环境下分别怎么做。这个问题其实...

企业级项目组件选型(一)分布式任务调度平台

官网地址:https://www.xuxueli.com/xxl-job/能力介绍架构图安全性为提升系统安全性,调度中心和执行器进行安全性校验,双方AccessToken匹配才允许通讯;调度中心和执...

python多进程的分布式任务调度应用场景及示例

多进程的分布式任务调度可以应用于以下场景:分布式爬虫:importmultiprocessingimportrequestsdefcrawl(url):response=re...

SpringBoot整合ElasticJob实现分布式任务调度

介绍ElasticJob是面向互联网生态和海量任务的分布式调度解决方案,由两个相互独立的子项目ElasticJob-Lite和ElasticJob-Cloud组成。它通过弹性调度、资源管控、...

分布式可视化 DAG 任务调度系统 Taier 的整体流程分析

Taier作为袋鼠云的开源项目之一,是一个分布式可视化的DAG任务调度系统。旨在降低ETL开发成本,提高大数据平台稳定性,让大数据开发人员可以在Taier直接进行业务逻辑的开发,而不用关...

SpringBoot任务调度:@Scheduled与TaskExecutor全面解析

一、任务调度基础概念1.1什么是任务调度任务调度是指按照预定的时间计划或特定条件自动执行任务的过程。在现代应用开发中,任务调度扮演着至关重要的角色,它使得开发者能够自动化处理周期性任务、定时任务和异...

取消回复欢迎 发表评论: