2015-06-19 173 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