Skip to content

Commit d3888db

Browse files
authored
Merge pull request #914 from trifolium-x/dev
添加了对solon的插件支持
2 parents 5e31029 + 00f6fc9 commit d3888db

File tree

13 files changed

+569
-0
lines changed

13 files changed

+569
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
<module>weekend</module>
142142
<module>generator</module>
143143
<module>spring-boot-starter</module>
144+
<module>solon-plugin</module>
144145
</modules>
145146

146147
<build>

solon-plugin/pom.xml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ The MIT License (MIT)
4+
~
5+
~ Copyright (c) 2017 abel533@gmail.com
6+
~
7+
~ Permission is hereby granted, free of charge, to any person obtaining a copy
8+
~ of this software and associated documentation files (the "Software"), to deal
9+
~ in the Software without restriction, including without limitation the rights
10+
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
~ copies of the Software, and to permit persons to whom the Software is
12+
~ furnished to do so, subject to the following conditions:
13+
~
14+
~ The above copyright notice and this permission notice shall be included in
15+
~ all copies or substantial portions of the Software.
16+
~
17+
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
~ THE SOFTWARE.
24+
-->
25+
26+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
27+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
28+
<modelVersion>4.0.0</modelVersion>
29+
<parent>
30+
<groupId>tk.mybatis</groupId>
31+
<artifactId>mapper-modules</artifactId>
32+
<version>${revision}</version>
33+
</parent>
34+
<artifactId>mapper-solon-plugin</artifactId>
35+
<packaging>jar</packaging>
36+
37+
<name>mapper-solon-plugin</name>
38+
<description>Solon Support for Mapper</description>
39+
<url>https://github.com/abel533/solon-plugin/</url>
40+
41+
<properties>
42+
<solon.version>2.7.3</solon.version>
43+
</properties>
44+
45+
<dependencies>
46+
47+
<dependency>
48+
<groupId>org.noear</groupId>
49+
<artifactId>mybatis-solon-plugin</artifactId>
50+
<version>${solon.version}</version>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>tk.mybatis</groupId>
55+
<artifactId>mapper-core</artifactId>
56+
<version>${project.version}</version>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>tk.mybatis</groupId>
61+
<artifactId>mapper-base</artifactId>
62+
<version>${project.version}</version>
63+
</dependency>
64+
65+
<dependency>
66+
<groupId>org.noear</groupId>
67+
<artifactId>solon-test-junit4</artifactId>
68+
<version>${solon.version}</version>
69+
<scope>test</scope>
70+
</dependency>
71+
72+
<dependency>
73+
<groupId>com.h2database</groupId>
74+
<artifactId>h2</artifactId>
75+
<version>1.4.200</version>
76+
<scope>test</scope>
77+
</dependency>
78+
79+
<dependency>
80+
<groupId>com.zaxxer</groupId>
81+
<artifactId>HikariCP</artifactId>
82+
<version>4.0.3</version>
83+
<scope>test</scope>
84+
</dependency>
85+
86+
</dependencies>
87+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package tk.mybatis.solon;
2+
3+
import org.apache.ibatis.solon.MybatisAdapter;
4+
import org.apache.ibatis.solon.MybatisAdapterFactory;
5+
import org.noear.solon.core.BeanWrap;
6+
import org.noear.solon.core.Props;
7+
8+
/**
9+
* @title: tkMybatis Adapter Factory
10+
* @author: trifolium.wang
11+
* @date: 2024/4/1
12+
* @since 2.7.3
13+
*/
14+
public class TkMapperAdapterFactory implements MybatisAdapterFactory {
15+
@Override
16+
public MybatisAdapter create(BeanWrap dsWrap) {
17+
return new TkMapperMybatisAdapter(dsWrap);
18+
}
19+
20+
@Override
21+
public MybatisAdapter create(BeanWrap dsWrap, Props dsProps) {
22+
return new TkMapperMybatisAdapter(dsWrap, dsProps);
23+
}
24+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package tk.mybatis.solon;
2+
3+
import org.apache.ibatis.mapping.Environment;
4+
import org.apache.ibatis.session.Configuration;
5+
import org.apache.ibatis.session.SqlSessionFactory;
6+
import org.apache.ibatis.solon.integration.MybatisAdapterDefault;
7+
import org.noear.solon.core.BeanWrap;
8+
import org.noear.solon.core.Props;
9+
import org.noear.solon.core.PropsConverter;
10+
import org.noear.solon.core.VarHolder;
11+
import tk.mybatis.mapper.entity.Config;
12+
import tk.mybatis.mapper.mapperhelper.MapperHelper;
13+
14+
/**
15+
* @title: TkMybatis Adapter
16+
* @author: trifolium.wang
17+
* @date: 2024/4/1
18+
* @since 2.7.3
19+
*/
20+
public class TkMapperMybatisAdapter extends MybatisAdapterDefault {
21+
22+
protected Config tkConfig;
23+
24+
protected MapperHelper mapperHelper;
25+
26+
protected TkMapperMybatisAdapter(BeanWrap dsWrap) {
27+
super(dsWrap);
28+
29+
dsWrap.context().getBeanAsync(Config.class, bean -> {
30+
tkConfig = bean;
31+
});
32+
33+
dsWrap.context().getBeanAsync(MapperHelper.class, bean -> {
34+
mapperHelper = bean;
35+
});
36+
}
37+
38+
protected TkMapperMybatisAdapter(BeanWrap dsWrap, Props dsProps) {
39+
super(dsWrap, dsProps);
40+
41+
dsWrap.context().getBeanAsync(Config.class, bean -> {
42+
tkConfig = bean;
43+
});
44+
45+
dsWrap.context().getBeanAsync(MapperHelper.class, bean -> {
46+
mapperHelper = bean;
47+
});
48+
}
49+
50+
@Override
51+
protected void initConfiguration(Environment environment) {
52+
config = new tk.mybatis.mapper.session.Configuration();
53+
config.setEnvironment(environment);
54+
55+
Props mybatisProps = dsProps.getProp("configuration");
56+
if (!mybatisProps.isEmpty()) {
57+
PropsConverter.global().convert(mybatisProps, config, Configuration.class, null);
58+
}
59+
}
60+
61+
@Override
62+
public SqlSessionFactory getFactory() {
63+
if (factory == null) {
64+
builderMapperHelper();
65+
factory = factoryBuilder.build(config);
66+
}
67+
return factory;
68+
}
69+
70+
@Override
71+
public void injectTo(VarHolder varH) {
72+
super.injectTo(varH);
73+
74+
//@Db("db1") Config tkConfig;
75+
if (Config.class.isAssignableFrom(varH.getType())) {
76+
varH.setValue(this.tkConfig);
77+
}
78+
79+
//@Db("db1") tk.mybatis.mapper.session.Configuration configuration;
80+
if (tk.mybatis.mapper.session.Configuration.class.isAssignableFrom(varH.getType())) {
81+
varH.setValue(getConfiguration());
82+
}
83+
84+
//@Db("db1") MapperHelper mapperHelper;
85+
if (MapperHelper.class.isAssignableFrom(varH.getType())) {
86+
varH.setValue(this.mapperHelper);
87+
}
88+
}
89+
90+
/**
91+
* 通过使用 tk.mybatis.mapper.session.Configuration
92+
* 替换 MyBatis 中的 org.apache.ibatis.session.Configuration.
93+
* 重写原 Configuration 中的 addMappedStatement实现
94+
*/
95+
private void builderMapperHelper() {
96+
Props cfgProps = dsProps.getProp("tk.mapper");
97+
98+
if (tkConfig == null) {
99+
tkConfig = new Config();
100+
}
101+
102+
if (!cfgProps.isEmpty()) {
103+
PropsConverter.global().convert(cfgProps, tkConfig, Config.class, null);
104+
}
105+
if (mapperHelper == null) {
106+
mapperHelper = new MapperHelper();
107+
}
108+
109+
mapperHelper.setConfig(tkConfig);
110+
((tk.mybatis.mapper.session.Configuration) config).setMapperHelper(mapperHelper);
111+
}
112+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package tk.mybatis.solon;
2+
3+
import org.apache.ibatis.solon.integration.MybatisAdapterManager;
4+
import org.noear.solon.core.AppContext;
5+
import org.noear.solon.core.Plugin;
6+
7+
/**
8+
* @title: TkMybatis的Solon插件
9+
* @author: trifolium.wang
10+
* @date: 2024/4/1
11+
* @since 2.7.3
12+
*/
13+
public class XPluginImpl implements Plugin {
14+
15+
16+
@Override
17+
public void start(AppContext context) throws Throwable {
18+
19+
MybatisAdapterManager.setAdapterFactory(new TkMapperAdapterFactory());
20+
}
21+
22+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
solon.plugin=tk.mybatis.solon.XPluginImpl
2+
solon.plugin.priority=3
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package tk.mybatis.solon.test;
2+
3+
import org.noear.solon.Solon;
4+
5+
/**
6+
* @title: TkMapperTest
7+
* @author: trifolium.wang
8+
* @date: 2024/4/2
9+
*/
10+
public class TkMapperTest {
11+
12+
public static void main(String[] args) {
13+
Solon.start(TkMapperTest.class, args);
14+
}
15+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package tk.mybatis.solon.test.conf;
2+
3+
import com.zaxxer.hikari.HikariDataSource;
4+
import org.noear.solon.annotation.Bean;
5+
import org.noear.solon.annotation.Configuration;
6+
import org.noear.solon.annotation.Inject;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
10+
import javax.sql.DataSource;
11+
import java.sql.Connection;
12+
import java.sql.Statement;
13+
14+
/**
15+
* @title: TestConfig
16+
* @author: trifolium.wang
17+
* @date: 2024/4/2
18+
*/
19+
@Configuration
20+
public class TestConfig {
21+
22+
Logger log = LoggerFactory.getLogger(TestConfig.class);
23+
24+
@Bean(name = "db1", typed = true)
25+
public DataSource db1(@Inject("${test.db1}") HikariDataSource ds) {
26+
try {
27+
Connection conn = ds.getConnection();
28+
Statement statement = conn.createStatement();
29+
statement.execute("CREATE TABLE user (" +
30+
" `id` bigint NOT NULL AUTO_INCREMENT PRIMARY KEY," +
31+
" `name` varchar(255) DEFAULT NULL," +
32+
" `age` int DEFAULT NULL," +
33+
" `create_time` datetime DEFAULT NULL," +
34+
" `is_del` tinyint(1) DEFAULT NULL" +
35+
")");
36+
37+
statement.execute("INSERT INTO `user` (`id`, `name`, `age`, `create_time`, `is_del`) VALUES (1, '张三', 11, '2024-04-02 13:38:56', 0);\n" +
38+
"INSERT INTO `user` (`id`, `name`, `age`, `create_time`, `is_del`) VALUES (2, '李四', 3, '2024-04-02 13:39:08', 0);\n" +
39+
"INSERT INTO `user` (`id`, `name`, `age`, `create_time`, `is_del`) VALUES (3, '张麻子', 43, '2024-04-02 13:39:20', 0);");
40+
statement.close();
41+
conn.close();
42+
} catch (Exception e) {
43+
log.error(e.getMessage(), e);
44+
throw new RuntimeException("Datasource initialization Failure!");
45+
}
46+
return ds;
47+
}
48+
}

0 commit comments

Comments
 (0)