First published on Enaium's personal blog
The official use casesJimmer
used in this article are used to introduce the usage method, and at the same time support and , this article is used to introduce, in fact, it is more convenient than to use, here for the convenience of everyone to understand, use to introduce, this article is just a brief introduction, more Please refer to the official document for the contentJimmer
Jimmer
Java
Kotlin
Java
Kotlin
Java
Java
Jimmer
Let’s not start with the introduction of the entity class. Here is a brief introduction to the relationship between the three tables used:
BookStore
A bookstore can have multipleBook
Book
Books can belong to multipleBookStore
, can have multipleAuthor
Author
Authors can have multipleBook
, many-to-many book-to-author relationships.
Inquire
Jimmer
It can be used SpringData
(not SpringDataJPA
), but here we will first introduce SpringData
the use method of separation, but still in SpringBoot
the environment, here we use H2
the memory database, Jimmer
support H2
, MySQL
, PostgreSQL
, Oracle
and other databases, here we use H2
the memory database.
The queries here are all used Controller
for demonstration.
Find all bookstores
createQuery
It is to create a query, select
which is to select the field to be queried, and here it is directly passed in BookStoreTable
to represent all the fields to be queried.
What is used here sql
is the object used Jimmer
. Sql
This object is Jimmer
the core object. All queries are realized through this object, and Spring
the injection method used is injected into JSqlClient
the object.
final BookStoreTable bookStore = BookStoreTable.$;//这里的$是一个静态方法,返回一个BookStoreTable对象
sql.createQuery(bookStore).select(bookStore).execute();
The query results are as follows:
[
{
"createdTime": "2023-05-27 11:00:37",
"modifiedTime": "2023-05-27 11:00:37",
"id": 1,
"name": "O'REILLY",
"website": null
},
{
"createdTime": "2023-05-27 11:00:37",
"modifiedTime": "2023-05-27 11:00:37",
"id": 2,
"name": "MANNING",
"website": null
}
]
specify query fields
This is how you need to query the specified field. Here name
is BookStoreTable
a field, but here Controller
is BookStore
an object, so just query all the fields as above.
sql.createQuery(bookStore).select(bookStore.name()).execute();
As in the above example, if we insist on querying the specified field and do not want to define a new DTO
object, then this Jimmer
can also be implemented very simply, that is, the in- Jimmer
useFetchr
BookStore
The fields used Fetchr
to specify the query
sql.createQuery(bookStore).select(bookStore.fetch(BookStoreFetcher.$.name())).execute();
The query results are as follows:
[
{
"id": 2,
"name": "MANNING"
},
{
"id": 1,
"name": "O'REILLY"
}
]
Surprisingly, Controller
the return type is , but there are only and fields BookStore
in the query results .id
name
Controller
Here I post the complete code, List
the type of is BookStore
the entity class of , which is Jimmer
the power of , without defining DTO
the object, you can realize the function of querying the specified field.
@GetMapping("/simpleList")
public List<BookStore> findSimpleStores() {
final BookStoreTable bookStore = BookStoreTable.$;//这里的$是一个静态方法,返回一个BookStoreTable对象
return sql.createQuery(bookStore).select(bookStore.fetch(BookStoreFetcher.$.name())).execute();
}
Like entity classes Table
, Fetcher
it is also possible to declare a static constant.
private static final Fetcher<BookStore> SIMPLE_FETCHER = BookStoreFetcher.$.name();
This is how it can be used.
sql.createQuery(bookStore).select(bookStore.fetch(SIMPLE_FETCHER)).execute();
Fetcher
The use described in detail next
Query all scalar fields, that is, non-relational fields.
private static final Fetcher<BookStore> DEFAULT_FETCHER = BookStoreFetcher.$.allScalarFields();//这里的allScalarFields()就是查询所有标量字段
Fields not BookStore
queried on top of all scalar fieldsname
private static final Fetcher<BookStore> DEFAULT_FETCHER = BookStoreFetcher.$.allScalarFields().name(false);//这里的name(false)就是不查询name字段
Specify query related fields
像这样查询所有书店的所有书籍,并且查询书籍的所有作者,这样就可以使用Fetcher
来实现,如果在使用传统ORM
框架时,这里就需要定义一个DTO
对象来接收查询结果,但是在Jimmer
中,不需要定义DTO
对象,就可以实现查询指定字段的功能,可能有读者会问了,没有DTO
前端怎么接收数据呢,这里先剧透一下,Jimmer
会根据后端写的Fetcher
来生成前端的DTO
,这里就不多说了,后面会详细介绍.
private static final Fetcher<BookStore> WITH_ALL_BOOKS_FETCHER =
BookStoreFetcher.$
.allScalarFields()//查询所有标量字段
.books(//查询关联字段
BookFetcher.$//书籍的Fetcher
.allScalarFields()//查询所有标量字段
.authors(//查询关联字段
AuthorFetcher.$//作者的Fetcher
.allScalarFields()//查询所有标量字段
)
);
稍剧透一点,这里如果使用Kotlin
来编写会更加简洁,因为Kotlin
中的DSL
特性
private val WITH_ALL_BOOKS_FETCHER = newFetcher(BookStore::class).by {
allScalarFields()//查询所有标量字段
books {//查询关联字段
allScalarFields()//查询所有标量字段
authors {//查询关联字段
allScalarFields()//查询所有标量字段
}
}
}
这么一看Kotlin
确实比Java
简洁很多,但本篇文章还是介绍的是Java
的使用方法.
指定查询条件和计算结果字段
如果需要查询书店中所有书籍的平均价格,那么就要查询书店中所有书籍的价格,然后计算平均值,这里先把查询的代码写出来,然后在介绍如何把计算结果字段添加到Fetcher
中.
sql.createQuery(bookStore)//这里的bookStore是一个BookStoreTable对象
.where(bookStore.id().in(ids))//要查询的书店的id集合,也可以直接指定id,比如.eq(1L)
.groupBy(bookStore.id())//按照书店的id分组
.select(
bookStore.id(),//查询书店的id
bookStore.asTableEx().books(JoinType.LEFT).price().avg().coalesce(BigDecimal.ZERO)//查询书店中所有书籍的平均价格
)
.execute();//这样执行查询后,返回的结果就是书店的id和书店中所有书籍的平均价格,在Jimmer中会返回一个List<Tuple2<...>>类型的结果,其中Tuple元组的数量和查询的字段数量一致,这里就是2个字段,所以就是Tuple2
这里最后的select
是查出了书店的 id 和书店中所有书籍的平均价格,asTableEx()
是为了突破Jimmer
的限制,Jimmer
中的Table
只能查询标量字段,而不能查询关联字段,这里的asTableEx()
就是为了查询关联字段,asTableEx()
的参数是JoinType
,这里的JoinType
是LEFT
,表示左连接,如果不指定JoinType
,默认是INNER
,表示内连接.
这里的avg()
是计算平均值的意思,coalesce(BigDecimal.ZERO)
是为了防止计算结果为null
,如果计算结果为null
,那么就返回BigDecimal.ZERO
.
这里介绍如何把计算结果字段添加到Fetcher
中,这样就又引出了一个Jimmer
的功能计算属性
计算属性
在Jimmer
中如果要添加计算属性,那么就要实现TransientResolver
接口,这里先把代码贴出来,然后再详细介绍.
@Component
public class BookStoreAvgPriceResolver implements TransientResolver<Long, BigDecimal> {
@Override
public Map<Long, BigDecimal> resolve(Collection<Long> ids) {
return null;
}
}
这里的ids
就是书店的 id 集合,这里的resolve
方法就是计算书店中所有书籍的平均价格,这里的Long
是书店的 id,BigDecimal
是书店中所有书籍的平均价格,这里的resolve
方法返回的Map
的key
就是书店的 id,value
就是书店中所有书籍的平均价格.
接着配合上面写的查询代码,完成计算的代码
BookStoreTable bookStore = BookStoreTable.$;
return sql.createQuery(bookStore)
.where(bookStore.id().in(ids))
.groupBy(bookStore.id())
.select(
bookStore.id(),
bookStore.asTableEx().books(JoinType.LEFT).price().avg().coalesce(BigDecimal.ZERO)
)
.execute()//这里的execute()返回的结果是List<Tuple2<Long, BigDecimal>>类型的
.stream()//这里把List转换成Stream
.collect(
Collectors.toMap(Tuple2::get_1, Tuple2::get_2)//这里把List<Tuple2<Long, BigDecimal>>转换成Map<Long, BigDecimal>
);
这样一个TransientResolver
的实现就完成了,接着就是把TransientResolver
添加到实体类中
Jimmer
中定义实体类是在接口中定义的
@Transient(BookStoreAvgPriceResolver.class)//这里的BookStoreAvgPriceResolver.class就是上面写的计算属性的实现
BigDecimal avgPrice();//这里的avgPrice()就是计算属性,这里的BigDecimal就是计算属性的类型
这样就可以直接在Fetcher
中查询计算属性了
private static final Fetcher<BookStore> WITH_ALL_BOOKS_FETCHER =
BookStoreFetcher.$
.allScalarFields()
.avgPrice()//这里就是查询计算属性
//...省略
接着看戏生成的SQL
代码和查询结果,这里照样省略其他查询只关注标量字段和计算属性
select
tb_1_.ID,
coalesce(
avg(tb_2_.PRICE), ? /* 0 */
)
from BOOK_STORE tb_1_
left join BOOK tb_2_
on tb_1_.ID = tb_2_.STORE_ID
where
tb_1_.ID in (
? /* 1 */
)
group by
tb_1_.ID
{
"createdTime": "2023-05-27 12:04:39",
"modifiedTime": "2023-05-27 12:04:39",
"id": 1,
"name": "O'REILLY",
"website": null,
"avgPrice": 58.5
}
定义实体类
在Jimmer
中定义实体类是在接口中定义的,这里先把代码贴出来,然后再详细介绍.
BookStore
@Entity//这里的@Entity就是实体类
public interface BookStore extends BaseEntity {
@Id//这里的@Id就是主键
@GeneratedValue(strategy = GenerationType.IDENTITY)//这里的strategy = GenerationType.IDENTITY就是自增长
long id();//这里的id()就是实体类的id
@Key
String name();//业务主键
@Null//这里的@Null就是可以为null,建议使用Jetbrains的@Nullable
String website();
@OneToMany(mappedBy = "store", orderedProps = {
@OrderedProp("name"),
@OrderedProp(value = "edition", desc = true)
})//这里的@OneToMany就是一对多,这里的mappedBy = "store"就是Book中的store字段,这里的orderedProps就是排序字段
List<Book> books();
@Transient(BookStoreAvgPriceResolver.class)//这里的BookStoreAvgPriceResolver.class就是上面写的计算属性的实现
BigDecimal avgPrice();//这里的avgPrice()就是计算属性,这里的BigDecimal就是计算属性的类型
}
Book
@Entity
public interface Book extends TenantAware {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
long id();
@Key//这里的@Key就是业务主键
String name();
@Key//和上面的name()一样,这里的@Key就是业务主键,表示name和edition的组合是唯一的
int edition();
BigDecimal price();
@Nullable
@ManyToOne
BookStore store();
@ManyToMany(orderedProps = {
@OrderedProp("firstName"),
@OrderedProp("lastName")
})//这里的@ManyToMany就是多对多,这里的orderedProps就是排序字段
@JoinTable(
name = "BOOK_AUTHOR_MAPPING",//这里的name就是中间表的表名
joinColumnName = "BOOK_ID",//这里的joinColumnName就是中间表的外键
inverseJoinColumnName = "AUTHOR_ID"//这里的inverseJoinColumnName就是中间表的外键
)
List<Author> authors();
}
Author
@Entity
public interface Author extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
long id();
@Key
String firstName();
@Key
String lastName();
Gender gender();//这里的Gender就是枚举类型
@ManyToMany(mappedBy = "authors", orderedProps = {
@OrderedProp("name"),
@OrderedProp(value = "edition", desc = true)
})//这里的@ManyToMany就是多对多,这里的mappedBy = "authors"就是Book中的authors字段,这里的orderedProps就是排序字段
List<Book> books();
}
public enum Gender {
@EnumItem(name = "M")//这里的name表示在数据库中存储的值
MALE,
@EnumItem(name = "F")
FEMALE
}
如果使用过Spring Data JPA
的话,这里的代码应该很熟悉,Jimmer
中的实体类的关联关系和Spring Data JPA
中的关联关系是一样的.
生成前端代码
还记得前面的剧透吗,现在开始正式介绍如何生成前端代码,这里先把生成的代码贴出来,然后再详细介绍.
DTO
There are a lot of generated based on Controller
the return type here , so I won’t post it here, just post one code.Fetcher
DTO
BookStoreDto
export type BookStoreDto = {
//只有查询书店的name
"BookStoreService/SIMPLE_FETCHER": {
readonly id: number
readonly name: string
}
//查询书店的所有字段
"BookStoreService/DEFAULT_FETCHER": {
readonly id: number
readonly createdTime: string
readonly modifiedTime: string
readonly name: string
readonly website?: string
}
//查询书店的所有字段和书店中所有书籍的所有字段还有书籍的所有作者的所有字段
"BookStoreService/WITH_ALL_BOOKS_FETCHER": {
readonly id: number
readonly createdTime: string
readonly modifiedTime: string
readonly name: string
readonly website?: string
readonly avgPrice: number //这里的avgPrice就是计算属性
readonly books: ReadonlyArray<{
readonly id: number
readonly createdTime: string
readonly modifiedTime: string
readonly name: string
readonly edition: number
readonly price: number
readonly authors: ReadonlyArray<{
readonly id: number
readonly createdTime: string
readonly modifiedTime: string
readonly firstName: string
readonly lastName: string
readonly gender: Gender
}>
}>
}
}
Controller
BookStoreController
Only look at the main request here
Here Jimmer
all Controller
the requests are put in one Controller
, here Controller
is BookStoreController
, here BookStoreController
is BookStore
the entity class , the code Controller
here is as followsBookStoreController
async findComplexStoreWithAllBooks(options: BookStoreServiceOptions['findComplexStoreWithAllBooks']): Promise<
BookStoreDto['BookStoreService/WITH_ALL_BOOKS_FETCHER'] | undefined
> {
let _uri = '/bookStore/';
_uri += encodeURIComponent(options.id);
_uri += '/withAllBooks';
return (await this.executor({uri: _uri, method: 'GET'})) as BookStoreDto['BookStoreService/WITH_ALL_BOOKS_FETCHER'] | undefined
}
async findSimpleStores(): Promise<
ReadonlyArray<BookStoreDto['BookStoreService/SIMPLE_FETCHER']>
> {
let _uri = '/bookStore/simpleList';
return (await this.executor({uri: _uri, method: 'GET'})) as ReadonlyArray<BookStoreDto['BookStoreService/SIMPLE_FETCHER']>
}
async findStores(): Promise<
ReadonlyArray<BookStoreDto['BookStoreService/DEFAULT_FETCHER']>
> {
let _uri = '/bookStore/list';
return (await this.executor({uri: _uri, method: 'GET'})) as ReadonlyArray<BookStoreDto['BookStoreService/DEFAULT_FETCHER']>
}
Configure code generation
You need to specify the access address of the generated code in the configuration, because Jimmer
the generated front-end code is a compressed package, you can download the generated source code by visiting this address
jimmer:
client:
ts:
path: /ts.zip #这里的path就是访问地址
Then configure Controller
the return type
@GetMapping("/simpleList")
public List<@FetchBy("SIMPLE_FETCHER") BookStore> findSimpleStores() {
final BookStoreTable bookStore = BookStoreTable.$;
return sql.createQuery(bookStore).select(bookStore.fetch(SIMPLE_FETCHER)).execute();
}
Annotations are used here FetchBy
, where the value is Fetcher
the constant of the current class. If Fetcher
it is not under the current class, you can specify the class in the annotation ownerType
to specify it.Fetcher
Well, the basic use ofJimmer
Jimmer is over. If you want to know more about how to use it, you can check Jimmer
the documentation or watch Jimmer
the video tutorial recorded by the author . Computed properties of -0.7