At the beginning of the use of automatic generation of entity class tools JPA and Mybatis - Gernertor these two ways to automatically generate entity class, today first introduced the first way to use JPACopy the code

Automatically generate entity classes

Add JPA to the project

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



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

Connect to the database in IDEA

1. We use IDEA in our daily tools, so we take IDEA as an example. Connect to dateBase in IDEA,



2. Link to the database address



3. Select the database that you have created

Use IDEA to generate Entity

1. Click the newly created JPA option

2. Select the address of the connected database, and the corresponding address value will be displayed. Packge is the name of the corresponding package, which can be created in the project.



3. The following is the creation result state

JPA practice use

1. Create a SpringBot project.

<! <dependency> <groupId>org.springframework.boot</groupId> <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 >Copy the code

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);
    }
}

Copy the code

3. Create a Repository and call it like Mapper

4. Here 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 = "cannot be empty ") @requestParam Long redUserId){return ResponseEntity.ok(company0EntityService.findNameByRefUserId(redUserId)); }}Copy the code

The above is for personal use of JPA. Next time, I will introduce some use of Mybatis – Gerator. If there is anything wrong in the article, please kindly comment, thank you