spring3+hibernate4 sessionFactory 注入(最新修改)


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">


<!-- 数据库连接池 -->
<!-- 本地 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
<property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property 
name="url" value="jdbc:mysql://localhost:3306/blog" /> <property name="username" 
value="root" /> <property name="password" value="123456" /> </bean>



<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>


<property name="packagesToScan" value="com.blog.entity" />
</bean>


<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 用注解方式注入bean -->
<context:annotation-config />
<context:component-scan base-package="com.blog" />


</beans>




package com.blog.dao;

import javax.annotation.Resource;

import org.hibernate.Session;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;


/**
 * Created with IntelliJ IDEA. User: Juyan Date: 12-12-15 Time: 下午8:49 To change
 * this template use File | Settings | File Templates.
 */
@Component
public class SuperDao {
@Resource
SessionFactory sessionFactory;


public Session getSession() {
Session session = sessionFactory.getCurrentSession();
return session;
}


}




@Repository
public class UserDAO extends SuperDao {


private static final Logger log = Logger.getLogger(UserDAO.class);
// property constants
public static final String EMAIL = "email";
public static final String PASS_WORD = "passWord";
public static final String USER_NAME = "userName";


/*
* (non-Javadoc)

* @see com.blog.dao.UserService#save(com.blog.entity.User)
*/
public void save(User transientInstance) {
log.info("saving User instance");
try {
getSession().save(transientInstance);
log.info("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
......

转载于:https://my.oschina.net/qsyan/blog/96485

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值