hibernate

Chinasb's Blog

首頁
Google App Engine
Ralasafe
開發手冊
Spring註解大全
OFC2
NoSQL實戰
螞蟻運動
Browsing: / Home / 2011 / 六月 / spring2.5整合hibernate3.3的泛型Dao並加入spring jdbc的支持
Print Email Shortlink
spring2.5整合hibernate3.3的泛型Dao並加入spring jdbc的支持
BY ETHAN ON 2011 年 06 月 01 日 IN JAVA
原文鏈接:http://blog.csdn.net/tom_221x/archive/2009/08/22/4473873.aspx

以前寫的一個改進版泛型dao,在這裏 。基本實現了0代碼編寫dao。現在又加入了spring jdbc的支持,使得dao即可以用實體對象進行數據存取,有可以用jdbc的底層化操作刪除,更新。結構見圖:




具體代碼和上圖對應:

DaoTest.java

01
package com.test;
02
import javax.annotation.Resource;
03
import org.junit.Test;
04
import org.junit.runner.RunWith;
05
import org.springframework.test.context.ContextConfiguration;
06
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
07
import com.hibernate.entityclass.Daotest;
08
import com.tinylight.dao.base.IBaseDao;
09
@RunWith(SpringJUnit4ClassRunner.class)
10
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
11
public class DaoTest {
12

13
@Resource(name="demoDao")
14
private IBaseDao<Daotest,Integer> demoDao;
15

16
@Test
17
public void test(){
18

19
}
20
}
BaseDao.java

001
/**
002
*
003
*/
004
package com.tinylight.dao.base;
005
import java.io.Serializable;
006
import java.util.Iterator;
007
import java.util.List;
008
import org.hibernate.Query;
009
import org.hibernate.SQLQuery;
010
import org.hibernate.Session;
011
import org.hibernate.StatelessSession;
012
import org.springframework.orm.hibernate3.HibernateTemplate;
013
import com.tinylight.dao.hibernate.GenericDao;
014
import com.tinylight.dao.hibernate.GenericEntityDao;
015
import com.tinylight.dao.jdbc.SimpleJdbcDao;
016
import com.tinylight.dao.support.Page;
017
/**
018
* @author scott.Cgi
019
* @since 2009-5-12
020
* 提供dao的所有操作<br>
021
* 實現類由spring注入:<br>
022
* {@link com.tinylight.dao.hibernate.GenericEntityDao}
023
* {@link com.tinylight.dao.hibernate.GenericDao}
024
* {@link com.tinylight.dao.jdbc.SimpleJdbcDao}
025
*/
026
public class BaseDao<T,PK extends Serializable> implements IBaseDao<T,PK>{
027
protected Class<T> entityClass;// DAO所管理的Entity類型.
028
private GenericEntityDao<T,PK> gedao;
029
private GenericDao gdao;
030
private SimpleJdbcDao sjdao;
031

032
public Class<T> getEntityClass() {return entityClass;}
033
public void setEntityClass(Class<T> entityClass) {this.entityClass = entityClass;}
034

035
public GenericEntityDao<T, PK> getGedao() {return gedao;}
036
public void setGedao(GenericEntityDao<T, PK> gedao) {this.gedao = gedao;}
037

038
public GenericDao getGdao() {return gdao;}
039
public void setGdao(GenericDao gdao) {this.gdao = gdao;}
040

041
public SimpleJdbcDao getSjdao() {return sjdao;}
042
public void setSjdao(SimpleJdbcDao sjdao) {this.sjdao = sjdao;}
043
/**
044
*讓spring提供構造函數注入
045
*/
046
public BaseDao(Class<T> type) {
047
this.entityClass = type;
048
}
049

050
public BaseDao(){}
051

052
public void clear() {
053
gdao.clear();
054
}
055

056
public Query createQuery(String hql, Object... values) {
057
return gdao.createQuery(hql, values);
058
}
059

060
public void delete(T entityObject) {
061
gedao.delete(entityObject);
062
}
063

064
public void deleteById(PK id) {
065
gedao.deleteById(id);
066
}
067

068
public void evict(T entityObject) {
069
gedao.evict(entityObject);
070
}
071

072
public List<T> find(String hql, Object... values) {
073
return gdao.find(hql, values);
074
}
075

076
public List<T> findByNamedParams(String hql, String[] paramNames,
077
Object... values) {
078
return gdao.findByNamedParams(hql, paramNames, values);
079
}
080

081
public void flush() {
082
gdao.flush();
083
}
084

085
public List<T> getAll() {
086
return gedao.getAll();
087
}
088

089
public T getById(PK id) {
090
return gedao.getById(id);
091
}
092

093
public Session getNativeHibernateSession() {
094
return gdao.getNativeHibernateSession();
095
}
096

097
public StatelessSession getNativeStatelessHibernateSession() {
098
return gdao.getNativeStatelessHibernateSession();
099
}
100

101
public HibernateTemplate getSpringHibernateTemplate() {
102
return gdao.getSpringHibernateTemplate();
103
}
104

105
public Iterator<T> iterator(String hql, Object... values) {
106
return gdao.iterator(hql, values);
107
}
108

109
public SimpleJdbcDao jdbc() {
110
return sjdao;
111
}
112

113
public T load(PK id) {
114
return gedao.load(id);
115
}
116

117
public void load(T entityObject, PK id) {
118
gedao.load(entityObject, id);
119
}
120

121
public T merge(T entityObject) {
122
return gedao.merge(entityObject);
123
}
124

125
public SQLQuery nativeSqlQuery(String sql) {
126
return gdao.nativeSqlQuery(sql);
127
}
128

129
public Page<T> pagedQuery(String countHql,String hql, int pageNo, int pageSize,
130
Object... values) {
131
return gdao.pagedQuery(countHql,hql, pageNo, pageSize, values);
132
}
133

134
public Page<T> pagedQueryByStartNo(String countHql,String hql, int startNo, int pageSize,
135
Object... values) {
136
return gdao.pagedQueryByStartNo(countHql,hql, startNo, pageSize, values);
137
}
138

139
public void refresh(T entityObject) {
140
gedao.refresh(entityObject);
141
}
142

143
public void save(T entityObject) {
144
gedao.save(entityObject);
145
}
146

147
}
IBaseDao.java

