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>
java




package com.blog.dao; mysql

import javax.annotation.Resource; spring

import org.hibernate.Session; sql

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; } } ......
相關文章
相關標籤/搜索