关于java:springboot使用JPA实践使用

一开始应用主动生成实体类工具有JPA和mybatis-gernertor这两种形式来主动生成实体类的形式,明天先介绍第一种应用jpa 

主动生成实体类

我的项目中增加JPA

1.在所在我的项目下增加JPA,点击File->Project Sructure->Modules 点击红色框框中的加号,

2.点击idea界面左上角View->Tool Windows->Persistence

在IDEA中连贯上数据库

1,日常咱们工具都是应用的IDEA的形式,所以明天以IDEA为例阐明。在IDEA中连贯datebase,

2.链接上数据库地址

3.抉择连贯上本人创立的数据库

利用IDEA生成Entity

1,点击刚刚新建的JPA选项

2.抉择曾经连贯上的数据库地址,则会呈现对应的地址值,Packge是对应包名,能够本人在我的项目中创立,点击对应表能够创立对应的表,

3.上面为创立成绩状态

JPA实际应用

1.创立一个springbot我的项目。

        <!-- SpringBoot Web容器 -->
        <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.创立启动类

/**
 * @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.创立一个Repository,即可像Mapper一样调用

4.上面是测试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 = "不能为空") @RequestParam Long redUserId){
        return  ResponseEntity.ok(company0EntityService.findNameByRefUserId(redUserId));
    }
}

以上为集体应用jpa,下次讲介绍mybatis-gerator的一些应用,文章如有写的不妥之处,望各位不吝赐教,谢谢

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理