我如何连接到MySQL数据库

huangapple go评论106阅读模式
英文:

How can I connect to MySQL database

问题

I'm trying to connect to MySQL Database with SpringBoot, but I got this error below:

尝试使用SpringBoot连接到MySQL数据库,但出现以下错误:

Application failed to start with classpath: [file:/C:/Dev/Repositorios/jira-quality/target/classes/, file:/C:/Users/jboscod/.m2/repository/mysql/mysql-connector-java/8.0.21/mysql-connector-java-8.0.21.jar, file:/C:/Users/jboscod/.m2/repository/org/springframework/boot/spring-boot-starter-data-jpa/2.1.9.RELEASE/spring-boot-starter-data-jpa-2.1.9.RELEASE.jar

应用程序启动失败,类路径为:[file:/C:/Dev/Repositorios/jira-quality/target/classes/, file:/C:/Users/jboscod/.m2/repository/mysql/mysql-connector-java/8.0.21/mysql-connector-java-8.0.21.jar, file:/C:/Users/jboscod/.m2/repository/org/springframework/boot/spring-boot-starter-data-jpa/2.1.9.RELEASE/spring-boot-starter-data-jpa-2.1.9.RELEASE.jar

Failed to load property source from location 'classpath:/application-desenv.yml'

无法从位置 'classpath:/application-desenv.yml' 加载属性源

My pom is this way:

我的pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>br.com.oss.jira.quality</groupId>
    <artifactId>jira-quality</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jira-quality</name>
    <description>Gerenciador de inconsistências no Jira</description>

    <properties>
        <java.version>1.8</java.version>
        <lombok.version>1.18.6</lombok.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- More dependencies... -->

    </dependencies>

    <!-- More configuration... -->

</project>

And my application.properties is below:

我的application.properties文件如下:

spring.jpa.hibernate.ddl-auto = none

spring.datasource.url = jdbc:mysql://localhost:3306/jiraquality

spring.datasource.username = root

spring.datasource.password = admin

spring.datasource.driver-class-name = com.mysql.jdbc.Driver

I don't know if the problem is in my pom or my application file.

我不知道问题是在我的pom文件还是我的应用程序文件中。

英文:

I´m tryng to connect to MySQL Database with SpringBoot, but I got this error below:

Application failed to start with classpath: [file:/C:/Dev/Repositorios/jira-quality/target/classes/, file:/C:/Users/jboscod/.m2/repository/mysql/mysql-connector-java/8.0.21/mysql-connector-java-8.0.21.jar, file:/C:/Users/jboscod/.m2/repository/org/springframework/boot/spring-boot-starter-data-jpa/2.1.9.RELEASE/spring-boot-starter-data-jpa-2.1.9.RELEASE.jar

Failed to load property source from location 'classpath:/application-desenv.yml'

My pom is this way:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
	&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
	&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
	&lt;version&gt;2.1.9.RELEASE&lt;/version&gt;
	&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;groupId&gt;br.com.oss.jira.quality&lt;/groupId&gt;
&lt;artifactId&gt;jira-quality&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;jira-quality&lt;/name&gt;
&lt;description&gt;Gerenciador de inconsist&#234;ncias no Jira&lt;/description&gt;

&lt;properties&gt;
	&lt;java.version&gt;1.8&lt;/java.version&gt;
	&lt;lombok.version&gt;1.18.6&lt;/lombok.version&gt;
&lt;/properties&gt;

&lt;dependencies&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;mysql&lt;/groupId&gt;
		&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
		&lt;scope&gt;runtime&lt;/scope&gt;
	&lt;/dependency&gt;

	&lt;!-- &lt;dependency&gt;
		&lt;groupId&gt;com.oracle.database.jdbc&lt;/groupId&gt;
		&lt;artifactId&gt;ojdbc8&lt;/artifactId&gt;
		&lt;version&gt;12.2.0.1&lt;/version&gt;
	&lt;/dependency&gt; --&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
	&lt;/dependency&gt;

	&lt;!-- &lt;dependency&gt;
		&lt;groupId&gt;org.hsqldb&lt;/groupId&gt;
		&lt;artifactId&gt;hsqldb&lt;/artifactId&gt;
		&lt;scope&gt;runtime&lt;/scope&gt;
	&lt;/dependency&gt;  --&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-mail&lt;/artifactId&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-quartz&lt;/artifactId&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
		       &lt;!-- Exclude the default Jackson dependency --&gt;
&lt;!-- 			&lt;exclusions&gt; --&gt;
&lt;!-- 				&lt;exclusion&gt; --&gt;
&lt;!-- 					&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; --&gt;
&lt;!-- 					&lt;artifactId&gt;spring-boot-starter-json&lt;/artifactId&gt; --&gt;
&lt;!-- 				&lt;/exclusion&gt; --&gt;
&lt;!-- 			&lt;/exclusions&gt; --&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
		&lt;artifactId&gt;lombok&lt;/artifactId&gt;
		&lt;version&gt;${lombok.version}&lt;/version&gt;
		&lt;scope&gt;provided&lt;/scope&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
		&lt;scope&gt;test&lt;/scope&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;
		&lt;artifactId&gt;gson&lt;/artifactId&gt;
		&lt;version&gt;${gson.version}&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;io.springfox&lt;/groupId&gt;
		&lt;artifactId&gt;springfox-swagger2&lt;/artifactId&gt;
		&lt;version&gt;2.7.0&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;io.springfox&lt;/groupId&gt;
		&lt;artifactId&gt;springfox-swagger-ui&lt;/artifactId&gt;
		&lt;version&gt;2.7.0&lt;/version&gt;
		&lt;scope&gt;compile&lt;/scope&gt;
	&lt;/dependency&gt;
&lt;/dependencies&gt;

&lt;profiles&gt;
	&lt;profile&gt;
		&lt;id&gt;desenv&lt;/id&gt;
		&lt;properties&gt;
			&lt;activatedProperties&gt;desenv&lt;/activatedProperties&gt;
		&lt;/properties&gt;
		&lt;activation&gt;
			&lt;activeByDefault&gt;true&lt;/activeByDefault&gt;
		&lt;/activation&gt;
	&lt;/profile&gt;
	&lt;profile&gt;
		&lt;id&gt;prod&lt;/id&gt;
		&lt;properties&gt;
			&lt;activatedProperties&gt;prod&lt;/activatedProperties&gt;
		&lt;/properties&gt;
	&lt;/profile&gt;
&lt;/profiles&gt;

&lt;build&gt;
	&lt;plugins&gt;
		&lt;plugin&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
		&lt;/plugin&gt;
	&lt;/plugins&gt;
    &lt;resources&gt;
    	&lt;resource&gt;
        	&lt;directory&gt;src/main/resources&lt;/directory&gt;
        	&lt;filtering&gt;true&lt;/filtering&gt;
    	&lt;/resource&gt;
	&lt;/resources&gt;
&lt;/build&gt;

&lt;/project&gt;

And my application.properties is below:

spring.jpa.hibernate.ddl-auto = none

spring.datasource.url = jdbc:mysql://localhost:3306/jiraquality

spring.datasource.username = root

spring.datasource.password = admin

spring.datasource.driver-class-name = com.mysql.jdbc.Driver

I don´t know if the problem is in my pom or my application file.

答案1

得分: 1

我注意到 Failed to load property source from location &#39;classpath:/application-desenv.yml&#39;

你尝试过将你的 application.properties 更改为 application-desenv.yml 吗?

英文:

I noticed that Failed to load property source from location &#39;classpath:/application-desenv.yml&#39;

Have you try to change your application.properties to application-desenv.yml?

答案2

得分: 0

如果您的pom文件中没有添加Spring Data JPA,请添加以下内容:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

然后,清理您的项目并执行Maven更新。这样它将成功工作。

英文:

If you don't add Spring Data JPA on you pom file add this:

    &lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
	&lt;/dependency&gt;


  
	&lt;dependency&gt;
       &lt;groupId&gt;mysql&lt;/groupId&gt;
       &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
       &lt;scope&gt;runtime&lt;/scope&gt;
	&lt;/dependency&gt;

Clean your project and take Maven update.
It will be working successfully.

huangapple
  • 本文由 发表于 2020年7月31日 11:01:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63185166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定