将 MySQL 连接到 Spring 应用程序
Posted
技术标签:
【中文标题】将 MySQL 连接到 Spring 应用程序【英文标题】:Connect MySQL to Spring application 【发布时间】:2014-11-13 19:53:02 【问题描述】:我想使用 mysql 数据库而不是像 hsqldb 这样的运行时数据库。我已经克隆了this repository,它使用 hsqldb 作为其数据库。
因为我想学习如何将关系数据库与基于 rest 的 spring 应用程序一起使用。因此,我对 pom.xml 进行了以下更改: 更改 pom.xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springsource.restbucks</groupId>
<artifactId>restbucks</artifactId>
<packaging>war</packaging>
<version>1.0.0.BUILD-SNAPSHOT</version>
<name>Spring RESTBucks</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.5.RELEASE</version>
</parent>
<properties>
<spring-data-releasetrain.version>Evans-RC1</spring-data-releasetrain.version>
<tomcat.version>8.0.9</tomcat.version>
</properties>
<dependencies>
<!-- Spring Data REST -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- JDK 8 DateTime support for Hibernate -->
<dependency>
<groupId>org.jadira.usertype</groupId>
<artifactId>usertype.extended</artifactId>
<version>3.2.0.GA</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<!-- Database >
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Misc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx-events</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-libs-snapshot</id>
<url>http://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-libs-snapshot</id>
<url>http://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
在 application.properties (spring-restbucks/src/main/resources/application.properties) 中,我做了以下更改:
# JPA
spring.jpa.show-sql=true
server.port=8080
spring.datasource.url=jdbc:mysql://localhost/restBucks
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=
但我面临大约 15 个错误,例如:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderInitializer' defined in file [/home/jimish/projects/spring_projects/spring/spring-restbucks/target/classes/org/springsource/restbucks/order/OrderInitializer.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springsource.restbucks.order.OrderInitializer]: Constructor threw exception; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
因此,如果有人可以建议如何将 mysql 连接到 Spring 应用程序的路径,那就太好了。 谢谢。
【问题讨论】:
确保您的设置正确,您的数据库存在并且已启动并正在运行,并且您可以与该用户连接到它。还要确保你为 Hibernate 选择了正确的方言,否则你可能会得到错误的 SQL。 嘿,我的数据库已经存在并且正在运行...但是我不知道如何为休眠选择方言。你能推荐一些相关的有用的东西吗? 我建议阅读文档,而不是随便乱搞。但是设置spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
应该可以解决问题。您可能还想将完整的堆栈跟踪添加到您的帖子中。
【参考方案1】:
在类路径中的 application.properties 中仅包含以下配置,它将像魅力一样工作:
spring.datasource.url=jdbc:mysql://localhost/jpa_example
spring.datasource.username=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
这个“spring.jpa.database-platform”指定你要使用的数据库。可能是postgress,MySQL等,根据需要指定。
【讨论】:
【参考方案2】:解释@M。 Deinum 的评论...您需要指定 JPA 配置信息,因为 Spring RestBucks App 正在使用 Spring Data JPA。
添加标准 JPA 属性并指定数据库平台 (Common Application Properties) 应该可以让您的 JPA 连接正常工作。
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
# Enable spring data repos
spring.data.jpa.repositories.enabled=true
# Replace with your connection string
spring.datasource.url=jdbc:mysql://localhost:3306/restbucks
# Replace with your credentials
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=com.mysql.jdbc.Driver
【讨论】:
【参考方案3】:在您的 application.properties 文件中添加以下属性:
# Use one of create/update/create-update
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/db?createDatabaseIfNotExist=true
spring.datasource.username=root //your db user name
spring.datasource.password=root //your db user password
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
#If your MySql version 5 then use MySQL5Dialect
# Enable spring data repos
spring.data.jpa.repositories.enabled=true
spring.jpa.show-sql=true
spring.jpa.database=mysql
【讨论】:
【参考方案4】:将 MySQL Connector/J 添加到类路径:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.5</version>
</dependency>
这是基于 Spring 的应用程序中文件 application.properties 的示例:
datasource.mine.poolSize=30
spring.datasource.url=jdbc:mysql://127.0.0.1/vy?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
【讨论】:
【参考方案5】:数据库服务器 IP、端口号和数据库名称
spring.datasource.url=jdbc:mysql://localhost/sample
数据库服务器用户名
spring.datasource.username=root
数据库服务器密码
spring.datasource.password=root123
每次服务器启动时更新数据库
spring.jpa.hibernate.ddl-auto=update
【讨论】:
【参考方案6】:spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/boot
spring.datasource.username=root
spring.datasource.password=
我已尝试使用上述配置,但 1 小时后未连接我通过在密码 =(密码 =)后放置一个空格来连接
【讨论】:
【参考方案7】:请添加mysql-connector-java依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
并在 application.properties 中添加下面提到的属性
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
spring.datasource.url=jdbc:mysql://localhost:3306/yourDatabaseName?useSSL=false
spring.datasource.username=root
spring.datasource.password=YourPassword
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
【讨论】:
【参考方案8】:您可以验证您的 pom 文件并确保最少以下 jar 可用
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!-- Spring AOP -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<!-- MySQL JDBC connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
**Make sure you are reading your db.properties file from application-context.xml**
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
**Make sure you use proper datasource and entity manager in application-context.xml**
<bean id="dataSource" class="our.DataSource" destroy-method="close">
<property name="driverClass" value="$driver"/>
<property name="jdbcUrl" value="$url"/>
<property name="user" value="$username"/>
<property name="password" value="$password"/>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="spring-jpa" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="showSql" value="false" />
<property name="databasePlatform" value="$dialect" />
</bean>
</property>
</bean>
**Make sure you are adding minimum of below properties in db.properties**
dialect=org.hibernate.dialect.MySQL5InnoDBDialect
url=jdbc:mysql://localhost:3306/dbName
driver=com.mysql.jdbc.Driver
username=root
password=test
【讨论】:
他在解析结果时遇到问题,应用程序运行良好,因此依赖关系正常。此外,依赖项来自一个 Spring Boot 启动项目,该项目已经包含正确的依赖项。以上是关于将 MySQL 连接到 Spring 应用程序的主要内容,如果未能解决你的问题,请参考以下文章
将 Spring Boot 应用程序连接到 Google Cloud 中的 MySQL 数据库
Dockerized Spring Boot 应用程序连接到主 MySQL [重复]