2012-06-05 109 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的這種類路徑不一樣,對吧? –

相關問題