SSM integration articles] three. SSM integration + + unit test cases Affairs Chapter III (four chapters)

Spring + SpringMvc + MyBatis transaction integration + + Chapter III unit test cases

github源码(day56-ssm-transaction)https://github.com/1196557363/ideaMavenProject

Some knowledge can not understand the reference SSM integrate a articles]. Spring + SpringMvc + MyBatis simple case

The case connected to the chapter [chapter] integration SSM III. Chapter II + integration + transaction unit test cases SSM (four chapters)

7.test transaction configuration

7.1 day56-ssm-transaction create a new table

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `name` varchar(11) NOT NULL,
  `account` double(8,2) DEFAULT NULL,
  PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `user` VALUES ('czh', '10000.00');
INSERT INTO `user` VALUES ('wpj', '100000.00');

7.2 New Entity class, dao, mapper, service and serviceImpl

7.2.1 User

package com.wpj.bean;

/**
 * ClassName: User
 * Description:
 *
 * @author JieKaMi
 * @version 1.0
 * @date: 2020\1\8 0008 21:01
 * @since JDK 1.8
 */
public class User {
    private String name;
    private Double account;
    public User() {}
    public User(String name, Double account) {
        this.name = name;
        this.account = account;
    }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public Double getAccount() { return account; }
    public void setAccount(Double account) { this.account = account; }
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", account=" + account +
                '}';
    }
}

7.2.2 IUserDao3

(Module I of the project in a project's IUserDao and IUserDao2 repeated so named IUserDao3, behind one correspondence)
package com.wpj.dao;

/**
 * ClassName: IUserDao
 * Description:
 *
 * @author JieKaMi
 * @version 1.0
 * @date: 2020\1\8 0008 21:03
 * @since JDK 1.8
 */
public interface IUserDao3 {
     /**
     * 扣钱
     * @param outName  扣钱方
     * @param money    扣多少钱
     */
    void out(@Param("name") String outName,@Param("money") Double money);
    /**
     * 得钱
     * @param inName 得钱方
     * @param money  得多少钱
     */
    void in(@Param("name") String inName,@Param("money") Double money);
}

7.2.3 IUserDao3.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.wpj.dao.IUserDao3">
    <update id="out">
        UPDATE user SET account=account-#{money} WHERE name=#{name}
    </update>
    <update id="in">
        UPDATE user SET account=account+#{money} WHERE name=#{name}
    </update>
</mapper>

7.2.4 IUserService3

package com.wpj.service;

/**
 * ClassName: IUserService3
 * Description:
 *
 * @author JieKaMi
 * @version 1.0
 * @date: 2020\1\8 0008 21:11
 * @since JDK 1.8
 */
public interface IUserService3 {
	// 记住这个方法名
    void transfer(String outName,String inName,Double money);

}

7.2.5 UserServiceImpl3

package com.wpj.service.impl;

import com.wpj.dao.*;
import com.wpj.service.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;

/**
 * ClassName: UserServiceImpl3
 * Description:
 *
 * @author JieKaMi
 * @version 1.0
 * @date: 2020\1\8 0008 21:12
 * @since JDK 1.8
 */
@Service
public class UserServiceImpl3 implements IUserService3 {

    @Autowired
    private IUserDao3 iUserDao3;

    public void transfer(String outName, String inName, Double money) {

        System.out.println("正在扣钱。。。。");
        iUserDao3.out(outName,money);
		// 模拟错误
		int i = 10 / 0;		

        System.out.println("正在得钱。。。。");
        iUserDao3.in(inName,money);
        System.out.println("操作完成。。。。");
    }
}

7.2.5 Test

import com.wpj.bean.*;
import com.wpj.service.*;
import org.apache.ibatis.session.*;
import org.junit.*;
import org.junit.runner.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.test.context.*;
import org.springframework.test.context.junit4.*;

import javax.sql.*;
import java.util.*;

/**
 * ClassName: SSMTest
 * Description:
 *
 * @author JieKaMi
 * @version 1.0
 * @date: 2020\1\8 0008 17:51
 * @since JDK 1.8
 */

@RunWith(value= SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:spring-context.xml")
public class SSMTest {

    @Autowired
    private IUserService3 iUserService3;

    @Test
    public void testTransfer(){
        iUserService3.transfer("czh","wpj",1000.0);
    }
}

7.2.6 results

Here Insert Picture Description
Wrong to do so being given out, if the transaction is successfully configured, it should be rolled back the transaction, that data will not change, but to see the implementation of deducting money database found exception occurs, the transaction is not even roll back! It is a question of affairs? Definitely ~. Let's look at the configuration of the transaction profile spring-context.xml
Here Insert Picture Description
this time only need to modify the service method named affairs policy is matched on the line, or add a write operation.

To be continued. . . .
  1. Spring and SpringMVC integration [SSM] integration three articles. SSM integration affairs + + unit test cases end (four chapters)
Published 42 original articles · won praise 8 · views 2438

Guess you like

Origin blog.csdn.net/TheNew_One/article/details/103897975