2015-06-19 88 views
0

我已上傳的公共項目在Github上:春天引導和道

https://github.com/sotish/SpringMVC11.git 

http://imgur.com/VC9q10C

我的模型類:

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 is

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的

enter image description here

我試圖連接到使用SpringDriverManager或的BasicDataSource學生數據庫。

如何去呢?

我的主應用程序類是:

@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,請建議我朝正確的方向發展。我希望有人會提供幫助,因爲我已經堅持了幾天。

我想用彈簧驅動程序管理器建立連接池:

@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連接,在這?

現在我想注入該到我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; 
    } 

我得到這個當我運行該項目作爲春季啓動應用程序:

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

如果您是Spring的新手,請了解核心框架,mvc和Spring數據(適用於JPA)。 Spring Boot只是基於這些最佳實踐的最佳實踐和自動配置,因此如果您正在學習Spring,那麼它很大程度上就像是「魔術」。 – ikumen

+0

彈簧啓動背後的想法是,它將配置所有必要的數據源等。你不需要做任何你已經完成的配置數據源等 – ArunM

+0

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-data-jpa ..下載此代碼並執行,您可以看到啓動是如何工作的。 – ArunM

回答

1

你忘了正確的驅動程序類名:

的java.lang。IllegalStateException異常:無法加載JDBC驅動程序類[jdbc.driverClassName]

更改聲明

dbs.setDriverClassName("jdbc.driverClassName");

dbs.setDriverClassName("com.mysql.jdbc.Driver");

,你應該是好去。那麼,至少這個例外應該消失了。並且不要忘記將驅動程序添加到您的課程路徑中。

編輯:

  • 你有無限遞歸:dm()呼籲dm()而不是簡單地返回dbs
  • 您在Student實體(應該是JPA之一)中輸入錯誤的@Id註釋。
  • 您似乎實現您的DAO並使用JDBC訪問數據庫。使用JPA時,您應該使用EntityManager來做到這一點。在這種情況下,當使用Spring Data JPA(類路徑的一部分)時,甚至不需要使用EntityManager來實現DAO,只需使用所需的方法定義JPA存儲庫Java接口即可。
+0

謝謝指出。這是一個GitHub鏈接:https://github.com/sotish/SpringMVC11.git – javaz

+0

感謝Brian查看代碼並對延遲響應抱歉。讓我糾正錯誤,我會回來... – javaz