2012-06-05 41 views
1

我尝试执行简单的请求通过JdbcTemplate的MySql数据库,但我有一个错误,当框架加载和解析XML文件whicj定义数据源我:Spring:如何定义数据库配置?

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:util="http://www.springframework.org/schema/util" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 

    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
     <property name="url" value="jdbc:mysql://localhost:3306/spring_training"/> 
     <property name="username" value="root"/> 
     <property name="password" value="pass"/> 
    </bean> 
</beans> 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 
    <display-name>SpringTrainingTemplate</display-name> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring-config.xml /WEB-INF/jdbc-config.xml</param-value> 
</context-param> 

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<servlet> 
    <servlet-name>hello</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/servlet-context.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>hello</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

和调用它的控制器:

@Controller 
public class HomeController { 

@Autowired 
private ExampleService exampleService; 

@RequestMapping(value = "/details", method = RequestMethod.GET) 
public String details(Model model) { 

    ApplicationContext context = new ClassPathXmlApplicationContext("jdbc-config.xml"); 
    ExampleDao dao = (ExampleDao) context.getBean("ExampleDao"); 
    List<Application> list = dao.getAllApplications(); 

    model.addAttribute("application", list.get(0).getName()); 
    model.addAttribute("descriptionOfApplication", list.get(0).getDescription()); 

    return "details"; 
} 
} 

public class ExampleDao { 

private String request = "select * from application"; 

private JdbcTemplate jdbcTemplate; 

@Autowired 
private DataSource dataSource; 

public ExampleDao(DataSource dataSource) { 
    this.jdbcTemplate = new JdbcTemplate(dataSource); 
} 

public List<Application> getAllApplications() { 
    List<Application> applications = this.jdbcTemplate.query(request, new RowMapper<Application>() { 
     @Override 
     public Application mapRow(ResultSet rs, int i) throws SQLException { 
      Application application = new Application(); 
      application.setName(rs.getString("name")); 
      application.setType(rs.getString("type")); 
      application.setDescription(rs.getString("description")); 
      application.setDownloads(rs.getInt("downloads")); 
      return application; 
     } 
    }); 
    return applications; 
} 
} 

enter image description here

WHE我运行它,并输入http://localhost:8080/details我有一个500的异常与此消息堆栈跟踪:

root cause 
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [jdbc-config.xml]; nested exception is java.io.FileNotFoundException: class path resource [jdbc-config.xml] cannot be opened because it does not exist 

你能解释我如何配置分辩的方式,或者JDBC连接我的做法是正确的我应该在哪里寻找解决方案?所有的帮助将不胜感激。谢谢。

回答

3

Spring无法找到您的jdbc-config.xml配置文件。

你可以把它放在你的类路径,而不是WEB-INF文件夹中,并加载它在你的web.xml是这样的:

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:spring-config.xml,classpath:jdbc-config.xml</param-value> 
</context-param> 

一个好的做法是创建文件夹在你的src文件夹中的主要资源并将它们添加到类路径中。然后你可以把spring配置文件放在src/resources文件夹中。

+0

感谢您的答案和示例。你能解释一下如何把这些文件放入类路径中吗? –

+0

在IDEA中通过“项目结构 - >模块”将WebContent项目也标记为“src”。它允许我通过classpath引用jdbc-config.xml和spring-config.xml,但我仍然有同样的问题和错误消息。 –

+0

在项目结构>模块的intellij中,您有一个“Sources”选项卡。在此选项卡中,您可以在模块结构的左侧和右侧看到源文件夹。要添加新的源文件夹,请右键单击要添加的文件夹,然后单击Sources条目 - 右窗格顶部有一个名为Sources的按钮,用于执行相同的操作 –

1

类路径资源[JDBC-config.xml文件]不能打开,因为它 不存在

是文件名是正确的,是它定位于哪里?指定数据库连接的文件不是你说的应该在的地方 - 在类路径上。

+0

谢谢。你能解释一下如何在classpath中添加这些文件吗?这与jdk的这种类路径不一样,对吧? –

相关问题