Java 将 MySQL 连接到 Spring 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25930191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connect MySQL to Spring application
提问by jimish
I want to use MySQL database rather than using runtime database like hsqldb. I have cloned this repositoryand it is using hsqldb as its database.
我想使用 MySQL 数据库而不是像 hsqldb 那样使用运行时数据库。我已经克隆了这个存储库,它使用 hsqldb 作为它的数据库。
As I want to learn how to use relational database with rest based spring application. So I have made following changes to pom.xml: changed pom.xml:
因为我想学习如何将关系数据库与基于休息的 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.Hymanson.datatype</groupId>
<artifactId>Hymanson-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>
and in application.properties (spring-restbucks/src/main/resources/application.properties), I have made following changes:
在 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=
but I am facing about 15 errors like:
但我面临大约 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
So if anyone can suggest path for how to connect mysql to spring application that would be great. Thanks.
因此,如果有人可以建议如何将 mysql 连接到 spring 应用程序的路径,那就太好了。谢谢。
回答by Suvasis
You can verify your pom file and make sure minimum of below jars are available
您可以验证您的 pom 文件并确保至少有以下 jars 可用
<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
回答by Eddie B
Expounding on @M. Deinum's comment... You need to specify the JPA configuration information as the Spring RestBucks Appis using Spring Data JPA.
解释@M。Deinum 的评论... 您需要指定 JPA 配置信息,因为Spring RestBucks App使用的是Spring Data JPA。
Adding standard JPA properties and specifying the database-platform (Common Application Properties) should get your JPA connection working.
添加标准 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
回答by gauravbhatt13
Include only the following configuration in your application.properties in classpath and it will work like a charm:
在类路径中的 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
回答by Vinoj Vetha
Database Server IP, Port Number and DB Name
数据库服务器 IP、端口号和数据库名称
spring.datasource.url=jdbc:mysql://localhost/sample
spring.datasource.url=jdbc:mysql://localhost/sample
Database Server UserName
数据库服务器用户名
spring.datasource.username=root
spring.datasource.username=root
Database Server Password
数据库服务器密码
spring.datasource.password=root123
spring.datasource.password=root123
Update database on every Time Server starts
在每个时间服务器启动时更新数据库
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.ddl-auto=更新
回答by Do Nhu Vy
Add MySQL Connector/J to classpath:
将 MySQL Connector/J 添加到类路径:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.5</version>
</dependency>
This is an example of file application.propertiesinside Spring-based application:
这是基于 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
回答by Mahfuz Ahmed
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/boot
spring.datasource.username=root
spring.datasource.password=
I was tried with above configuration but was not connected after 1 hour I was get connected by putting an SPACE after password= (password= )
我尝试使用上述配置,但 1 小时后未连接我通过在 password= (password= ) 后放置一个空格来连接