问题描述
<hibernate-configuration><session-factory><propertyname="connection.driver_class">com.mysql.jdbc.Driver</property><propertyname="connection.url">jdbc:mysql://localhost:3306/bbs</property><propertyname="connection.username">root</property><propertyname="connection.password">111111</property><propertyname="show_sql">true</property><propertyname="format_sql">true</property><propertyname="hbm2ddl.auto">update</property><mappingresource="wts/entity/User.hbm.xml"/></session-factory></hibernate-configuration>applicationContext.xml配置,注入sessionFactory和hibernateTemplete,bbsDao注入hibernateTemplete<beanid="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><propertyname="configLocation"value="classpath:hibernate.cfg.xml"></property></bean><beanid="hibernateTemplete"class="org.springframework.orm.hibernate4.HibernateTemplate"><propertyname="sessionFactory"ref="sessionFactory"></property></bean><beanid="bbsDao"class="wts.dao.impl.bbsDaoImpl"><propertyname="hibernateTemplate"ref="hibernateTemplete"></property></bean>以下是具体用法,首先继承HibernateDaoSupport类,该类提供了HibernateTemplate的getter和setter方法。publicclassbbsDaoImplextendsHibernateDaoSupport{publicvoidsaveUser(Useruser){this.getHibernateTemplate().save(user);}}
解决方案
解决方案二:
因为你集成了HibernateDaoSupport,所以你可以使用super.getTemplate,如果你需要自己注入的话,那么就没有必要继承了