先来spring配置
<?xml version="1.0" encoding="UTF-8" ?>
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
default-autowire="byName"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 上下文注释配置 -->
<context:annotation-config />
<context:component-scan base-package="com.ffour.yixing.entity" />
<!-- 引入项目配置属性文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- 数据库驱动类 -->
<property name="driverClassName" value="${database.driverClassName}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
<!-- 会话工厂 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<!-- 上下文组件扫描 -->
<context:component-scan base-package="com.ffour.yixing"></context:component-scan>
<!-- 定义事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" >
<property name="dataSource" ref="dataSource"/>
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="list*" propagation="REQUIRED" read-only="true" />
<tx:method name="load*" propagation="REQUIRED" read-only="true" />
<tx:method name="select*" propagation="REQUIRED" read-only="true" />
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
<!-- 其他方法使用默认的事务设置 -->
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="interceptorPointCuts"
expression="execution(* com.ffour.yixing.service.impl.*Impl.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="interceptorPointCuts" />
</aop:config>
<bean id="sysUserInfoDao" class="com.ffour.yixing.dao.impl.SysUserInfoDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="sysUserInfoSV" class="com.ffour.yixing.service.impl.SysUserInfoImpl">
<property name="sysUserInfoDao" ref="sysUserInfoDao"></property>
</bean>
<bean id="sysStudentUserInfoDao" class="com.ffour.yixing.dao.impl.SysStudentUserInfoDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="sysStudentUserInfoSV" class="com.ffour.yixing.service.impl.SysStudentUserInfoImpl">
<property name="sysStudentUserInfoDao" ref="sysStudentUserInfoDao"></property>
</bean>
接下来是hibernate的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- 方言配置 -->
org.hibernate.dialect.Oracle10gDialect
<!-- 是否在控制台显示sql -->
true
<!-- 是否自动提交事务 -->
true
<!-- 数据库批量查询最大数 -->
50
<!-- 数据库批量更新,添加,删除操作最大数 -->
20
org.springframework.orm.hibernate4.SpringSessionContext
<!-- 设置自动创建|更新|验证数据库表结构 -->
true
false
true
<mapping resource="com/ffour/yixing/entity/YSysUserinfo.hbm.xml"/>
<mapping resource="com/ffour/yixing/entity/SysBusinessUserInfo.hbm.xml"/>
<mapping resource="com/ffour/yixing/entity/SysStudentUserInfo.hbm.xml"/>
</session-factory>
hibernate的配置,刚才上传的代码有的消失了
这是我的Dao层的实现类
package com.ffour.yixing.dao.impl;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.ffour.yixing.dao.interfaces.BaseDao;
@SuppressWarnings("unchecked")
public class BaseDaoImpl implements BaseDao{
private Class<T> clazz;
/**
* 通过构造方法指定DAO的具体实现类
*/
public BaseDaoImpl() {
ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
clazz = (Class<T>) type.getActualTypeArguments()[0];
System.out.println("DAO的真实实现类是:" + this.clazz.getName());
}
/**
* 向DAO层注入SessionFactory
*/
@Resource
private SessionFactory sessionFactory;
/**
* 获取当前工作的Session
*/
protected Session getSession() {
return this.sessionFactory.getCurrentSession();
}
@Override
public void save(T entity) {
// TODO Auto-generated method stub
this.getSession().save(entity);
this.getSession().flush();
}
@Override
public void update(T entity) {
// TODO Auto-generated method stub
this.getSession().update(entity);
System.out.println("111");
this.getSession().flush();
}
@Override
public void delete(Serializable id) {
// TODO Auto-generated method stub
this.getSession().delete(this.findById(id));
this.getSession().flush();
}
@Override
public T findById(Serializable id) {
// TODO Auto-generated method stub
return (T) this.getSession().get(this.clazz, id);
}
@Override
public List<T> findByHQL(String hql, Object... params) {
// TODO Auto-generated method stub
Query query = this.getSession().createQuery(hql);
for (int i = 0; params != null && i < params.length; i++){
query.setParameter(i, params[i]);
}
return query.list();
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
还有,我在action直接写测试函数的时候,用假数据传入,也不行,就是不更新
然而,不管我怎么做,更新的数据就是不更新,还是原来的,百度了好久,用flush也不行,配置defaultAutoCommit也不可以,注解也不行,真的找不到原因了,求各位帮帮忙吧,新手上路,请多关照
有没有人帮帮忙啊,我实在是找不到这个原因
1.action有没有传数据进来,你在action判断下是否为null
2.你接口有没有引用dao的实现类
上面没问题的话,你看看update传进来的entity直接给指定一下类型 比如 student entity = (student)entity
如果配置是正确的,直接junit都是可以的