001
/**
002
*
003
*/
004
package com.tinylight.dao.base;
005
import java.io.Serializable;
006
import java.util.Iterator;
007
import java.util.List;
008
import org.hibernate.Query;
009
import org.hibernate.SQLQuery;
010
import org.hibernate.Session;
011
import org.hibernate.StatelessSession;
012
import org.springframework.orm.hibernate3.HibernateTemplate;
013
import com.tinylight.dao.jdbc.SimpleJdbcDao;
014
import com.tinylight.dao.support.Page;
015
/**
016
* @author scott.Cgi
017
* @since 2009-5-12
018
*
019
*/
020
public interface IBaseDao<T,PK extends Serializable> {
021
/**
022
* 根據主鍵類型的id獲取實體對象,立即執行查詢返回對象,數據庫沒有匹配則返回null
023
*/
024
public T getById(PK id);
025

026
/**
027
* 獲取實體類型的全部對象
028
*/
029
public List<T> getAll();
030

031
/**
032
* 獲取實體對象的代理,如果數據庫沒有匹配則異常,實體類有關聯其它對象則延時加載
033
* @param id
034
* @return
035
*/
036
public T load(PK id);
037

038
/**
039
* 把數據加載到指定的非持久化實例上
040
* @param entityObject
041
* @param id
042
*/
043
public void load(T entityObject,PK id);
044

045
/**
046
* 刪除對象.
047
*/
048
public void delete(T entityObject);
049

050
/**
051
* 根據id刪除對象
052
* @param id
053
*/
054
public void deleteById(PK id);
055

056
/**
057
* 強迫裝載對象和它的集合,使用了觸發器的數據字段比較適合使用
058
* @param entityObject
059
*/
060
public void refresh(T entityObject);
061

062
/**
063
* 消除與 Hibernate Session 的關聯
064
* @param entityObject
065
*/
066
public void evict(T entityObject);
067

068
/**
069
* 保存對象.<br>
070
* 如果對象已在本session中持久化了,不做任何事。<br>
071
* 如果另一個seesion擁有相同的持久化標識,拋出異常。<br>
072
* 如果沒有持久化標識屬性,調用save()。<br>
073
* 如果持久化標識表明是一個新的實例化對象,調用save()。<br>
074
* 如果是附帶版本信息的(version或timestamp)且版本屬性表明爲新的實例化對象就save()。<br>
075
* 否則調用update()重新關聯託管對象
076
* @param entityObject
077
*/
078
public void save(T entityObject);
079

080
/**
081
* 如果對象已在本session中持久化了,覆蓋原有的<br>
082
* 如果session中沒有對應對象,從數據庫加載<br>
083
* 如果是脫管對象,則什麼都不做
084
* @param entityObject
085
* @return
086
*/
087
public T merge(T entityObject);
088

089
/**
090
* 根據hql查詢,直接使用HibernateTemplate的find函數.
091
* @param <T>
092
* @param hql
093
* @param values
094
* @return
095
*/
096
public List<T> find(String hql, Object... values);
097

098
/**
099
* 根據命名參數查詢
100
* @param <T>
101
* @param hql 帶有命名參數的hql語句
102
* @param paramNames 命名參數的名字
103
* @param values 命名參數的值<br>
104
* <b>例如:</b><br>
105
* findByNamedParams("from Test where t1 = :t",new String[]{"t"},tValue);
106
* @return
107
*/
108
public List<T> findByNamedParams(String hql,String[] paramNames,Object...values);
109

110
/**
111
* 創建Query對象.<br>
112
* 對於需要first,max,fetchsize,cache,cacheRegion等諸多設置的函數,可以在返回Query後自行設置.
113
* @param hql
114
* @param values
115
* @return
116
*/
117
public Query createQuery(String hql,Object... values);
118

119
/**
120
* 執行一些必須的sql語句把內存中的對象同步到數據庫中
121
*/
122
public void flush();
123

124
/**
125
* 清除對象緩存
126
*/
127
public void clear();
128

129
/**
130
* 返回iterator接口類型的結果
131
* @param <T>
132
* @param hql
133
* @param values
134
* @return
135
*/
136
public Iterator<T> iterator(String hql,Object...values);
137

138
/**
139
* @return 當前上下文的原生Hibernate session對象,依然受到spring事務管理不需要手動close
140
*/
141
public Session getNativeHibernateSession();
142

143
/**
144
* @return 當前上下文的原生Hibernate StatelessSession對象<br>
145
* 此對象不級聯關聯實例,忽略集合不觸發Hibernate事件模型和攔截器,沒有一級緩存,沒有持久化上下文,接近JDBC.
146
*/
147
public StatelessSession getNativeStatelessHibernateSession();
148

149
/**
150
* 執行本地查詢獲得SQLQuery對象<br>
151
* 可以調用addEntity(*.class).list();獲得對應實體list集合<br>
152
* addEntity.add(*.class).addJoin(*.class).list();獲得一對多代理對象<br>
153
* 更多用法見google
154
* @param sql
155
* @return
156
*/
157
public SQLQuery nativeSqlQuery(String sql);
158

159
/**
160
* @param <T>
161
* @param countHql 計算數據總條數的hql語句(就是帶count(*)的hql)
162
* @param hql
163
* @param pageNo 頁面號
164
* @param pageSize 頁面容量
165
* @param values
166
* @return
167
*/
168
public Page<T> pagedQuery(String countHql,String hql, int pageNo, int pageSize, Object... values);
169

170
/**
171
* @param <T>
172
* @param countHql 計算數據總條數的hql語句(就是帶count(*)的hql)
173
* @param hql
174
* @param startNo 分頁從哪一條數據開始
175
* @param pageSize 頁面容量
176
* @param values
177
* @return
178
*/
179
public Page<T> pagedQueryByStartNo(String countHql,String hql, int startNo, int pageSize, Object... values);
180

181
/**
182
* @return 獲得spring的HibernateTemplate擁有更多的功能
183
*/
184
public HibernateTemplate getSpringHibernateTemplate();
185

186
/**
187
* @return 獲得jdbc操作的超絢酷dao
188
*/
189
public SimpleJdbcDao jdbc();
190
}
GenericDao.java

