SpringBoot2.3.0配置JPA的实现示例
程序员文章站
2024-01-24 21:55:16
jpa顾名思义就是java persistence api的意思,是jdk 5.0注解或xml描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。依赖 spring-boot-star...
jpa顾名思义就是java persistence api的意思,是jdk 5.0注解或xml描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
依赖
- spring-boot-starter-data-jdbc
- spring-boot-starter-data-jpa
- mysql-connector-java
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-jdbc</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-jpa</artifactid> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <scope>runtime</scope> </dependency>
application.yml
spring.jpa.hibernate.ddl-auto=update
- 如果数据库内没有表或表结构改变时根据entity创建/更新
spring.jpa.show-sql=true
- 控制台打印sql
spring.jpa.database
- 指定数据库类型,可写可不写
spring: datasource: driver-class-name: com.mysql.cj.jdbc.driver url: jdbc:mysql://localhost:3306/spring_boot?servertimezone=gmt%2b8 username: root password: root jpa: hibernate: ddl-auto: update show-sql: true database: mysql
pojo
- @data是lombok
- @entity告诉jpa这是一个实体类(和数据表映射的类)
- @table(name = “jpa_user”) 给表起名,不写默认为类名小写(user)
- @id设置主键
- @generatedvalue(strategy = generationtype.identity) 使用自增
- @column(name = “jpa_username”,length = 40) 给列起名,不写默认为属性名(username)
import lombok.data; import javax.persistence.*; @entity @table(name = "jpa_user") @data public class user { @id @generatedvalue(strategy = generationtype.identity) private integer id; @column(name = "jpa_username",length = 40) private string username; @column(name = "jpa_password") private string password; }
repository
jparepository的继承关系
所以继承jparepository
- jparepository<t, id>
- 第一个泛型就是要操作的entity
- 第二个泛型就是entity的id主键类型
- jparepository<user, integer>
package com.live.repository; import com.live.model.user; import org.springframework.data.jpa.repository.jparepository; public interface userrepository extends jparepository<user,integer> { }
controller(测试)
@autowired @autowired userrepository userrepository;
package com.live.controller; import com.live.model.user; import com.live.repository.userrepository; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.pathvariable; import org.springframework.web.bind.annotation.restcontroller; import java.util.list; import java.util.optional; @restcontroller public class userjpacontroller { @autowired userrepository userrepository; @getmapping("/findall") public list<user> findall() { return userrepository.findall(); } @getmapping("/findone/{id}") public optional<user> findone(@pathvariable(value = "id") integer id) { return userrepository.findbyid(id); } @getmapping("/insert") public user insertone(user user) { return userrepository.save(user); } }
测试
注意:
- 对字段操作时,使用的是实体类的属性名(username,password)
- 而不是在@column(name=“jpa_username”)设置的jpa_username
查询所有
插入
到此这篇关于springboot2.3.0配置jpa的实现示例的文章就介绍到这了,更多相关springboot2.3.0配置jpa内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!