Spring + Spring MVC + MyBatis 整合
1.所需要Jar包
<!-- Spring3. 0.1 包 --> org.springframework.web- 3.0 . 1 系列 <!-- 公共包 --> slf4j-api- 1.5 . 6 .jar slf4j-log4j12- 1.5 . 6 .jar log4j- 1.2 . 13 .jar commons-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 .jar mybatis-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.jar validation-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
随机推荐
- 【Ubuntu16]】ufw
Usage: ufw COMMAND Commands: enable enables the firewall 开启ufw防火墙 disable disables the firewall 禁用防火 ...
- js history对象 手机物理返回键
有兴趣的可以了解下history对象,不感兴趣也可以直接跳到手机物理返回键监听部分 使用场景: 场景1:项目中一个表单页面,需得填写验证码,填写验证码后提交,由于使用的form直接提交,没有使用AJA ...
- Andrew Ng机器学习课程笔记--week9(下)(推荐系统&协同过滤)
本周内容较多,故分为上下两篇文章. 本文为下篇. 一.内容概要 1. Anomaly Detection Density Estimation Problem Motivation Gaussian ...
- Spring MVC控制层传递对象后在JSP页面中的取值方法
List<Order> orders = new ArrayList<Order>(); for (int i = 0; i < 3; i++) { Order t = ...
- XWPFRun属性详解
XWPFRun是XWPFDocument中的一段文本对象(就是一段文字) 创建文档对象 XWPFDocument docxDocument = new XWPFDocument(); 创建段落对象 X ...
- html+css手记
----------------------html定义和基本结构---------------------- HTML是 HyperText Mark-up Language 的首字母简写,意思是超 ...
- Spring框架Controller层(表现层)针对方法参数是Bean时HttpServletRequest绑定参数值问题解释
在做项目的时候,有一个需求是将数据库中的信息封装到实体类返回到jsp界面 传过来的参数只是实体类的id属性,然后根据id属性去查数据库,事情就是这样,然后 结果遇到很奇怪的事情,在jsp页面中使用EL ...
- matlab-常用函数(1)
rng('shuffle'): matlab help文档中的解释 rng('shuffle'): seeds the random number generator based on the cur ...
- ios 初体验<UIButton 控件>
1.创建UIButton 跟其他方式不同,不是直接alloc,init 创建 用工厂化方式创建 UIButton *sureBtn = [UIButton buttonWithType:UIButto ...
- java动态代理(JDK和cglib实现对比)
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt214 JAVA的动态代理 代理模式 代理模式是常用的java设计模式,他的特 ...