Spring boot using JPA practice

Time:2021-7-29
At the beginning, JPA and mybatis gernertor are two ways to automatically generate entity classes. Today, I will introduce the first way to use JPA

Automatically generate entity classes

Add JPA to project

1. Add JPA under the project, click file – > project structure – > modules, and click the plus sign in the red box,
Spring boot using JPA practice
2. Click View – > tool windows – > persistence in the upper left corner of the idea interface

Spring boot using JPA practice

Connect to the database in idea

1. Our daily tools use idea, so today we take idea as an example. Connect datebase in idea,
Spring boot using JPA practice
2. Database address on the link
Spring boot using JPA practice
3. Select to connect to the database you created
Spring boot using JPA practice

Generate entity using idea

1. Click the JPA option just created
Spring boot using JPA practice

2. Select the connected database address, and the corresponding address value will appear. Packge is the corresponding package name, which can be created in the project. Click the corresponding table to create the corresponding table,
Spring boot using JPA practice
3. The following is the status of creating achievements
Spring boot using JPA practice

JPA practical use

1. Create a springbot project.

<!--  Springboot web container -- >
        <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-boot-starter-data-jpa</artifactId>
            <version>2.0.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.12</version>
        </dependency>

2. Create 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 and call it like mapper
Spring boot using JPA practice

4. The following 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));
    }
}

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, I hope you will give me your advice. Thank you