mybatis-plus-extension-solon-plugin
<dependency>
<groupId>org.noear</groupId>
<artifactId>mybatis-plus-extension-solon-plugin</artifactId>
</dependency>
1、描述
数据扩展插件,为 Solon Data 提供基于 mybatis-plus + mybatis-plus-extension 的框架适配,以提供ORM支持。
用于替代:mybatis-plus-solon + mybatis-plus-extension 的组合(mybatis-plus-extension 与 spring 有深度绑定,不适合直接使用)
2、配置示例
# 配置数据源
demo.db1:
schema: rock
jdbcUrl: jdbc:mysql://localhost:3306/rock?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=true
driverClassName: com.mysql.cj.jdbc.Driver
username: root
password: 123456
# 配置数据源对应的 mybatis 信息
mybatis.db1:
typeAliases: #支持包名 或 类名(.class 结尾)
- "demo4021.model"
mappers: #支持包名 或 类名(.class 结尾)或 xml(.xml结尾 或 *.xml 结尾)
- "demo4021.dso.mapper"
configuration: #扩展配置(要与 Configuration 类的属性一一对应)
cacheEnabled: false
mapUnderscoreToCamelCase: true
metaObjectHandler: "demo4031.dso.MetaObjectHandlerImpl"
dbConfig:
logicDeleteField: "deleted"
Mapper 配置注意事项:
- 通过 mapper 类包名配置。 xml 与 mapper 需同包同名
mybatis.db1.mappers: "demo4021.dso.mapper"
- 通过 xml 目录进行配置。xml 可以固定在一个资源目录下
mybatis.db1.mappers: "mybatis/db1/*.xml"
3、应用示例
//启动应用
public class DemoApp {
public static void main(String[] args) {
Solon.start(DemoApp.class, args);
}
}
//配置数据源
@Configuration
public class Config {
//此下的 db1 与 mybatis.db1 将对应在起来 //可以用 @Db("db1") 注入mapper
//typed=true,即除了名字外还是默认数据源 //可以用 @Db 注入 mapper
@Bean(value = "db1", typed = true)
public DataSource db1(@Inject("${demo.db1}") HikariDataSource ds) {
return ds;
}
//调整 db1 的配置(如:增加插件)
@Bean
public void db1_cfg(@Db("db1") org.apache.ibatis.session.Configuration cfg) {
MybatisPlusInterceptor plusInterceptor = new MybatisPlusInterceptor();
plusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
cfg.setCacheEnabled(false);
cfg.addInterceptor(plusInterceptor);
}
}
//应用
@Service
public class AppService{
//可用 @Db 或 @Db("db1") 注入
@Db
AppMapper appMapper; //xml sql mapper
//可用 @Db 或 @Db("db1")
@Db
BaseMapper<App> appBaseMapper; //base mapper
public void test(){
//三种不同接口的样例
App app1 = appMapper.getAppById(12);
App app2 = appBaseMapper.getById(12);
}
}
具体可参考:
https://github.com/noear/solon-examples/tree/main/4.Solon-Data/demo4031-mybatisplus