UserDao类:
public interface UserDao {
public boolean updateuser(User u);
}
UserDaoImpl:实现类
public class UserDaoImpl extends JdbcDaoSupport implements UserDao{
//根据ID修改用户
private static final String UPDATEUSER="UPDATE USER_INFO SET USER_PWD=?,USER_DES=? WHERE USER_ID=?";
public boolean updateuser(User u) {
int index=0;
Object[] params=new Object[]{u.getUser_pwd(),u.getUser_des(),u.getUser_id()};
index=this.getJdbcTemplate().update(UPDATEUSER, params);
if(index>0){
return true;
}
return false;
}
}User:实体类
public class User implements Serializable {private static final long serialVersionUID = 6804869866083154612L;private String user_id=""; //用户名private String user_pwd="";//用户密码private String user_des="";//用户描述public String getUser_id() {
return user_id;
}public void setUser_id(String user_id) {
this.user_id = user_id;
}public String getUser_pwd() {
return user_pwd;
}public void setUser_pwd(String user_pwd) {
this.user_pwd = user_pwd;
}public String getUser_des() {
return user_des;
}public void setUser_des(String user_des) {
this.user_des = user_des;
}
}UserAction :action类
public class UserAction extends Action{private UserDao userdao;public void setUserdao(UserDao userdao) {
this.userdao = userdao;
}public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
//ApplicationContext ctx = this.getWebApplicationContext();
//Spring完成注入和初始化
//UserDao udao=(UserDao)ctx.getBean("userDAO");List<User> ulist=userdao.findall();System.out.println(ulist);for (User user : ulist) {
System.out.println(user.getUser_id());
}
boolean fal;
boolean fal1;
//boolean fal=userdao.deleteuser("SDJFD17");
User u=new User();
u.setUser_id("SDGFD16");
u.setUser_pwd("3333");
u.setUser_des("款到即发");fal=userdao.updateuser(u);//修改1User u1=new User();
u1.setUser_id("SDFVD15");
u1.setUser_pwd("ggggggggg----------------日日日日日日日日日日");//密码长度值只10个长度,过长会出错,修改不了
u1.setUser_des("款到即发");fal1=userdao.updateuser(u1);//修改2System.out.println("---"+fal+"----"+fal1);
return null;
}
}
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:aop="http://www.springframework.org/schema/aop"   
  xmlns:tx="http://www.springframework.org/schema/tx"   
  xsi:schemaLocation="http://www.springframework.org/schema/beans  
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
  http://www.springframework.org/schema/tx  
  http://www.springframework.org/schema/tx/spring-tx-2.0.xsd   
  http://www.springframework.org/schema/aop  
  http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">   
    
  <bean id="dataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName"
  value="com.mysql.jdbc.Driver">
  </property>
  <property name="url"
  value="jdbc:mysql://localhost/jquery?useUnicode=true&amp;characterEncoding=utf-8">
  </property>
  <property name="username" value="root"></property>
  <property name="password" value="admin"></property>   
  </bean>
    
    
  <!-- 事务管理 -->   
  ------------------------------------------------  
  <bean id ="JdbcTemplate"   
  class ="org.springframework.jdbc.core.JdbcTemplate">   
  <property name ="dataSource">   
  <ref bean ="dataSource"/>   
  </property>   
  </bean>   
------------------------------------    
    
  <bean id="txManager"   
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
  <property name="dataSource" ref="dataSource"/>   
  </bean>   
    
  <tx:advice id="txAdvice" transaction-manager="txManager">   
  <tx:attributes>  
  <tx:method name="add*" propagation="REQUIRED" />  
  <tx:method name="delete*" propagation="REQUIRED" />  
  <tx:method name="update*" propagation="REQUIRED" />  
  <tx:method name="add*" propagation="REQUIRED" />  
  <!-- <tx:method name="*" propagation="true" />-->    <tx:method name="*" rollback-for="SQLException" />//设置回滚了,结果无效,照样修改方法1能修改
    
  </tx:attributes>   
  </tx:advice>   
    
  <aop:config>  
  <aop:pointcut id="allManagerMethod"  
  expression="execution(* com.dao.imp.*.*(..))" />  
  <aop:advisor advice-ref="txAdvice"  
  pointcut-ref="allManagerMethod" />  
  </aop:config>  
    
  <bean id="userDAO" class="com.dao.imp.UserDaoImpl">
  <property name="dataSource">
  <ref bean="dataSource" />
  </property>
  </bean>
  <bean name="/test" class="com.action.UserAction" >
  <property name="userdao">
  <ref bean="userDAO"/>
  </property>
  </bean>
    </beans>
我action里修改方法1能修改,修改方法2不能修改,因为密码长度太大出错,只要一个不成功就回滚,我applicationContext.xml配置文件设置回滚了但是无效<tx:method name="*" rollback-for="SQLException" />
 <bean id ="JdbcTemplate"   
  class ="org.springframework.jdbc.core.JdbcTemplate">   
  <property name ="dataSource">   
  <ref bean ="dataSource"/>   
  </property>   
  </bean> 这个bean配置麻烦解释下

解决方案 »

  1.   

    <bean id ="JdbcTemplate"   
      class ="org.springframework.jdbc.core.JdbcTemplate">   
      <property name ="dataSource">   
      <ref bean ="dataSource"/>   
      </property>   
      </bean>
    [/Quote]
    这个就是Spring对JDBC的轻度封装.
      

  2.   

     <bean id ="JdbcTemplate"   
      class ="org.springframework.jdbc.core.JdbcTemplate">   
      <property name ="dataSource">   
      <ref bean ="dataSource"/>   
      </property>   
      </bean> 这个bean配置麻烦解释下
    不会吧,这个Bean都没懂,就去弄事务?