属性'dataSource'是必需的Spring JDBC

时间:2015-11-29 22:17:49

标签: spring spring-mvc jdbctemplate

我尝试了下面的代码,但它显示以下错误

  

需要嵌套异常数据源

Spring Tool Suite

版本:3.7.1.RELEASE

servlet的context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />
    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>
    <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <beans:property name="url" value="jdbc:mysql://localhost:3306/mkyong"/>
        <beans:property name="username" value=""/>
        <beans:property name="password" value=""/>
    </beans:bean>
    <beans:bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <beans:property name="dataSource" ref="dataSource" />
    </beans:bean>
    <context:component-scan base-package="com.tehnocracksolutions.JdbcExample" />
</beans:beans>

控制器

package com.tehnocracksolutions.JdbcExample.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tehnocracksolutions.JdbcExample.Model.CreateModel;

@Controller
public class UserController {

    @RequestMapping(value="create",method=RequestMethod.POST)
    public String createUser(){
        CreateModel model = new CreateModel();
        model.insertdata();
        return "welcome";
    }
}

模型:使用mySQL进行数据库连接似乎数据源未正确传递

package com.tehnocracksolutions.JdbcExample.Model;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

public class CreateModel {
    @Autowired
    DataSource datasource;

    public void insertdata(){
        JdbcTemplate jdbcTemplate =  new JdbcTemplate(datasource);
        jdbcTemplate.execute("insert into data values('nayan','m','m','t')");
    }

    public DataSource getDatasource() {
        return datasource;
    }

    public void setDatasource(DataSource datasource) {
        this.datasource = datasource;
    }
}

3 个答案:

答案 0 :(得分:1)

使用配置的spring bean数据源,不要使用

import javax.sql.DataSource;

答案 1 :(得分:0)

您的配置是正确的。只需检查您是否在名为&#34; mkyong&#34;。

的mysql中创建了数据库

答案 2 :(得分:0)

第一种方式: 将类文件中的数据源声明为:

org.springframework.jdbc.datasource.DriverManagerDataSource dataSource;
第二种方式: 或者只是在CreateModel中声明jdbc模板并使用Jdbc模板自动装配它。然后,您无需在CreateModel类中声明DataSource。