说明
Solon 不像 Jfinal 提供了基础的数据库能力,是通过集成第三方的 ORM 来实现的,当然 Solon 家也有自己的 ORM(wood)。结合自己使用了多个 ORM 的情况,选择了无 XML 的 easy-query。虽然 Solon 也有 activerecord 的插件,但是 Jfinal 框架内,使用还是不够趁手。
个人确实比较喜欢 activerecord,后续也会补充对 easy-query 的扩展,比如继承 activerecord 的 sql 模版管理,基于 map 的 Model Bean。
这里使用的是 MySQL 数据库,其他数据库是类似的,切换对应的驱动即可。
创建模块
在 IDEA 中通过 File->New->Modules... 可以创建新的模块 demo-orm,基础代码和配置可以从demo01中拷贝过来,或者自己手动创建。
修改依赖
使用初始器创建项目,增加 web 和 easy-query 依赖,同时增加 swagger 依赖,并非必须,只是为了方便测试接口。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| plugins { id 'java' id "io.freefair.lombok" }
group = "com.example"
version "${demoVersion}" description = "orm demo"
dependencies { implementation platform(project(":demo-parent"))
implementation("org.noear:solon-web") implementation("org.noear:solon-logging-logback") implementation("org.noear:solon-openapi2-knife4j") implementation("com.zaxxer:HikariCP") implementation("mysql:mysql-connector-java") implementation("com.easy-query:sql-solon-plugin") annotationProcessor("org.mapstruct:mapstruct-processor:${mapstructVersion}") annotationProcessor("com.easy-query:sql-processor:${easyQueryVersion}") testImplementation("org.noear:solon-test-junit5") }
compileJava { options.encoding = "UTF-8" options.compilerArgs << "-parameters" }
jar { manifest { attributes "Main-Class": "com.example.demo.orm.DemoOrmApp" }
dependsOn(configurations.runtimeClasspath)
duplicatesStrategy = DuplicatesStrategy.EXCLUDE from(configurations.runtimeClasspath.collect { if (it.isDirectory()) it else zipTree(it) }) { exclude 'META-INF/MANIFEST.MF' exclude 'META-INF/*.SF' exclude 'META-INF/*.DSA' exclude 'META-INF/*.RSA' }
def sourcesMain = sourceSets.main sourcesMain.allSource.forEach { println("add from sources: ${it.name}") } from(sourcesMain.output) }
|
创建数据库
1 2 3 4 5 6 7 8
| CREATE TABLE `demo_dept` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'id', `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '部门名称', `code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '部门编码', `create_at` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '时间时间', `update_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='部门表';
|
修改配置
app.yml
1 2 3 4 5 6 7 8
| solon.dataSource: db1: jdbcUrl: jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=true driverClassName: com.mysql.cj.jdbc.Driver username: demo password: 123456
|
配置数据源
1 2 3 4 5 6 7
| @Configuration public class EasyQueryConfig { @Bean(name = "db1", typed = true) public DataSource db1(@Inject("${demo.db1}") HikariDataSource ds) { return ds; } }
|
配置文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
@Configuration public class SwaggerConfig { @Inject private OpenApiExtensionResolver openApiExtensionResolver;
@Bean("adminApi") public DocDocket adminApi() { DocDocket docDocket = new DocDocket() .groupName("Admin 端接口") .info( new ApiInfo() .title("porpoise-demo") .description("在线API文档") .contact(new ApiContact().name("CrazyAirhead").email("l4qiang@hotmail.com")) .version("1.0")) .schemes(Scheme.HTTP.toValue()) .globalResponseInData(true) .vendorExtensions(openApiExtensionResolver.buildExtensions()) .apis("com.example.demo");
return docDocket; } }
|
增加 model
easy-query 提供了插件可以生成 model,通过IDEA 的database 连接数据库,选择表,右键选择TableEntityGenerate,填写对应的信息,点击 ok。


生成代码后需要使用Ctrl+enter,呼出IDEA的代码生成菜单,点击 EntityQueryImplement。

如果没有使用插件,自己创建实体类即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| package com.example.demo.orm.model;
import com.easy.query.core.annotation.Column; import com.easy.query.core.annotation.EntityProxy; import com.easy.query.core.annotation.Table; import com.easy.query.core.proxy.ProxyEntityAvailable; import com.example.demo.orm.model.proxy.DeptEntityProxy; import lombok.Data;
import java.time.LocalDateTime;
@Data @Table(value = "demo_dept") @EntityProxy public class DeptEntity implements ProxyEntityAvailable<DeptEntity, DeptEntityProxy> {
@Column(primaryKey = true, value = "id", generatedKey = true) private Long id;
private String name;
private String code;
private LocalDateTime createAt;
private LocalDateTime updateAt;
}
|
增加Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| @Controller @Mapping("/dept") @Api("部门管理") public class DeptController { @Db("db1") private EasyEntityQuery entityQuery;
@Mapping @Get @ApiOperation("获取列表") public List<DeptEntity> list() { return entityQuery.queryable(DeptEntity.class).toList(); }
@Mapping @Post @ApiOperation("新增") public Boolean add(@Body DeptEntity dept) { return entityQuery.insertable(dept).executeRows(true) > 0L; }
@Mapping @Put @ApiOperation("更新") public Boolean update(@Body DeptEntity dept) { return entityQuery.updatable(dept).executeRows() > 0L; }
@Mapping("/{id}") @Get @ApiOperation("获取") public DeptEntity get(Long id) { return entityQuery.queryable(DeptEntity.class).whereById(id).firstOrNull(); }
@Mapping("/{id}") @Delete @ApiOperation("删除") public Boolean delete(Long id) { return entityQuery .getEasyQueryClient() .deletable(DeptEntity.class) .allowDeleteStatement(true) .whereById(id) .executeRows() > 0L; }
|
验证
验证有很多种方式,这里通过 swagger 文档进行调用。浏览器输入 http://localhost:8080/doc.html
创建

查询

其他的接口可以自行尝试。
小结
通过以上步骤已经,可以实现对 dept 表的基础增删改查了,可以自己继续添加一些功能进行尝试性的修改。Solon 第三方的 orm 插件非常的多,如果不喜欢使用 easy-query,也可以选择其他 orm 框架进行尝试。
完整代码可以从 https://gitee.com/CrazyAirhead/porpoise-demo 获取。