SSM手把手整合教程&测试事务
自打来了博客园就一直在看帖,学到了很多知识,打算开始记录的学习到的知识点
今天我来写个整合SpringMVC4 spring4 mybatis3&测试spring事务的教程,如果有误之处,还请批评指正。
本文是原创文章,转载请注明出处
一、准备工作:
开发环境:
- idea(源码文章最后会给出,不下载也一样,下边都贴出来了)
- MySQL 5.7
- Tomcat 8.5
- Google chrome的插件postman(发送json数据)
- navicat for MySQL(用MySQL命令行也行)
本教程用到的jar包如下(源码中本文中用到所有jar都已经给出):
Spring以及springMVC的jar包:
Mybatis的jar包&mybatis兼容spring的jar以及jdbc驱动:
使用的数据源源为阿里巴巴的druid,和fastjson解析json
Tomcat8.5,这个一般的IDE都不用管,配置好就可以
文件上传下载的支持,本教程中没有写,如果有时间我会补上
Mybatis-generator 自动生成Mapper和mapper.xml用的
二、文件结构
三、建表:
建表语句等已经存在了com.hellz.sql包中
- create DATABASE mybatis;
- use mybatis;
- create table teacher
- (
- tid int(10) primary key auto_increment,
- tname varchar(20),
- tusername varchar(20),
- tpassword varchar(15)
- );
- create table student
- (
- sid int(6) primary key auto_increment,
- sname varchar(20),
- ssex int(1),#0代表未知,1代表男,2代表女
- sage int(3),
- steacherid int(10)
- );
- insert into teacher (tname,tusername,tpassword) VALUES ('小李','username','');#其实已经设置主键自增了就不用设置tid了,这条tid=1
四、代码
Controllor包中
DemoController代码:
- package com.hellxz.controller;
- import com.hellxz.entity.Student;
- import com.hellxz.entity.User;
- import com.hellxz.service.StudentService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.ModelMap;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.servlet.ModelAndView;
- import javax.servlet.http.HttpServletRequest;
- @Controller
- @RequestMapping("user")
- public class DemoController {
- // 注入studentservice
- @Autowired
- private StudentService studentService;
- @RequestMapping("/demo")//此处测试springmvc是否工作
- public String demo(){
- return "demo";
- }
- @RequestMapping("/mv")//此处测试modelAndView传参前端
- public ModelAndView modelAndView(){
- ModelAndView modelAndView = new ModelAndView();
- modelAndView.addObject("id",1);
- modelAndView.addObject("name","aini");
- modelAndView.setViewName("demo");
- return modelAndView;
- }
- @RequestMapping("/request")//此处测试request传参到服务端
- public String request(HttpServletRequest request){
- String id = request.getParameter("id");
- String name = request.getParameter("name");
- return "demo";//在此处打断点测试传参是否成功
- }
- @RequestMapping("/modelmap")//此处测试modelmap传参给页面
- public String modelMap(ModelMap modelMap){
- //给demo.jsp传参
- User u = new User();
- u.setId(1);
- u.setName("xiaomingming");
- modelMap.addAttribute("user",u);
- return "demo";
- }
- /**
- * 此处用于测试json传值
- * 我在studentServiceImpl中把这个方法插入了一个teacher
- * 为了验证事务出错的回滚,如果之前teacher表中没有数据则不会回滚
- */
- @RequestMapping("/json")
- public String json(@RequestBody Student student){
- studentService.createStudent(student);//此处打断点debug查看student是否传入
- return "demo";
- }
- }
dao包里的使用mybatis-generator直接生成,一会讲。
Entity包中
Student类:
- package com.hellxz.entity;
- public class Student {
- private Integer sid;
- private String sname;
- private Integer ssex;
- private Integer sage;
- private Integer steacherid;
- private Teacher teacher;
- public Teacher getTeacher() {
- return teacher;
- }
- public void setTeacher(Teacher teacher) {
- this.teacher = teacher;
- }
- public Integer getSid() {
- return sid;
- }
- public void setSid(Integer sid) {
- this.sid = sid;
- }
- public String getSname() {
- return sname;
- }
- public void setSname(String sname) {
- this.sname = sname == null ? null : sname.trim();
- }
- public Integer getSsex() {
- return ssex;
- }
- public void setSsex(Integer ssex) {
- this.ssex = ssex;
- }
- public Integer getSage() {
- return sage;
- }
- public void setSage(Integer sage) {
- this.sage = sage;
- }
- public Integer getSteacherid() {
- return steacherid;
- }
- public void setSteacherid(Integer steacherid) {
- this.steacherid = steacherid;
- }
- }
teacher类:
- package com.hellxz.entity;
- import java.util.List;
- public class Teacher {
- private Integer tid;
- private String tname;
- private String tusername;
- private String tpassword;
- private List<Student> student;
- public List<Student> getStudents() {
- return student;
- }
- public void setStudents(List<Student> students) {
- this.student = students;
- }
- public Integer getTid() {
- return tid;
- }
- public void setTid(Integer tid) {
- this.tid = tid;
- }
- public String getTname() {
- return tname;
- }
- public void setTname(String tname) {
- this.tname = tname == null ? null : tname.trim();
- }
- public String getTusername() {
- return tusername;
- }
- public void setTusername(String tusername) {
- this.tusername = tusername == null ? null : tusername.trim();
- }
- public String getTpassword() {
- return tpassword;
- }
- public void setTpassword(String tpassword) {
- this.tpassword = tpassword == null ? null : tpassword.trim();
- }
- }
- User类:
- package com.hellxz.entity;
- /**
- * Created by HELLXZ on 2017/8/2.
- */
- public class User {
- private int id;
- private String name;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- Interceor包中
- Interceptor类:
- package com.hellxz.interceptor;
- import org.springframework.web.servlet.HandlerInterceptor;
- import org.springframework.web.servlet.ModelAndView;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- * Created by HELLXZ on 2017/8/3.
- */
- public class Interceptor implements HandlerInterceptor{
- @Override
- public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
- return true;
- }
- @Override
- public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
- System.out.println("方法执行了");
- }
- @Override
- public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
- System.out.println("方法结束了");
- }
- }
- resource包中
- jdbc.properties代码:
- jdbc.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false
- jdbc.username=root
- jdbc.password=root
- validationQuery=select 1
- spring-config.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:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
- <!--开启包扫描-->
- <context:component-scan base-package="com.hellxz.*.**"/>
- <!--开启注解支持-->
- <context:annotation-config/>
- <!--开启切面支持-->
- <aop:aspectj-autoproxy proxy-target-class="true"/>
- <!--定位jdbc.properties 配置文件-->
- <context:property-placeholder location="classpath:com/hellxz/resource/jdbc.properties"/>
- <!--创建sqlSessionFactory并指定dataSource & *mapper.xml的路径-->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="mapperLocations" value="classpath:com/hellxz/dao/**.xml"/>
- </bean>
- <!--mybatis加载Mapper扫描配置器,指定Mapper类的包,不能用classpath:来指定!!-->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.hellxz.dao"/>
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
- </bean>
- <!--开启spring事务支持-->
- <tx:annotation-driven transaction-manager="transactionManager"/>
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"/>
- </bean>
- <!--使用阿里巴巴的druid数据源, 别的数据源请另行参考-->
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
- <property name="url" value="${jdbc.url}"/>
- <property name="username" value="${jdbc.username}"/>
- <property name="password" value="${jdbc.password}"/>
- <property name="initialSize" value="5"/>
- <property name="minIdle" value="0"/>
- <property name="validationQuery" value="${validationQuery}"/>
- <property name="maxActive" value="20"/>
- <property name="maxWait" value="6000"/>
- <property name="removeAbandoned" value="true"/>
- <property name="removeAbandonedTimeout" value="1800"/>
- </bean>
- </beans>
- Spring-mvc.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:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation=" http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
- <context:component-scan base-package="com.hellxz.**"/>
- <!--开启包扫描-->
- <context:component-scan base-package="com.hellxz.**">
- <!--作为spring的子容器,springMVC不具有事务能力,即无法处理@service注解的事务rollback操作-->
- <!--如果在同一事务下的两个操作,其中有一条错误,那么理应受spring事务管理,一起rollback-->
- <!--如果仅仅写为<context:component-scan base-package="com.hellxz.**"/>-->
- <!--spring作为父容器会失去处理事务的能力,相当于被springMVC抢了-->
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
- </context:component-scan>
- <!--使用fastjson解析json数据,需要注意的是一定要写在<mvc:annotation-driven前,否则失去解析作用-->
- <mvc:annotation-driven>
- <mvc:message-converters>
- <bean id="jsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
- <property name="supportedMediaTypes">
- <list>
- <value>text/html;charset=utf-8</value>
- <value>application/json;charset=utf-8</value>
- </list>
- </property>
- </bean>
- </mvc:message-converters>
- </mvc:annotation-driven>
- <!--开启MVC注解支持-->
- <mvc:annotation-driven/>
- <!--加载上下文注解配置-->
- <context:annotation-config/>
- <!--指明springmvc拦截器位置-->
- <mvc:interceptors>
- <!--全局拦截-->
- <bean id="intercepter" class="com.hellxz.interceptor.Interceptor"/>
- </mvc:interceptors>
- <!--视图解析器,controller返回的字符串在这里加上前缀&后缀,才能作为请求进入浏览器,最下方添加了jstl语句的支持-->
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/view/"/>
- <property name="suffix" value=".jsp"/>
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
- </bean>
- <!--文件上传需要的设置-->
- <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <property name="maxUploadSize" value="500000"/>
- <property name="maxInMemorySize" value="1024000"/>
- <property name="defaultEncoding" value="utf-8"/>
- </bean>
- </beans>
- Service包:
- StudentService接口:
- package com.hellxz.service;
- import com.hellxz.dao.StudentMapper;
- import com.hellxz.entity.Student;
- import org.springframework.stereotype.Service;
- /**
- * Created by HELLXZ on 2017/8/3.
- */
- public interface StudentService {
- int createStudent(Student student);
- }
- StudentServiceImpl类:
- package com.hellxz.service;
- import com.hellxz.dao.StudentMapper;
- import com.hellxz.dao.TeacherMapper;
- import com.hellxz.entity.Student;
- import com.hellxz.entity.Teacher;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import com.hellxz.service.StudentService;
- /**
- * Created by HELLXZ on 2017/8/3.
- */
- @Service("studentServiceImpl")
- public class StudentServiceImpl implements StudentService{
- @Autowired
- private StudentMapper studentMapper;
- @Autowired
- private TeacherMapper teacherMapper;
- @Override
- @Transactional
- public int createStudent(Student student) {
- studentMapper.insertSelective(student);
- //后边的都是测试是否事务错误回滚,建议试一下,请先插入一条teacher数据,以确保出错!哈哈
- Teacher teacher = new Teacher();
- teacher.setTid(1);
- teacher.setTname("小王");
- teacher.setTusername("11111");
- teacher.setTpassword("11111");
- teacherMapper.insertSelective(teacher);
- return 0;
- }
- }
- TeacherService接口:
- package com.hellxz.service;
- import com.hellxz.entity.Teacher;
- /**
- * Created by HELLXZ on 2017/8/3.
- */
- public interface TeacherService {
- int createTeacher(Teacher teacher);
- }
- TeacherServiceImpl类:
- package com.hellxz.service;
- import com.hellxz.dao.TeacherMapper;
- import com.hellxz.entity.Teacher;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- /**
- * Created by HELLXZ on 2017/8/3.
- */
- @Service("teacherServiceImpl")
- public class TeacherServiceImpl implements TeacherService{
- @Autowired
- private TeacherMapper teacherMapper;
- @Override
- public int createTeacher(Teacher teacher) {
- teacherMapper.insertSelective(teacher);
- return 0;
- }
- }
web.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
- version="3.1">
- <!--上下文配置定位,定位spring的配置文件,并加上监听器-->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:com/hellxz/resource/spring-config.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!--springMVC的分派servlet-->
- <servlet>
- <servlet-name>ssm</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--在servlet启动的时候加载springMVC的配置文件-->- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:com/hellxz/resource/spring-mvc.xml</param-value>
- </init-param>
<!--启动顺序1,表示优先加载-->- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>ssm</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </web-app>
测试页面demo.jsp:
- <%--
- Created by IntelliJ IDEA.
- User: HELLXZ
- Date: 2017/8/2
- Time: 18:44
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Demo</title>
- </head>
- <body>
- Hello !! this page named Demo<br>
- <%--接收modelmap--%>
- ${user.id}<br>
- ${user.name}<br>
- <%--接收model and view--%>
- ${id}<br>
- ${name}<br>
- </body>
- </html>
- 下面说一下mybatis-generator的用法,我们来生成Mapper类和Mapperxml,请确保已经建表了!!!
- 如图,随便找个地方新建个文件夹,把jdbc驱动还有mybatis-generator的jar包放进去
- 新建一个TXT,改名generatorConfig.xml,复制下面的代码,如果你要直接用在自己的项目里,请直接修改包的结构就好,很简单。
- generatorConfig.xml:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
- "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
- <generatorConfiguration>
- <!--classPathEntry:数据库的JDBC驱动 -->
- <classPathEntry
- location="G:\mybatis-generator\mysql-connector-java-5.1.34.jar" />
- <context id="MysqlTables" targetRuntime="MyBatis3">
- <!-- 注意这里面的顺序确定的,不能随变更改 -->
- <!-- 自定义的分页插件 <plugin type="com.deppon.foss.module.helloworld.shared.PaginationPlugin"/> -->
- <!-- 可选的(0 or 1) -->
- <!-- 注释生成器 -->
- <commentGenerator>
- <!-- 是否去除自动生成的注释 true:是 : false:否 -->
- <property name="suppressAllComments" value="true" />
- </commentGenerator>
- <!-- 必须的(1 required) -->
- <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
- <jdbcConnection driverClass="com.mysql.jdbc.Driver"
- connectionURL="jdbc:mysql://localhost:3306/mybatis"
- userId="root" password="root">
- </jdbcConnection>
- <!-- 可选的(0 or 1) -->
- <!-- 类型转换器或者加类型解析器 -->
- <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和
- NUMERIC 类型解析为java.math.BigDecimal -->
- <javaTypeResolver>
- <property name="forceBigDecimals" value="false" />
- </javaTypeResolver>
- <!-- 必须的(1 required) -->
- <!-- java模型生成器 -->
- <!-- targetProject:自动生成代码的位置 -->
- <javaModelGenerator targetPackage="com.hellxz.entity"
- targetProject="src"
- >
- <!-- TODO enableSubPackages:是否让schema作为包的后缀 -->
- <property name="enableSubPackages" value="true" />
- <!-- 从数据库返回的值被清理前后的空格 -->
- <property name="trimStrings" value="true" />
- </javaModelGenerator>
- <!-- 必须的(1 required) -->
- <!-- map xml 生成器 -->
- <sqlMapGenerator targetPackage="com.hellxz.dao"
- targetProject="src">
- <property name="enableSubPackages" value="true" />
- </sqlMapGenerator>
- <!-- 可选的(0 or 1) -->
- <!-- mapper 或者就是dao接口生成器 -->
- <javaClientGenerator targetPackage="com.hellxz.dao"
- targetProject="src"
- type="XMLMAPPER">
- <property name="enableSubPackages" value="true" />
- </javaClientGenerator>
- <!-- 必须的(1...N) -->
- <!-- pojo 实体生成器 -->
- <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->
- <!-- schema即为数据库名 可不写 -->
- <table tableName="student" domainObjectName="Student"
- enableInsert="true" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" selectByExampleQueryId="false">
- <!-- 忽略字段 可选的(0 or 1) -->
- <!-- <ignoreColumn column="is_use" /> -->
- <!--//无论字段是什么类型,生成的类属性都是varchar。 可选的(0 or 1) 测试无效 -->
- <!-- <columnOverride column="city_code" jdbcType="VARCHAR" /> -->
- </table>
- <table tableName="teacher" domainObjectName="Teacher"
- enableInsert="true" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" selectByExampleQueryId="false">
- <!-- 忽略字段 可选的(0 or 1) -->
- <!-- <ignoreColumn column="is_use" /> -->
- <!--//无论字段是什么类型,生成的类属性都是varchar。 可选的(0 or 1) 测试无效 -->
- <!-- <columnOverride column="city_code" jdbcType="VARCHAR" /> -->
- </table>
- </context>
- </generatorConfiguration>
- 粘贴保存,然后运行—cmd 打开命令行,cd到此文件夹,执行如下代码:
- java -jar mybatis-generator-core-1.3..jar -configfile generatorConfig.xml –overwrite
- 出现successful就已经成功了,把新生成的src文件加中的两个文件夹文件按照文章开头的文件结构放进去就好。
- 配置完成,debug模式启动服务器,什么?你服务器不会配置?出门左转百度吧老兄!
- 这样子就算成功了,下面进行测试
- 起一个浏览器输入localhost:8080/user/demo 回车,(elipse需要在/user前加项目名,这里用idea),如果进入此界面则说明springMVC正常
- 下面只需要测试Spring的事务是否正常了
- 我们先捋一下流程(还不会画思维导图,谅解):
- 发送json —-> controller解析处理封装student —-> 找到RequestMapping(”/json”)
- —-> 打断点查看student对象是否正确,继续,调用studentService的createStudent()—-> 找到studentServiceIml调用具体方法
- —-> 方法中调用了mybatis的mapper的方法,同时我们加了条坏数据插入到teacher表 —-> 如果两条数据都没有插进去,则说明spring事务正常,同一事务中的只要有错误就会回滚了
- 好,我们开始:
- 使用postman向localhost:8080/user/json发送如下json语句,如图
- 注意json用的是花括号!如果你已经给student插数据了,那么请找个没有使用的sid
- 可以看见已经传了个student对象进来了,先别继续,查看数据库
- 我们看到student表中还是空的,这么说来,sid=1是可以插进来的,当然teacher里一定要有一条数据哦~
- 剩下的大家可以打断点继续跟踪,我这继续说就有点侮辱大家智商了,方法执行完毕后我们再来查看数据是否正常
- 不出所料,student表中并没有插入数据,事务回滚成功!
- 如果有疑问,比如说怀疑根本没连上mybatis,或者说本来就插不进去,那么请删除teacher表中的所有数据,再postman发一下json查看一下,囧,我已经做了,见下图
- Student
- Teacher
- 我们再捋一下流程:
- 我们使用postman发送一条json到localhost:8080/user/json,
- 这条数据会在DemoController中进行解析,封装成一个student对象,因为我们已经注入了StudentService对象,我们把student对象传给他,因为是接口,会传给StudentServiceImpl实现类,实现类中我们先使用StudentMapper对象通过mybatis向数据库插入刚才传过来的对象,自动解析成数据插值,在刚才的方法下边我们故意弄了一条错误的teacher数据。
- 如果看了我写的建表SQL语句可能会好奇:为什么我要插入一条teacher数据?其实原因很简单,teacher的主键是自增的,我插入已经占用的主键的值肯定是错误的,我们要测试的是spring的事务,如果同一个事务中有多条数据进行插值,如果有一条错误我们想让它们同时完成或者同时不完成,那么这就很有用了。正常来讲,查询数据库你会发现这两条都没有插入。
- 如果想试试事务失效是什么样子,请手动修改spring-mvc.xml中的代码
- <!--open package scanner-->
- <context:component-scan base-package="com.hellxz.**">
- <!--if you do not set this data, when you insert two transaction together and one of them is error-->
- <!--and you want to if correct let them all succeed or false all rollback, you must set this-->
- <!--you do not set unless you want one data succeed and one false-->
- <!--it means springmvc has no transaction power of "@Service", but Spring has it.-->
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
- </context:component-scan>
改为:
- <context:component-scan base-package="com.hellxz.**"/>
- 好了,这就是我为大家准备的ssm教程。第一次写,写了好几个小时-_-||
controller中还有几个测试传参的有兴趣可以试验下
- 源码包就是我的项目直接拿出来的,不知道博客园有没有专门上传附件的地方,传到百度云了
源码是我之前输入法有问题写的注释,xml注释大多用的英文的,具体请参考本贴
链接:http://pan.baidu.com/s/1min414W
[hide]密码:6v6s[/hide]
第一次编辑2017-08-03 22:30:23
SSM手把手整合教程&测试事务的更多相关文章
- SSM简单整合教程&测试事务
自打来了博客园就一直在看帖,学到了很多知识,打算开始记录的学习到的知识点 今天我来写个整合SpringMVC4 spring4 mybatis3&测试spring事务的教程,如果有误之处,还请 ...
- SSM简明教程:简单的十步教你搭建人生第一个SSM框架[ SSM框架整合教程(Spring+SpringMVC+MyBatis) ]
SSM_BookSystem SSM框架基础 SSM_BookSystem ---> Hello CRUD 说明:本项目目前包含基础的CRUD 日期:2017-05-01 22:25:37 作者 ...
- 手把手整合SSM框架
前言 如果看过前几篇文章,对 Spring 和 MyBatis 有了一定了解,一定想上手试试.这篇文章从 0 到 1,手把手整合 SSM (Spring.Spring MVC.MyBatis). 本篇 ...
- SSM 框架-06-详细整合教程(IDEA版)(Spring+SpringMVC+MyBatis)
SSM 框架-06-详细整合教程(IDEA版)(Spring+SpringMVC+MyBatis) SSM(Spring.Spring MVC和Mybatis)如果你使用的是 Eclipse,请查看: ...
- SSM 框架-05-详细整合教程(Eclipse版)(Spring+SpringMVC+MyBatis)
SSM 框架-05-详细整合教程(Eclipse版)(Spring+SpringMVC+MyBatis) 如果你使用的是 Intellij IDEA,请查看: SSM的配置流程详细的写了出来,方便很少 ...
- SSM框架——详细整合教程
SSM框架——详细整合教程(Spring+SpringMVC+MyBatis) 1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Jav ...
- SSM框架整合搭建教程
自己配置了一个SSM框架,打算做个小网站,这里把SSM的配置流程详细的写了出来,方便很少接触这个框架的朋友使用,文中各个资源均免费提供! 一. 创建web项目(eclipse) File-->n ...
- SSM框架简介及整合教程
1.Spring Spring 框架是 Java 应用最广的框架,它的成功来源于理念,而不是技术本身,它的理念包括 IoC (控制反转) 和 A面向切面编程).Spring框架是个轻量级的Java E ...
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)【申明:来源于网络】
SSM框架--详细整合教程(Spring+SpringMVC+MyBatis)[申明:来源于网络] 地址:http://blog.csdn.net/u014662268/article/details ...
随机推荐
- Java订单号生成,唯一订单号(日均千万级别不重复)
Java订单号生成,唯一订单号 相信大家都可以搜索到很多的订单的生成方式,不懂的直接百度.. 1.订单号需要具备以下几个特点. 1.1 全站唯一性. 1.2 最好可读性. 1.3 随机性,不能重复,同 ...
- @NotEmpty、@NotBlank、@NotNull的区别
@NotEmpty 用在集合类上面 @NotBlank 用在String上面 @NotNull 用在基本类型上 只有简单的结果,但是再更具体一点的内容就搜不到了,所以去看了看源码,发现了如下的注释 ...
- bash脚本之数组学习
在bash中可使用索引数组和关联数组,bash在4.0版本之后才添加了对关联数组的支持 一.索引数组 1.定义索引数组 # 方式1 array_value=(1 2 3 4 5 6)或者array_v ...
- Python day02 三元运算
type 查看数据类型.2 **32 :2的32次方 .浮点的表示类型是小数,但是小数不仅仅包括浮点 浮点数用来处理实数,即带有小数的数字 三元运算: result = 值1 if 条件 el ...
- 小白的Python之路 day5 time,datatime模块详解
一.模块的分类 可以分成三大类: 1.标准库 2.开源模块 3.自定义模块 二.标准库模块详解 1.time与datetime 在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时 ...
- Hibernate中关于HQL查询返回List<Object>数据的结果集问题
---恢复内容开始--- 开发中遇到的一个小问题,使用Hibernate中的HQL查询时,使用query.list()查询出来的是一个List<Object>结果集 原来代码: publi ...
- Transact-SQL参考--学习笔记
基本的就不累赘了. 运算符 除法: dividend / divisor 如果用一个整数的 divisor 去除整数的 dividend,其结果是一个整数,小数部分被截断,如果要有小数可以将divid ...
- 强化学习之Sarsa (时间差分学习)
上篇文章讲到Q-learning, Sarsa与Q-learning的在决策上是完全相同的,不同之处在于学习的方式上 这次我们用openai gym的Taxi来做演示 Taxi是一个出租车的游戏,把顾 ...
- 申请的服务器安装tomcat后不能访问的问题
新申请的阿里云服务器,操作系统是CentOS6.5,安装jdk1.8版本,用java -version命令可以查看jdk版本,安装正确会有版本显示.然后安装tomcat,tomcat安装后默认端口是8 ...
- 【矩阵快速幂】bzoj1297 [SCOI2009]迷路
1297: [SCOI2009]迷路 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1407 Solved: 1007[Submit][Status ...