Spring + Spring MVC + MyBatis 整合
1.所需要Jar包
<!-- Spring3.0.1包 -->org.springframework.web-3.0.1 系列<!-- 公共包 -->slf4j-api-1.5.6.jarslf4j-log4j12-1.5.6.jar log4j-1.2.13.jarcommons-logging-1.1.1.jar asm-3.1.jar cglib-2.2.jar<!-- mybatis与Spring的整合所需的包 -->mybatis-3.0.5.jar aopalliance-1.0.jar mybatis-spring-1.0.1.jarmybatis-generator-core-1.3.1.jar(mybatis代码生成器包)<!-- jdbc driven -->mysql-connector-java-3.1.6-bin.jar<!-- JSR验证-Hibernate validate 4.1 -->hibernate-validator-4.1.0.Final.jarvalidation-api-1.0.0.GA.jar<!-- Spring Json 支持包 -->jackson-all-1.8.1.jar |
2.web.xml配置
Servlet配置
org.springframework.web.servlet.DispatcherServlet
init-param配置servlet初始化文件.
以及servlet-mapping配置.
应用路径配置
webAppRootKey
Log4j配置:Log4jConfigLocation、Log4jRefreshInterval
Spring上下文配置:contextConfigLocation
Spring字符集过滤器配置:org.springframework.web.filter.CharacterEncodingFilter
Spring监听器配置:org.springframework.web.context.ContextLoaderListener
log4j监听器配置:org.springframework.web.util.Log4jConfigListener
3.spring mvc - servlet.xml配置
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<!-- 启动mvc注解驱动 --><mvc:annotation-driven/><!-- 组件scanner主要是自动去注入指定包里的对象 --><context:component-scan base-package="com.los.mvc.controller"/><!-- ViewResolver & View 映射关系 --><!-- InternalResourceViewResolver 基于resource对jsp/jstl的支持 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> <!-- InternalResourceViewResolver viewClass默认值就是JstlView --> <property name="viewClass"value="org.springframework.web.servlet.view.JstlView"></property> </bean> <!-- 自定义拦截器配置 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/json*"/> <bean class="com.los.mvc.interceptor.MyInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> <!-- 国际化配置 --> <bean id="messageSource"class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="message"></property> </bean> |
4. Spring上下文 -- applicationContext.xml 配置
<!-- 支持注解 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<import resource="service.xml"/>
<import resource="dao.xml"/>
<import resource="orm.xml"/>
service.xml dao.xml 配置@service 和 @Repository
5. Mybatis3.0.5-Spring 整合 -- orm.xml
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<!-- DataSource配置 --><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="username" value="root" /> <property name="password" value="root" /></bean> <!-- 注册事务管理器(Mybatis将事务转交给Spring来管理) --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /></bean><!-- SqlSessionFactory配置(Mybatis核心是sqlSessionFactory来获取orm处理对象, dataSource, mapperLocations配置mybaits自动生成的xml文件.就是注入映射关系.) --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations"value="classpath:/com/los/mvc/mapper/*.xml" /></bean><!-- MapperScanner配置.自动去搜索mapper里的对象,并注入. --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.los.mvc.dao" /></bean><!-- 启动Spring注解事务 --><tx:annotation-driven/> |
6. mybatis自动生成器配置 -- generatorConfig.xml
sqlMapGenerator --sqlMpper.xml生成器
javaClientGenerator --ModelDao生成器
javaModelGenerator --Model生成器
com.los.util.MBG.java 运行会自动生成mybatis代码.然后再配置orm.xml
7. Controller层配置
类注解
@Controller
@RequestMapping("/json")为访问该层的路径.
方法注解
@RequestMapping(method = RequestMethod.GET) 只有get方法才能访问.
@ResponseBody 自动将返回的封装成json,方法返回值必须是map<String,?>类型.
@RequestMapping(value="/doLogin") value=”doLogin”为访问该方法的handler mapping
return "login/login";会通过ViewResolver找到对应的view
return "redirect:/user/toRegister.html";为spring-mvc的重定向.
@InitBinder()为绑定器,可以为request传来的数据进行数据类型转换.
数据自动验证
方法中参数需要有后面的两个(@Valid User user,BindingResult result).@Valid的支持标准是JSR,Hibernate Validate 4是对该标准比较好的实现.需要在Model类中配置验证的注解.判断验证是否正确通过result.hasErrors()或者result.hasFieldErrors()来判断,通过result.getAllErrors()或者result.getFieldErrors()来获取Errors然后遍历Errors获取相关想要的信息,例如Error.getDeafaultMessage(),这个是获取错误信息.具体的错误验证机制还地在Model类中配置.
属性注解
@Autowired 会为该属性自动注入bean,默认方式是byType.也可以用@Resource这个注解默认是byName.
8. Service层配置.(业务层)
类注解
@Service 为@Component的子注解,分工更明细.
@Transactional 可以为该业务类声明一个全类的事务.也可以将事务写在方法上.根据不同的需要.
方法注解
@Transactional(readOnly = true)
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class) 类的事务声明,可以设置隔离级别和传播属性,以及要回滚的异常名或者异常类,不需要回滚的异常名或者异常类.异常通常抛出给controller层来处理
属性注解
@Autowired @Resource
9. Repository层配置.(持久层DaoImpl)
类注解
@Repository 为@Component的子注解,意为持久层,分工更明细.一般不在这层处理事务.
10.Entry层配置(Model层)
类注解
@Entry
验证注解,常用的有:
@NotEmpty
@NotNull
@Size(min=2,max=10,message=”xx必须在{min}和{max}之间”)
@DecimalMax
@AssertFalse @AssertTrue
@Null
@Valid
@URL(protocol=,host=, port=,regexp=, flags=)
一般情况下属性或者方法可以放多个约束注解,hibernate validate会以随机的顺序去验证这些约束.所以多个注解约束会有可能同一个属性返回多个message.所以有时候需要只返回一条message,则需要使用验证组Groups来达成.组别序列可以把一系列的组别按照一定的顺序排列在一起,然后逐个验证,只要有一个组别验证失败,就不继续验证剩余的组别。
@GroupSequence({User.class,GroupB.class,GroupC.class})验证组的顺序,约束里不指定group的为默认的User.class组.
约束组放在类前,User.class为默认的约束组,GroupB,GroupC为空的接口.写在User外同个java文件下.
@NotEmpty(message="密码不能为空")
@Size(min=4,max=20,message="密码长度必须在{min}-{max}范围内",groups = GroupB.class)
如果@NotEmpty验证失败了,就不会继续验证@Size
本文章转载至LosMessi博客
Spring + Spring MVC + MyBatis 整合的更多相关文章
- spring jpa和mybatis整合
spring jpa和mybatis整合 前一阵子接手了一个使用SpringBoot 和spring-data-jpa开发的项目 后期新加入一个小伙伴,表示jpa相比mybatis太难用,多表联合的查 ...
- ssm整合说明与模板-Spring Spring MVC Mybatis整合开发
ssm整合说明 spring+spring mvc+mybatis 说明 源码下载 由于之前存在ssh框架,spring+struts+hibernate,其中spring负责aop与ioc,所以一般 ...
- Spring4+Spring MVC+MyBatis整合思路
1.Spring框架的搭建 这个很简单,只需要web容器中注册org.springframework.web.context.ContextLoaderListener,并指定spring加载配置文件 ...
- spring, spring mvc, mybatis整合文件配置详解
转自:http://www.cnblogs.com/wxisme/p/4924561.html 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用 ...
- Spring Cloud部署+Mybatis整合
一:架构简介 Spring Cloud是微服务思想的体现.每个项目单独部署,我只需要知道你服务的name就能直接调用你,而不关心你的ip和端口的变化.当接口服务不可用的时候,我能感知到你无法用了,就不 ...
- Spring MVC + MyBatis整合(IntelliJ IDEA环境下)
一些重要的知识: mybais-spring.jar及其提供的API: SqlSessionFactoryBean: SqlSessionFactory是由SqlSessionFactoryBuild ...
- Spring+MVC+Mybatis整合
本文是对慕课网上"搞定SSM开发"路径的系列课程的总结,详细的项目文档和课程总结放在github上了.点击查看 什么是秒杀业务 网站售卖某产品时,规定在某个日期开始售卖限量的产品, ...
- Spring+Spring MVC+MyBatis整合
一.准备工作 1.1导入所需jar包 1.2数据库 CREATE TABLE `t_customer` ( `id` ) NOT NULL AUTO_INCREMENT, `username` ...
- JAVA 框架 / SSM / SSM SPRING+SPING MVC + MYBATIS 三大框架整合详细步骤
http://how2j.cn/k/ssm/ssm-tutorial/1137.html
随机推荐
- 一起来学linux:用户与用户组
linux的文件属性以及管理方法和windows是完全不同的,所以学习linux首先来了解下用户以及文件权限是怎么回事 p { margin-bottom: 0.25cm; line-height: ...
- jquery 的 each 方法中 return 的坑
jquery 的 each 方法中 return 的坑 Chapter 0 在项目中使用 jquery 的 each 方法时想在 each 的循环中返回一个布尔类型的值于是掉进一个坑中... Chap ...
- pip源相关问题
指定源地址安装: pip install -i http://pypi.douban.com/simple/ packagename pip install -i http://pypi.tuna.t ...
- 详解HTTPS加速原理
HTTPS是什么? http叫超文本传输协议,使用TCP端口80,默认情况下数据是明文传送的,数据可以通过抓包工具捕获到,因此在interner上,有些比较重要的站点的http服务器需要使用PKI(公 ...
- Oracle 的process和Session
Oracle 的process和Session 1.process 和session的概念:process:这个参数限制了能够连接到SGA的操作系统进程数(或者是Windows 系统中的线程数),这个 ...
- Cookie中的HttpOnly详解
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt377 1.什么是HttpOnly? 如果您在cookie中设置了HttpOn ...
- 基于大数据的电影网站项目开发之CentOS的安装(一)
一.下载VMware并安装,至于安装教程从网上搜索进行参考 二.下载系统镜像文件,这里使用的是CentOS-6.5-x86_64-bin-DVD1.iso 三.安装镜像文件 运行VMware work ...
- 第1阶段——关于u-boot目标文件start.o中.globl 和.balignl理解(3)
汇编程序中以.开头的名称并不是指令的助记符,不会被翻译成机器指令,而是给汇编器一些特殊指示,称为伪操作. .globl _start 作用:声明一个_start全局符号(Symbol), 这个_sta ...
- js模拟点击事件实现代码
js模拟点击事件实现代码 类型:转载 时间:2012-11-06 在实际的应用开发中,我们会常常用到JS的模事件,比如说点击事件,举个简单的例子,点击表单外的"提交"按钮来提交表单 ...
- 转:java获得当前文件路径
第一种: File f = new File(this.getClass().getResource("/").getPath()); System.out.println(f); ...