-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
252f40d
commit 988b068
Showing
11 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# mini-mybatis | ||
该项目是mybatis的mini版本,包含动态SQL,参数绑定,结果集处理,以及插件支持等核心功能,写mini-mybatis的初衷是想参照现有mybatis的功能,自己从0到1实现一个简化版,同时也希望帮助其他人熟悉mybatis的设计思想以及源码理解,为降低代码复杂度,该项目主要以实现核心功能为主,不会太注重性能以及线程安全等问题。该项目采用一步一步(step-by-step)的方式完善功能,每一个小功能模块使用一个独立的分支,分支前缀带有递增序号,序号由小到大表示功能的完善程度。 | ||
# 功能模块 | ||
[1.注册Mapper接口](https://github.com/FuriousPws002/mini-mybatis/wiki/1.%E6%B3%A8%E5%86%8CMapper%E6%8E%A5%E5%8F%A3 "Markdown") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.furious</groupId> | ||
<artifactId>mini-mybatis</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
|
||
<name>mini-mybatis</name> | ||
<description>mini-mybatis</description> | ||
<properties> | ||
<java.version>8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.13.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>8</source> | ||
<target>8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
27 changes: 27 additions & 0 deletions
27
src/main/java/org/apache/ibatis/binding/BindingException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.apache.ibatis.binding; | ||
|
||
/** | ||
* @author furious 2024/4/3 | ||
*/ | ||
public class BindingException extends RuntimeException { | ||
|
||
public BindingException() { | ||
super(); | ||
} | ||
|
||
public BindingException(String message) { | ||
super(message); | ||
} | ||
|
||
public BindingException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public BindingException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
protected BindingException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | ||
super(message, cause, enableSuppression, writableStackTrace); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.apache.ibatis.binding; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
|
||
import org.apache.ibatis.session.SqlSession; | ||
|
||
/** | ||
* @author furious 2024/4/3 | ||
*/ | ||
public class MapperProxy<T> implements InvocationHandler { | ||
|
||
private final SqlSession sqlSession; | ||
private final Class<T> mapperInterface; | ||
|
||
public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface) { | ||
this.sqlSession = sqlSession; | ||
this.mapperInterface = mapperInterface; | ||
} | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
return null; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/org/apache/ibatis/binding/MapperProxyFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.apache.ibatis.binding; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Proxy; | ||
|
||
import org.apache.ibatis.session.SqlSession; | ||
|
||
/** | ||
* @author furious 2024/4/3 | ||
*/ | ||
public class MapperProxyFactory<T> { | ||
|
||
private final Class<T> mapperInterface; | ||
|
||
public MapperProxyFactory(Class<T> mapperInterface) { | ||
this.mapperInterface = mapperInterface; | ||
} | ||
|
||
public <T> T newInstance(SqlSession sqlSession) { | ||
InvocationHandler handler = new MapperProxy(sqlSession, mapperInterface); | ||
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[]{mapperInterface}, handler); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/org/apache/ibatis/binding/MapperRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.apache.ibatis.binding; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import org.apache.ibatis.session.Configuration; | ||
import org.apache.ibatis.session.SqlSession; | ||
|
||
/** | ||
* @author furious 2024/4/3 | ||
*/ | ||
public class MapperRegistry { | ||
|
||
private final Configuration config; | ||
private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<>(); | ||
|
||
public MapperRegistry(Configuration config) { | ||
this.config = config; | ||
} | ||
|
||
public <T> boolean hasMapper(Class<T> type) { | ||
return knownMappers.containsKey(type); | ||
} | ||
|
||
public <T> void addMapper(Class<T> type) { | ||
//不是接口不支持,直接返回 | ||
if (!type.isInterface() || hasMapper(type)) { | ||
return; | ||
} | ||
knownMappers.put(type, new MapperProxyFactory<>(type)); | ||
} | ||
|
||
public <T> T getMapper(Class<T> type, SqlSession sqlSession) { | ||
MapperProxyFactory<?> mapperProxyFactory = knownMappers.get(type); | ||
if (Objects.isNull(mapperProxyFactory)) { | ||
throw new BindingException(type + " type is not exist"); | ||
} | ||
return mapperProxyFactory.newInstance(sqlSession); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/org/apache/ibatis/session/Configuration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.apache.ibatis.session; | ||
|
||
import org.apache.ibatis.binding.MapperRegistry; | ||
|
||
/** | ||
* mybatis全局配置类 | ||
* | ||
* @author furious 2024/4/3 | ||
*/ | ||
public class Configuration { | ||
|
||
private final MapperRegistry mapperRegistry = new MapperRegistry(this); | ||
|
||
public Configuration() { | ||
} | ||
|
||
public <T> void addMapper(Class<T> type) { | ||
mapperRegistry.addMapper(type); | ||
} | ||
|
||
public <T> T getMapper(Class<T> type, SqlSession sqlSession) { | ||
return mapperRegistry.getMapper(type, sqlSession); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.apache.ibatis.session; | ||
|
||
import java.io.Closeable; | ||
|
||
/** | ||
* @author furious 2024/3/29 | ||
*/ | ||
public interface SqlSession extends Closeable { | ||
|
||
Configuration getConfiguration(); | ||
|
||
<T> T getMapper(Class<T> type); | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/org/apache/ibatis/session/defaults/DefaultSqlSession.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.apache.ibatis.session.defaults; | ||
|
||
import java.io.IOException; | ||
|
||
import org.apache.ibatis.session.Configuration; | ||
import org.apache.ibatis.session.SqlSession; | ||
|
||
/** | ||
* @author furious 2024/4/3 | ||
*/ | ||
public class DefaultSqlSession implements SqlSession { | ||
|
||
private final Configuration configuration; | ||
|
||
public DefaultSqlSession(Configuration configuration) { | ||
this.configuration = configuration; | ||
} | ||
|
||
@Override | ||
public Configuration getConfiguration() { | ||
return configuration; | ||
} | ||
|
||
@Override | ||
public <T> T getMapper(Class<T> type) { | ||
return configuration.getMapper(type, this); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
|
||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/test/java/org/apache/ibatis/binding/MapperRegistryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.apache.ibatis.binding; | ||
|
||
import org.apache.ibatis.dao.UserMapper; | ||
import org.apache.ibatis.session.Configuration; | ||
import org.apache.ibatis.session.SqlSession; | ||
import org.apache.ibatis.session.defaults.DefaultSqlSession; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* @author furious 2024/4/3 | ||
*/ | ||
public class MapperRegistryTest { | ||
|
||
@Test | ||
public void test() { | ||
Configuration configuration = new Configuration(); | ||
configuration.addMapper(UserMapper.class); | ||
SqlSession sqlSession = new DefaultSqlSession(configuration); | ||
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); | ||
Assert.assertNotNull(userMapper); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.apache.ibatis.dao; | ||
|
||
/** | ||
* @author furious 2024/4/3 | ||
*/ | ||
public interface UserMapper { | ||
} |