我嘗試下面的代碼,但它顯示了以下錯誤屬性「數據源」是必需的春天JDBC
嵌套異常數據源需要
春天工具套件
版本: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;
}
}
您應該發佈整個堆棧跟蹤... –
您已經在應用上下文xml中用dataSource配置了jdbcTemplate。你可以直接在你的CreateModel類中注入jdbcTemplate。 –