我有一些問題啓動了有關如何訪問mysql數據的spring指南(請參閱此鏈接:https://spring.io/guides/gs/accessing-data-mysql/)。我稍微調整了班,所以我有這樣的代碼:創建名爲'dataSource'的bean時出錯+ Spring Boot +休眠
的pom.xml
<?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> <groupId>org.springframework</groupId> <artifactId>gs-mysql-data</artifactId> <version>0.1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- Use MySQL Connector-J --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
我用這mainController: MainController.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.resource.iPbackend.UserRepository;
import com.resource.iPbackend.User;
@Controller
@RequestMapping(path = "/main")
public class MainController {
@Autowired
private UserRepository userRepository;
@RequestMapping(path = "/reg", method = RequestMethod.POST)
public @ResponseBody String regNewUser (@RequestParam String firstName, @RequestParam String lastName, @RequestParam String email, @RequestParam String password, @RequestParam String username) {
User n = new User();
n.setFirstName(firstName);
n.setLastName(lastName);
n.setEmail(email);
n.setPassword(password);
n.setUsername(username);
userRepository.save(n);
return "User is stored in database: " + n;
}
@GetMapping(path = "/all")
public @ResponseBody Iterable<User> getAllUsers() {
return userRepository.findAll();
}
}
這個倉庫
一起: UserRepository.java
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.resource.iPbackend.User;
@Repository
public interface UserRepository extends CrudRepository<User, Long> {
}
而這個實體: User.java
import org.springframework.data.annotation.Id;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String firstName;
private String lastName;
private String email;
private String password;
private String username;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
最後,我有這個Application.class:
@EnableAutoConfiguration
@SpringBootApplication
public class IPbackendApplication {
public static void main(String[] args) {
SpringApplication.run(IPbackendApplication.class, args);
}
}
我收到此錯誤:
org.springframework.beans.factory.UnsatisfiedDependencyException:錯誤創建名爲「org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration」豆:不滿意的依賴,通過構造函數的參數表示0;嵌套異常是org.springframework.beans.factory.BeanCreationException:在類路徑資源[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration $ Tomcat.class]中定義名稱爲'dataSource'的Bean時出錯:通過工廠方法實現的Bean失敗;嵌套異常是org.springframework.beans.BeanInstantiationException:無法實例化[org.apache.tomcat.jdbc.pool.DataSource]:工廠方法'dataSource'拋出異常;嵌套異常是java.lang.IllegalArgumentException:URL必須以'jdbc' 位於org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)〜[spring-beans-4.3.10.RELEASE。 jar:4.3.10.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)〜[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193)〜[彈簧豆-4.3.10.RELEASE.jar:4.3.10.RELEASE]
感謝您的幫助!
缺少相關代碼:您的數據庫配置在哪裏用於Spring? –