spring boot,hibernate jpa,Mysql集成【原创】

一、引入maven包

spring boot的maven包我就不在这里介绍了,只贴出hibernate的maven包:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.data</groupId>
	<artifactId>spring-data-jpa</artifactId>
	<optional>true</optional>
	<exclusions>
		<exclusion>
			<artifactId>jcl-over-slf4j</artifactId>
			<groupId>org.slf4j</groupId>
		</exclusion>
	</exclusions>
</dependency>
<dependency>
	<groupId>org.hibernate</groupId>
	<artifactId>hibernate-entitymanager</artifactId>
	<optional>true</optional>
</dependency>
<dependency>
	<groupId>org.hibernate</groupId>
	<artifactId>hibernate-validator</artifactId>
	<optional>true</optional>
	<version>6.0.12.Final</version>
</dependency>
<dependency>
	<groupId>org.hibernate.validator</groupId>
	<artifactId>hibernate-validator-annotation-processor</artifactId>
</dependency>
<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-dbcp2</artifactId>
</dependency>
spring boot,hibernate jpa,Mysql集成【原创】

二、hibernate和mysql配置项

我的项目配置文件是使用application.yml,所以直接在该文件中添加如下配置:

spring.datasource:
  continue-on-error: false
  separator: ;
  driver-class-name: com.mysql.cj.jdbc.Driver
  generate-unique-name: true
  url: jdbc:mysql://127.0.0.1:3306/poobase?useUnicode=true&characterEncoding=utf8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
  username: root
  password: 数据库密码
  tomcat.test-on-borrow: true
  tomcat.max-wait: 20000
  tomcat.max-active: 50
  tomcat.max-idle: 20
  tomcat.min-idle: 15
spring.jpa:
  database: MYSQL
  database-platform: org.hibernate.dialect.MySQLDialect
  properties.hibernate.dialect: org.hibernate.dialect.MySQL5Dialect
  hibernate.naming.physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
  hibernate.naming.implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl
  generate-ddl: false
  hibernate.ddl-auto: none
  properties.hibernate.current_session_context_class: org.springframework.orm.hibernate5.SpringSessionContext
  hibernate.use-new-id-generator-mappings: false
  open-in-view: true
  show-sql: true
spring boot,hibernate jpa,Mysql集成【原创】

如果你使用的是自定义配置的话,根据自己的自动配置类进行调整。

说明:

spring.jpa.hibernate.naming.physical-strategy:

hibernate升级到5.x以后,实体类和数据表的表名、列明需要指定转换策略,该项配置的作用是当实体类没有明确标明数据表名和列名时候的转换策略,我这里使用的是org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl,表示表名和列名不做任何转换。

ImplicitNamingStrategyJpaCompliantImpl:

默认的命名策略,兼容JPA 2.0的规范,不做任何转换。

三、启用hibernate jpa

在spring boot入口类中添加如下注解配置表示启用jpa配置项:

@ComponentScan(basePackages= {"com.mco"})
@EnableJpaRepositories(basePackages = {"com.mco"})
@EntityScan(basePackages = {"com.mco"})
spring boot,hibernate jpa,Mysql集成【原创】

四、DAO类连接数据源配置

注意:文章到这里还没有完,由于篇幅限制,完整内容请到hongfu951博客上查看

完整内容URL地址:spring boot基于Mysql集成hibernate jpa【原创】

相关推荐