分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

7. JPA - Hibernate

发布时间:2023-09-06 02:20责任编辑:赖小花关键词:Hibernate

在说具体如何在springboot 使用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/testspring.datasource.username = rootspring.datasource.password = rootspring.datasource.driverClassName = com.mysql.jdbc.Driverspring.datasource.max-active=20spring.datasource.max-idle=8spring.datasource.min-idle=8spring.datasource.initial-size=10 ########################################################### Java Persistence Api######################################################### Specify the DBMSspring.jpa.database = MYSQL# Show or not log for each sqlqueryspring.jpa.show-sql = true# Hibernate ddl auto(create, create-drop, update)spring.jpa.hibernate.ddl-auto = update# Naming strategy#[org.hibernate.cfg.ImprovedNamingStrategy #org.hibernate.cfg.DefaultNamingStrategy]spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy# stripped before adding them tothe 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 ??????privatelongid;//主键. ??????privateString name;//测试名称.//其它代码省略.

这时候运行就会在数据库看到demo表了。

7. JPA - Hibernate

原文地址:https://www.cnblogs.com/blackCatFish/p/9899974.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved