完整版spring hibernate整合采用annotation
程序员文章站
2022-06-13 15:26:00
...
Keyword.java
//*******************************************************************//
//
//** 创建人: 何岳军
//
//** 描 述: message表(留言表)实体bean
//
//** 版 本: 湖南农业大学关工委网站2010版
//
//*******************************************************************//
package cn.hauggw.beans;
import java.util.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="tb_keyword")
public class Keyword {
private long id; // 自动编号
private int uid; // 用户id
private String keyword; // 关键字
private int property; // 属性 0 1
private Date adddate; //日期
@Id
@GeneratedValue(generator = "paymentableGenerator")
@GenericGenerator(name = "paymentableGenerator", strategy = "identity")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name="uid")
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
@Column(name="keyword")
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
@Column(name="property")
public int getProperty() {
return property;
}
public void setProperty(int property) {
this.property = property;
}
@Column(name="adddate")
public Date getAdddate() {
return adddate;
}
public void setAdddate(Date adddate) {
this.adddate = adddate;
}
}
DAO.java (接口类)
//*******************************************************************//
//
//** 创建人: 何岳军
//
//** 描 述: DAO接口
//
//** 版 本: 湖南农业大学关工委网站2010版
//
//*******************************************************************//
package cn.hauggw.service;
public interface DAO {
/**
* 获取记录总数
* @param entityClass 实体类
* @return
*/
public <T> long getCount(Class<T> entityClass);
/**
* 清除一级缓存的数据
*/
public void clear();
/**
* 保存实体
* @param entity 实体id
*/
public void save(Object entity);
/**
* 更新实体
* @param entity 实体id
*/
public void update(Object entity);
/**
* 删除实体
* @param entityClass 实体类
* @param entityid 实体id
*/
public <T> void delete(Class<T> entityClass, Object entityid);
/**
* 删除实体
* @param entityClass 实体类
* @param entityids 实体id数组
*/
public <T> void delete(Class<T> entityClass, Object[] entityids);
/**
* 获取实体
* @param <T>
* @param entityClass 实体类
* @param entityId 实体id
* @return
*/
public <T> T find(Class<T> entityClass, Object entityId);
}
DAOService.java
//*******************************************************************//
//
//** 创建人: 何岳军
//
//** 描 述: DAO实现
//
//** 版 本: 湖南农业大学关工委网站2010版
//
//*******************************************************************//
package cn.hauggw.service.impl;
import java.io.Serializable;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import cn.hauggw.service.DAO;;
@Transactional
public class DAOService implements DAO {
@Resource private SessionFactory sessionFactory;
public void clear() {
sessionFactory.getCurrentSession().clear();
}
public <T> void delete(Class<T> entityClass, Object entityid) {
delete(entityClass,new Object[]{entityid});
}
public <T> void delete(Class<T> entityClass, Object[] entityids) {
for(Object id : entityids){
sessionFactory.getCurrentSession().delete(sessionFactory.getCurrentSession().load(entityClass, (Serializable) id));
}
}
@SuppressWarnings("unchecked")
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public <T> T find(Class<T> entityClass, Object entityId) {
return (T) sessionFactory.getCurrentSession().get(entityClass, (Serializable) entityId);
}
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public <T> long getCount(Class<T> entityClass) {
return 0;
}
public void save(Object entity) {
sessionFactory.getCurrentSession().persist(entity);
}
public void update(Object entity) {
sessionFactory.getCurrentSession().merge(entity);
}
}
bean.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:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:annotation-config/> <!-- <context:component-scan base-package="cn" /> --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/> <property name="url" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ggwnet"/> <property name="username" value="sa"/> <property name="password" value="hyj_lk"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="annotatedClasses"> <list> <!-- <value>classpath:/cn/hauggw/beans</value> --> <value>cn.hauggw.beans.Message</value> <value>cn.hauggw.beans.Keyword</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.SQLServerDialect hibernate.show_sql=true hibernate.format_sql=true </value> </property> </bean> <!-- 事务管理器 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="txManager"/> <bean id="daoService" class="cn.hauggw.service.impl.DAOService" /> <bean id="keywordService" class="cn.hauggw.service.impl.KeywordServiceBean" /> </beans>
推荐阅读
-
Spring+Hibernate+Struts(SSH)框架整合实战
-
Spring 整合 Hibernate 时启用二级缓存实例详解
-
【原创】整合Spring4+Hibernate4+Struts2时NullPointerException问题解决
-
Spring Boot整合EasyExcel(完整版包含上传解析excel和下载模板)
-
Spring和Hibernate的整合操作示例
-
Spring入门(四)— 整合Struts和Hibernate
-
小型用户权限例子struts2.18+spring3.0+hibernate3.5+annotation(源码)
-
小型用户权限例子struts2.18+spring3.0+hibernate3.5+annotation(源码)
-
【Spring】Spring与Hibernate整合(十六)
-
记录后端之旅:初学S2SH框架,整合struts2、spring、Hibernate三大框架