首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
编程语言
> 详细
Spring+SpringMVC+MyBatis+Maven框架整合
时间:
2016-05-06 15:17:37
阅读:
210
评论:
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年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
Spring Cloud 从入门到精通(一)Nacos 服务中心初探
2021-07-29
基础的排序算法
2021-07-29
SpringBoot|常用配置介绍
2021-07-29
关于 .NET 与 JAVA 在 JIT 编译上的一些差异
2021-07-29
C语言常用函数-toupper()将字符转换为大写英文字母函数
2021-07-29
《手把手教你》系列技巧篇(十)-java+ selenium自动化测试-元素定位大法之By class name(详细教程)
2021-07-28
4-1 YAML配置文件 注入 JavaBean中
2021-07-28
【python】 用来将对象持久化的 pickle 模块
2021-07-28
马拉车算法
2021-07-28
用Python进行冒泡排序
2021-07-28
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!