I. Introduction
The java springboot background project uses jpa
the query database, and also uses it jhipster
. This thing can automatically generate basic Controller
, Service
, Dao
, JavaBean
and other related codes to reduce repeated development.
Here is a summary of how to use it.
2. The method of automatically generating java code by jhipster
1. You need to install node first, and the version is consistent with that of other colleagues.
2. In order to ensure that the code generated by jhipster matches the version of the project framework, you can copy a compressed package from other people's computers, and then unzip it to your own directory (myname is the user name of your computer C:\Users\myname\AppData\Roaming\npm
). The compressed package contains the following files :
//文件夹
node_modules
//文件
jhipster
//文件
jhipster.cmd
Among them, there is a folder in the node_modules folder generator-jhipster
, which is the jhipster dependency package.
3. If step 2 is not required, you can use the command:
npm install -g generator-jhipster
//npm install -g [email protected]
//npm uninstall -g generator-jhipster
Install jhipster globally, and it will eventually be downloaded generator-jhipster
to the corresponding directory.
.jhipster
4. Open the java project, you can create a new folder in the project folder , and then create a Mytable.json
file in this folder, the content sample is as follows:
{
"changelogDate": "20221125234248",
"databaseType": "sql",
"dto": "mapstruct",
"entityTableName": "mytable",
"fields": [{
"fieldName": "level",
"fieldValidateRules": ["required"],
"fieldType": "Integer",
"javadoc": "级别"
}, {
"fieldName": "name",
"fieldValidateRulesMaxlength": 50,
"fieldValidateRules": ["required", "maxlength"],
"fieldType": "String",
"javadoc": "名称"
}, {
"fieldName": "creator",
"fieldValidateRulesMaxlength": 20,
"fieldValidateRules": ["maxlength"],
"fieldType": "String",
"javadoc": "创建人"
}, {
"fieldName": "createTime",
"fieldType": "Instant",
"javadoc": "创建时间"
}, {
"fieldName": "updator",
"fieldValidateRulesMaxlength": 20,
"fieldValidateRules": ["maxlength"],
"fieldType": "String",
"javadoc": "修改人"
}, {
"fieldName": "updateTime",
"fieldType": "Instant",
"javadoc": "修改日期"
}, {
"fieldName": "order",
"fieldType": "Integer",
"javadoc": "排序顺序"
}, {
"fieldName": "isDel",
"fieldType": "Integer",
"javadoc": "是否删除。1是逻辑删除。"
}],
"fluentMethods": true,
"jpaMetamodelFiltering": true,
"pagination": "pagination",
"relationships": [],
"searchEngine": false,
"service": "serviceClass"
}
5. Use the cmd window to enter .jhipster
the folder and execute the command:
jhipster entity Mytable
Then there will be some options, you can choose the first one, and then enter y, after the operation is completed, the relevant java files will be automatically created in the project.
6. Among the automatically created java files,
com.my.web.rest.MytableResource.java
there are Controller layer files;
com.my.service.MytableService.java
Service layer files;
com.my.repository.MytableRepository.java
Dao layer files;
com.my.service.dto.MytableDTO.java
JavaBean files;
and some other files.
7. It is also used in the project liquibase
, so there are also in the generated jhipster
file:
项目名\src\main\resources\config\liquibase\master.xml
, this will add a new line of include, which will be used when the project starts (you can comment that line if it is not used);
项目名\src\main\resources\config\liquibase\changelog\20221125234248_added_entity_Mytable.xml
, it will also be used when the project starts.
8. When the project starts, it will read 20221125234248_added_entity_Mytable.xml
and then query the database databasechangelog
table (generated by liquibase). If there is no data, create a new database table according to the xml file Mytable
;
if there is a change that does not pass the verification, an error will be reported. (However, it does not affect the project startup, but it will affect the execution of other xml files)
3. Remarks
1. The database table built in this way has no default field default. It seems that jhipster does not support default fields at present.
2. If the project startup verification fails due to the change of the field in the database table, you can:
(1) Modify Mytable.json, and then regenerate the java code automatically (note that if the used code is overwritten, restore the code yourself);
(2) ) Back up the database table, and then rename the table; (
3) Find the line that failed the verification ( the field content will be reported when the project starts an error report, click this field to search), and delete this line after backup. (Or correct the fields.) (4) After the operation (3), start the project, and the project will automatically generate the table again , and no error will be reported.databasechangelog
MD5SUM
MD5SUM
Mytable
fieldValidateRulesMaxlength
3. Note that the and in jhipster's json fieldValidateRules
cannot be used indiscriminately:
// "fieldValidateRulesMaxlength": 11,
// "fieldValidateRules": ["maxlength"],
If you use it wrong, it will cause an error when calling the controller layer to pass in parameters:
//这个是本人的level字段用了这2个值后报的错(integer类型)
Resolved exception caused by handler execution: javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.Size' validating type 'java.lang.Integer'. Check configuration for 'level'
So just don't use these two fields to save trouble.