001
/**
002
*
003
*/
004
package com.tinylight.dao.hibernate;
005
import java.util.Iterator;
006
import java.util.List;
007
import org.hibernate.Query;
008
import org.hibernate.SQLQuery;
009
import org.hibernate.Session;
010
import org.hibernate.StatelessSession;
011
import org.springframework.orm.hibernate3.HibernateTemplate;
012
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
013
import com.tinylight.dao.support.Page;
014
/**
015
* 繼承自spring的HibernateDaoSupport<br>
016
* 提供了和具體實體類無關的數據庫操作
017
* @author scott.Cgi
018
* @since 2009-5-10
019
*
020
*/
021
public class GenericDao extends HibernateDaoSupport {
022

023
/**
024
* 根據hql查詢,直接使用HibernateTemplate的find函數.
025
* @param <T>
026
* @param hql
027
* @param values
028
* @return
029
*/
030
@SuppressWarnings("unchecked")
031
public <T> List<T> find(String hql, Object... values) {
032
return this.getHibernateTemplate().find(hql, values);
033
}
034

035
/**
036
* 根據命名參數查詢
037
* @param <T>
038
* @param hql 帶有命名參數的hql語句
039
* @param paramNames 命名參數的名字
040
* @param values 命名參數的值<br>
041
* <b>例如:</b><br>
042
* findByNamedParams("from Test where t1 = :t",new String[]{"t"},tValue);
043
* @return
044
*/
045
@SuppressWarnings("unchecked")
046
public <T> List<T> findByNamedParams(String hql,String[] paramNames,Object...values){
047
return this.getHibernateTemplate().findByNamedParam(hql, paramNames, values);
048
}
049

050
/**
051
* 創建Query對象.<br>
052
* 對於需要first,max,fetchsize,cache,cacheRegion等諸多設置的函數,可以在返回Query後自行設置.
053
* @param hql
054
* @param values
055
* @return
056
*/
057
public Query createQuery(String hql,Object... values) {
058
//這裏的false表示不創建session保證,當前操作在spring同一個事務的管理下
059
Query query = this.getSession(false).createQuery(hql);
060
if (values != null) {
061
for (int i = 0; i < values.length; i++) {
062
query.setParameter(i, values[i]);
063
}
064
}
065
return query;
066
}
067

068
/**
069
* 執行一些必須的sql語句把內存中的對象同步到數據庫中
070
*/
071
public void flush() {
072
this.getHibernateTemplate().flush();
073
}
074

075
/**
076
* 清除對象緩存
077
*/
078
public void clear() {
079
this.getHibernateTemplate().clear();
080
}
081

082
/**
083
* 返回iterator接口類型的結果
084
* @param <T>
085
* @param hql
086
* @param values
087
* @return
088
*/
089
@SuppressWarnings("unchecked")
090
public <T> Iterator<T> iterator(String hql,Object...values){
091
return this.getHibernateTemplate().iterate(hql, values);
092
}
093

094
/**
095
* @return 當前上下文的原生Hibernate session對象,依然受到spring事務管理不需要手動close
096
*/
097
public Session getNativeHibernateSession(){
098
return this.getSessionFactory().getCurrentSession();
099
}
100

101
/**
102
* @return 當前上下文的原生Hibernate StatelessSession對象<br>
103
* 此對象不級聯關聯實例,忽略集合不觸發Hibernate事件模型和攔截器,沒有一級緩存,沒有持久化上下文,接近JDBC.
104
*/
105
public StatelessSession getNativeStatelessHibernateSession(){
106
return this.getSessionFactory().openStatelessSession();
107
}
108

109
/**
110
* 執行本地查詢獲得SQLQuery對象<br>
111
* 可以調用addEntity(*.class).list();獲得對應實體list集合<br>
112
* addEntity.add(*.class).addJoin(*.class).list();獲得一對多代理對象<br>
113
* 更多用法見google
114
* @param sql
115
* @return
116
*/
117
public SQLQuery nativeSqlQuery(String sql){
118
return this.getSession(false).createSQLQuery(sql);
119
}
120

121
/**
122
* @param <T>
123
* @param countHql 計算數據總條數的hql語句(就是帶count(*)的hql)
124
* @param hql
125
* @param pageNo 頁面號
126
* @param pageSize 頁面容量
127
* @param values
128
* @return
129
*/
130
@SuppressWarnings("unchecked")
131
public <T> Page<T> pagedQuery(String countHql,String hql, int pageNo, int pageSize, Object... values) {
132
// Count查詢
133
List<T> countlist = this.getHibernateTemplate().find(countHql, values);
134
long totalCount = (Long) countlist.get(0);
135
if (totalCount < 1)
136
return new Page<T>();
137
// 當前頁的開始數據索引
138
long startIndex = Page.getStartOfPage(pageNo, pageSize);
139
Query query = this.createQuery(hql, values);
140
List<T> list = query.setFirstResult((int) startIndex).setMaxResults(pageSize).list();
141
return new Page<T>(startIndex, totalCount, pageSize, list);
142
}
143

144
/**
145
* @param <T>
146
* @param countHql 計算數據總條數的hql語句(就是帶count(*)的hql)
147
* @param hql
148
* @param startNo 分頁從哪一條數據開始
149
* @param pageSize 頁面容量
150
* @param values
151
* @return
152
*/
153
@SuppressWarnings("unchecked")
154
public <T> Page<T> pagedQueryByStartNo(String countHql,String hql, int startNo, int pageSize, Object... values){
155
// Count查詢
156
List<T> countlist = getHibernateTemplate().find(countHql, values);
157
long totalCount = (Long) countlist.get(0);
158
if (totalCount < 1)
159
return new Page();
160

161
int startIndex = startNo;
162
Query query = createQuery(hql, values);
163
List<T> list = query.setFirstResult(startIndex).setMaxResults(pageSize).list();
164
return new Page<T>(startIndex, totalCount, pageSize, list);
165
}
166

167
/**
168
* @return 獲得spring的HibernateTemplate擁有更多的功能
169
*/
170
public HibernateTemplate getSpringHibernateTemplate(){
171
return this.getHibernateTemplate();
172
}
173
}
GenericEntityDao.java

