前面的文章:
struts+spring+hibernate
的
web
應(yīng)用
<
一
>
架構(gòu)搭建
struts+spring+hibernate
的
web
應(yīng)用
<
二
> Dao
層代碼編寫
?
現(xiàn)在開始編寫
Service
層代碼:
在
com.game.products.services.iface
包中新建
ProductsService
接口,代碼如下:
?
package
?com.game.products.services.iface;

import
?java.util.List;

import
?com.game.products.model.Products;


public
?
interface
?ProductsService?
{
????
void
?addProduct(Products?pd);
//
添加記錄
????
void
?deleteProduct(Products?pd);
//
刪除記錄????
????List?getProducts();
//
獲得所有記錄
????
int
?getRows();;
//
獲得總行數(shù)
????List?getProducts(
int
?pageSize,?
int
?startRow)?;
//
獲得一段記錄
????Products?getProduct(String?gameId);
//
根據(jù)ID獲得記錄
????String?getMaxID();
//
獲得最大ID值
????
void
?updateProductd(Products?pd);
//
修改記錄
????List?queryProducts(String?fieldname,String?value);
//
根據(jù)條件查詢的所有記錄
????
int
?getRows(String?fieldname,String?value);
//
獲得總行數(shù)
????List?queryProducts(String?fieldname,String?value,
int
?pageSize,?
int
?startRow);
//
根據(jù)條件查詢的一段記錄
}
在
com.game.products.services
包中新建
ProductsServiceImp
類,這個(gè)類實(shí)現(xiàn)了
ProductsService
接口,代碼如下:
package
?com.game.products.services;

import
?java.util.List;

import
?com.game.products.dao.iface.ProductsDao;
import
?com.game.products.model.Products;
import
?com.game.products.services.iface.ProductsService;


public
?
class
?ProductsServiceImp?
implements
?ProductsService
{
????
private
?ProductsDao?productsDao;
????

????
public
?ProductsServiceImp()
{}
????

????
/**?*/
/**
?????*?函數(shù)說明:添加信息
?????*?參數(shù)說明:對(duì)象?
?????*?返回值:
?????
*/
????
public
?
void
?addProduct(Products?pd)?
{
????????productsDao.addProduct(pd);
????}
????
/**?*/
/**
?????*?函數(shù)說明:刪除信息
?????*?參數(shù)說明:?對(duì)象
?????*?返回值:
?????
*/
????
public
?
void
?deleteProduct(Products?pd)?
{
????????productsDao.deleteProduct(pd);
????}
????
/**?*/
/**
?????*?函數(shù)說明:獲得所有的信息
?????*?參數(shù)說明:?
?????*?返回值:信息的集合
?????
*/
????
public
?List?getProducts()?
{
????????
return
?productsDao.getProducts();
????}
????

????
/**?*/
/**
?????*?函數(shù)說明:獲得總行數(shù)
?????*?參數(shù)說明:?
?????*?返回值:總行數(shù)
?????
*/
????
public
?
int
?getRows()?
{
????????
return
?productsDao.getRows();
????}
????

????
/**?*/
/**
?????*?函數(shù)說明:獲得一段信息
?????*?參數(shù)說明:?
?????*?返回值:信息的集合
?????
*/
????
public
?List?getProducts(
int
?pageSize,?
int
?startRow)?
{
????????
return
?productsDao.getProducts(pageSize,?startRow);
????}
????
/**?*/
/**
?????*?函數(shù)說明:獲得一條的信息
?????*?參數(shù)說明:?ID
?????*?返回值:對(duì)象
?????
*/
????
public
?Products?getProduct(String?gameId)?
{
????????
return
?productsDao.getProduct(gameId);
????}
????
/**?*/
/**
?????*?函數(shù)說明:獲得最大ID
?????*?參數(shù)說明:?
?????*?返回值:最大ID
?????
*/
????
public
?String?getMaxID()?
{
????????
return
?productsDao.getMaxID();
????}
????
/**?*/
/**
?????*?函數(shù)說明:修改信息
?????*?參數(shù)說明:?對(duì)象
?????*?返回值:
?????
*/
????
public
?
void
?updateProductd(Products?pd)?
{
????????productsDao.updateProductd(pd);
????}
????
/**?*/
/**
?????*?函數(shù)說明:查詢信息
?????*?參數(shù)說明:?集合
?????*?返回值:
?????
*/
????
public
?List?queryProducts(String?fieldname,String?value)?
{
????????
return
?productsDao.queryProducts(fieldname,?value);
????}
????

????
/**?*/
/**
?????*?函數(shù)說明:獲得總行數(shù)
?????*?參數(shù)說明:?
?????*?返回值:總行數(shù)
?????
*/
????
public
?
int
?getRows(String?fieldname,String?value)?
{
????????
return
?productsDao.getRows(fieldname,?value);
????}
????

????
/**?*/
/**
?????*?函數(shù)說明:查詢一段信息
?????*?參數(shù)說明:?集合
?????*?返回值:
?????
*/
????
public
?List?queryProducts(String?fieldname,String?value,
int
?pageSize,?
int
?startRow)?
{
????????
return
?productsDao.queryProducts(fieldname,?value,pageSize,startRow);
????}
????
public
?ProductsDao?getProductsDao()?
{
????????
return
?productsDao;
????}
????
public
?
void
?setProductsDao(ProductsDao?productsDao)?
{
????????
this
.productsDao?
=
?productsDao;
????}
}
?
基本的業(yè)務(wù)層代碼就這些了。因?yàn)檫€有分頁的業(yè)務(wù),所以接下來編寫分頁的代碼。
分頁是個(gè)公共的類,所以放在
com.game.commons
中。
Pager
類,封裝了分頁需要的屬性,代碼如下:
package
?com.game.commons;

