完整版spring hibernate整合采用annotation

本文介绍了一个基于Java的关键词实体Bean定义及其与之对应的DAO接口实现细节,包括实体类字段、Hibernate注解使用及DAO接口的具体方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://www.diybl.com/course/3_program/java/javajs/20100719/462761.html

Keyword.java

001 //*******************************************************************//
002 //
003 //** 创建人: 何岳军
004 //
005 //** 描 述: message表(留言表)实体bean
006 //
007 //** 版 本: 湖南农业大学关工委网站2010版
008 //
009 //*******************************************************************//
010 package cn.hauggw.beans;
011
012 import java.util.*;
013
014 import javax.persistence.Column;
015 import javax.persistence.Entity;
016 import javax.persistence.GeneratedValue;
017 import javax.persistence.Id;
018 import javax.persistence.Table;
019
020 import org.hibernate.annotations.GenericGenerator;
021 @Entity
022 @Table(name="tb_keyword")
023 public class Keyword {
024
025 private long id; // 自动编号
026 private int uid; // 用户id
027 private String keyword; // 关键字
028 private int property; // 属性 0 1
029 private Date adddate; //日期
030
031 @Id
032 @GeneratedValue(generator = "paymentableGenerator")
033 @GenericGenerator(name = "paymentableGenerator", strategy = "identity")
034 public long getId() {
035 return id;
036 }
037 public void setId(long id) {
038 this.id = id;
039 }
040 @Column(name="uid")
041 public int getUid() {
042 return uid;
043 }
044 public void setUid(int uid) {
045 this.uid = uid;
046 }
047 @Column(name="keyword")
048 public String getKeyword() {
049 return keyword;
050 }
051 public void setKeyword(String keyword) {
052 this.keyword = keyword;
053 }
054 @Column(name="property")
055 public int getProperty() {
056 return property;
057 }
058 public void setProperty(int property) {
059 this.property = property;
060 }
061 @Column(name="adddate")
062 public Date getAdddate() {
063 return adddate;
064 }
065 public void setAdddate(Date adddate) {
066 this.adddate = adddate;
067 }
068
069
070 }
071
072
073
074
075
076 DAO.java (接口类)
077
078 //*******************************************************************//
079 //
080 //** 创建人: 何岳军
081 //
082 //** 描 述: DAO接口
083 //
084 //** 版 本: 湖南农业大学关工委网站2010版
085 //
086 //*******************************************************************//
087 package cn.hauggw.service;
088
089 public interface DAO {
090
091 /**
092 * 获取记录总数
093 * @param entityClass 实体类
094 * @return
095 */
096 public <T> long getCount(Class<T> entityClass);
097
098 /**
099 * 清除一级缓存的数据
100 */
101 public void clear();
102
103 /**
104 * 保存实体
105 * @param entity 实体id
106 */
107 public void save(Object entity);
108
109 /**
110 * 更新实体
111 * @param entity 实体id
112 */
113 public void update(Object entity);
114
115 /**
116 * 删除实体
117 * @param entityClass 实体类
118 * @param entityid 实体id
119 */
120 public <T> void delete(Class<T> entityClass, Object entityid);
121
122 /**
123 * 删除实体
124 * @param entityClass 实体类
125 * @param entityids 实体id数组
126 */
127 public <T> void delete(Class<T> entityClass, Object[] entityids);
128
129 /**
130 * 获取实体
131 * @param <T>
132 * @param entityClass 实体类
133 * @param entityId 实体id
134 * @return
135 */
136 public <T> T find(Class<T> entityClass, Object entityId);
137
138 }
139
140
141
142
143
144 DAOService.java
145
146
147
148 //*******************************************************************//
149 //
150 //** 创建人: 何岳军
151 //
152 //** 描 述: DAO实现
153 //
154 //** 版 本: 湖南农业大学关工委网站2010版
155 //
156 //*******************************************************************//
157 package cn.hauggw.service.impl;
158
159 import java.io.Serializable;
160
161 import javax.annotation.Resource;
162
163 import org.hibernate.SessionFactory;
164 import org.springframework.transaction.annotation.Propagation;
165 import org.springframework.transaction.annotation.Transactional;
166 import cn.hauggw.service.DAO;;
167
168 @Transactional
169 public class DAOService implements DAO {
170 @Resource private SessionFactory sessionFactory;
171 public void clear() {
172 sessionFactory.getCurrentSession().clear();
173 }
174
175 public <T> void delete(Class<T> entityClass, Object entityid) {
176 delete(entityClass,new Object[]{entityid});
177 }
178
179 public <T> void delete(Class<T> entityClass, Object[] entityids) {
180 for(Object id : entityids){
181 sessionFactory.getCurrentSession().delete(sessionFactory.getCurrentSession().load(entityClass, (Serializable) id));
182 }
183 }
184 @SuppressWarnings("unchecked")
185 @Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
186 public <T> T find(Class<T> entityClass, Object entityId) {
187 return (T) sessionFactory.getCurrentSession().get(entityClass, (Serializable) entityId);
188 }
189
190 @Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
191 public <T> long getCount(Class<T> entityClass) {
192 return 0;
193 }
194
195 public void save(Object entity) {
196 sessionFactory.getCurrentSession().persist(entity);
197 }
198
199 public void update(Object entity) {
200 sessionFactory.getCurrentSession().merge(entity);
201 }
202
203 }





bean.xml



01 <?xml version="1.0" encoding="UTF-8"?>
02 <beans xmlns="http://www.springframework.org/schema/beans"
03 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04 xmlns:context="http://www.springframework.org/schema/context"
05 xmlns:aop="http://www.springframework.org/schema/aop"
06 xmlns:tx="http://www.springframework.org/schema/tx"
07 xsi:schemaLocation="http://www.springframework.org/schema/beans
08 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
09 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
10 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
11 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
12
13 <context:annotation-config/>
14 <!-- <context:component-scan base-package="cn" /> -->
15 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
16 <property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
17 <property name="url" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ggwnet"/>
18 <property name="username" value="sa"/>
19 <property name="password" value="hyj_lk"/>
20
21 </bean>
22
23 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
24 <property name="dataSource" ref="dataSource"/>
25 <property name="annotatedClasses">
26 <list>
27 <!-- <value>classpath:/cn/hauggw/beans</value> -->
28 <value>cn.hauggw.beans.Message</value>
29 <value>cn.hauggw.beans.Keyword</value>
30 </list>
31 </property>
32 <property name="hibernateProperties">
33 <value>
34 hibernate.dialect=org.hibernate.dialect.SQLServerDialect
35 hibernate.show_sql=true
36 hibernate.format_sql=true
37 </value>
38 </property>
39 </bean>
40 <!-- 事务管理器 -->
41 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
42 <property name="sessionFactory" ref="sessionFactory"/>
43 </bean>
44 <tx:annotation-driven transaction-manager="txManager"/>
45
46 <bean id="daoService" class="cn.hauggw.service.impl.DAOService" />
47 <bean id="keywordService" class="cn.hauggw.service.impl.KeywordServiceBean" />
48
49 </beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值