view sourceprint?
001
/**
002
*
003
*/
004
package com.tinylight.dao.hibernate;
005
import java.io.Serializable;
006
import java.util.List;
007
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
008
/**
009
* 負責爲單個Entity對象提供CRUD操作的Hibernate DAO基類.<br>
010
* 子類只要在類定義時指定所管理Entity的Class<br>
011
* 即擁有對單個Entity對象的CRUD操作.
012
* @author scott.Cgi
013
* @since 2009-5-11
014
*
015
*/
016
public class GenericEntityDao<T,PK extends Serializable> extends HibernateDaoSupport {
017

018
protected Class<T> entityClass;// DAO所管理的Entity類型.
019
public void setEntityClass(Class<T> type){//注入實體類型
020
this.entityClass=type;
021
}
022
public Class<T> getEntityClass(){
023
return this.entityClass;
024
}
025

026
public GenericEntityDao(){}
027
public GenericEntityDao(Class<T> entityClass){this.entityClass = entityClass;}
028
/**
029
* 根據主鍵類型的id獲取實體對象,立即執行查詢返回對象,數據庫沒有匹配則返回null
030
*/
031
@SuppressWarnings("unchecked")
032
public T getById(PK id) {
033
return (T)this.getHibernateTemplate().get(this.entityClass, id);
034
}
035

036
/**
037
* 獲取實體類型的全部對象
038
*/
039
@SuppressWarnings("unchecked")
040
public List<T> getAll() {
041
return (List<T>)(this.getHibernateTemplate().loadAll(this.entityClass));
042
}
043

044
/**
045
* 獲取實體對象的代理,如果數據庫沒有匹配則異常,實體類有關聯其它對象則延時加載
046
* @param id
047
* @return
048
*/
049
@SuppressWarnings("unchecked")
050
public T load(PK id){
051
return (T)this.getHibernateTemplate().load(this.entityClass, id);
052
}
053

054
/**
055
* 把數據加載到指定的非持久化實例上
056
* @param entityObject
057
* @param id
058
*/
059
public void load(T entityObject,PK id){
060
this.getHibernateTemplate().load(entityObject, id);
061
}
062

063
/**
064
* 刪除對象.
065
*/
066
public void delete(T entityObject) {
067
this.getHibernateTemplate().delete(entityObject);
068
}
069

070
/**
071
* 根據id刪除對象
072
* @param id
073
*/
074
public void deleteById(PK id){
075
this.delete(this.getById(id));
076
}
077

078
/**
079
* 強迫裝載對象和它的集合,使用了觸發器的數據字段比較適合使用
080
* @param entityObject
081
*/
082
public void refresh(T entityObject){
083
this.getHibernateTemplate().refresh(entityObject);
084
}
085

086
/**
087
* 消除與 Hibernate Session 的關聯
088
* @param entityObject
089
*/
090
public void evict(T entityObject){
091
this.getHibernateTemplate().evict(entityObject);
092
}
093

094
/**
095
* 保存對象.<br>
096
* 如果對象已在本session中持久化了,不做任何事。<br>
097
* 如果另一個seesion擁有相同的持久化標識,拋出異常。<br>
098
* 如果沒有持久化標識屬性,調用save()。<br>
099
* 如果持久化標識表明是一個新的實例化對象,調用save()。<br>
100
* 如果是附帶版本信息的(version或timestamp)且版本屬性表明爲新的實例化對象就save()。<br>
101
* 否則調用update()重新關聯託管對象
102
* @param entityObject
103
*/
104
public void save(T entityObject){
105
this.getHibernateTemplate().saveOrUpdate(entityObject);
106
}
107

108
/**
109
* 如果對象已在本session中持久化了,覆蓋原有的<br>
110
* 如果session中沒有對應對象,從數據庫加載<br>
111
* 如果是脫管對象,則什麼都不做
112
* @param entityObject
113
* @return
114
*/
115
@SuppressWarnings("unchecked")
116
public T merge(T entityObject){
117
return (T)this.getHibernateTemplate().merge(entityObject);
118
}
119

120
}
SimpleJdbcDao.java

