[首页]
[文章]
[教程]
首页
Web开发
Windows开发
编程语言
数据库技术
移动平台
系统服务
微信
设计
布布扣
其他
数据分析
首页
>
编程语言
> 详细
Spring+SpringMVC+MyBatis+Maven框架整合
时间:
2016-05-06 15:15:25
阅读:
150
评论:
0
收藏:
0
[点我收藏+]
本文记录了Spring+SpringMVC+MyBatis+Maven框架整合的记录,主要记录以下几点
一、Maven需要引入的jar包
二、Spring与SpringMVC的配置分离
三、Spring与MyBatis的整合
一、Maven需要引入的jar包
本文默认读者已经掌握Maven的使用,Maven配置片段如下
Xml代码
<!-- 引入spring-webmvc与spring-jdbc -->
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-webmvc
</
artifactId
>
<
version
>
${springframework.version}
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-jdbc
</
artifactId
>
<
version
>
${springframework.version}
</
version
>
</
dependency
>
<!-- 引入mybatis与mybatis-spring整合包 -->
<
dependency
>
<
groupId
>
org.mybatis
</
groupId
>
<
artifactId
>
mybatis
</
artifactId
>
<
version
>
${mybatis.version}
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.mybatis
</
groupId
>
<
artifactId
>
mybatis-spring
</
artifactId
>
<
version
>
${mybatis-spring.version}
</
version
>
</
dependency
>
<!-- 引入oracle数据库jdbc驱动包 -->
<
dependency
>
<
groupId
>
com.oracle
</
groupId
>
<
artifactId
>
ojdbc14
</
artifactId
>
<
version
>
${oracle14.version}
</
version
>
</
dependency
>
<!-- 引入c3p0连接池依赖包 -->
<
dependency
>
<
groupId
>
c3p0
</
groupId
>
<
artifactId
>
c3p0
</
artifactId
>
<
version
>
${c3p0.version}
</
version
>
</
dependency
>
二、Spring与SpringMVC的配置分离
1、有必要说明一下,web.xml中配置的执行顺序:
listener>filter>servlet,而同一种配置片段则按照从上到下的顺序执行。
下载地址
主流的Java后台 SSM 框架 springmvc spring mybatis 项目
2、web.xml的配置片段,下面的配置信息将Spring与SpringMVC的配置分别放到了applicationContext*.xml和springmvc-servlet.xml
Xml代码
<!-- 配置spring-web上下文监听器 -->
<
listener
>
<
listener-class
>
org.springframework.web.context.ContextLoaderListener
</
listener-class
>
</
listener
>
<!-- 配置需要读取的spring配置文件路径 -->
<!-- classpath*表示读取多个classpath -->
<!-- applicationContext*表示匹配多个applicationContext开头的spring配置文件 -->
<
context-param
>
<
param-name
>
contextConfigLocation
</
param-name
>
<
param-value
>
classpath*:applicationContext*.xml
</
param-value
>
</
context-param
>
<!-- 配置springmvc的DispatcherServlet,处理所有.do结尾的url -->
<
servlet
>
<
servlet-name
>
springmvc
</
servlet-name
>
<
servlet-class
>
org.springframework.web.servlet.DispatcherServlet
</
servlet-class
>
<!-- 配置springmvc的配置文件路径 -->
<
init-param
>
<
param-name
>
contextConfigLocation
</
param-name
>
<
param-value
>
classpath:springmvc-servlet.xml
</
param-value
>
</
init-param
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>
springmvc
</
servlet-name
>
<
url-pattern
>
*.do
</
url-pattern
>
</
servlet-mapping
>
<!-- 配置springmvc编码拦截器 -->
<
filter
>
<
filter-name
>
encodingFilter
</
filter-name
>
<
filter-class
>
org.springframework.web.filter.CharacterEncodingFilter
</
filter-class
>
<
init-param
>
<
param-name
>
encoding
</
param-name
>
<
param-value
>
utf-8
</
param-value
>
</
init-param
>
<
init-param
>
<
param-name
>
forceEncoding
</
param-name
>
<
param-value
>
true
</
param-value
>
</
init-param
>
</
filter
>
<
filter-mapping
>
<
filter-name
>
encodingFilter
</
filter-name
>
<
url-pattern
>
/*
</
url-pattern
>
</
filter-mapping
>
3、springmvc的配置片段如下,springmvc-servlet.xml
Xml代码
<!-- 自动扫描注解,只扫描的Controller注解,其它注解的扫描交给spring去处理 -->
<
context:component-scan
base-package
=
"org.jisonami.controller"
>
<
context:include-filter
type
=
"annotation"
expression
=
"org.springframework.stereotype.Controller"
/>
</
context:component-scan
>
<!-- 配置springmvc的视图解析器 -->
<
bean
id
=
"viewResolver"
class
=
"org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix
=
"/WEB-INF/content/"
p:suffix
=
".jsp"
>
</
bean
>
4、目前本例中只是用了一个spring配置文件,即applicationContext.xml,如下:
<!-- 自动扫描spring注解,排除springmvc已扫描的Controller注解 -->
Xml代码
<
context:component-scan
base-package
=
"org.jisonami"
>
<
context:exclude-filter
type
=
"annotation"
expression
=
"org.springframework.stereotype.Controller"
/>
</
context:component-scan
>
5、关于spring配置文件中的xml头部:
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:p
=
"http://www.springframework.org/schema/p"
xmlns:context
=
"http://www.springframework.org/schema/context"
xsi:schemaLocation
="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
>
6、此时已经可以在代码中使用注解来配置spring的bean了,即如下形式的代码完成依赖注入:
Java代码
@Autowired
private
UserService userService;
三、Spring与MyBatis的整合
1、MyBatis的使用主要是使用Mapper接口+Mapper.xml中写sql的方式来实现更灵活的dao层,这一部分在与spring整合之后是不变的
而mybatis的全局配置文件则是SqlMapConfig.xml,也可以是其它的名字。
2、整合之前,数据库的连接信息是在SqlMapConfig.xml中配置的,并且Mapper的扫描也是在SqlMapConfig.xml中配置的
最麻烦的是我们完成crud操作的代码有比较多的冗余,即如下所示的形式:
Java代码
// 完成一个新增操作
InputStream is = Resources.getResourceAsStream(
"SqlMapConfig.xml"
);
SqlSessionFactory sessionFactory =
new
SqlSessionFactoryBuilder().build(is);
SqlSession session = sessionFactory.openSession();
UserMapper userMapper = session.getMapper(UserMapper.
class
);
userMapper.save(user);
session.commit();
session.close();
3、与spring整合的目的则是将SqlSessionFactory、SqlSession、UserMapper的创建和SqlSession的事物提交与关闭交给spring容器进行管理,
实现只需要调用一行代码的效果,即
Java代码
userMapper.save(user);
4、整合之后,数据库的连接信息与Mapper的扫描的配置片段直接移到applicationContext.xml中去了,完成SqlSessionFactory、SqlSession、UserMapper注入
applicationContext.xml中mybatis的配置片段:
Xml代码
<!-- mybatis与spring整合 -->
<!-- 加载数据库配置文件 -->
<
context:property-placeholder
location
=
"classpath:DBConfig.properties"
/>
<!-- 配置数据源,使用c3p0连接池 -->
<
bean
id
=
"dataSource"
class
=
"com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method
=
"close"
>
<
property
name
=
"driverClass"
value
=
"${jdbc.driver}"
>
</
property
>
<
property
name
=
"jdbcUrl"
value
=
"${jdbc.url}"
>
</
property
>
<
property
name
=
"user"
value
=
"${jdbc.user}"
>
</
property
>
<
property
name
=
"password"
value
=
"${jdbc.pass}"
>
</
property
>
</
bean
>
<!-- 配置sqlSessionFactory -->
<
bean
id
=
"sqlSessionFactory"
class
=
"org.mybatis.spring.SqlSessionFactoryBean"
>
<
property
name
=
"configLocation"
value
=
"classpath:SqlMapConfig.xml"
/>
<
property
name
=
"dataSource"
ref
=
"dataSource"
/>
</
bean
>
<!-- 扫描mapper接口 -->
<
bean
class
=
"org.mybatis.spring.mapper.MapperScannerConfigurer"
>
<
property
name
=
"basePackage"
value
=
"org.jisonami.mybatis.mapper"
>
</
property
>
<
property
name
=
"sqlSessionFactoryBeanName"
value
=
"sqlSessionFactory"
>
</
property
>
</
bean
>
5、原来的SqlMapConfig.xml文件中只剩下寥寥几行配置信息,
Xml代码
<
configuration
>
<!-- 给entity起别名,在mapper配置文件中写sql语句时会用到 -->
<
typeAliases
>
<
package
name
=
"org.jisonami.entity"
/>
</
typeAliases
>
</
configuration
>
6、以UserMapper.xml配置片段为例,描述整合后的简洁编程方式
Xml代码
<!-- mapper的命名空间namespace是Mapper接口的全限定名 -->
<
mapper
namespace
=
"org.jisonami.mybatis.mapper.UserMapper"
>
<!-- id是唯一标识符,与Mapper接口的方法名保持一致,参数类型parameterType是参数类型的全限定名,这里使用的是别名 -->
<
insert
id
=
"save"
parameterType
=
"User"
>
<
selectKey
keyColumn
=
"id"
keyProperty
=
"id"
resultType
=
"String"
order
=
"BEFORE"
>
select sys_guid() from dual
</
selectKey
>
insert into t_user(id, name, password) values(#{id}, #{name}, #{password})
</
insert
>
</
mapper
>
UserMapper接口中的方法声明如下:
Java代码
public
interface
UserMapper {
public
void
save(User user);
}
调用部分代码,直接注入UserMapper
Java代码
@Autowired
private
UserMapper userMapper;
public
void
save(User user) {
userMapper.save(user);
}
Spring+SpringMVC+MyBatis+Maven框架整合
原文:http://blog.csdn.net/kuandai225/article/details/51321183
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年09月23日 (328)
2021年09月24日 (313)
2021年09月17日 (191)
2021年09月15日 (369)
2021年09月16日 (411)
2021年09月13日 (439)
2021年09月11日 (398)
2021年09月12日 (393)
2021年09月10日 (160)
2021年09月08日 (222)
最新文章
更多>
2021/09/28 scripts
2022-05-27
vue自定义全局指令v-emoji限制input输入表情和特殊字符
2022-05-27
9.26学习总结
2022-05-27
vim操作
2022-05-27
深入理解计算机基础 第三章
2022-05-27
C++ string 作为形参与引用传递(转)
2022-05-27
python 加解密
2022-05-27
JavaScript-对象数组里根据id获取name,对象可能有children属性
2022-05-27
SQL语句——保持现有内容在后面增加内容
2022-05-27
virsh命令文档
2022-05-27
教程昨日排行
更多>
1.
list.reverse()
2.
Django Admin 管理工具
3.
AppML 案例模型
4.
HTML 标签列表(功能排序)
5.
HTML 颜色名
6.
HTML 语言代码
7.
jQuery 事件
8.
jEasyUI 创建分割按钮
9.
jEasyUI 创建复杂布局
10.
jEasyUI 创建简单窗口
友情链接
汇智网
PHP教程
插件网
关于我们
-
联系我们
-
留言反馈
- 联系我们:wmxa8@hotmail.com
© 2014
bubuko.com
版权所有
打开技术之扣,分享程序人生!