急急急 急急急 SpringJDBC 声明式事物不回滚 求大家帮忙看看
basedao
public class BaseDao<T, PK extends Serializable> extends JdbcDaoSupport{
/**
* 添加 ,属性和数据库字段名一致,返回主键
* @param objForSave ,继承Saveable 的接口的类
* @return
*/
public Number saveAndReturnKey(Saveable objForSave){
return saveAndReturnKey(objForSave, objForSave.getTableName(), objForSave.getKeyColumns());
}
/**
* 添加实体,返回主键
* @param objForSave
* @param tableName
* @param columnAndValue
* @param keyColumns
* @return
*/
public Number saveAndReturnKey(Object objForSave,String tableName,String...keyColumns){
SimpleJdbcInsert insertActor = getSimpleJdbcInsert();
insertActor.setTableName(tableName);
insertActor.usingGeneratedKeyColumns(keyColumns);
Number newId = insertActor.executeAndReturnKey(new BeanPropertySqlParameterSource(objForSave)) ;
traceSql(insertActor.getInsertString());
return newId;
}
* 更新数据的方法
* @param sql
* @param T entity
*/
public void update(String sql,T entity){
getJdbcTemplate().update(sql,entity);
}
}
@Repository("userMgrDao")
@SuppressWarnings("unchecked")
public class UserMgrDao extends BaseDao{
//添加一个用户
public Users addUser(final Users users){
users.setId(saveAndReturnKey(users).intValue());
return users;
}
/**
* 删除数据的方法
* @param id
*/
public void delete(String id){
String sql = "delete from users where id=?";
update(sql, id);
}
}
@Service("userMgrService")
@Transactional
public class UserMgrService extends BaseService implements IUserMgrService{
public UserMgrService(){
}
private static Logger logger = Logger.getLogger(UserMgrService.class);
@Autowired
private UserMgrDao userMgrDao;
@Transactional(rollbackFor=Throwable.class)
public void save(Users user) throws ServiceException {
try {
user.setImportdate(new Date());
user.setFast(1L);
user.setUpdatedate(new Date());
user.setImportuserid(1L);
user.setUpdateuserid(1L);
userMgrDao.addUser(user);
}catch (Exception e) {
throw new ServiceException("获取失败", e);
}
}
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)
public void delete(String id){
try {
userMgrDao.delete(id);
} catch (Exception e) {
logger.error(e);
}
}
}
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework