Solon

beetlsql-solon-plugin

<dependency>
    <groupId>org.noear</groupId>
    <artifactId>beetlsql-solon-plugin</artifactId>
</dependency>

1、描述

数据扩展插件,为 Solon Data 提供基于 beetlsql 的框架适配,以提供ORM支持。

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
  

# 配置印射的是 SQLManagerBuilder 字段(1.10.3 开始支持)
beetlsql.db1:
  dialect: "mysql" #快捷配置:方言
  slaves: "db2,db3"  #快捷配置:从库配置
  dev: true  #快捷配置:是否调试模式
  nc: "org.wisecloud.common.db.beetlsql.JPANameConversion" #字段映射
  dbStyle: "org.beetl.sql.core.db.MySqlStyle" #字段映射:与 dialect 效果相同
  interceptorList: #字段映射
    - "org.beetl.sql.ext.DebugInterceptor" #与 dev 效果相同
    - "org.beetl.sql.ext.Slf4JLogInterceptor"

3、应用示例

//启动应用
public class DemoApp {
    public static void main(String[] args) {
        Solon.start(DemoApp.class, args, app -> {
            //订阅事件,后以定制
            app.onEvent(SQLManagerBuilder.class, c -> {
                //启用开发或调试模式(可以打印sql)
                if (Solon.cfg().isDebugMode() || Solon.cfg().isFilesMode()) {
                    if (c.getName().contains("db1")) {//如有必要,可通过 name 进行识别
                        c.addInterDebug();
                    }
                }
            });
        });
    }
}

//配置数据源
@Configuration
public class Config {
    @Bean(name="db1", typed=true, attrs="dialect=mysql")
    public DataSource db1(@Inject("${demo.db1}") HikariDataSource ds) {
        return ds;
    }
}

//应用
@Service
public class AppService{
    @Db
    AppMapper appMapper; //xml sql mapper

    @Db
    BaseMapper<App> appBaseMapper; //base mapper
    
    public void test(){
        //三种不同接口的样例
        App app1 = appMapper.getAppById(12);
        App app2 = appBaseMapper.getById(12);
    }
}

4、分页查询

模板示例(下面的 \ 要去掉)

appx_getlist_page
===
\```sql
select
    -- @pageTag(){
    app_id,name
    -- @} 
from appx where app_id > #{app_id}
\```

代码示例:

public class PageService {
    @Db
    SqlMapper sqlMapper;

    public PageRequest<AppxModel> page() throws Exception{
        //分页查询
        PageRequest pageRequest = DefaultPageRequest.of(1,10);
        return sqlMapper.appx_getlist_page(pageRequest, 1);
    }
}

具体可参考:

https://gitee.com/noear/solon_demo/tree/master/demo09.solon_beetlsql