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
阅读(2083)
评论(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