001
/**
002
*
003
*/
004
package com.tinylight.dao.jdbc;
005
import java.sql.Connection;
006
import java.sql.SQLException;
007
import org.springframework.jdbc.core.JdbcTemplate;
008
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
009
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
010
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
011
import org.springframework.jdbc.datasource.DataSourceUtils;
012
import org.springframework.jdbc.support.GeneratedKeyHolder;
013
import org.springframework.jdbc.support.KeyHolder;
014
import org.springframework.jdbc.support.rowset.SqlRowSet;
015
/**
016
* 繼承spring的SimpleJdbcDaoSupport
017
* 提供對數據庫jdbc級別的操作
018
* 內部使用spring的SimpleJdbcTemplate與JdbcTemplate
019
* @author scott.Cgi
020
* @since 2009-5-7
021
*/
022
public class SimpleJdbcDao extends SimpleJdbcDaoSupport {
023

024
/**
025
* 提供對錶的更改和刪除操作
026
* @param sql 要執行的sql語句
027
* @param args 變參
028
* @return 影響的行數
029
*/
030
public int update(String sql,Object...args){
031
return this.getSimpleJdbcTemplate().update(sql, args);
032
}
033

034
/**
035
* 批量更新多條記錄
036
* @param sql 多條sql組成的數組(不帶參數的)
037
* @see 帶參數的見: <br>
038
* getJdbcTemplate().batchUpdate(String[] sql,BatchPreparedStatementSetter pss)
039
* @return 影響行數數組
040
*/
041
public int[] batchUpdate(String[] sql){
042
return this.getJdbcTemplate().batchUpdate(sql);
043
}
044

045
/**
046
* 獲取行數
047
* @param countSql 計算行數的sql語句
048
* @return
049
*/
050
public long countRows(String countSql){
051
return this.getJdbcTemplate().queryForLong(countSql);
052
}
053

054
/**
055
* 獲取本地的Connection對象
056
* @return
057
*/
058
public Connection getNativeConn(){
059

060
//從當前線程綁定的數據連接獲取連接
061
Connection conn = DataSourceUtils.getConnection(this.getJdbcTemplate().getDataSource());
062
try {
063
conn = this.getJdbcTemplate().getNativeJdbcExtractor().getNativeConnection(conn);
064
} catch (SQLException e) {
065
e.printStackTrace();
066
return null;
067
}
068

069
return conn;
070
}
071

072
/**
073
* 獲得斷開數據庫連接的行集,大結果集會消耗內存,受到maxSize的限制
074
* @param sql 要執行的sql語句帶?佔位符
075
* @param params 填充佔位符的數組
076
* @param types 填充參數類型(java.sql.Types中的常量)
077
* 例如:new int[]{Types.VARCHAR,Types.DATE}
078
* @return 影響的行數<br>
079
* <b>注:</b> params和types同時爲空,sql爲不帶?佔位符;僅僅types爲空時,由spring去猜測類型
080
*/
081
public SqlRowSet queryForRowSet(String sql,Object[] params,int[] types){
082

083
if(params != null && types != null){
084
return this.getJdbcTemplate().queryForRowSet(sql, params, types);
085
}else if(params != null && types == null){
086
return this.getJdbcTemplate().queryForRowSet(sql, params);
087
}else {
088
return this.getJdbcTemplate().queryForRowSet(sql);
089
}
090
}
091

092
/**
093
* 提供對錶的更改和刪除操作
094
* @param hql 使用了命名參數的sql語句
095
* @param sps 例如:<br>
096
* new BeanPropertySqlParamterSource(javaBean其屬性要和命名參數對應);<br>
097
* new MapSqlParameterSource().add("命名參數",參數對應的值).add()...可以鏈式調用
098
* @return KeyHolder主鍵持有者對象;如果是新增數據,KeyHolder持有新增的主鍵值<br>
099
* 有3個方法可調用:<br>getKey()一個數字主鍵<br>
100
* getKeys()複合主鍵Map結構<br>
101
* getKeyList()多個主鍵由多個Map組成的List
102
*/
103
public KeyHolder updateNamedParamer(String hql,SqlParameterSource sps){
104
KeyHolder keyHolder = new GeneratedKeyHolder();
105
this.getSimpleJdbcTemplate().getNamedParameterJdbcOperations().update(hql, sps, keyHolder);
106
return keyHolder;
107
}
108

109
/**
110
* 執行sql語句,如創建表等
111
* @param sql
112
*/
113
public void executeSql(String sql){
114
this.getJdbcTemplate().execute(sql);
115
}
116

117
/**
118
* @return 獲得spring的JdbcTemplate使用更多功能
119
*/
120
public JdbcTemplate getSpringJdbcTemplate(){
121
return this.getJdbcTemplate();
122
}
123

124
/**
125
* 引入jdk5.0語法的JdbcTemplate的簡化版本
126
* @return 獲得spring的SimpleJdbcTemplate使用更多功能
127
*/
128
public SimpleJdbcTemplate getSpringSimplaJdbcTemplate(){
129
return this.getSimpleJdbcTemplate();
130
}
131
}
Page.java

001
package com.tinylight.dao.support;
002
import java.io.Serializable;
003
import java.util.Collections;
004
import java.util.List;
005
/**
006
* 分頁對象.包含當前頁數據及分頁信息如總記錄數.
007
*
008
* @author scott.Cgi
009
* @since 2008-6-29
010
*/
011
public class Page<T> implements Serializable {
012
private static final long serialVersionUID = -5624189033006412710L;
013
private static long DEFAULT_PAGE_SIZE = 20;
014
private long pageSize = DEFAULT_PAGE_SIZE; // 每頁的記錄數
015
private long start; // 當前頁第一條數據在List中的位置,從0開始
016
private List<T> data = Collections.emptyList(); // 當前頁中存放的記錄
017
private long totalCount = 0; // 總記錄數
018
/**
019
* 構造方法,只構造空頁.
020
*/
021
public Page() {
022
this(0l, 0l, DEFAULT_PAGE_SIZE, Collections.<T>emptyList());
023
}
024
/**
025
* 默認構造方法.
026
*
027
* @param start 本頁數據在數據庫中的起始位置
028
* @param totalSize 數據庫中總記錄條數
029
* @param pageSize 本頁容量
030
* @param data 本頁包含的數據
031
*/
032
public Page(long start, long totalSize, long pageSize, List<T> data) {
033
this.pageSize = pageSize;
034
this.start = start;
035
this.totalCount = totalSize;
036
this.data = data;
037
}
038
/**
039
* 取總記錄數.
040
*/
041
public long getTotalCount() {
042
return this.totalCount;
043
}
044
/**
045
* 取總頁數.
046
*/
047
public long getTotalPageCount() {
048
if (totalCount % pageSize == 0)
049
return totalCount / pageSize;
050
else
051
return totalCount / pageSize + 1;
052
}
053
/**
054
* 取每頁數據容量.
055
*/
056
public Long getPageSize() {
057
return pageSize;
058
}
059
/**
060
* 取當前頁中的記錄.
061
*/
062
public List<T> getResult() {
063
return data;
064
}
065
/**
066
* 取該頁當前頁碼,頁碼從1開始.
067
*/
068
public long getCurrentPageNo() {
069
return start / pageSize + 1;
070
}
071
/**
072
* 該頁是否有下一頁.
073
*/
074
public boolean hasNextPage() {
075
return this.getCurrentPageNo() < this.getTotalPageCount();
076
}
077
/**
078
* 該頁是否有上一頁.
079
*/
080
public boolean hasPreviousPage() {
081
return this.getCurrentPageNo() > 1;
082
}
083
/**
084
* 獲取任一頁第一條數據在數據集的位置,每頁條數使用默認值.
085
*
086
*/
087
protected static long getStartOfPage(long pageNo) {
088
return getStartOfPage(pageNo, DEFAULT_PAGE_SIZE);
089
}
090
/**
091
* 獲取任一頁第一條數據在數據集的位置.
092
*
093
* @param pageNo 從1開始的頁號
094
* @param pageSize 每頁記錄條數
095
* @return 該頁第一條數據
096
*/
097
public static long getStartOfPage(long pageNo, long pageSize) {
098
return (pageNo - 1) * pageSize;
099
}
100
}
applicationContext-annotation.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:dwr="http://www.directwebremoting.org/schema/spring-dwr"
05
xmlns:context="http://www.springframework.org/schema/context"
06
xmlns:aop="http://www.springframework.org/schema/aop"
07
xsi:schemaLocation="http://www.springframework.org/schema/beans
08

