架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 17603|回复: 7

[资料] 【实战】spring boot hibernate druid mysql项目搭建教程[附源码]

[复制链接]
发表于 2019-2-20 14:44:57 | 显示全部楼层 |阅读模式
我搞Java也是半吊子水平,对spring全家桶也不是非常熟悉,根据网上的教程和自己的一些经验搭建的项目框架。
项目环境:jdk版本:1.8.0_171,mysql版本:5.7.17,spring boot:2.1.1.RELEASE,开发工具:idea。

Hibernate与JPA

本篇文章中的web项目选用Hibernate作为持久层框架。在Spring Boot中,我们需要了解另一个概念:JPA

上一句话可能有些歧义,并不是说JPA就是Spring Boot中的概念。而是Java Persistence Api,中文译名为:Java持久层Api。

JPA是一个基于ORM(或叫O/R mapping ,对象关系映射)的标准规范,在这个规范中,JPA只定义标准规则,不提供实现。

目前,JPA的主要实现有Hibernate,EclipseLink,OpenJPA等。

由于Hibernate在数据访问解决技术领域的霸主地位,所以JPA标准基本由Hibernate主导。

SpringBoot这支自动步枪

spring框架中提供Spring Data JPA作为开发者应用Hibernate框架的接口工具。我们的Spring Boot则提供了一款全自动的“自动依赖模块”:spring-boot-starter-data-jpa


Pom依赖结构:

有人会有pom文件并没有关于hibernate的引用的疑问?

QQ截图20190220125527.jpg

原因是因为如上图,spring-boot-starter-data-jpa依赖hibernate-core,在引用spring-boot-starter-data-jpa的时候,maven项目将自动引入所依赖的包。


application.yml 配置文件:

spring 配置数据源信息官方链接:https://docs.spring.io/spring-bo ... lication-properties

