Skip to content

Commit

Permalink
执行静态sql
Browse files Browse the repository at this point in the history
  • Loading branch information
FuriousPws002 committed Apr 8, 2024
1 parent 10c2777 commit 28af87f
Show file tree
Hide file tree
Showing 17 changed files with 349 additions and 1 deletion.
Binary file added assets/img/seq_static_sql_execution.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
</dependencies>

<build>
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/org/apache/ibatis/binding/MapperMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.apache.ibatis.binding;

import java.lang.reflect.Method;
import java.util.Objects;

import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;

/**
* @author furious 2024/4/8
*/
public class MapperMethod {

private final SqlCommand command;

public MapperMethod(Class<?> mapperInterface, Method method, Configuration config) {
this.command = new SqlCommand(config, mapperInterface, method);
}

public Object execute(SqlSession sqlSession, Object[] args) {
Object result;
switch (command.getType()) {
case INSERT: {
result = sqlSession.insert(command.getName(), args);
break;
}
default:
throw new BindingException(command.getType() + " not support");
}
return result;
}

public static class SqlCommand {

private final String name;
private final SqlCommandType type;

public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {
final String methodName = method.getName();
String statementId = mapperInterface.getName() + "." + methodName;
MappedStatement ms = configuration.getMappedStatement(statementId);
if (Objects.isNull(ms)) {
throw new BindingException(statementId + " statement not found");
}
this.name = ms.getId();
this.type = ms.getSqlCommandType();
}

public String getName() {
return name;
}

public SqlCommandType getType() {
return type;
}
}

}
3 changes: 2 additions & 1 deletion src/main/java/org/apache/ibatis/binding/MapperProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface) {

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return null;
final MapperMethod mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
return mapperMethod.execute(sqlSession, args);
}
}
13 changes: 13 additions & 0 deletions src/main/java/org/apache/ibatis/executor/Executor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.apache.ibatis.executor;

import java.sql.SQLException;

import org.apache.ibatis.mapping.MappedStatement;

/**
* @author furious 2024/4/8
*/
public interface Executor {

int update(MappedStatement ms, Object parameter) throws SQLException;
}
27 changes: 27 additions & 0 deletions src/main/java/org/apache/ibatis/executor/ExecutorException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.apache.ibatis.executor;

/**
* @author furious 2024/4/8
*/
public class ExecutorException extends RuntimeException {

public ExecutorException() {
super();
}

public ExecutorException(String message) {
super(message);
}

public ExecutorException(String message, Throwable cause) {
super(message, cause);
}

public ExecutorException(Throwable cause) {
super(cause);
}

protected ExecutorException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
47 changes: 47 additions & 0 deletions src/main/java/org/apache/ibatis/executor/SimpleExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.apache.ibatis.executor;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Objects;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.executor.statement.StatementUtil;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.Configuration;

/**
* @author furious 2024/4/8
*/
public class SimpleExecutor implements Executor {

private final Configuration configuration;

public SimpleExecutor(Configuration configuration) {
if (Objects.isNull(configuration.getDataSource())) {
throw new ExecutorException("dataSource in Configuration cannot be empty");
}
this.configuration = configuration;
}

@Override
public int update(MappedStatement ms, Object parameter) throws SQLException {
Statement stmt = null;
try {
StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, null);
stmt = prepareStatement(handler);
return handler.update(stmt);
} finally {
StatementUtil.closeStatement(stmt);
}
}

private Statement prepareStatement(StatementHandler handler) throws SQLException {
Statement stmt;
Connection connection = configuration.getDataSource().getConnection();
connection.setAutoCommit(true);
stmt = handler.prepare(connection, null);
return stmt;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.apache.ibatis.executor.statement;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Objects;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.Configuration;

/**
* @author furious 2024/4/8
*/
public class PrepareStatementHandler implements StatementHandler {

protected final Configuration configuration;
protected final Executor executor;
protected final MappedStatement mappedStatement;
protected BoundSql boundSql;

public PrepareStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
this.configuration = mappedStatement.getConfiguration();
this.executor = executor;
this.mappedStatement = mappedStatement;
if (Objects.isNull(boundSql)) {
boundSql = mappedStatement.getBoundSql(parameterObject);
}
this.boundSql = boundSql;
}

@Override
public Statement prepare(Connection connection, Integer transactionTimeout) throws SQLException {
return connection.prepareStatement(boundSql.getSql());
}

@Override
public int update(Statement statement) throws SQLException {
PreparedStatement ps = (PreparedStatement) statement;
ps.execute();
return ps.getUpdateCount();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.apache.ibatis.executor.statement;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
* @author furious 2024/4/8
*/
public interface StatementHandler {

Statement prepare(Connection connection, Integer transactionTimeout) throws SQLException;

int update(Statement statement) throws SQLException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.apache.ibatis.executor.statement;

import java.sql.SQLException;
import java.sql.Statement;

/**
* @author furious 2024/4/8
*/
public enum StatementUtil {
;

public static void closeStatement(Statement statement) {
if (statement != null) {
try {
statement.close();
} catch (SQLException ignore) {
}
}
}

}
13 changes: 13 additions & 0 deletions src/main/java/org/apache/ibatis/mapping/MappedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ public String getId() {
return id;
}

public SqlCommandType getSqlCommandType() {
return sqlCommandType;
}

public Configuration getConfiguration() {
return configuration;
}

public BoundSql getBoundSql(Object parameterObject) {
BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
return new BoundSql(boundSql.getSql());
}

public static class Builder {
private MappedStatement mappedStatement = new MappedStatement();

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/apache/ibatis/session/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
import java.util.Map;
import java.util.Set;

import javax.sql.DataSource;

import org.apache.ibatis.binding.MapperRegistry;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.PrepareStatementHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;

/**
Expand All @@ -17,6 +22,8 @@
*/
public class Configuration {

private DataSource dataSource;

private final MapperRegistry mapperRegistry = new MapperRegistry(this);
private final Set<String> loadedResources = new HashSet<>();
private final Map<String, MappedStatement> mappedStatements = new HashMap<>();
Expand Down Expand Up @@ -55,4 +62,16 @@ public Collection<String> getMappedStatementNames() {
public Collection<MappedStatement> getMappedStatements() {
return mappedStatements.values();
}

public DataSource getDataSource() {
return dataSource;
}

public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
return new PrepareStatementHandler(executor, mappedStatement, parameterObject, boundSql);
}
}
3 changes: 3 additions & 0 deletions src/main/java/org/apache/ibatis/session/SqlSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ public interface SqlSession extends Closeable {

<T> T getMapper(Class<T> type);

int insert(String statement, Object parameter);

int update(String statement, Object parameter);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package org.apache.ibatis.session.defaults;

import java.io.IOException;
import java.util.Objects;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.SimpleExecutor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;

Expand All @@ -11,9 +15,18 @@
public class DefaultSqlSession implements SqlSession {

private final Configuration configuration;
private final Executor executor;

public DefaultSqlSession(Configuration configuration) {
this(configuration, null);
}

public DefaultSqlSession(Configuration configuration, Executor executor) {
if (Objects.isNull(executor)) {
executor = new SimpleExecutor(configuration);
}
this.configuration = configuration;
this.executor = executor;
}

@Override
Expand All @@ -26,6 +39,21 @@ public <T> T getMapper(Class<T> type) {
return configuration.getMapper(type, this);
}

@Override
public int insert(String statement, Object parameter) {
return update(statement, parameter);
}

@Override
public int update(String statement, Object parameter) {
try {
MappedStatement ms = configuration.getMappedStatement(statement);
return executor.update(ms, null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public void close() throws IOException {

Expand Down
Loading

0 comments on commit 28af87f

Please sign in to comment.