从 0 开始手写一个 Mybatis 框架,三步搞定!
MyBatis框架的核心功能其实不难,无非就是动态代理和jdbc的操作,难的是写出来可扩展,高内聚,低耦合的规范的代码。
本文完成的Mybatis功能比较简单,代码还有许多需要改进的地方,大家可以结合Mybatis源码去动手完善。
1. Mybatis框架流程简介
我们对上图进行分析总结:
- mybatisconfig.xml,配置文件的名称不是固定的,配置了全局的参数的配置,全局只能有一个配置文件。
- Mapper.xml 配置多个statemement,也就是多个sql,整个mybatis框架中可以有多个Mappe.xml配置文件。
- 基本实现
- 带有缓存功能的实现
- HashMap,KV格式的数据类型
- Java的基本数据类型
- POJO,java的对象
根据上文Mybatis流程,我简化了下,分为以下步骤:
我们经常在使用框架时看到Session,Session到底是什么呢?一个Session仅拥有一个对应的数据库连接。类似于一个前段请求Request,它可以直接调用exec(SQL)来执行SQL语句。
3.创建Executor,封装JDBC操作数据库
4.创建MapperProxy,使用动态代理生成Mapper对象
3. 实现自己的Mybatis
<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 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>
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');
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>
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;
}
}
package com.liugh.config; import java.util.List;
public class MapperBean { private String interfaceName; //接口名
private List<Function> list; //接口下所有方法
//省略 get set方法... }
package com.liugh.config; public class Function {
private String sqltype;
private String funcName;
private String sql;
private Object resultType;
private String parameterType;
//省略 get set方法
}
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));
} }
package com.liugh.sqlSession; public interface Excutor {
public <T> T query(String statement,Object parameter);
}
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;
}
}
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;
}
}
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);
}
}
从 0 开始手写一个 Mybatis 框架,三步搞定!的更多相关文章
- 从0 开始手写一个 RPC 框架,轻松搞定!
Java技术栈 www.javastack.cn 优秀的Java技术公众号 来源:juejin.im/post/5c4481a4f265da613438aec3 之前在 RPC框架底层到底什么原理得知 ...
- 剖析手写Vue,你也可以手写一个MVVM框架
剖析手写Vue,你也可以手写一个MVVM框架# 邮箱:563995050@qq.com github: https://github.com/xiaoqiuxiong 作者:肖秋雄(eddy) 温馨提 ...
- 看年薪50W的架构师如何手写一个SpringMVC框架
前言 做 Java Web 开发的你,一定听说过SpringMVC的大名,作为现在运用最广泛的Java框架,它到目前为止依然保持着强大的活力和广泛的用户群. 本文介绍如何用eclipse一步一步搭建S ...
- 自己手写一个SpringMVC 框架
一.了解SpringMVC运行流程及九大组件 1.SpringMVC 的运行流程 · 用户发送请求至前端控制器DispatcherServlet · DispatcherServlet收到请求调用 ...
- 手写一个HTTP框架:两个类实现基本的IoC功能
jsoncat: 仿 Spring Boot 但不同于 Spring Boot 的一个轻量级的 HTTP 框架 国庆节的时候,我就已经把 jsoncat 的 IoC 功能给写了,具体可以看这篇文章&l ...
- 手写一个RPC框架
一.前言 前段时间看到一篇不错的文章<看了这篇你就会手写RPC框架了>,于是便来了兴趣对着实现了一遍,后面觉得还有很多优化的地方便对其进行了改进. 主要改动点如下: 除了Java序列化协议 ...
- 【Spring系列】自己手写一个 SpringMVC 框架
参考文章 一.了解SpringMVC运行流程及九大组件 1.SpringMVC的运行流程 1)用户发送请求至前端控制器DispatcherServlet 2)DispatcherServlet收到请求 ...
- 自己手写一个SpringMVC框架
前端框架很多,但没有一个框架称霸,后端框架现在Spring已经完成大一统.所以学习Spring是Java程序员的必修课. Spring框架对于Java后端程序员来说再熟悉不过了,以前只知道它用的反射实 ...
- 手写一个SpringMVC框架(转)
一:梳理SpringMVC的设计思路 本文只实现自己的@Controller.@RequestMapping.@RequestParam注解起作用,其余SpringMVC功能读者可以尝试自己实现. 1 ...
随机推荐
- JavaSE基础知识(5)—面向对象(5.5 this和super关键字)
一.this关键字 1.说明 this关键字代表当前类的对象,可以访问本类的属性.方法.构造器注意:谁调用该方法,则this就指谁 2.语法 访问属性: this.属性名 = 值; System.ou ...
- How to decode input data from a contract transaction without ABI?
1 I've found some libraries which decode input from transaction, but all of them require ABI of cont ...
- Linux驱动之USB总线驱动程序框架简析
通用串行总线(USB)是主机和外围设备之间的一种连接.USB总线规范有1.1版和2.0版,当然现在已经有了3.0版本.USB1.1支持两种传输速度:低速为1.5Mbps,高速为12Mbps.USB2. ...
- 利用正则表达式实现python强口令检测
""" Chapter 7 模式匹配和正则表达式 1 用import re 导入正则表达式模块 2 用re.compile()函数创建一个Regex对象(记得使用原始字符 ...
- 27. pt-table-checksum
在主库执行命令: pt-table-checksum -h 192.168.100.101 -P 3306 -u admin -p admin \--nocheck-binlog-format --r ...
- java idea导入ecli项目
转:https://blog.csdn.net/deng11408205/article/details/79723213 1.关闭所有项目:开启idea进入导入项目选项 2.选择.classpath ...
- ssh多台主机之间不用密码远程
二.多台服务器相互无密码访问 多台服务器相互无密码访问,与两台服务器单向无密码访问的原理是一样的,只不过由于是多台服务器之间相互无密码访问,不能象两台服务器无密码登录那样直接上传,步骤如下: 1.在需 ...
- 加密 解密 RSA & AES & DES
git: https://github.com/XHTeng/XHCryptorTools rsa RSA加解密中必须考虑到的密钥长度.明文长度和密文长度问题.明文长度需要小于密钥长度,而密文长度则等 ...
- node-sass 不能正常安装解决办法
web前端在安装node包时,总是报错,究其原因是node-sass没有被正常安装. 根本原因是国内网络的原因. 最终的解决方法是通过淘宝的npm镜像安装node-sass 首先安装cnpm npm ...
- (25)Teach girls bravery, not perfection
https://www.ted.com/talks/reshma_saujani_teach_girls_bravery_not_perfection/transcript00:12So a few ...