09
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
10

11
http://www.springframework.org/schema/context
12

13
http://www.springframework.org/schema/context/spring-context-2.5.xsd
14

15
http://www.directwebremoting.org/schema/spring-dwr
16

17
http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd
18

19
http://www.springframework.org/schema/aop
20

21
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
22

23
<context:annotation-config/>
24
</beans>
applicationContext-list.xml

01
<?xml version="1.0" encoding="UTF-8"?>
02
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
03
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
04
xmlns:context="http://www.springframework.org/schema/context"
05
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
06
<description>導入applicationContext文件列表,可隨時通過註釋disable不需要的模塊</description>
07
<!-- 導入spring aop配置文件 -->
08
<import resource="applicationContext-tx.xml" />
09
<!-- 導入spring dao配置文件 -->
10
<import resource="tinylight-dao.xml" />
11

12
</beans>
applicationContext-tx.xml

01
<?xml version="1.0" encoding="UTF-8"?>
02
<!-- 使用aop/tx命名空間 -->
03
<beans xmlns="http://www.springframework.org/schema/beans"
04
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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 http://www.springframework.org/schema/beans/spring-beans.xsd
08
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
09
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
10
<!-- 配置事務管理器bean,使用HibernateTransactionManager事務管理器 -->
11
<bean id="txManager"
12
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
13
<!-- 爲事務管理器注入sessionFactory" -->
14
<property name="sessionFactory">
15
<ref bean="sessionFactory" />
16
</property>
17
</bean>
18
<!-- 通過aop定義事務增強切面 -->
19
<aop:config>
20
<!-- 使用強大的切點表達式語言輕鬆定義目標方法 -->
21
<aop:pointcut id="serviceMethod"
22
expression="execution(* com.tinylight.dao.base.IBaseDao.*(..))" />
23
<!-- 引用事務增強 -->
24
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
25
</aop:config>
26
<!-- 事務增強 -->
27
<tx:advice id="txAdvice" transaction-manager="txManager">
28
<!-- 屬性事務定義 -->
29
<tx:attributes>
30
<!--OpenSessionInViewFilter在getSession的時候,會把獲取回來的session的flush mode 設爲FlushMode.NEVER。
31
然後把該sessionFactory綁定到TransactionSynchronizationManager,使request的整個過程都使用同一個session,
32
在請求過後再接除該sessionFactory的綁定,最後closeSessionIfNecessary根據該session是否已和transaction綁定來決定是否關閉session。
33
在這個過程中,若HibernateTemplate 發現自當前session有不是readOnly的transaction,就會獲取到FlushMode.AUTO Session,使方法擁有寫權限。
34
也即是,如果有不是readOnly的transaction就可以由Flush.NEVER轉爲Flush.AUTO,擁有insert,update,delete操作權限,如果沒有transaction,
35
並且沒有另外人爲地設flush model的話,則doFilter的整個過程都是Flush.NEVER。所以受transaction保護的方法有寫權限,沒受保護的則沒有 -->
36
<tx:method name="*" />
37
</tx:attributes>
38
</tx:advice>
39

40
</beans>
applicationContext.xml

001
<?xml version="1.0" encoding="UTF-8"?>
002
<beans xmlns="http://www.springframework.org/schema/beans"
003
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
004
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
005
<!-- 使用外部文件配置數據源的屬性 -->
006
<bean
007
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
008
<property name="locations">
009
<list>
010
<!-- 外部文件地址 -->
011
<value>classpath:database_conn.properties</value>
012
</list>
013
</property>
014
<property name="fileEncoding" value="utf-8" />
015
</bean>
016
<!-- 配置數據源 -->
017
<bean id="dataSource"
018
class="com.mchange.v2.c3p0.ComboPooledDataSource"
019
destroy-method="close">
020
<property name="driverClass" value="${driverClassName}" />
021
<property name="jdbcUrl" value="${url}"/>
022
<property name="user" value="${username}"/>
023
<property name="password" value="${password}"/>
024

025
<!--連接池中保留的最小連接數。-->
026
<property name="minPoolSize" value="5" />
027

028
<!--連接池中保留的最大連接數。Default: 15 -->
029
<property name="maxPoolSize" value="20" />
030

031
<!--初始化時獲取的連接數,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
032
<property name="initialPoolSize" value="10" />
033

034
<!--最大空閒時間,60秒內未使用則連接被丟棄。若爲0則永不丟棄。Default: 0 -->
035
<property name="maxIdleTime" value="60" />
036

037
<!--當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數。Default: 3 -->
038
<property name="acquireIncrement" value="5" />
039