##########################################
###datasource--指定mysql数据库连接信息
###spring.datasource.url:mysql地址
###spring.datasource.user:数据库用户名
###spring.datasource.password:数据库密码
###spring.datasource.driverClassName:对应数据库驱动
###spring.datasource.max-active:指定连接池中最大的活跃连接数
###spring.datasource.max-idle:指定连接池最大的空闲连接数量.
###spring.datasource.min-idle:指定必须保持连接的最小值(For DBCP and Tomcat connection pools)
###spring.datasource.initial-size:指定启动连接池时,初始建立的连接数量
###
###其他信息总结:
###spring.dao.exceptiontranslation.enabled是否开启PersistenceExceptionTranslationPostProcessor,默认为true
###spring.datasource.abandon-when-percentage-full设定超时被废弃的连接占到多少比例时要被关闭或上报
###spring.datasource.allow-pool-suspension使用Hikari pool时,是否允许连接池暂停,默认为: false
###spring.datasource.alternate-username-allowed是否允许替代的用户名.
###spring.datasource.auto-commit指定updates是否自动提交.
###spring.datasource.catalog指定默认的catalog.
###spring.datasource.commit-on-return设置当连接被归还时,是否要提交所有还未完成的事务
###spring.datasource.connection-init-sql指定连接被创建,再被添加到连接池之前执行的sql.
###spring.datasource.connection-init-sqls使用DBCP connection pool时,指定初始化时要执行的sql
###spring.datasource.connection-properties.[key]在使用DBCP connection pool时指定要配置的属性
###spring.datasource.connection-test-query指定校验连接合法性执行的sql语句
###spring.datasource.connection-timeout指定连接的超时时间,毫秒单位.
###spring.datasource.continue-on-error在初始化数据库时,遇到错误是否继续,默认false
###spring.datasource.data指定Data (DML)脚本
###spring.datasource.data-source-class-name指定数据源的全限定名.
###spring.datasource.data-source-jndi指定jndi的地址
###spring.datasource.data-source-properties.[key]使用Hikari connection pool时,指定要设置的属性
###spring.datasource.db-properties使用Tomcat connection pool,指定要设置的属性
###spring.datasource.default-auto-commit是否自动提交.
###spring.datasource.default-catalog指定连接默认的catalog.
###spring.datasource.default-read-only是否设置默认连接只读.
###spring.datasource.default-transaction-isolation指定连接的事务的默认隔离级别.
###spring.datasource.driver-class-name指定driver的类名,默认从jdbc url中自动探测.
###spring.datasource.fair-queue是否采用FIFO返回连接.
###spring.datasource.health-check-properties.[key]使用Hikari connection pool时,在心跳检查时传递的属性
###spring.datasource.idle-timeout指定连接多久没被使用时,被设置为空闲,默认为10ms
###spring.datasource.ignore-exception-on-pre-load当初始化连接池时,是否忽略异常.
###spring.datasource.init-sql当连接创建时,执行的sql
###spring.datasource.initial-size指定启动连接池时,初始建立的连接数量
###spring.datasource.initialization-fail-fast当创建连接池时,没法创建指定最小连接数量是否抛异常
###spring.datasource.initialize指定初始化数据源,是否用data.sql来初始化,默认: true
###spring.datasource.isolate-internal-queries指定内部查询是否要被隔离,默认为false
###spring.datasource.jdbc-interceptors使用Tomcat connection pool时,指定jdbc拦截器,分号分隔
###spring.datasource.jdbc-url指定JDBC URL.
###spring.datasource.jmx-enabled是否开启JMX,默认为: false
###spring.datasource.jndi-name指定jndi的名称.
###spring.datasource.leak-detection-threshold使用Hikari connection pool时,多少毫秒检测一次连接泄露.
###spring.datasource.log-abandoned使用DBCP connection pool,是否追踪废弃statement或连接,默认为: false
###spring.datasource.log-validation-errors当使用Tomcat connection pool是否打印校验错误.
###spring.datasource.login-timeout指定连接数据库的超时时间.
###spring.datasource.max-age指定连接池中连接的最大年龄
###spring.datasource.max-lifetime指定连接池中连接的最大生存时间,毫秒单位.
###spring.datasource.max-open-prepared-statements指定最大的打开的prepared statements数量.
###spring.datasource.max-wait指定连接池等待连接返回的最大等待时间,毫秒单位.
###spring.datasource.maximum-pool-size指定连接池最大的连接数,包括使用中的和空闲的连接.
###spring.datasource.min-evictable-idle-time-millis指定一个空闲连接最少空闲多久后可被清除.
###spring.datasource.min-idle指定必须保持连接的最小值(For DBCP and Tomcat connection pools)
###spring.datasource.minimum-idle指定连接维护的最小空闲连接数,当使用HikariCP时指定.
###spring.datasource.name指定数据源名.
###spring.datasource.num-tests-per-eviction-run指定运行每个idle object evictor线程时的对象数量
###spring.datasource.password指定数据库密码.
###spring.datasource.platform指定schema要使用的Platform(schema-${platform}.sql),默认为: all
###spring.datasource.pool-name指定连接池名字.
###spring.datasource.pool-prepared-statements指定是否池化statements.
###spring.datasource.propagate-interrupt-state在等待连接时,如果线程被中断,是否传播中断状态.
###spring.datasource.read-only当使用Hikari connection pool时,是否标记数据源只读
###spring.datasource.register-mbeans指定Hikari connection pool是否注册JMX MBeans.
###spring.datasource.remove-abandoned指定当连接超过废弃超时时间时,是否立刻删除该连接.
###spring.datasource.remove-abandoned-timeout指定连接应该被废弃的时间.
###spring.datasource.rollback-on-return在归还连接时,是否回滚等待中的事务.
###spring.datasource.schema指定Schema (DDL)脚本.
###spring.datasource.separator指定初始化脚本的语句分隔符,默认: ;
###spring.datasource.sql-script-encoding指定SQL scripts编码.
###spring.datasource.suspect-timeout指定打印废弃连接前的超时时间.
###spring.datasource.test-on-borrow当从连接池借用连接时,是否测试该连接.
###spring.datasource.test-on-connect创建时,是否测试连接
###spring.datasource.test-on-return在连接归还到连接池时是否测试该连接.
###spring.datasource.test-while-idle当连接空闲时,是否执行连接测试.
###spring.datasource.time-between-eviction-runs-millis指定空闲连接检查、废弃连接清理、空闲连接池大小调整之间的操作时间间隔
###spring.datasource.transaction-isolation指定事务隔离级别,使用Hikari connection pool时指定
###spring.datasource.url指定JDBC URL.
###spring.datasource.use-disposable-connection-facade是否对连接进行包装,防止连接关闭之后被使用.
###spring.datasource.use-equals比较方法名时是否使用String.equals()替换==.
###spring.datasource.use-lock是否对连接操作加锁
###spring.datasource.username指定数据库名.
###spring.datasource.validation-interval指定多少ms执行一次连接校验.
###spring.datasource.validation-query指定获取连接时连接校验的sql查询语句.
###spring.datasource.validation-query-timeout指定连接校验查询的超时时间.
###spring.datasource.validation-timeout设定连接校验的超时时间,当使用Hikari connection pool时指定
###spring.datasource.validator-class-name用来测试查询的validator全限定名.
###spring.datasource.xa.data-source-class-name指定数据源的全限定名.
###spring.datasource.xa.properties指定传递给XA data source的属性
###spring.datasource.url=jdbc:mysql://localhost:3306/test?user=root&password=root&useUnicode=true&characterEncoding=UTF8
##########################################
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
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
##########################################
##SPring JPA配置信息
###spring.jpa.database指定目标数据库.
###spring.jps.show-sq:是否显示sql语句
###spring.jpa.hibernate.ddl-auto指定DDL mode (none, validate, update, create, create-drop). 当使用内嵌数据库时,默认是create-drop,否则为none.
###spring.jpa.hibernate.naming-strategy指定命名策略.
###其他:
###spring.jpa.database-platform指定目标数据库的类型.
###spring.jpa.generate-ddl是否在启动时初始化schema,默认为false
###spring.jpa.hibernate.ddl-auto指定DDL mode (none, validate, update, create, create-drop). 当使用内嵌数据库时,默认是create-drop,否则为none.
###spring.jpa.hibernate.naming-strategy指定命名策略.
###spring.jpa.open-in-view是否注册OpenEntityManagerInViewInterceptor,绑定JPA EntityManager到请求线程中,默认为: true
###spring.jpa.properties添加额外的属性到JPA provider.
###spring.jpa.show-sql是否开启sql的log,默认为: false
##########################################
spring.jpa.database = MYSQL
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.freemarker.charset=UTF-8

