-
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.
Merge pull request #5 from FuriousPws002/04-parameter-binding-with-pa…
…ram-annotation 04 parameter binding with param annotation
- Loading branch information
Showing
41 changed files
with
896 additions
and
6 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
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
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,18 @@ | ||
package org.apache.ibatis.annotations; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* @author furious 2024/4/10 | ||
*/ | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.PARAMETER) | ||
public @interface Param { | ||
|
||
String value(); | ||
} |
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
49 changes: 49 additions & 0 deletions
49
src/main/java/org/apache/ibatis/builder/SqlSourceBuilder.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,49 @@ | ||
package org.apache.ibatis.builder; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.apache.ibatis.mapping.ParameterMapping; | ||
import org.apache.ibatis.mapping.SqlSource; | ||
import org.apache.ibatis.parsing.GenericTokenParser; | ||
import org.apache.ibatis.parsing.TokenHandler; | ||
import org.apache.ibatis.session.Configuration; | ||
|
||
/** | ||
* @author furious 2024/4/10 | ||
*/ | ||
public class SqlSourceBuilder extends BaseBuilder { | ||
public SqlSourceBuilder(Configuration configuration) { | ||
super(configuration); | ||
} | ||
|
||
public SqlSource parse(String originalSql, Class<?> parameterType) { | ||
ParameterMappingTokenHandler handler = new ParameterMappingTokenHandler(configuration); | ||
GenericTokenParser parser = new GenericTokenParser("#{", "}", handler); | ||
String sql = parser.parse(originalSql); | ||
return new StaticSqlSource(sql, handler.getParameterMappings()); | ||
} | ||
|
||
private static class ParameterMappingTokenHandler extends BaseBuilder implements TokenHandler { | ||
|
||
private List<ParameterMapping> parameterMappings = new ArrayList<>(); | ||
|
||
public ParameterMappingTokenHandler(Configuration configuration) { | ||
super(configuration); | ||
} | ||
|
||
public List<ParameterMapping> getParameterMappings() { | ||
return parameterMappings; | ||
} | ||
|
||
@Override | ||
public String handleToken(String content) { | ||
parameterMappings.add(buildParameterMapping(content)); | ||
return "?"; | ||
} | ||
|
||
private ParameterMapping buildParameterMapping(String content) { | ||
return new ParameterMapping(configuration, content); | ||
} | ||
} | ||
} |
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
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
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
14 changes: 14 additions & 0 deletions
14
src/main/java/org/apache/ibatis/executor/parameter/ParameterHandler.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,14 @@ | ||
package org.apache.ibatis.executor.parameter; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
|
||
/** | ||
* @author furious 2024/4/10 | ||
*/ | ||
public interface ParameterHandler { | ||
|
||
Object getParameterObject(); | ||
|
||
void setParameters(PreparedStatement ps) throws SQLException; | ||
} |
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
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
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
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
56 changes: 56 additions & 0 deletions
56
src/main/java/org/apache/ibatis/mapping/ParameterMapping.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,56 @@ | ||
package org.apache.ibatis.mapping; | ||
|
||
import org.apache.ibatis.session.Configuration; | ||
import org.apache.ibatis.type.TypeHandler; | ||
|
||
/** | ||
* @author furious 2024/4/10 | ||
*/ | ||
public class ParameterMapping { | ||
|
||
private Configuration configuration; | ||
private String property; | ||
private Class<?> javaType = Object.class; | ||
private TypeHandler typeHandler; | ||
|
||
public ParameterMapping() { | ||
} | ||
|
||
public ParameterMapping(Configuration configuration, String property) { | ||
this.configuration = configuration; | ||
this.property = property; | ||
this.typeHandler = configuration.getTypeHandlerRegistry().getUnknownTypeHandler(); | ||
} | ||
|
||
public Configuration getConfiguration() { | ||
return configuration; | ||
} | ||
|
||
public void setConfiguration(Configuration configuration) { | ||
this.configuration = configuration; | ||
} | ||
|
||
public String getProperty() { | ||
return property; | ||
} | ||
|
||
public void setProperty(String property) { | ||
this.property = property; | ||
} | ||
|
||
public Class<?> getJavaType() { | ||
return javaType; | ||
} | ||
|
||
public void setJavaType(Class<?> javaType) { | ||
this.javaType = javaType; | ||
} | ||
|
||
public TypeHandler getTypeHandler() { | ||
return typeHandler; | ||
} | ||
|
||
public void setTypeHandler(TypeHandler typeHandler) { | ||
this.typeHandler = typeHandler; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/org/apache/ibatis/parsing/GenericTokenParser.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,31 @@ | ||
package org.apache.ibatis.parsing; | ||
|
||
import org.apache.commons.lang3.ArrayUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
/** | ||
* @author furious 2024/4/10 | ||
*/ | ||
public class GenericTokenParser { | ||
|
||
private final String openToken; | ||
private final String closeToken; | ||
private final TokenHandler handler; | ||
|
||
public GenericTokenParser(String openToken, String closeToken, TokenHandler handler) { | ||
this.openToken = openToken; | ||
this.closeToken = closeToken; | ||
this.handler = handler; | ||
} | ||
|
||
public String parse(String text) { | ||
String[] tokens = StringUtils.substringsBetween(text, openToken, closeToken); | ||
if (ArrayUtils.isEmpty(tokens)) { | ||
return text; | ||
} | ||
for (String token : tokens) { | ||
text = text.replace(openToken + token + closeToken, handler.handleToken(token)); | ||
} | ||
return text; | ||
} | ||
} |
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,9 @@ | ||
package org.apache.ibatis.parsing; | ||
|
||
/** | ||
* @author furious 2024/4/10 | ||
*/ | ||
public interface TokenHandler { | ||
|
||
String handleToken(String content); | ||
} |
Oops, something went wrong.