我在Github上传了一个公共项目:
https://github.com/sotish/SpringMVC11.git
我的模特课是:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.data.annotation.Id;
@EntityScan
@Entity
@Table(name = "Student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(nullable= false, unique=true)
private String firstName;
@Column(nullable= false, unique=true)
private String lastName;
@Column(nullable= false, unique=true)
private int age;
@Column(nullable= false)
private String email;
//getter and setters
public int getId() {
return id;
}
public void setId(int 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 Integer getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
我创建了一个学生界面: -
package com.sat.Dao;
import java.util.List;
public interface Student {
public void insert(Student student);
public Boolean update();
public Student findById(int id);
public Student findByFirstname(String firstname);
List<Student> select(int id, String firstname, String lastname, String email, int age);
List<Student> selectAll();
}
studentDao是
import java.util.List;
public class StudentDao implements Student{
@Override
public void insert(Student student) {
// TODO Auto-generated method stub
}
@Override
public Boolean update() {
// TODO Auto-generated method stub
return null;
}
@Override
public Student findById(int id) {
// TODO Auto-generated method stub
return null;
}
@Override
public Student findByFirstname(String firstname) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<Student> select(int id, String firstname, String lastname,
String email, int age) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<Student> selectAll() {
// TODO Auto-generated method stub
return null;
}
}
我在mySql上有一个数据库
我正在尝试使用SpringDriverManager或BasicdataSource连接到Student数据库。
如何去做?
我的主要应用类是:
@SpringBootApplication
@ComponentScan ({"com.sat", "com.sat.controller"} )
@Configuration
@EnableAutoConfiguration
@EnableWebMvc
public class SpringMvc10Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication application = new SpringApplication(SpringMvc10Application.class);
application.setShowBanner(false);;
application.run(args);
System.out.println("Let's inspect the beans provided by Spring Boot:");
}
}
我的应用程序属性文件是
# Spring MVC
#spring.view.prefix:classpath:/templates/
#spring.view.suffix:.html
#spring.view.view-names:jsp/*, html/*
#spring.thymeleaf.view-names:thymeleaf/*
name=Phil, david
# Server
server.port=8088
#override the spring parameter 'create-drop', 'create' creates the schema deleting the previous data
spring.jpa.hibernate.ddl-auto=create
# no sql in the log
spring.jpa.show-sql=false
# mySQL
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/student
database.username=root
database.password=root
# THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.check-template-location=true
#spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.excluded-view-names= # comma-separated list of view names that should be excluded from resolution
#spring.thymeleaf.view-names= well, # comma-separated list of view names that can be resolved
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html # ;charset=<encoding> is added
spring.thymeleaf.cache= true
# set to false for hot refresh
我的简单index.html是用户插入存储在数据库中的输入的地方:
<!DOCTYPE html>
<html> <!--xmlns:th="http://www.thymeleaf.org">-->
<head>
<title>Thymeleaf tutorial: exercise 2</title>
<!--<link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" />-->
<meta charset="utf-8" />
</head>
<body>
<h1>Thymeleaf tutorial - Student Info</h1>
<h2>Student information</h2>
<input type="text" name="fname" value="" id="firstName"/>
<input type="text" name="lname" value="" id="lastName"/>
<label for="submit">submit</label><input type="submit" name="submit" value="submit" id="submit"/>
<span th:text="${name}"> </span>
</body>
</html>
现在我很困惑,因为我正在学习很多教程但是我在springBoot上看不太多。我不知道该怎么走。
在我学春天的时候,请建议我正确的方向。我希望有人会帮助,因为我已经坚持了好几天了。
我想使用spring Driver Manager创建一个连接池:
@Bean (name = "dataSource")
public DataSource dm() {
DriverManagerDataSource dbs = new DriverManagerDataSource();
dbs.setDriverClassName("jdbc.driverClassName");
dbs.setUrl("jdbc:mysql://localhost:3306/student");
dbs.setUsername("root");
dbs.setPassword("root");
// dbs.max-active=100;
return dm();
}
如何设置maxActive Connections,在这?
现在我想将它注入我的StudentDaoImp类,如下所示:
@Override
public List<Student> select(int id, String firstname, String lastname,
String email, int age) throws Exception {
java.sql.Connection con = ds.getConnection();
// List
con.close();
return null;
}
当我将项目作为Spring Boot App运行时,我得到了这个:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in com.sat.SpringMvc10Application: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dm' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [jdbc.driverClassName]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMetho
有人可以帮我纠正错误。
由于
答案 0 :(得分:1)
您忘记设置正确的驱动程序类名称:
java.lang.IllegalStateException:无法加载JDBC驱动程序类[jdbc.driverClassName]
更改声明
dbs.setDriverClassName("jdbc.driverClassName");
到
dbs.setDriverClassName("com.mysql.jdbc.Driver");
你应该好好去。好吧,至少这个例外应该消失了。并且不要忘记将驱动程序添加到类路径中。
编辑:
dm()
调用dm()
而不是简单地返回dbs
。@Id
实体中导入了错误的Student
注释(应该是JPA注释)。EntityManager
来执行此操作。在这种情况下,当使用Spring Data JPA(类路径的一部分)时,甚至不需要使用EntityManager
实现DAO,您只需使用所需的方法定义JPA存储库Java接口即可。