整个包结构

QQ截图20190220142130.jpg

数据源配置:




实体类:



数据库操作接口:

数据库操作接口继承自JpaRepository泛型类,在继承之时,即获得了其赐予开发者的神之力量:

QQ截图20190220142931.jpg

我们可以直接调用这些方法,进行数据库操作,同时我们也可以通过自定义HQL来完成符合个性的其他操作,如:

这段代码中,通过@Query注解,完成了HQL的书写,其中“:name”与下方的@Param注解中参数保持一致。

注意:在HQL中的表名应该是ORM映射的类名,而且HQL与传统的SQL语句有一些出入,比如“*”的意义不尽相同等等。

如果不习惯使用HQL,也可以使用SQL语句:



上述方法,在执行时亲测依然奏效,nativeQuery为true代表使用SQL语言

控制器类:

启动项目

Hibernate会自动将实体类映射到数据库中,为我们建立相关的数据库表,它会根据@Table、@Column、@Id、@GeneratedValue(strategy = GenerationType.AUTO)等注解实现数据库表的自动匹配。省去了大量建表的工作。


(启动项目之前,需要先把数据库的建好,表会自动创建)

QQ截图20190220143332.jpg

hibernate_sequence表是@GeneratedValue注解完成的id生成策略,GenerationType.AUTO代表自动生成,前提是id属性必须是int或者long类型的属性,如果是String类型的id,那么添加此注解于id之上,会立即报错。因此,如果是针对于String类型的id,则不需要为id添加@GeneratedValue(strategy = GenerationType.AUTO)注解。

请求测试

分别通过get和post来完成数据的请求和数据的保存。


调用 http://127.0.0.1:8079/api/test/create 接口post请求添加数据,如下图:

QQ截图20190220143615.jpg

数据库查询如下:

QQ截图20190220143615.jpg

通过get请求如下接口获取数据:

http://127.0.0.1:8079/api/test/byName?name=itsvse.com
http://127.0.0.1:8079/api/test/byId?id=1
http://127.0.0.1:8079/api/test/getAll

QQ截图20190220143828.jpg

注意:如上接口请求,Java对大小写非常敏感,无论是url地址还是参数,都要区分大小写!一个字母大写或者小写了,都会导致请求不到接口或则参数为空!

Druid 监控情况如下:


QQ截图20190220141057.jpg

QQ截图20190220141106.jpg

最后,附上源代码:

游客,如果您要查看本帖隐藏内容请回复


(完)





上一篇:springboot创建项目后运行报错:Failed to configure a DataSource
下一篇:解优下午茶 | 2019年2月20日 星期三
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2019-2-21 02:19:13 | 显示全部楼层
看看学习学习。
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2019-7-5 17:27:02 | 显示全部楼层
学习学习。。。
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2019-7-22 14:15:41 | 显示全部楼层
看看学习学习。
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2019-11-21 21:08:12 | 显示全部楼层
【实战】spring boot hibernate druid mysql项目搭建教
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2020-3-30 13:52:54 | 显示全部楼层
非常感谢
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2020-6-28 15:13:11 | 显示全部楼层
看看大佬的代码学习下
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2020-11-18 23:57:19 | 显示全部楼层
学习学习
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2024-6-5 11:40

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表