整合 spring 4(包括mvc、context、orm) + mybatis 3 示例
来源:互联网 发布:淘宝服装图标 编辑:程序博客网 时间:2024/05/21 17:44
作者各必备工具的版本如下:
测试用表 CITY_CODE_CN:
注意存储引擎不可以是不支持事务的 MyISAM(InnoDB 和 MyISAM 的区别参考博客《MySql 存储引擎的选取》)。
表 CITY_CODE_CN 映射到的 xml 文件 com.defonds.bdp.city.mapper.CityMapper.xml:
表 CITY_CODE_CN 映射到的 java 文件 com.defonds.bdp.city.bean.City.java:
处理所有 MyBatis 的配置文件的配置文件 mybatis-config.xml:
无须将每个小配置单独导入,mapUnderscoreToCamelCase 是把 db CITY_CODE_CN 的下划线字段和 java 实体 bean 的驼峰标识相互转化。
Spring 上下文配置文件 bdp-applicationContext.xml:
Spring MVC 配置文件 bdpmvc-servlet.xml:
将所有 Spring 配置文件注册到 web.xml:
数据库连接配置文件 jdbc.properties:
log4j 配置文件 log4j.properties:
测试用 mapper 文件 com.defonds.bdp.city.mapper.CityMapper.java:
服务层处理类 com.defonds.bdp.city.service.CityService.java:
最后的是 mvc controller 文件 com.defonds.bdp.city.controller.CityController.java:
我们来看一下我们的 dbp 项目结构:
控制台打印日志:
2015-08-31 18:29:13 DEBUG com.defonds.bdp.city.service.CityService:45 - before insert the first city
2015-08-31 18:29:13 DEBUG com.defonds.bdp.city.mapper.CityMapper.insertCity:54 - ==> Preparing: insert into city_code_cn (city_code, city_jb, province_code, city_name, city, province) values ( ?, ?, ?, ?, ?, ? )
2015-08-31 18:29:13 DEBUG com.defonds.bdp.city.mapper.CityMapper.insertCity:54 - ==> Parameters: 1100(String), 1(String), 1100(String), 天津市(String), 天津市(String), 天津市(String)
2015-08-31 18:29:13 DEBUG com.defonds.bdp.city.mapper.CityMapper.insertCity:54 - <== Updates: 1
2015-08-31 18:29:13 DEBUG com.defonds.bdp.city.service.CityService:47 - after insert the first city, and before insert the second city
2015-08-31 18:29:13 DEBUG com.defonds.bdp.city.mapper.CityMapper.insertCity:54 - ==> Preparing: insert into city_code_cn (city_code, city_jb, province_code, city_name, city, province) values ( ?, ?, ?, ?, ?, ? )
2015-08-31 18:29:13 DEBUG com.defonds.bdp.city.mapper.CityMapper.insertCity:54 - ==> Parameters: null, null, null, null, null, null
2015-08-31 18:29:13 ERROR com.defonds.bdp.city.controller.CityController:97 - org.springframework.dao.DataIntegrityViolationException:
### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'CITY_CODE' cannot be null
### The error may involve com.defonds.bdp.city.mapper.CityMapper.insertCity-Inline
### The error occurred while setting parameters
### SQL: insert into city_code_cn (city_code, city_jb, province_code, city_name, city, province) values ( ?, ?, ?, ?, ?, ? )
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'CITY_CODE' cannot be null
; SQL []; Column 'CITY_CODE' cannot be null; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'CITY_CODE' cannot be null
查看 db 的 CITY_CODE_CN 表,没有新记录插入,spring 4 + mybatis 3 成功。
- MySQL:5.6.25-log MySQL Community Server (GPL) (下载地址)
- Tomcat:apache-tomcat-7.0.63 (下载链接)
- Java EE - Eclipse:Luna Service Release 1 v4.4.1 (下载链接)
- Spring:4.2.0.RELEASE (无须下载)
- MyBatis:3.3.0 (无须下载)
- JDK:1.7.0_67 (下载链接)
项目搭建
第三方包依赖 pom.xml:测试用表 CITY_CODE_CN:
注意存储引擎不可以是不支持事务的 MyISAM(InnoDB 和 MyISAM 的区别参考博客《MySql 存储引擎的选取》)。
表 CITY_CODE_CN 映射到的 xml 文件 com.defonds.bdp.city.mapper.CityMapper.xml:
表 CITY_CODE_CN 映射到的 java 文件 com.defonds.bdp.city.bean.City.java:
处理所有 MyBatis 的配置文件的配置文件 mybatis-config.xml:
无须将每个小配置单独导入,mapUnderscoreToCamelCase 是把 db CITY_CODE_CN 的下划线字段和 java 实体 bean 的驼峰标识相互转化。
Spring 上下文配置文件 bdp-applicationContext.xml:
Spring MVC 配置文件 bdpmvc-servlet.xml:
将所有 Spring 配置文件注册到 web.xml:
数据库连接配置文件 jdbc.properties:
log4j 配置文件 log4j.properties:
测试用 mapper 文件 com.defonds.bdp.city.mapper.CityMapper.java:
服务层处理类 com.defonds.bdp.city.service.CityService.java:
最后的是 mvc controller 文件 com.defonds.bdp.city.controller.CityController.java:
我们来看一下我们的 dbp 项目结构:
事务测试
访问事务测试页面如下:控制台打印日志:
2015-08-31 18:29:13 DEBUG com.defonds.bdp.city.service.CityService:45 - before insert the first city
2015-08-31 18:29:13 DEBUG com.defonds.bdp.city.mapper.CityMapper.insertCity:54 - ==> Preparing: insert into city_code_cn (city_code, city_jb, province_code, city_name, city, province) values ( ?, ?, ?, ?, ?, ? )
2015-08-31 18:29:13 DEBUG com.defonds.bdp.city.mapper.CityMapper.insertCity:54 - ==> Parameters: 1100(String), 1(String), 1100(String), 天津市(String), 天津市(String), 天津市(String)
2015-08-31 18:29:13 DEBUG com.defonds.bdp.city.mapper.CityMapper.insertCity:54 - <== Updates: 1
2015-08-31 18:29:13 DEBUG com.defonds.bdp.city.service.CityService:47 - after insert the first city, and before insert the second city
2015-08-31 18:29:13 DEBUG com.defonds.bdp.city.mapper.CityMapper.insertCity:54 - ==> Preparing: insert into city_code_cn (city_code, city_jb, province_code, city_name, city, province) values ( ?, ?, ?, ?, ?, ? )
2015-08-31 18:29:13 DEBUG com.defonds.bdp.city.mapper.CityMapper.insertCity:54 - ==> Parameters: null, null, null, null, null, null
2015-08-31 18:29:13 ERROR com.defonds.bdp.city.controller.CityController:97 - org.springframework.dao.DataIntegrityViolationException:
### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'CITY_CODE' cannot be null
### The error may involve com.defonds.bdp.city.mapper.CityMapper.insertCity-Inline
### The error occurred while setting parameters
### SQL: insert into city_code_cn (city_code, city_jb, province_code, city_name, city, province) values ( ?, ?, ?, ?, ?, ? )
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'CITY_CODE' cannot be null
; SQL []; Column 'CITY_CODE' cannot be null; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'CITY_CODE' cannot be null
查看 db 的 CITY_CODE_CN 表,没有新记录插入,spring 4 + mybatis 3 成功。
源码下载
本文侧重讲 spring ioc、orm 和 mybatis 的事务集成,有评论问关于 spring mvc 的相关配置,请参考另一篇专门讲 spring mvc 搭建的博客《零基础搭建 spring mvc 4 项目(本文基于 Servlet 3.0)》,本文就是在该文基础上进行进一步添加了 service 和 dao 层的扩展而已。本文示例项目源码已共享到 CSDN 资源,有兴趣的朋友可以去下载下来参考:http://download.csdn.net/detail/defonds/9068147。参考资料
- http://edwin.baculsoft.com/2015/01/a-simple-spring-4-and-mybatis-transaction-example/
- http://zhaozhiming.github.io/blog/2014/11/15/spring4-and-mybatis/
0 0
- 整合 spring 4(包括mvc、context、orm) + mybatis 3 示例
- 整合 spring 4(包括mvc、context、orm) + mybatis 3 示例
- 整合 spring 4(包括mvc、context、orm) + mybatis 3 示例
- MyBatis-Spring MVC 整合示例(带源码)
- MyBatis+Spring整合示例
- MyBatis+Spring整合示例
- MyBatis+Spring整合示例
- Spring MVC Mybatis 整合
- spring mvc mybatis 整合
- Spring MVC整合MyBatis
- spring mvc mybatis 整合
- spring mvc mybatis 整合
- Spring mvc+Mybatis整合
- Spring MVC整合Mybatis
- Spring+MVC+Mybatis整合
- 整合spring+spring mvc+mybatis
- Spring+Spring MVC+MyBatis整合
- spring spring-mvc mybatis 整合
- 【典型例题】遍历文件夹下面的文件;(递归算法)
- A+B (III)
- C语言栈帧破坏
- pugixml 丢失空格的解决方法
- 网络流Dinic
- 整合 spring 4(包括mvc、context、orm) + mybatis 3 示例
- 欢迎使用CSDN-markdown编辑器
- 2.1 VB语言基础——关键字和标识符
- Java Map《自己记录记忆》
- Binary Search Tree analog
- 初识CPU卡、SAM卡
- Java集合框架总结
- 堆和栈的区别(转过无数次的文章)
- [VM学习] - VM虚拟机桥接模式无法上网的困惑