From f95ffc2394d1fe760e9115403f5fc0e36a289711 Mon Sep 17 00:00:00 2001 From: Zen Huifer Date: Mon, 5 Feb 2024 09:36:10 +0800 Subject: [PATCH] write a line into test.file --- ...sefb38a84-c3c6-11ee-b834-acde48001122.java | 98 +++++++++++++++++++ ...sf007d42c-c3c6-11ee-b834-acde48001122.java | 79 +++++++++++++++ ...sf055bfca-c3c6-11ee-b834-acde48001122.java | 20 ++++ 3 files changed, 197 insertions(+) create mode 100644 docs/spring/csefb38a84-c3c6-11ee-b834-acde48001122.java create mode 100644 docs/spring/csf007d42c-c3c6-11ee-b834-acde48001122.java create mode 100644 docs/spring/csf055bfca-c3c6-11ee-b834-acde48001122.java diff --git a/docs/spring/csefb38a84-c3c6-11ee-b834-acde48001122.java b/docs/spring/csefb38a84-c3c6-11ee-b834-acde48001122.java new file mode 100644 index 00000000..bd900de4 --- /dev/null +++ b/docs/spring/csefb38a84-c3c6-11ee-b834-acde48001122.java @@ -0,0 +1,98 @@ +/** + * Copyright 2009-2018 the original author or authors. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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 List query(Statement statement, ResultHandler resultHandler) throws SQLException { + PreparedStatement ps = (PreparedStatement) statement; + ps.execute(); + return resultSetHandler.handleResultSets(ps); + } + + @Override + public Cursor 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); + } + +} diff --git a/docs/spring/csf007d42c-c3c6-11ee-b834-acde48001122.java b/docs/spring/csf007d42c-c3c6-11ee-b834-acde48001122.java new file mode 100644 index 00000000..8ea0dd04 --- /dev/null +++ b/docs/spring/csf007d42c-c3c6-11ee-b834-acde48001122.java @@ -0,0 +1,79 @@ +/** + * Copyright 2009-2019 the original author or authors. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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); + } + + +} diff --git a/docs/spring/csf055bfca-c3c6-11ee-b834-acde48001122.java b/docs/spring/csf055bfca-c3c6-11ee-b834-acde48001122.java new file mode 100644 index 00000000..4b725ce8 --- /dev/null +++ b/docs/spring/csf055bfca-c3c6-11ee-b834-acde48001122.java @@ -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("开始玩了"); + } +}