此案例使用Java Project创建,实现简单的Spring与Hibernate整合,理清思路很简单,主要是有Service层调用DAO层的方法,Spring核心配置文件中配置:数据源、Hibernate使用的Sessionfactory、和使用Hibernate必须使用的事务配置。
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"
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">
<!-- Spring注解 -->
<context:component-scan base-package="com.sh." />
<!-- 数据源 -->
<bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/lease?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="Sun_1024" />
</bean>
<!-- Hibernate SessionFactory对象 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan" value="com.sh.model.entity" />
</bean>
<!-- Hibernate 事务对象 (事务监控 SessionFactory对象)-->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 事务代理 (结合代码中的 注解 @Transactional)-->
<tx:annotation-driven transaction-manager="txManager"
proxy-target-class="true" />
</beans>
实体类使用的Hibernate注解
package com.sh.model.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="userinfo")
public class UserInfo {
private Integer userId;
private String userName;
private String userPwd;
private Integer userType;
private String remarks;
@Id
@GeneratedValue
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public Integer getUserType() {
return userType;
}
public void setUserType(Integer userType) {
this.userType = userType;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
}
创建基础的Sessionfactory共其它类调用
package com.sh.dao;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class BaseSessionFactory {
@Resource
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory(){
return sessionFactory;
}
public Session getSession(){
return sessionFactory.getCurrentSession();
}
}
UserDao业务层逻辑接口
package com.sh.dao;
import java.util.List;
import com.sh.model.entity.UserInfo;
public interface UserDao {
public List<UserInfo> userList();
}
UserDaoImpl实现UserDao接口
package com.sh.dao.impl;
import java.util.List;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.sh.dao.BaseSessionFactory;
import com.sh.dao.UserDao;
import com.sh.model.entity.UserInfo;
@Repository
@Transactional
public class UserDaoImpl extends BaseSessionFactory implements UserDao {
@Override
public List<UserInfo> userList() {
String hql = "FROM UserInfo";
List<UserInfo> userList = getSession().createQuery(hql).list();
System.out.println(userList.size());
return userList;
}
}
服务层的接口UserService
package com.sh.service;
import java.util.List;
import com.sh.model.entity.UserInfo;
public interface UserService {
public List<UserInfo> listUser();
}
实现UserService接口
package com.sh.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.sh.dao.UserDao;
import com.sh.model.entity.UserInfo;
import com.sh.service.UserService;
@Service
public class UserServiceImpl implements UserService{
@Resource
private UserDao userDao;
@Override
public List<UserInfo> listUser() {
List< UserInfo> list = userDao.userList();
if(list.size()>0){
return list;
}
return null;
}
}
测试类的创建
package test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sh.model.entity.UserInfo;
import com.sh.service.impl.UserServiceImpl;
public class Test {
public static void main(String[] args) {
String path="applicationContext.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(path);
UserServiceImpl us = (UserServiceImpl)context.getBean("userServiceImpl");
List<UserInfo> userlist = us.listUser();
System.out.println(userlist.size());
}
}