简体   繁体   中英

Spring: How to define database config?

I try to execute simple request to MySql database via jdbcTemplate but I have an error when framework load and parse xml file whicj define my datasource:

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

and Controller that invoke it:

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

在此输入图像描述

Whe I run it and input http://localhost:8080/details I have got an 500 exception with stacktrace with this message:

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

Can you explain me how to configure jdbc connection in rigth way or if my approach is correct where I should look for a solution of my issue? All help would be appreciated. Thanks.

Spring cannot find your jdbc-config.xml configuration file.

You can put it in your classpath instead of the WEB-INF folder and load it in your web.xml like this:

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

A good practice is to create folders main and resources in your src folder and to add them in the classpath. Then you can put the spring config file in the src/resources folder.

class path resource [jdbc-config.xml] cannot be opened because it does not exist

Is the file name correct, where is it located? the file specifying the db connection is not where you said it should be - on the classpath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM