HibernateTemplate的配置和使用:
1、配置bean文件:因为要用到sessionFactory索性就都复制了过来.也方便大家看
[html]
< xml version="1.0" encoding="UTF-8" >
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.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create
pointcut-ref="entryPointMethod"
/>
2、使用@resource注入,然后调用save方法:
[java]
@Component("userDaoImpl")
public class UserDaoImpl implements UserDao{
@Resource
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
@Override
public void save(User u) {
try {
hibernateTemplate.save(u);
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
作者:zhang6622056