从零开始学Spring Boot(3)——JPA
来源:互联网 发布:linux系统chown指令 编辑:程序博客网 时间:2024/06/15 04:53
在说具体如何在spring boot 使用Hibernate前,先抛装引玉些知识点?什么是JPA呢?
JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
http://baike.baidu.com/link?url=LdqIXvzTr0RDjY2yoRdpogDdzaZ_L-DrIOpLLzK1z38quk6nf2ACoXEf3pWKTElHACS7vTawPTmoFv_QftgT_q
接下里就说本文章重点了,那么怎么操作呢?只需要如下配置就可以了?
pom.xml配置:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
application.properties配置:
########################################################
###datasource
########################################################
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
########################################################
### Java Persistence Api
########################################################
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
那么就可以使用Hibernate带来的好处了,在实体类注解@Entity就会自动进行表的DDL操作了。
我们在com.kfit.test.bean.Demo 中加入注解:@Entity
@Entity//加入这个注解,Demo就会进行持久化了,在这里没有对@Table进行配置,请自行配置。
publicclass Demo {
@Id @GeneratedValue
private long id;//主键.
private String name;//测试名称.
//其它代码省略.
这时候运行就会在数据库看到demo表了。
- 从零开始学Spring Boot(3)——JPA
- 从零开始学spring-boot(2)-集成spring-data-jpa
- 从零开始学spring-boot(2)-集成spring-data-jpa
- (7)JPA - Hibernate【从零开始学Spring Boot】
- (8). 使用JPA保存数据【从零开始学Spring Boot】
- 7. JPA - Hibernate【从零开始学Spring Boot】
- 7. JPA - Hibernate【从零开始学Spring Boot】
- 63.JPA/Hibernate/Spring Data概念【从零开始学Spring Boot】
- (3)Spring Boot热部署【从零开始学Spring Boot】
- (3)Spring Boot热部署【从零开始学Spring Boot】
- 8. 使用JPA保存数据【从零开始学Spring Boot】
- 8. 使用JPA保存数据【从零开始学Spring Boot】
- 64.JPA命名策略【从零开始学Spring Boot】
- 从零开始学Spring Boot
- 从零开始学spring-boot(3)-集成logback日志
- (0)前言【从零开始学Spring Boot】
- 77. Spring Boot Use Thymeleaf 3【从零开始学Spring Boot】
- 从零开始学spring-boot(1)-Hello spring boot!
- 102道java算法
- PAT1060【大模拟啊】
- 产生死锁的必要条件和解决方法
- 机器学习实战---读书笔记: 第5章 基Logistic回归
- tomcat增加用户后仍提示拒绝权限的问题
- 从零开始学Spring Boot(3)——JPA
- 使用typedef 定义数组
- 《Android从菜鸟到高手》——002“面向对象思想”在Android的基本应用
- 深入分析Java ClassLoader原理
- No.513 Find Bottom Left Tree Value
- Hello R!
- Java单例模式(singleton),饿汉式和内部类的区别
- eclipse使用git上传项目到GitHub
- 日志总结-java变量类型