根据Dao层系列第一篇 http://my.oschina.net/yaolifei/blog/145161 改写,加入spring
首先看spring的配置文件:
applicationContext.xml
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:property-placeholder location="jdbc.properties" />
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}" />
<property name="username" value= "${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="${jdbc.maxActive}" />
<property name="validationQuery" value="${jdbc.validationQuery}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>user.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="userDao" class="com.yaolifei.test.user.dao.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="userService" class="com.yaolifei.test.user.service.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
</beans>
有改动的地方主要是使用了Hibernate的getCurrentSession()方法获得session,可以来看下dao的实现:
UserDaoImpl.java
package com.yaolifei.test.user.dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.yaolifei.test.user.domain.User;
public class UserDaoImpl implements UserDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public User getById(Integer id) {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
User user = (User) session.get(User.class, id);
session.getTransaction().commit();
return user;
}
@Override
public List<User> getAll() {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Query query = session.createQuery("from User");
List<User> list = query.list();
session.getTransaction().commit();
return list;
}
@Override
public void save(User user) {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
}
@Override
public void update(User user) {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.update(user);
session.getTransaction().commit();
}
@Override
public void remove(User user) {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.delete(user);
session.getTransaction().commit();
}
}
关于getCurrentSession和openSession的区别请参考:
http://javachikuang.iteye.com/blog/271879
本例完整代码详见:http://git.oschina.net/yaolifei/test/tree/master/test-hibernate-spring
来源:oschina
链接:https://my.oschina.net/u/574527/blog/145619