简介
本文使用 Jimmer 写了一个小案例,不和任何框架耦合,即没有使用任何其它熟知的 Java/Kotlin 框架。如果集成框架,那么整合后使用更加方便、简单。
本文使用 Gradle + Kotlin 编写 Demo。源代码示例见:https://github.com/jhlzlove/gradle-project-example/tree/main/jimmer-example
Action!
依赖
build.gradle.kts
内容如下:
build.gradle.kts 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 plugins { kotlin("jvm" ) version "1.9.23" id("com.google.devtools.ksp" ) version "1.9.23-1.0.20" } val jimmerVersion = "0.8.132" dependencies { ksp("org.babyfish.jimmer:jimmer-ksp:${jimmerVersion} " ) implementation("org.babyfish.jimmer:jimmer-sql-kotlin:${jimmerVersion} " ) runtimeOnly("org.postgresql:postgresql:42.7.3" ) implementation("ch.qos.logback:logback-classic:1.5.6" ) testImplementation(kotlin("test" )) } kotlin { jvmToolchain(21 ) } kotlin { sourceSets.main { kotlin.srcDir("build/generated/ksp/main/kotlin" ) } }
注意:
Jimmer 的 ksp 插件需要和自己使用的 Kotlin 版本一致或者兼容;
如果使用的 Java + Maven
pom.xml 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 <dependencies > <dependency > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > <version > ${lombok.version}</version > </dependency > <dependency > <groupId > org.babyfish.jimmer</groupId > <artifactId > jimmer-spring-boot-starter</artifactId > <version > ${jimmer.version}</version > </dependency > </dependencies > <build > <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-compiler-plugin</artifactId > <version > 3.10.1</version > <configuration > <annotationProcessorPaths > <path > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > <version > ${lombok.version}</version > </path > <path > <groupId > org.babyfish.jimmer</groupId > <artifactId > jimmer-apt</artifactId > <version > ${jimmer.version}</version > </path > </annotationProcessorPaths > </configuration > </plugin > ... </build >
若是多模块项目,plugin 部分请放在实体类所在的 pom 文件中。当然你想都放也是没问题哒!
数据库脚本
数据库名称为 db_jimmer
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 CREATE TABLE public.db_test ( id bigserial NOT NULL , username varchar NOT NULL , gender varchar NOT NULL , birthday date NULL , address varchar NULL , create_time timestamptz DEFAULT CURRENT_TIMESTAMP NULL , update_time timestamptz DEFAULT CURRENT_TIMESTAMP NULL , remark varchar NULL , CONSTRAINT db_test_pk PRIMARY KEY (id) ); COMMENT ON COLUMN public.db_test.username IS '姓名' ; COMMENT ON COLUMN public.db_test.gender IS '性别' ; COMMENT ON COLUMN public.db_test.birthday IS '生日' ; COMMENT ON COLUMN public.db_test.address IS '住址' ;
代码
Jimmer 这款 ORM 可以与任何框架集成,由于本文的 demo 没有任何的框架集成,所以这里的实现非常的简单粗暴。如果想要自己实现数据库连接池,可以自己编写 DataSource
的实现。
Jimmer 配置
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 class JimmerConfig { companion object { fun connect () : Connection { val url = "jdbc:postgresql://localhost:5432/db_jimmer" val username = "postgres" val password = "postgres" return DriverManager.getConnection(url, username, password) } fun sqlClient () : KSqlClient = newKSqlClient { setExecutor(Executor.log()) setSqlFormatter(SqlFormatter.PRETTY) setConnectionManager { connect().use { proceed(it) } } setDialect(PostgresDialect()) setDatabaseValidationMode(DatabaseValidationMode.ERROR) setDatabaseNamingStrategy(DefaultDatabaseNamingStrategy.LOWER_CASE) } } }
实体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @Entity @Table(name = "db_test" ) interface User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) val id: Int @Key val username: String val gender: String val birthday: LocalDate? val address: String? val createTime: LocalDateTime? val updateTime: LocalDateTime? val remark: String? }
主函数运行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 fun main () { val sqlClient = JimmerConfig.sqlClient() val user = new(User::class ).by { username = "江湖浪子" gender = "MALE" address = "地球" birthday = LocalDate.parse("2000-10-01" ) } sqlClient.save(user) val list = sqlClient.createQuery(User::class ) { select(table) }.execute() println(list) }