flio
::
首頁
::
聯系
:: ::
管理
0 Posts :: 6 Stories :: 6 Comments :: 0 Trackbacks
留言簿
(2)
給我留言
查看公開留言
查看私人留言
文章分類
信息安全
前端技術(2)
框架技術(2)
軟件開發
軟件架構
文章檔案
2012年5月 (4)
搜索
最新評論
1.?re: ajax+json渲染html
54
--14
2.?re: ajax+json渲染html
4556fd
--7252
3.?re: SpringMVC+hibernate3(2.mvc三層實現)
評論內容較長,點擊標題查看
--zuidaima
4.?re: SpringMVC+hibernate3(2.mvc三層實現) [未登錄]
期待樓主 出整合hibernate的代碼啊
--ken
5.?re: SpringMVC+hibernate3(2.mvc三層實現)
哥又頂了
--x7
SpringMVC+hibernate3(2.mvc三層實現)
本來準備直接寫srping+hibernate的集成了,不過為了讓自己鞏固知識,為了讓看到文章的更了解流程,所以把最開始搭建的流程走下。
上篇講到了springmvc實現了控制層的helloworld實例,這篇接著寫service層,和dao層的整合。
這里會需要幾個配置文件,applicationContext.xml,test-service.xml,test-dao.xml
applicationContext主要就是包含后面兩個文件了。其實這三個文件可以寫在一個配置文件中,不過為了更好的分離,所以拆開了dao的配置文件,和service的配置文件。
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
"
xsi:schemaLocation
=
"
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http:
//
www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
"
>
<!--
數據訪問層配置
-->
<
import
resource
=
"
classpath:/configSource/test-dao.xml
"
/>
<!--
服務層配置
-->
<
import
resource
=
"
classpath:/configSource/test-service.xml
"
/>
</
beans
>
test-dao.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:tx
=
"
http://www.springframework.org/schema/tx
"
xmlns:context
=
"
http://www.springframework.org/schema/context
"
xmlns:p
=
"
http://www.springframework.org/schema/p
"
xsi:schemaLocation
=
"
http:
//
www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http:
//
www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http:
//
www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
">
<!--
掃描com.baobaotao.dao包下所有標注@Repository的DAO組件
-->
<
context:component
-
scan base
-
package
=
"
com.Integrat.*.dao
"
/>
</
beans
>
test-service.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:tx
=
"
http://www.springframework.org/schema/tx
"
xmlns:aop
=
"
http://www.springframework.org/schema/aop
"
xmlns:context
=
"
http://www.springframework.org/schema/context
"
xmlns:p
=
"
http://www.springframework.org/schema/p
"
xsi:schemaLocation
=
"
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http:
//
www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http:
//
www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http:
//
www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
<!--
掃描com.baobaotao.service包下所有標注@Service的服務組件
-->
<
context:component
-
scan base
-
package
=
"
com.Integrat.*.service
"
/>
</
beans
>
test-dao.xml,test-service.xml 都是經過了簡單的配置,并不是完整的,請注意,只是演示一個配置的過程,一步步的配置,記下每一步的作用,了解下原理。
可以看到上面兩個配置文件每個里面都只有1行語句,就是自動掃描注解的service和dao,如果這里不配置的話,在service和dao類上注解了也沒法解析,可能爆出bean創建錯誤
注意賦值的時候,需要改下目錄結構(com.Integrat.*.service 這里改成自己的service路徑*表示通配符)
配置好了就開始寫代碼吧,首先寫dao層吧代碼如下:
@Repository
public
class
DemoDao
extends
BaseDao
<
AdminDicVO
>
{
public
void
demo()
{
System.out.println(
"
demoDAO執行了
"
);
}
}
如果調用了dao層會簡單在控制臺輸出一段話,值得注意的是在類名上面要寫上注解@Repository ,表示這是dao層
接著寫service層代碼如下:
@Service
public
class
DemoService
{
@Autowired
private
DemoDao dao;
public
void
Test()
{
System.out.println(
"
service調用dao層
"
);
dao.demo();
}
}
這里需要注意的是在類名上面需要寫上@service注解,表示這是一個service,在private DemoDao dao; 上面要寫上注解@Autowired,自動注入這個dao,
接下來就看controller層代碼了:
@Controller
@RequestMapping(
"
/demo
"
)
public
class
DemoController
{
@Autowired
private
DemoService service;
//
這里注入service
@RequestMapping(
"
/demo1
"
)
public
String demo()
{
System.out.println(
"
controller執行demo,調用service
"
);
service.Test();
return
null
;
}
}
就這么簡單的代碼就可以通過controller調用service,通過service調用dao層了
接下來就訪問下
http://localhost:8080/
項目名/demo/demo1.test 就可以看到控制臺打印出的數據了。
這樣mvc3層就結合起來了。往后,只需要在在dao層加入hibernate,封裝basedao,在service層配置事務,就ok了。
整合hibernate待續...
posted on 2012-05-30 20:33
flio
閱讀(2084)
評論(3)
編輯
收藏
所屬分類:
框架技術
Feedback
#
re: SpringMVC+hibernate3(2.mvc三層實現)
2012-05-31 08:34
x7
哥又頂了
回復
更多評論
#
re: SpringMVC+hibernate3(2.mvc三層實現) [未登錄]
2012-09-19 10:51
ken
期待樓主 出整合hibernate的代碼啊
回復
更多評論
#
re: SpringMVC+hibernate3(2.mvc三層實現)
2015-02-13 22:46
zuidaima
spring mvc demo教程源代碼下載:
http://zuidaima.com/share/kspringmvc-p1-s1.htm
回復
更多評論
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關文章:
SpringMVC+hibernate3(2.mvc三層實現)
SpringMVC+hibernate3(1.讓springmvc跑起來)
Copyright @ flio
Powered by:
.Text
and
ASP.NET
Theme by:
.NET Monster
主站蜘蛛池模板:
马尔康县
|
商水县
|
永康市
|
文化
|
富顺县
|
开原市
|
弥勒县
|
东海县
|
陆丰市
|
长顺县
|
梁山县
|
分宜县
|
天门市
|
万安县
|
波密县
|
曲麻莱县
|
门头沟区
|
奎屯市
|
称多县
|
蓬安县
|
沂水县
|
惠安县
|
台南县
|
柳州市
|
聊城市
|
和田县
|
拉孜县
|
南陵县
|
都兰县
|
西华县
|
怀来县
|
小金县
|
宜兰县
|
海安县
|
湄潭县
|
永平县
|
灵宝市
|
黄陵县
|
正定县
|
阳城县
|
鹿泉市
|