import
?java.math.
*
;


public
?
class
?Pager?
{
????
private
?
int
?totalRows;?
//
總行數(shù)
????
private
?
int
?pageSize?
=
?
30
;?
//
每頁顯示的行數(shù)
????
private
?
int
?currentPage;?
//
當(dāng)前頁號(hào)
????
private
?
int
?totalPages;?
//
總頁數(shù)
????
private
?
int
?startRow;?
//
當(dāng)前頁在數(shù)據(jù)庫(kù)中的起始行
????

????
public
?Pager()?
{
????}
????

????
public
?Pager(
int
?_totalRows)?
{
????????totalRows?
=
?_totalRows;
????????totalPages
=
totalRows
/
pageSize;
????????
int
?mod
=
totalRows
%
pageSize;

????????
if
(mod
>
0
)
{
????????????totalPages
++
;
????????}
????????currentPage?
=
?
1
;
????????startRow?
=
?
0
;
????}
????

????
public
?
int
?getStartRow()?
{
????????
return
?startRow;
????}
????
public
?
int
?getTotalPages()?
{
????????
return
?totalPages;
????}
????
public
?
int
?getCurrentPage()?
{
????????
return
?currentPage;
????}
????
public
?
int
?getPageSize()?
{
????????
return
?pageSize;
????}
????
public
?
void
?setTotalRows(
int
?totalRows)?
{
????????
this
.totalRows?
=
?totalRows;
????}
????
public
?
void
?setStartRow(
int
?startRow)?
{
????????
this
.startRow?
=
?startRow;
????}
????
public
?
void
?setTotalPages(
int
?totalPages)?
{
????????
this
.totalPages?
=
?totalPages;
????}
????
public
?
void
?setCurrentPage(
int
?currentPage)?
{
????????
this
.currentPage?
=
?currentPage;
????}
????
public
?
void
?setPageSize(
int
?pageSize)?
{
????????
this
.pageSize?
=
?pageSize;
????}
????
public
?
int
?getTotalRows()?
{
????????
return
?totalRows;
????}
????
public
?
void
?first()?
{
????????currentPage?
=
?
1
;
????????startRow?
=
?
0
;
????}
????
public
?
void
?previous()?
{

????????
if
?(currentPage?
==
?
1
)?
{
????????????
return
;
????????}
????????currentPage
--
;
????????startRow?
=
?(currentPage?
-
?
1
)?
*
?pageSize;
????}
????
public
?
void
?next()?
{

????????
if
?(currentPage?
<
?totalPages)?
{
????????????currentPage
++
;
????????}
????????startRow?
=
?(currentPage?
-
?
1
)?
*
?pageSize;
????}
????
public
?
void
?last()?
{
????????currentPage?
=
?totalPages;
????????startRow?
=
?(currentPage?
-
?
1
)?
*
?pageSize;
????}
????
public
?
void
?refresh(
int
?_currentPage)?
{
????????currentPage?
=
?_currentPage;

????????
if
?(currentPage?
>
?totalPages)?
{
????????????last();
????????}
????}
}
?
PagerService
類,主要有個(gè)
getPager
方法返回
Pager
類。代碼如下:
package
?com.game.commons;


