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

扶醉桌前 提交于 2019-11-30 19:52:12


<?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;
}
}
......
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!