设为首页 加入收藏

TOP

Spring入门Blog[十四、Spring中hibernateTemplate的使用]
2014-11-24 08:29:24 】 浏览:3453
Tags:Spring 入门 Blog 十四 hibernateTemplate 使用

HibernateTemplate的配置和使用:

1、配置bean文件:因为要用到sessionFactory索性就都复制了过来.也方便大家看

[html]
< xml version="1.0" encoding="UTF-8" >
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-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">












class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

classpath:jdbc.properties



class="org.apache.commons.dbcp.BasicDataSource">











com/spring/model/user.hbm.xml
com/spring/model/userlog.hbm.xml




hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create























advice-ref="txManager"
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

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇(Relax 数论 1.1)POJ 2429 GCD & .. 下一篇java 接口 实现和继承关系

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目