Skip to content

Commit

Permalink
Merge pull request #914 from trifolium-x/dev
Browse files Browse the repository at this point in the history
添加了对solon的插件支持
  • Loading branch information
abel533 authored Apr 3, 2024
2 parents 5e31029 + 00f6fc9 commit d3888db
Show file tree
Hide file tree
Showing 13 changed files with 569 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
<module>weekend</module>
<module>generator</module>
<module>spring-boot-starter</module>
<module>solon-plugin</module>
</modules>

<build>
Expand Down
87 changes: 87 additions & 0 deletions solon-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ The MIT License (MIT)
~
~ Copyright (c) 2017 abel533@gmail.com
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"), to deal
~ in the Software without restriction, including without limitation the rights
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
~ copies of the Software, and to permit persons to whom the Software is
~ furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in
~ all copies or substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
~ THE SOFTWARE.
-->

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-modules</artifactId>
<version>${revision}</version>
</parent>
<artifactId>mapper-solon-plugin</artifactId>
<packaging>jar</packaging>

<name>mapper-solon-plugin</name>
<description>Solon Support for Mapper</description>
<url>https://github.com/abel533/solon-plugin/</url>

<properties>
<solon.version>2.7.3</solon.version>
</properties>

<dependencies>

<dependency>
<groupId>org.noear</groupId>
<artifactId>mybatis-solon-plugin</artifactId>
<version>${solon.version}</version>
</dependency>

