Java 无法将MySQL数据库连接到我的spring项目(尽管我读了所有文章)
我正在用GoogleAPI和SpringBoot构建一个小应用程序,我需要将mysql数据库连接到我的项目 第一:我所有的包都在同一个地方(为了避免这种问题) 因此,我首先通过克隆项目来尝试docexample() 以及它的返回值:未能获得JDBC连接 我尝试使用自己的应用程序,得到以下结果: 创建在+中定义的名为“entityManagerFactory”的bean时出错,也无法获取JDBC连接Java 无法将MySQL数据库连接到我的spring项目(尽管我读了所有文章),java,mysql,spring,Java,Mysql,Spring,我正在用GoogleAPI和SpringBoot构建一个小应用程序,我需要将mysql数据库连接到我的项目 第一:我所有的包都在同一个地方(为了避免这种问题) 因此,我首先通过克隆项目来尝试docexample() 以及它的返回值:未能获得JDBC连接 我尝试使用自己的应用程序,得到以下结果: 创建在+中定义的名为“entityManagerFactory”的bean时出错,也无法获取JDBC连接 application.properties spring.jpa.hibernate.ddl-a
application.properties
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://localhost:8181/testuser
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.properties.hibernate.dialect =
org.hibernate.dialect.MySQL5Dialect
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
dependencies :
<?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 http://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.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.devoteam.presales</groupId>
<artifactId>testspringsecu</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>testspringsecu</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.3.Final</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client</artifactId>
<version>1.27.0</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>js-cookie</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.23.0</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.23.0</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-sheets</artifactId>
<version>v4-rev493-1.23.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
存储库:
package com.devoteam.presales.testspringsecu;
import com.devoteam.presales.testspringsecu.UsersDevo;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UsersRepo extends CrudRepository<UsersDevo, Long> {
}
我还尝试了一些注释
编辑:我用文档示例解决了这个问题:spring.datasource.url=jdbc:mysql://localhost:8181/testuser?serverTimezone=EST5EDT
但我仍然有:rg.springframework.beans.factory.BeanCreationException:创建名为“entityManagerFactory”的bean时出错
通过我的应用程序,我做到了(在Loooog调查之后)
答案(对于像我这样的noob)是,您的实体必须与您的表完全相同。
我的错误是忘记了@Id
@私有整数ID上的GeneratedValue(strategy=GenerationType.AUTO)。
春天有时会很痛苦。。。(经常……)
别忘了?serverTimezone=EST5EDT在数据库url之后
我希望能为某人节省一些时间。你能发布
SpringBootServletInitializer
类吗?“testuser”听起来像个用户名。它也是您尝试连接到的数据库的名称吗?因为这是JDBC URL中所期望的。您的mysql服务器是否确实在本地主机上运行,并提供端口8181?用户root的数据库密码是否真的是“root”,和/或您是否在实际配置中指定了有效的数据库用户名和密码?您使用的是哪一版本的Connector/J?您正在指定版本8提供的驱动程序类的名称,但该名称与早期版本中适用的名称不同。连接器/J是否在类路径中?您的类路径中是否有不止一个版本?
package com.devoteam.presales.testspringsecu;
import com.devoteam.presales.testspringsecu.UsersDevo;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UsersRepo extends CrudRepository<UsersDevo, Long> {
}
package com.devoteam.presales.testspringsecu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(path = "/demo")
public class TestController {
@Autowired
UsersRepo usersRepo;
@GetMapping(path = "/all")
public @ResponseBody Iterable<UsersDevo> getAllUsers() {
return usersRepo.findAll();
}
}
package com.devoteam.presales.testspringsecu;
import java.security.Principal;
import java.util.*;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
@SpringBootApplication
@RestController
public class TestspringsecuApplication {
@RequestMapping("/user")
public Principal user(Model model, Principal principal) throws
JSONException {
OAuth2Authentication authentication = (OAuth2Authentication)
principal;
return principal;
}
@GetMapping("/user")
public ModelAndView method() {
System.out.println("icila");
return new ModelAndView("redirect:" + "/");
}
public static void main(String[] args) {
SpringApplication.run(TestspringsecuApplication.class, args);
}
}