0
点赞
收藏
分享

微信扫一扫

springboot整合jpa,步骤详细(图文结合讲解)

小沙坨 2022-04-16 阅读 27
java-ee

1,首先新建springboot工程,取名为springbootjpa,设置好java版本后点击next

2,选中三个依赖,lombok,spring Web和Spring Boot DevTools。然后点击finish。

Lombok:实体类中通过注解可省略getter,setter,有参构造,无参构造等。

Sprong Boot DevTools:应用提供一些开发时特性,包括默认值设置,自动重启,livereload 等。

Spring Web:springboot起步依赖,默认的服务器是tomcat。

下方Spring Boot 版本号一般是默认,不用管

 

 3,在pom.xml文件中添加SpringData jpa和数据库MySql的依赖。

 <!-- Spring Data JPA 依赖(重要) -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- MySQL 驱动(重要) -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.45</version>
        </dependency>

刷新依赖

 4,在application.properties文件中添加数据库连接配置:在这里我的数据库名是springbootjpa,用户名和密码大家改为自己的。

#自动生成数据库表(关键)
spring.jpa.hibernate.ddl-auto=update
#mysql数据库连接配置(非常重要)
spring.datasource.url = jdbc:mysql://localhost:3306/springbootjpa?serverTimezone=Asia/Shanghai
#数据库用户名
spring.datasource.username = root
#数据库密码
spring.datasource.password = x5
#mysql数据库驱动程序(重要)
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
#jpa配置:在控制台显示Hibernate的sql(可选)
spring.jpa.show-sql = true
#其他配置:关闭Thymeleaf 的缓存
spring.thymeleaf.cache = false

  5,在springbootjpa文件夹下创建entity文件夹,entity文件夹中创建实体类User,用于映射数据库表。

package com.example.springbootjpa.entity;

import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@Entity
@Data
public class User implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false,unique = true,length = 20)
    private String username;

    @Column(nullable = false,length = 20)
    private String password;

    @Column(nullable = false)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date regdate;
}

 

同时在MySql中新建数据库,命名为springbootjpa,不用建表,运行项目后会自动生成表。

启动项目:运行启动类SpringbootjpaApplication

 这时在我们创建的数据库springbootjpa中会自动生成表user。表的字段是我们在实体类User中的属性。

 

6,在springbootjpa文件夹下新建文件夹,命名为repository,在repository文件夹下新建接口类:命名为UserRepository,该接口要 extends JpaRepository<User,Long>接口,其中User表示实体模型,Long代表主键类型。

类上面添加注解@Repository,表示包含增删改查等功能的接口

 

到这里,关于jpa的依赖导入和配置基本完成。这里自带很多内置的增删改查方法,不用我们自己写sql语句,直接调用即可。

 7,在springbootjpa文件夹下新建文件夹controller,在controller文件夹中新建类UserController。用来进行jpa的测试。

package com.example.springbootjpa.controller;

import com.example.springbootjpa.entity.User;
import com.example.springbootjpa.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController

public class UserController {
    @Autowired
    private UserRepository userRepository;

    //查找所有user对象

    @GetMapping("findall")
    public List<User> findall(){
        List<User> userList=userRepository.findAll();
        return userList;
    }

    //添加User对象
    @RequestMapping("add")
    public void add(User user){
        userRepository.save(user);
    }

    //删除User对象
    @RequestMapping("deleteById")
    public void delete(@Param("id") Long id){
        userRepository.deleteById(id);
    }
}


在这里我写了三个方法,分别是查找,添加和删除。 在类上添加注解@RestController,表示输出格式为json格式。

我们在数据库的表中手动添加几条测试数据

8,再次运行项目,打开浏览器测试,输入http://localhost:8080/findall

即可得到数据库中添加的两条数据,说明jpa整合成功。

 

接下来在浏览器地址栏输入

http://localhost:8080/add?id=66&username=th6ree&password=thre6e&regdate=2022-04-16%2013:33:15

我们发现数据库多了一条数据,插入方法成功

在浏览器输入http://localhost:8080/deleteById?id=3

此时发现数据库里少了一条id为2的数据,说明删除方法测试成功。

 

以上就是springboot整合jpa的添加,显示,删除功能

 

举报

相关推荐

0 条评论