<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-base</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-test-junit4</artifactId>
<version>${solon.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>4.0.3</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tk.mybatis.solon;

import org.apache.ibatis.solon.MybatisAdapter;
import org.apache.ibatis.solon.MybatisAdapterFactory;
import org.noear.solon.core.BeanWrap;
import org.noear.solon.core.Props;

/**
* @title: tkMybatis Adapter Factory
* @author: trifolium.wang
* @date: 2024/4/1
* @since 2.7.3
*/
public class TkMapperAdapterFactory implements MybatisAdapterFactory {
@Override
public MybatisAdapter create(BeanWrap dsWrap) {
return new TkMapperMybatisAdapter(dsWrap);
}

@Override
public MybatisAdapter create(BeanWrap dsWrap, Props dsProps) {
return new TkMapperMybatisAdapter(dsWrap, dsProps);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package tk.mybatis.solon;

import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.solon.integration.MybatisAdapterDefault;
import org.noear.solon.core.BeanWrap;
import org.noear.solon.core.Props;
import org.noear.solon.core.PropsConverter;
import org.noear.solon.core.VarHolder;
import tk.mybatis.mapper.entity.Config;
import tk.mybatis.mapper.mapperhelper.MapperHelper;

/**
* @title: TkMybatis Adapter
* @author: trifolium.wang
* @date: 2024/4/1
* @since 2.7.3
*/
public class TkMapperMybatisAdapter extends MybatisAdapterDefault {

protected Config tkConfig;

protected MapperHelper mapperHelper;

protected TkMapperMybatisAdapter(BeanWrap dsWrap) {
super(dsWrap);

dsWrap.context().getBeanAsync(Config.class, bean -> {
tkConfig = bean;
});

dsWrap.context().getBeanAsync(MapperHelper.class, bean -> {
mapperHelper = bean;
});
}

protected TkMapperMybatisAdapter(BeanWrap dsWrap, Props dsProps) {
super(dsWrap, dsProps);

dsWrap.context().getBeanAsync(Config.class, bean -> {
tkConfig = bean;
});

dsWrap.context().getBeanAsync(MapperHelper.class, bean -> {
mapperHelper = bean;
});
}

@Override
protected void initConfiguration(Environment environment) {
config = new tk.mybatis.mapper.session.Configuration();
config.setEnvironment(environment);

Props mybatisProps = dsProps.getProp("configuration");
if (!mybatisProps.isEmpty()) {
PropsConverter.global().convert(mybatisProps, config, Configuration.class, null);
}
}

@Override
public SqlSessionFactory getFactory() {
if (factory == null) {
builderMapperHelper();
factory = factoryBuilder.build(config);
}
return factory;
}

@Override
public void injectTo(VarHolder varH) {
super.injectTo(varH);

//@Db("db1") Config tkConfig;
if (Config.class.isAssignableFrom(varH.getType())) {
varH.setValue(this.tkConfig);
}

//@Db("db1") tk.mybatis.mapper.session.Configuration configuration;
if (tk.mybatis.mapper.session.Configuration.class.isAssignableFrom(varH.getType())) {
varH.setValue(getConfiguration());
}

//@Db("db1") MapperHelper mapperHelper;
if (MapperHelper.class.isAssignableFrom(varH.getType())) {
varH.setValue(this.mapperHelper);
}
}

/**
* 通过使用 tk.mybatis.mapper.session.Configuration
* 替换 MyBatis 中的 org.apache.ibatis.session.Configuration.
* 重写原 Configuration 中的 addMappedStatement实现
*/
private void builderMapperHelper() {
Props cfgProps = dsProps.getProp("tk.mapper");

if (tkConfig == null) {
tkConfig = new Config();
}

if (!cfgProps.isEmpty()) {
PropsConverter.global().convert(cfgProps, tkConfig, Config.class, null);
}
if (mapperHelper == null) {
mapperHelper = new MapperHelper();
}

mapperHelper.setConfig(tkConfig);
((tk.mybatis.mapper.session.Configuration) config).setMapperHelper(mapperHelper);
}
}
22 changes: 22 additions & 0 deletions solon-plugin/src/main/java/tk/mybatis/solon/XPluginImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tk.mybatis.solon;

import org.apache.ibatis.solon.integration.MybatisAdapterManager;
import org.noear.solon.core.AppContext;
import org.noear.solon.core.Plugin;

/**
* @title: TkMybatis的Solon插件
* @author: trifolium.wang
* @date: 2024/4/1
* @since 2.7.3
*/
public class XPluginImpl implements Plugin {


@Override
public void start(AppContext context) throws Throwable {

MybatisAdapterManager.setAdapterFactory(new TkMapperAdapterFactory());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
solon.plugin=tk.mybatis.solon.XPluginImpl
solon.plugin.priority=3
15 changes: 15 additions & 0 deletions solon-plugin/src/test/java/tk/mybatis/solon/test/TkMapperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package tk.mybatis.solon.test;

import org.noear.solon.Solon;

/**
* @title: TkMapperTest
* @author: trifolium.wang
* @date: 2024/4/2
*/
public class TkMapperTest {

public static void main(String[] args) {
Solon.start(TkMapperTest.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package tk.mybatis.solon.test.conf;

import com.zaxxer.hikari.HikariDataSource;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;

/**
* @title: TestConfig
* @author: trifolium.wang
* @date: 2024/4/2
*/
@Configuration
public class TestConfig {

Logger log = LoggerFactory.getLogger(TestConfig.class);

@Bean(name = "db1", typed = true)
public DataSource db1(@Inject("${test.db1}") HikariDataSource ds) {
try {
Connection conn = ds.getConnection();
Statement statement = conn.createStatement();
statement.execute("CREATE TABLE user (" +
" `id` bigint NOT NULL AUTO_INCREMENT PRIMARY KEY," +
" `name` varchar(255) DEFAULT NULL," +
" `age` int DEFAULT NULL," +
" `create_time` datetime DEFAULT NULL," +
" `is_del` tinyint(1) DEFAULT NULL" +
")");

statement.execute("INSERT INTO `user` (`id`, `name`, `age`, `create_time`, `is_del`) VALUES (1, '张三', 11, '2024-04-02 13:38:56', 0);\n" +
"INSERT INTO `user` (`id`, `name`, `age`, `create_time`, `is_del`) VALUES (2, '李四', 3, '2024-04-02 13:39:08', 0);\n" +
"INSERT INTO `user` (`id`, `name`, `age`, `create_time`, `is_del`) VALUES (3, '张麻子', 43, '2024-04-02 13:39:20', 0);");
statement.close();
conn.close();
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RuntimeException("Datasource initialization Failure!");
}
return ds;
}
}
Loading

0 comments on commit d3888db

Please sign in to comment.