In the beginning, there are two ways to automatically generate entity class using JPA and MyBatis - Gernertor. Today, I will introduce the first way to use JPA

Automatically generate entity classes

Add JPA to the project

1. Add JPA to your Project, click file-> Project Sructure->Modules and click the plus sign in the red box.



2. Click View->Tool Windows->Persistence in the upper left corner of the Idea interface

Connect to the database in IDEA

1. We use IDEA as our daily tool, so we take IDEA as an example today. Connect to datebase in IDEA,



2. Link to database address



3. Select to connect to the database you created

Generate an Entity with IDEA

1. Click the newly created JPA option

Select the address of the database that has been connected, and the corresponding address value will appear. Packge is the corresponding package name, which can be created by yourself in the project. Click the corresponding table to create the corresponding table.



3. The following is to create the achievement state

JPA practice usage

1. Create a Springbot project.

<! -- SpringBoot Web container --> <dependency> < grouppid >org.springframework.boot</ grouppid > <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> < artifactId > spring - the boot - starter - data - jpa < / artifactId > < version > 2.0.4. RELEASE < / version > < / dependency > < the dependency > < the groupId > mysql < / groupId > < artifactId > mysql connector - Java < / artifactId > < version > 8.0.12 < / version > < / dependency >

2. Create a startup class

/**
 * @program: gitee-space
 * @Author felixwang
 * @Date on 2021/5/19  18:22
 * @Https https:felixwang.site
 * @QQ 2115376870
 * @Description
 */
@SpringBootApplication
public class AppAplication {
    public static void main(String[] args) {
        SpringApplication.run(AppAplication.class,args);
    }
}

3. Create a Repository that can be called like a Mapper

4. Next is the test controller

/** * @program: gitee-space * @Author felixwang * @Date on 2021/5/20 17:08 * @Https https:felixwang.site * @QQ 2115376870 * @Description  */ @RestController public class TestOne { @Autowired private Company0EntityService company0EntityService; @GetMapping("/test1") public String testJpa(){ return company0EntityService.getAll().toString(); } @getMapping ("test2") public responseEntity test1(@NotBlank(message = "not null ") @RequestParam Long redUserId){return ResponseEntity.ok(company0EntityService.findNameByRefUserId(redUserId)); }}

The above is for personal use of JPA. Next time, I will introduce some uses of MyBatis Gerator. If there is anything wrong with the article, please kindly give me your advice