簡體   English   中英

Spring:如何定義數據庫配置?

[英]Spring: How to define database config?

我嘗試通過jdbcTemplate執行對MySql數據庫的簡單請求,但是當框架加載和解析xml文件whicj定義我的數據源時我有一個錯誤:

<?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:

@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;
}
}

在此輸入圖像描述

我運行它並輸入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連接,或者我的方法是否正確,我應該尋找我的問題的解決方案? 所有幫助將不勝感激。 謝謝。

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文件夾中創建文件夾main和resources,並將它們添加到類路徑中。 然后,您可以將spring配置文件放在src / resources文件夾中。

類路徑資源[jdbc-config.xml]無法打開,因為它不存在

文件名是否正確,它位於何處? 指定數據庫連接的文件不在您所說的位置 - 在類路徑上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM