spring jdbcTemplete 数据提交到数据库

Devqin 发布于 2013/05/14 12:29
阅读 613
收藏 0

rt.我用的是spring的jdbcTemplete进行数据库的操作,一切都正常,就是不能往数据库里提交数据。

代码:

这是applicationContext.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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        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">
	  
    <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/crawldata"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
     <bean id = "transactionManager"   
         class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">   
         <property name = "dataSource" ref="dataSource"/>   
    </bean>  
    <!--利用了拦截器的原理。-->     
   <bean id="transactionInterceptor"    
         class="org.springframework.transaction.interceptor.TransactionInterceptor">     
        <property name="transactionManager" ref="transactionManager" />       
    <!-- 配置事务属性 -->  
   <property name="transactionAttributes">     
        <props>     
            <prop key="delete*">PROPAGATION_REQUIRED</prop>  
            <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>     
            <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>     
            <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>     
            <prop key="save*">PROPAGATION_REQUIRED</prop>     
            <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>     
       </props>     
   </property>     
   </bean> 
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
        <property name="dataSource" ref="dataSource" />  
    </bean>  
       <bean id="crawlDaoImpl" class="com.devqin.dao.crawlDaoImpl">  
       	<property name="jdbcTemplate" ref="jdbcTemplate" />  
   	</bean>
       <bean id="webAction" class="com.devqin.Action.webAction">  
       	<property name="cdi" ref="crawlDaoImpl" />  
   	</bean>
</beans>

数据库操作代码:

package com.devqin.dao;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;  
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 

import com.devqin.beans.link;
import com.devqin.beans.sRes;

public class crawlDaoImpl implements crawlDao {

	 @Autowired
	public JdbcTemplate jdbcTemplate;
	
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public boolean addLink(String link) {
		String sql = "INSERT INTO seedsdata ( `seeds`, `flag`, `souce`) VALUES ( '?', '0', 'c')";
		jdbcTemplate.update(sql);
		//System.out.println(link);
		return true;
		
	}



    

}
package com.devqin.Action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.devqin.beans.link;
import com.devqin.beans.sRes;
import com.devqin.dao.crawlDaoImpl;

@Controller
//@RequestMapping("/webAction")
public class webAction {
	@Autowired 
	public crawlDaoImpl cdi;
 
	public void setCdi(crawlDaoImpl cdi) {
		this.cdi = cdi;
	}

	//添加链接
 	@RequestMapping("/addLink.ac")
 	@ResponseBody
	public String addLink(@RequestParam String link) {
 		Boolean insert = cdi.addLink(link);
 		
 		if(insert){
 			return "success";
 		}else{
 			return "error";
 		}
	}
	 
}


加载中
0
Lob
Lob
sql语句是insert语句。但是为什么执行的是update的方法。求解?
0
sc19860709
sc19860709
很简单   因为 ?号 没有赋值
0
南湖船老大
南湖船老大

引用来自“perception”的答案

sql语句是insert语句。但是为什么执行的是update的方法。求解?

.....

update,insert,delete 都属于update

0
skyline520
skyline520
<bean id="transactionInterceptor" 23 class="org.springframework.transaction.interceptor.TransactionInterceptor">
 你这个拦截器的配置是没啥大的问题,可是你没有应用到 service上,所以事务出了问题

给你一个例子,我自己配置的
<bean  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
          <property name="beanNames">
              <list>
                    <value>*Service</value> //这里指定你想拦截的servcie bean规则
                    <value>*ServiceImpl</value>
              </list>
          </property>
          <property name="interceptorNames">
              <list>
                  <value>transactionInterceptor// 这里指定你的拦截器</value>
              </list>
          </property>
	 </bean>
仅供参考,利用这段代码使你的拦截器生效
0
Devqin
Devqin
<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        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">
	  

    <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/crawldata"/>
        <property name="username" value="root"/>
        <property name="password" value="qintengfei"/>
    </bean>
    
    
     <bean id = "transactionManager"   
         class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">   
         <property name = "dataSource" ref="dataSource"/>   
    </bean>
    
    
    
    <bean  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
          <property name="beanNames">
              <list>
                    <value>crawlDaoImpl</value>
              </list>
          </property>
          <property name="interceptorNames">
              <list>
                  <value>transactionInterceptor</value>
              </list>
          </property>
     </bean>
    
    
      
    <!--利用了拦截器的原理。-->     
   <bean id="transactionInterceptor"    
         class="org.springframework.transaction.interceptor.TransactionInterceptor">     
        <property name="transactionManager" ref="transactionManager" />       
    <!-- 配置事务属性 -->  
   <property name="transactionAttributes">     
        <props>     
            <prop key="delete*">PROPAGATION_REQUIRED</prop>  
            <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>     
            <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>     
            <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>     
            <prop key="save*">PROPAGATION_REQUIRED</prop>     
            <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>     
       </props>     
   </property>     
   </bean> 
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
        <property name="dataSource" ref="dataSource" />  
    </bean>  
       <bean id="crawlDaoImpl" class="com.devqin.dao.crawlDaoImpl">  
       	<property name="jdbcTemplate" ref="jdbcTemplate" />  
   	</bean>
       <bean id="webAction" class="com.devqin.Action.webAction">  
       	<property name="cdi" ref="crawlDaoImpl" />  
   	</bean>
</beans>

0
Devqin
Devqin

引用来自“skylline”的答案

<bean id="transactionInterceptor" 23 class="org.springframework.transaction.interceptor.TransactionInterceptor">
 你这个拦截器的配置是没啥大的问题,可是你没有应用到 service上,所以事务出了问题

给你一个例子,我自己配置的
<bean  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
          <property name="beanNames">
              <list>
                    <value>*Service</value> //这里指定你想拦截的servcie bean规则
                    <value>*ServiceImpl</value>
              </list>
          </property>
          <property name="interceptorNames">
              <list>
                  <value>transactionInterceptor// 这里指定你的拦截器</value>
              </list>
          </property>
	 </bean>
仅供参考,利用这段代码使你的拦截器生效
按照下边的方式配置,报错
<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        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">
	  

    <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/crawldata"/>
        <property name="username" value="root"/>
        <property name="password" value="qintengfei"/>
    </bean>
    
    
     <bean id = "transactionManager"   
         class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">   
         <property name = "dataSource" ref="dataSource"/>   
    </bean>
    
    <bean  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
          <property name="beanNames">
              <list>
                    <value>crawlDaoImpl</value>
              </list>
          </property>
          <property name="interceptorNames">
              <list>
                  <value>transactionInterceptor</value>
              </list>
          </property>
     </bean>
    
    
      
    <!--利用了拦截器的原理。-->     
   <bean id="transactionInterceptor"    
         class="org.springframework.transaction.interceptor.TransactionInterceptor">     
        <property name="transactionManager" ref="transactionManager" />       
    <!-- 配置事务属性 -->  
   <property name="transactionAttributes">     
        <props>     
            <prop key="delete*">PROPAGATION_REQUIRED</prop>  
            <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>     
            <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>     
            <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>     
            <prop key="save*">PROPAGATION_REQUIRED</prop>     
            <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>     
       </props>     
   </property>     
   </bean> 
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
        <property name="dataSource" ref="dataSource" />  
    </bean>  
       <bean id="crawlDaoImpl" class="com.devqin.dao.crawlDaoImpl">  
       	<property name="jdbcTemplate" ref="jdbcTemplate" />  
   	</bean>
       <bean id="webAction" class="com.devqin.Action.webAction">  
       	<property name="cdi" ref="crawlDaoImpl" />  
   	</bean>
</beans>
严重: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webAction' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.sun.proxy.$Proxy6 implementing com.devqin.dao.crawlDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'com.devqin.dao.crawlDaoImpl' for property 'cdi'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy6 implementing com.devqin.dao.crawlDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.devqin.dao.crawlDaoImpl] for property 'cdi': no matching editors or conversion strategy found
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:532)
0
skyline520
skyline520
你这个是配置事务生成了代理,你要配置使用cgilib来实现AOP,自动代理拦截器[BeanNameAutoProxyCreator]的 proxyTargetClass 属性,设置为true
0
lxbzmy
lxbzmy
请你查看日志执行的输出。
0
ByteSun
ByteSun

webAction加:

@Component
@Transactional

ByteSun
ByteSun
没事务data怎么能写到数据库呢?
返回顶部
顶部