public
?
class
?PagerService?
{

????
public
?Pager?getPager(String?currentPage,String?pagerMethod,
int
?totalRows)?
{
????????
//
????定義pager對(duì)象,用于傳到頁面
????????Pager?pager?
=
?
new
?Pager(totalRows);
????????
//
????如果當(dāng)前頁號(hào)為空,表示為首次查詢?cè)擁?br />
????????
//
????如果不為空,則刷新pager對(duì)象,輸入當(dāng)前頁號(hào)等信息
????????
if
?(currentPage?
!=
?
null
)?
{
????????????pager.refresh(Integer.parseInt(currentPage));
????????}
????????
//
????獲取當(dāng)前執(zhí)行的方法,首頁,前一頁,后一頁,尾頁。
????????
if
?(pagerMethod?
!=
?
null
)?
{

????????????
if
?(pagerMethod.equals(
"
first
"
))?
{
????????????????pager.first();

????????????}
?
else
?
if
?(pagerMethod.equals(
"
previous
"
))?
{
????????????????pager.previous();

????????????}
?
else
?
if
?(pagerMethod.equals(
"
next
"
))?
{
????????????????pager.next();

????????????}
?
else
?
if
?(pagerMethod.equals(
"
last
"
))?
{
????????????????pager.last();
????????????}
????????}
????????
return
?pager;
????}
}
?
?
這個(gè)分頁方法比較簡(jiǎn)單,而且功能也齊全,許多頁面級(jí)的開源
table
中分頁很多也是基于這個(gè)原理,所以理解了這個(gè)分頁,對(duì)其他各種分頁技術(shù)的理解也就迎刃而解了。
?
服務(wù)層的代碼就這些了,接下來就可以寫
spring
的配置文件來用
spring
管理這些
Dao
和
Service
了。
在
spring-context
包中新建
applicationContext.xml
。配置的寫法如下:
<?
xml?version="1.0"?encoding="ISO-8859-1"
?>
<!
DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"?"http://www.springframework.org/dtd/spring-beans.dtd"
>
?
<
beans
>
????
<!--
?dataSource?config?
-->
????
<
bean?
id
="dataSource"
?class
="org.springframework.jndi.JndiObjectFactoryBean"
>
????????
<
property?
name
="jndiName"
>
????????????
<
value
>
java:comp/env/jdbc/game
</
value
>
????????
</
property
>
????
</
bean
>
<!--
?SessionFactory?
-->
????
<
bean?
id
="sessionFactory"
????????class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
>
????????
<
property?
name
="dataSource"
>
????????????
<
ref?
bean
="dataSource"
/>
????????
</
property
>
????????
<
property?
name
="configLocation"
>
????????????
<
value
>
classpath:com\game\bean\hibernate\hibernate.cfg.xml
</
value
>
????????
</
property
>
????????
????
</
bean
>
????
????
<!--
?TransactionManager??
-->
????
<
bean?
id
="transactionManager"
????????class
="org.springframework.orm.hibernate3.HibernateTransactionManager"
>
????????
<
property?
name
="sessionFactory"
>
????????????
<
ref?
local
="sessionFactory"
/>
????????
</
property
>
????
</
bean
>
<!--
?DAO?
-->
????
<
bean?
id
="productsDao"
?class
="com.game.products.dao.hibernate.ProductsMapDao"
>
????????
<
property?
name
="sessionFactory"
>
????????????
<
ref?
bean
="sessionFactory"
/>
????????
</
property
>
????
</
bean
>
????
????
<!--
?Services?
-->
?????
<
bean?
id
="productsService"
?class
="com.game.products.services.ProductsServiceImp"
>
????????
<
property?
name
="productsDao"
>
????????????
<
ref?
bean
="productsDao"
/>
????????
</
property
>
????
</
bean
>
?
<
bean?
id
="pagerService"
?class
="com.game.commons.PagerService"
>
????
</
bean
>
</beans>
?
配置文件不難,主要是些
IOC
控制。數(shù)據(jù)庫(kù)鏈接我采用的是數(shù)據(jù)源方式,需要在
tomcat
的conf文件夾下的
server.xml
中添加數(shù)據(jù)源,添加的數(shù)據(jù)如下:
<
Context?
path
="/game"
?docBase
="D:\tomcat-5.5.20\webapps\game"
?debug
="0"
?reloadable
="true"
>
????????
<
Resource
????????????
name
="jdbc/game"
????????????type
="javax.sql.DataSource"
????????????password
=""
????????????driverClassName
="net.sourceforge.jtds.jdbc.Driver"
????????????maxIdle
="2"
????????????maxWait
="5000"
????????????username
="sa"
????????????url
="jdbc:jtds:sqlserver://127.0.0.1:16899/game"
????????????maxActive
="4"
/>
????????
</
Context
>
?
這個(gè)數(shù)據(jù)源是針對(duì)
tomcat 5.5
以上版本的,以下版本的寫法有所不同,不同之處可以用
google
搜索得知。這個(gè)數(shù)據(jù)源很簡(jiǎn)單,并沒有過多的配置來優(yōu)化系統(tǒng),只是為了讓項(xiàng)目更容易讓人理解。需要注意都是,我的數(shù)據(jù)鏈接的JDBC包是jtds包,而不是普通的那個(gè)三個(gè)jar包。
?
下一篇,將寫
Web
層的代碼了。
struts+spring+hibernate
的
web
應(yīng)用
<
四
> Web
層代碼編寫(1)
struts+spring+hibernate
的
web
應(yīng)用
<
四
> Web
層代碼編寫(2)
posted on 2007-03-13 16:52
千山鳥飛絕 閱讀(14210)
評(píng)論(11) 編輯 收藏 所屬分類:
Web開發(fā)