-
Notifications
You must be signed in to change notification settings - Fork 19
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
Showing
3 changed files
with
197 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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* Copyright 2009-2018 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.executor.statement; | ||
|
||
import org.apache.ibatis.cursor.Cursor; | ||
import org.apache.ibatis.executor.Executor; | ||
import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator; | ||
import org.apache.ibatis.executor.keygen.KeyGenerator; | ||
import org.apache.ibatis.mapping.BoundSql; | ||
import org.apache.ibatis.mapping.MappedStatement; | ||
import org.apache.ibatis.mapping.ResultSetType; | ||
import org.apache.ibatis.session.ResultHandler; | ||
import org.apache.ibatis.session.RowBounds; | ||
|
||
import java.sql.*; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Clinton Begin | ||
*/ | ||
public class PreparedStatementHandler extends BaseStatementHandler { | ||
|
||
public PreparedStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) { | ||
super(executor, mappedStatement, parameter, rowBounds, resultHandler, boundSql); | ||
} | ||
|
||
@Override | ||
public int update(Statement statement) throws SQLException { | ||
// 类型转换 | ||
PreparedStatement ps = (PreparedStatement) statement; | ||
// 执行sql | ||
ps.execute(); | ||
// 受影响行数 | ||
int rows = ps.getUpdateCount(); | ||
Object parameterObject = boundSql.getParameterObject(); | ||
// key 生成器 | ||
KeyGenerator keyGenerator = mappedStatement.getKeyGenerator(); | ||
// 获取自增主键 | ||
keyGenerator.processAfter(executor, mappedStatement, ps, parameterObject); | ||
return rows; | ||
} | ||
|
||
@Override | ||
public void batch(Statement statement) throws SQLException { | ||
PreparedStatement ps = (PreparedStatement) statement; | ||
ps.addBatch(); | ||
} | ||
|
||
@Override | ||
public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException { | ||
PreparedStatement ps = (PreparedStatement) statement; | ||
ps.execute(); | ||
return resultSetHandler.handleResultSets(ps); | ||
} | ||
|
||
@Override | ||
public <E> Cursor<E> queryCursor(Statement statement) throws SQLException { | ||
PreparedStatement ps = (PreparedStatement) statement; | ||
ps.execute(); | ||
return resultSetHandler.handleCursorResultSets(ps); | ||
} | ||
|
||
@Override | ||
protected Statement instantiateStatement(Connection connection) throws SQLException { | ||
String sql = boundSql.getSql(); | ||
if (mappedStatement.getKeyGenerator() instanceof Jdbc3KeyGenerator) { | ||
String[] keyColumnNames = mappedStatement.getKeyColumns(); | ||
if (keyColumnNames == null) { | ||
return connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS); | ||
} else { | ||
return connection.prepareStatement(sql, keyColumnNames); | ||
} | ||
} else if (mappedStatement.getResultSetType() == ResultSetType.DEFAULT) { | ||
return connection.prepareStatement(sql); | ||
} else { | ||
return connection.prepareStatement(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY); | ||
} | ||
} | ||
|
||
@Override | ||
public void parameterize(Statement statement) throws SQLException { | ||
parameterHandler.setParameters((PreparedStatement) statement); | ||
} | ||
|
||
} |
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,79 @@ | ||
/** | ||
* Copyright 2009-2019 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.type; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class TypeAliasRegistryTest { | ||
|
||
@Test | ||
void shouldRegisterAndResolveTypeAlias() { | ||
TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry(); | ||
|
||
typeAliasRegistry.registerAlias("rich", "org.apache.ibatis.domain.misc.RichType"); | ||
|
||
assertEquals("org.apache.ibatis.domain.misc.RichType", typeAliasRegistry.resolveAlias("rich").getName()); | ||
} | ||
|
||
/** | ||
* 对注解 {@link Alias} 的测试用例 | ||
*/ | ||
@Test | ||
void testAnnotation() { | ||
TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry(); | ||
typeAliasRegistry.registerAlias(Hc.class); | ||
assertEquals("org.apache.ibatis.type.Hc", typeAliasRegistry.resolveAlias("hc").getName()); | ||
} | ||
|
||
@Test | ||
void shouldFetchArrayType() { | ||
TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry(); | ||
assertEquals(Byte[].class, typeAliasRegistry.resolveAlias("byte[]")); | ||
} | ||
|
||
@Test | ||
void shouldBeAbleToRegisterSameAliasWithSameTypeAgain() { | ||
TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry(); | ||
typeAliasRegistry.registerAlias("String", String.class); | ||
typeAliasRegistry.registerAlias("string", String.class); | ||
} | ||
|
||
@Test | ||
void shouldNotBeAbleToRegisterSameAliasWithDifferentType() { | ||
TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry(); | ||
assertThrows(TypeException.class, () -> typeAliasRegistry.registerAlias("string", BigDecimal.class)); | ||
} | ||
|
||
@Test | ||
void shouldBeAbleToRegisterAliasWithNullType() { | ||
TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry(); | ||
typeAliasRegistry.registerAlias("foo", (Class<?>) null); | ||
assertNull(typeAliasRegistry.resolveAlias("foo")); | ||
} | ||
|
||
@Test | ||
void shouldBeAbleToRegisterNewTypeIfRegisteredTypeIsNull() { | ||
TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry(); | ||
typeAliasRegistry.registerAlias("foo", (Class<?>) null); | ||
typeAliasRegistry.registerAlias("foo", String.class); | ||
} | ||
|
||
|
||
} |
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,20 @@ | ||
package com.huifer.mybatis.proxy; | ||
|
||
/** | ||
* 描述: | ||
* 主要业务的实现 | ||
* | ||
* @author huifer | ||
* @date 2019-02-24 | ||
*/ | ||
public class Person implements BaseMothed { | ||
@Override | ||
public void eat() { | ||
System.out.println("吃东西了"); | ||
} | ||
|
||
@Override | ||
public void play() { | ||
System.out.println("开始玩了"); | ||
} | ||
} |