040
<!--JDBC的標準參數,用以控制數據源內加載的PreparedStatements數量。但由於預緩存的statements
041
屬於單個connection而不是整個連接池。所以設置這個參數需要考慮到多方面的因素。
042
如果maxStatements與maxStatementsPerConnection均爲0,則緩存被關閉。Default: 0-->
043
<property name="maxStatements" value="0"/>
044
<!--每60秒檢查所有連接池中的空閒連接。Default: 0 -->
045
<property name="idleConnectionTestPeriod" value="60"/>
046

047
<!--定義在從數據庫獲取新連接失敗後重復嘗試的次數。Default: 30 -->
048
<property name="acquireRetryAttempts" value="0" />
049
<!--獲取連接失敗將會引起所有等待連接池來獲取連接的線程拋出異常。但是數據源仍有效
050
保留,並在下次調用getConnection()的時候繼續嘗試獲取連接。如果設爲true,那麼在嘗試
051
獲取連接失敗後該數據源將申明已斷開並永久關閉。Default: false-->
052
<property name="breakAfterAcquireFailure" value="true" />
053

054
<!--因性能消耗大請只在需要的時候使用它。如果設爲true那麼在每個connection提交的
055
時候都將校驗其有效性。建議使用idleConnectionTestPeriod或automaticTestTable
056
等方法來提升連接測試的性能。Default: false -->
057
<property name="testConnectionOnCheckout" value="false" />
058
</bean>
059
<!-- hibernate3 sessionfactory配置
060
使用AnnotationSessionFactoryBean創建基於JPA註解的SessionFactory-->
061
<bean id="sessionFactory"
062
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
063
<!-- 引用數據源 -->
064
<property name="dataSource">
065
<ref bean="dataSource" />
066
</property>
067
<property name="namingStrategy">
068
<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
069
</property>
070
<!-- hibernate的屬性具體見hibernate文檔 -->
071
<property name="hibernateProperties">
072
<props>
073
<prop key="hibernate.dialect">
074
org.hibernate.dialect.MySQLDialect
075
</prop>
076
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
077
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
078
<!-- 非0值 指定jdbc抓取數量的大小調用Statement.setFetchSize() -->
079
<prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
080
<!-- jdbc批量更新 建議5到30 -->
081
<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
082
<!--說明一下:如果不設置查詢緩存,那麼hibernate只會緩存使用load()方法獲得的單個持久化對象
083
如果想緩存使用findall()、list()、Iterator()、createCriteria()、createQuery()
084
等方法獲得的數據結果集的話就需要設置hibernate.cache.use_query_cache true 才行 -->
085
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
086
<prop key="hibernate.cache.provider_class">
087
org.hibernate.cache.EhCacheProvider
088
</prop>
089
<prop key="hibernate.cache.provider_configuration_file_resource_path">
090
${hibernate.ehcache_config_file}
091
</prop>
092
</props>
093
</property>
094

095
<property name="packagesToScan" value="com.hibernate.entityclass" />
096
</bean>
097

098
<!-- 加載spring功能列表文件 -->
099
<import resource="applicationContext-list.xml"/>
100
</beans>
database_conn.properties

01
#jdbc settings
02
driverClassName=org.gjt.mm.mysql.Driver
03
url=jdbc\:mysql\://127.0.0.1\:3306/mytest
04
username=root
05
password=123456
06
#hibernate settings
07
hibernate.show_sql=true
08
hibernate.format_sql=true
09
hibernate.cache.use_query_cache=false
10
hibernate.jdbc.fetch_size=10
11
hibernate.jdbc.batch_size=5
12
hibernate.ehcache_config_file=ehcache.xml
ehcache.xml

01
<ehcache>
02
<!-- Sets the path to the directory where cache .data files are created.
03

04
If the path is a Java System Property it is replaced by
05
its value in the running VM.
06

07
The following properties are translated:
08
user.home - User's home directory
09
user.dir - User's current working directory
10
java.io.tmpdir - Default temp file path -->
11
<diskStore path="java.io.tmpdir" />
12
<!--Default Cache configuration. These will applied to caches programmatically created through
13
the CacheManager.
14

15
The following attributes are required:
16

17
maxElementsInMemory - Sets the maximum number of objects that will be created in memory
18
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the
19
element is never expired.
20
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
21
has reached the maxInMemory limit.
22

23
The following attributes are optional:
24
timeToIdleSeconds - Sets the time to idle for an element before it expires.
25
i.e. The maximum amount of time between accesses before an element expires
26
Is only used if the element is not eternal.
27
Optional attribute. A value of 0 means that an Element can idle for infinity.
28
The default value is 0.
29
timeToLiveSeconds - Sets the time to live for an element before it expires.
30
i.e. The maximum time between creation time and when an element expires.
31
Is only used if the element is not eternal.
32
Optional attribute. A value of 0 means that and Element can live for infinity.
33
The default value is 0.
34
diskPersistent - Whether the disk store persists between restarts of the Virtual Machine.
35
The default value is false.
36
diskExpiryThreadIntervalSeconds- The number of seconds between runs of the disk expiry thread. The default value
37
is 120 seconds.
38
-->
39
<cache name="org.hibernate.cache.StandardQueryCache"
40
maxElementsInMemory="50"
41
eternal="false"
42
timeToIdleSeconds="3600"
43
timeToLiveSeconds="7200"
44
overflowToDisk="true" />
45
<cache name="org.hibernate.cache.UpdateTimestampsCache"
46
maxElementsInMemory="5000"
47
eternal="true"
48
overflowToDisk="true" />
49
<!--
50
默認的Cache配置。用來實現CacheManager.add(String cacheName)創建的緩存
51
memoryStoreEvictionPolicy:內存存儲與釋放策略。有三個值:
52
LRU -least recently used
53
LFU -least frequently used
54
FIFO-first in first out, the oldest element by creation time
55
diskPersistent :是否持久化磁盤緩存。
56
當這個屬性的值爲true時,系統在初始化的時候會在磁盤中查找文件名爲cache名稱
57
後綴名爲index的的文件,如CACHE_FUNC.index
58
-->
59
<defaultCache maxElementsInMemory="10000"
60
eternal="false"
61
timeToIdleSeconds="120"
62
timeToLiveSeconds="120"
63
overflowToDisk="true"
64
diskPersistent="false"
65
diskExpiryThreadIntervalSeconds="120"
66
memoryStoreEvictionPolicy="LFU"
67
/>
68
<!-- See http://ehcache.sourceforge.net/documentation/#mozTocId258426 for how to configure caching for your objects -->
69
</ehcache>
log4j.properties

01
log4j.rootLogger=INFO, stdout, logfile
02
log4j.logger.org.springframework=WARN
03
log4j.logger.org.hibernate=WARN
04
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
05
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
06
log4j.appender.stdout.layout.ConversionPattern=[%d] %-5p \: %m%n
07
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
08
log4j.appender.logfile.File=${webAppRootKey}/superDao.log
09
log4j.appender.logfile.MaxFileSize=512KB
10
# Keep three backup files.
11
log4j.appender.logfile.MaxBackupIndex=3
12
# Pattern to output: date priority [category] - message
13
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
14
log4j.appender.logfile.layout.ConversionPattern=[%d] %p [%c] - %m%n
tinyLight-dao.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
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
05
<description>tinyLight Dao配置</description>
06
<!-- 定義JdbcTemplate Bean(線程安全,執行底層sql語句) -->
07
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
08
<!-- 設置數據源 -->
09
<property name="dataSource" ref="dataSource" />
10
<!-- 設置jdbcTemplate創建的statement查詢數據時最大的超時時間,默認0 -->
11
<property name="queryTimeout" value="0" />
12
<!-- 設置底層ResultSet每次從數據庫返回的行數,默認0,Oracle默認100 -->
13
<property name="fetchSize" value="0" />
14
<!-- 設置底層ResultSet從數據庫返回的最大行數,默認0 -->
15
<property name="maxRows" value="0" />
16
<!-- 是否忽略sql警告信息,默認true,false JdbcTemplate將會拋出SQLWarningException -->
17
<property name="ignoreWarnings" value="true" />
18
<property name="nativeJdbcExtractor" ref="nativeJdbcExtractor" />
19
</bean>
20
<!-- 定義DBCP數據源的JDBC本地對象抽取器 -->
21
<bean id="nativeJdbcExtractor" lazy-init="true"
22
class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor" />
23
<!-- 定義封裝了jdbc操作數據的dao -->
24
<bean id="simpleJdbcDao" class="com.tinylight.dao.jdbc.SimpleJdbcDao">
25
<property name="jdbcTemplate" ref="jdbcTemplate" />
26
</bean>
27
<!-- 定義了與實體相關的dao -->
28
<bean id="genericEntityDao" class="com.tinylight.dao.hibernate.GenericEntityDao"
29
scope="prototype" lazy-init="true">
30
<property name="sessionFactory">
31
<ref bean="sessionFactory" />
32
</property>
33
</bean>
34
<!-- 定義了與實體無關的dao -->
35
<bean id="genericDao" class="com.tinylight.dao.hibernate.GenericDao">
36
<property name="sessionFactory">
37
<ref bean="sessionFactory" />
38
</property>
39
</bean>
40
<!--使用泛型DAO作爲抽象基類-->
41
<bean id="baseDao" class="com.tinylight.dao.base.BaseDao"
42
abstract="true" depends-on="genericEntityDao,genericDao">
43
<property name="gedao">
44
<ref bean="genericEntityDao" />
45
</property>
46
<property name="gdao">
47
<ref bean="genericDao" />
48
</property>
49
<property name="sjdao">
50
<ref bean="simpleJdbcDao"/>
51
</property>
52
</bean>
53
<!--
54
當然這裏也可以不必爲每一個實體類的配置像下面這樣的代碼,
55
那就要在spring註解注入dao到service類的時候,加上下面這個方法:
56
/**
57
* spring bean的後處理器,如果子類是spring初始化的bean,則在初始化後設置註解dao的實體類類型
58
* @throws IllegalArgumentException
59
* @throws IllegalAccessException
60
*/
61
@PostConstruct
62
@SuppressWarnings("unchecked")
63
protected void preparedDao() throws IllegalArgumentException, IllegalAccessException{
64
Class<? extends BaseService> cl = this.getClass();
65
for(Field f : cl.getDeclaredFields()){
66
log.info("Service類註解屬性名稱 = {},類型 = {}",f.getName(),f.getGenericType());
67
if(f.isAnnotationPresent(Resource.class) && f.getAnnotation(Resource.class).name().equals("baseDao")){
68
f.setAccessible(true);//修改private修飾權限
69
Type[] params = ((ParameterizedType)f.getGenericType()).getActualTypeArguments();
70
log.info("實體類的參數類型 = {}",params[0].toString());
71
((IBaseDao<?,?>)f.get(this)).changeEntityClass((Class)params[0]);//設置實體類類型
72
f.setAccessible(false);//恢復private修飾權限
73
}
74
}
75
}
76
這樣就可以爲每一個實體類加下面這段xml配置了,當然dao是不在學要寫一條代碼啦
77
-->
78

79
<!-- 配置實體Demodata的DAO -->
80
<bean id="demoDao" parent="baseDao">
81
<constructor-arg>
82
<value>com.hibernate.entityclass.Daotest</value>
83
</constructor-arg>
84
</bean>
85

86
</beans>


這個svn有源代碼,有興趣可以看看.
http://xeducation.googlecode.com/svn/trunk
是myeclipse的工程


Share this on: Mixx Delicious Digg Facebook Twitter
DAOHibernateJDBCSpring
Related StoriesMost Popular
Linux系統信息查看命令大全
MySQL架構方案
Struts2 註解功能
Struts2 零配置方法總結
Spring與Memcache的集成
Apache Mina ProtocolCodecFilter Introduction
Leave a Reply
Name *


Email *


Website


Comment


有人回覆時郵件通知我
發佈了38 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章