1

我尝试通过 jdbcTemplate 对 MySql 数据库执行简单请求,但是当框架加载和解析定义我的数据源的 xml 文件时出现错误:

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

在此处输入图像描述

当我运行它并输入时,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 连接,或者我的方法是否正确,我应该在哪里寻找我的问题的解决方案?所有帮助将不胜感激。谢谢。

4

2 回答 2

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 文件夹中创建文件夹 main 和 resources 并将它们添加到类路径中。然后你可以把 spring 配置文件放在 src/resources 文件夹中。

于 2012-06-05T09:44:42.607 回答
1

类路径资源 [jdbc-config.xml] 无法打开,因为它不存在

文件名是否正确,它位于何处?指定数据库连接的文件不在您所说的位置 - 在类路径上。

于 2012-06-05T09:32:51.840 回答