当前位置:网站首页>Kotlin's use of the no Arg compiler plug-in in gradle
Kotlin's use of the no Arg compiler plug-in in gradle
2022-07-03 11:09:00 【Lindroy】
1、 Preface
Recently in use Kotlin
+Spring Boot
Write a backend project , Entity classes habitually use Kotlin
Medium data class
, however Spring Requires a parameterless constructor , Otherwise you might throw it java.sql.SQLDataException
. To make data class
Can generate a parameterless constructor , There are two ways to do :
1. to data class
Each parameter in the constructor of is assigned a default value . such as :
data class User(
@TableId(value = "id", type = IdType.AUTO)
var id: Int?=-1,
var userName: String?=null, // user name
var age: Int?=null, // Age
var password: String?=null, // password
var name: String?=null, // full name
var email: String?=null // mailbox
) : Serializable
2. Use the parameterless compiler plug-in no-arg
. Let's take a look at this method .
2、 Specific steps
2.1 Add plug-in dependencies
First of all, it needs to be carried out at the beginning of the project build.gradle
Add no-arg
Plug in dependency , Version follows Kotlin
Consistent versions of :
plugins {
……
id "org.jetbrains.kotlin.plugin.noarg" version "1.3.41"
}
I use it plugins
In the form of , If you're using buildscript
Blocks can be added in this way :
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-noarg:1.3.41"
}
}
apply plugin: "kotlin-noarg"
2.2 Specify the nonparametric annotation list
Tell the truth , This step is not easy to understand when reading official documents , Here I only describe my experience after my own practice . First create an annotation class , We can define the class name as NoArg
:
annotation class NoArg
Then the most critical step came , Back to engineering build.gradle
file , Add a list of nonparametric comments :
noArg {
annotation("com.lindroid.projectname.annotation.NoArg")
}
annotation
The path in is what we created NoArg
The package directory where the annotation class is located . The path must be written well , Don't make mistakes ! At this point, the parameterless plug-in has been configured . We can do it in data class
Add @NoArg
annotation , In this way, the compiler can generate a parameterless constructor for it . An example is as follows :
@NoArg
data class User(
@TableId(value = "id", type = IdType.AUTO)
var id: Int?,
var userName: String?, // user name
var age: Int?, // Age
var password: String?, // password
var name: String?, // full name
var email: String? // mailbox
) : Serializable
2.3 kotlin-jpa Nonparametric annotation in
If you have added kotlin-jpa
plug-in unit , Basically, there is no need to add a parameterless plug-in alone .kotlin-jpa
The plug-in without parameters is packaged , When you use @Entity
、@Embeddable
And @MappedSuperclass
These notes , By default, no Parameter annotation is supported .
3、 Reference article
边栏推荐
- . Net core - a queuing system for wechat official account
- The solution that prompts "system group policy prohibits the installation of this device" under win10 system (home version has no group policy)
- What happened to those who focused on automated testing?
- 在职美团测试工程师的这八年,我是如何成长的,愿技术人看完都有收获
- 使用onvif协议操作设备
- 2021 reading summary (continuously updating)
- Lecture 1 number field
- 软件测试必学基本理论知识——APP测试
- 使用ML.NET+ONNX预训练模型整活B站经典《华强买瓜》
- Extern keyword
猜你喜欢
最高月薪18K 拥有好的“心态和选择”, 成功就差“认真和坚持”~
MAUI Developer Day in GCR
How can UI automated testing get out of trouble? How to embody the value?
T5 attempt
硬 货 | 一改测试步骤代码就全写?为什么不试试用 Yaml实现数据驱动?
ByteDance layoffs, test engineers were almost destroyed: how terrible is the routine behind the recruitment of large factories?
redis那些事儿
Stack, monotone stack, queue, monotone queue
解决undefined reference to `__aeabi_uidivmod‘和undefined reference to `__aeabi_uidiv‘错误
Do you really need automated testing?
随机推荐
《通信软件开发与应用》
Win10系统下提示“系统组策略禁止安装此设备”的解决方案(家庭版无组策略)
Qt:qss custom qgroupbox instance
硬 货 | 一改测试步骤代码就全写?为什么不试试用 Yaml实现数据驱动?
Imread change image display size
解决undefined reference to `__aeabi_uidivmod‘和undefined reference to `__aeabi_uidiv‘错误
C language project: student achievement system
Software testing e-commerce projects that can be written into your resume, don't you come in and get it?
The solution that prompts "system group policy prohibits the installation of this device" under win10 system (home version has no group policy)
IIS修改配置信息后不生效
Some abilities can't be learned from work. Look at this article, more than 90% of peers
Typescript learning summary
Software testing redis database
QT: QSS custom qtabwidget and qtabbar instances
Solutions of n-ary linear equations and their criteria
Qt:qss custom qspinbox instance
T5 attempt
Qt:qss custom qpprogressbar instance
Communication software development and Application
线性表的双链表