Skip to content

Commit

Permalink
[#noissue] Refactor mybatis configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Oct 17, 2023
1 parent 2fdacaf commit 1ed2177
Show file tree
Hide file tree
Showing 41 changed files with 664 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<property name="plugins">
<list>
<!-- use the patch version because of mybatis 3.2's incompatibility-->
<bean class="com.navercorp.pinpoint.web.dao.ibatis.BindingLogPlugin32"/>
<bean class="com.navercorp.pinpoint.mybatis.plugin.BindingLogPlugin"/>
</list>
</property>
</bean>
Expand Down
1 change: 1 addition & 0 deletions commons-mybatis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# pinpoint-commons-mybatis
97 changes: 97 additions & 0 deletions commons-mybatis/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2019 NAVER Corp.
~
~ 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.
-->

<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint</artifactId>
<version>2.6.0-SNAPSHOT</version>
</parent>

<artifactId>pinpoint-commons-mybatis</artifactId>
<packaging>jar</packaging>

<properties>
<jdk.version>11</jdk.version>
<jdk.home>${env.JAVA_11_HOME}</jdk.home>
</properties>

<dependencies>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-commons</artifactId>
</dependency>

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>


<!-- Logging dependencies -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>${log4j2.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2023 NAVER Corp.
*
* 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 com.navercorp.pinpoint.mybatis;

import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ExecutorType;

public class DefaultMyBatisConfigurationCustomizer implements MyBatisConfigurationCustomizer {

@Override
public void customize(Configuration config) {
config.setCacheEnabled(true);

// lazy loading
config.setLazyLoadingEnabled(true);
config.setAggressiveLazyLoading(true);

config.setUseGeneratedKeys(true);

// don't need "REUSE" because preparedStatements are cached at dbcp
config.setDefaultExecutorType(ExecutorType.SIMPLE);

// defaultQueryTimeout. unit is second
config.setDefaultStatementTimeout(5);

// underscore mapping of DB table
config.setMapUnderscoreToCamelCase(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2023 NAVER Corp.
*
* 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 com.navercorp.pinpoint.mybatis;

import com.navercorp.pinpoint.mybatis.plugin.BindingLogPlugin;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyBatisConfiguration {
@Bean
public MyBatisConfigurationCustomizer myBatisConfigurationCustomizer() {
return new DefaultMyBatisConfigurationCustomizer();
}

@Bean
public BindingLogPlugin bindingLogPlugin() {
return new BindingLogPlugin();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2023 NAVER Corp.
*
* 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 com.navercorp.pinpoint.mybatis;

import org.apache.ibatis.session.Configuration;

public interface MyBatisConfigurationCustomizer {
void customize(Configuration config);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2023 NAVER Corp.
*
* 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 com.navercorp.pinpoint.mybatis;

import org.apache.ibatis.type.TypeAliasRegistry;
import org.apache.ibatis.type.TypeHandlerRegistry;

public interface MyBatisRegistryHandler {
void registerTypeAlias(TypeAliasRegistry typeAliasRegistry);

void registerTypeHandler(TypeHandlerRegistry typeHandlerRegistry);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2023 NAVER Corp.
*
* 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 com.navercorp.pinpoint.mybatis;

import org.apache.ibatis.type.TypeAliasRegistry;
import org.apache.ibatis.type.TypeHandlerRegistry;

import java.util.List;
import java.util.Objects;

public class MyBatisRegistryHandlerChain implements MyBatisRegistryHandler {
private final List<? extends MyBatisRegistryHandler> handlers;

public MyBatisRegistryHandlerChain(List<? extends MyBatisRegistryHandler> handlers) {
this.handlers = Objects.requireNonNull(handlers, "handlers");
}

@Override
public void registerTypeAlias(TypeAliasRegistry typeAliasRegistry) {
for (MyBatisRegistryHandler handler : handlers) {
handler.registerTypeAlias(typeAliasRegistry);
}
}

@Override
public void registerTypeHandler(TypeHandlerRegistry typeHandlerRegistry) {
for (MyBatisRegistryHandler handler : handlers) {
handler.registerTypeHandler(typeHandlerRegistry);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
/*
* Copyright 2014 NAVER Corp.
* Copyright 2023 NAVER Corp.
*
* 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
* 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 com.navercorp.pinpoint.web.dao.ibatis;
package com.navercorp.pinpoint.mybatis.plugin;

import java.util.List;
import java.util.Properties;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
/*
* Copyright 2014 NAVER Corp.
* Copyright 2023 NAVER Corp.
*
* 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
* 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 com.navercorp.pinpoint.web.dao.ibatis;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
package com.navercorp.pinpoint.mybatis.plugin;

import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
Expand All @@ -40,10 +34,17 @@
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Properties;

/**
* Plugin for printing out the bind variables of {@link java.sql.PreparedStatement} and {@link java.sql.CallableStatement} with Query string.
* format of Query string can be changed with {@link BindLogFormatter}.
* base implementation is {@link com.navercorp.pinpoint.web.dao.ibatis.DefaultBindingLogFormatter}.
* base implementation is {@link DefaultBindingLogFormatter}.
* removeWhitespace option is supported
*
* @author emeroad
Expand All @@ -53,18 +54,18 @@
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})}
)
public class BindingLogPlugin32 implements Interceptor {
public class BindingLogPlugin implements Interceptor {

private static final Log pLogger = LogFactory.getLog(PreparedStatement.class);
private static final Log internalLogger = LogFactory.getLog(BindingLogPlugin32.class);
private static final Log internalLogger = LogFactory.getLog(BindingLogPlugin.class);

private BindLogFormatter bindLogFormatter;

public BindingLogPlugin32() {
public BindingLogPlugin() {
this.bindLogFormatter = new DefaultBindingLogFormatter();
}

public BindingLogPlugin32(BindLogFormatter bindLogFormatter) {
public BindingLogPlugin(BindLogFormatter bindLogFormatter) {
this.bindLogFormatter = Objects.requireNonNull(bindLogFormatter, "bindLogFormatter");
}

Expand Down Expand Up @@ -128,7 +129,7 @@ public Object plugin(Object target) {
}

/**
* {@link com.navercorp.pinpoint.web.dao.ibatis.DefaultBindingLogFormatter} is the implementation of {@link BindLogFormatter} supports removeWhitespace option.
* {@link DefaultBindingLogFormatter} is the implementation of {@link BindLogFormatter} supports removeWhitespace option.
* removeWhitespace : setting for printing out query format in a row.
*
* @param properties option properties
Expand Down
Loading

0 comments on commit 1ed2177

Please sign in to comment.