spring之事务管理

1.spring简介
spring中认为一切java类都是资源,而资源都是Bean,容纳这些Bean的是spring提供的Ioc容器,所以Spring是一种基于bean的编程。spring的作用主要是整合框架。

2.spring中的事务管理,首先事务的基本概念就是一处报错,全部回滚。这也是spring事务管理的基本作用。

3.spring事务管理分为xml 跟 注解

案例:

(1)实体类Employee.java

package com.gb.pojo;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Component;
@Component("emp")
public class Employee {

private Integer id;
private String realName;
private SexEnum sex;
private Date birthday;
private String mobile;
private String email;
private String positon;
private String note;

private WorkCard workCard;
private List taskList;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getRealName() {
    return realName;
}

public void setRealName(String realName) {
    this.realName = realName;
}

public SexEnum getSex() {
    return sex;
}

public void setSex(SexEnum sex) {
    this.sex = sex;
}

public Date getBirthday() {
    return birthday;
}

public void setBirthday(Date birthday) {
    this.birthday = birthday;
}

public String getMobile() {
    return mobile;
}

public void setMobile(String mobile) {
    this.mobile = mobile;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPositon() {
    return positon;
}

public void setPositon(String positon) {
    this.positon = positon;
}

public String getNote() {
    return note;
}

public void setNote(String note) {
    this.note = note;
}

public WorkCard getWorkCard() {
    return workCard;
}

public void setWorkCard(WorkCard workCard) {
    this.workCard = workCard;
}

public List getTaskList() {
    return taskList;
}

public void setTaskList(List taskList) {
    this.taskList = taskList;
}

@Override
public String toString() {
    return "Employee [id=" + id + ", realName=" + realName + ", sex=" + sex
            + ", birthday=" + birthday + ", mobile=" + mobile + ", email="
            + email + ", positon=" + positon + ", note=" + note
            + ", workCard=" + workCard + ", taskList=" + taskList + "]";
}
    }

(2)mapper接口类:EmployeeMapper -----这里主要测试update方法

package com.gb.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.gb.pojo.EmpParames;
import com.gb.pojo.Employee;

public interface EmployeeMapper {
Employee findEmployeeById(Integer id);

/**
 * 根据名字查询员工,名字可以传,也可以不传,当传时,名字需要作为条件
 * 当不传时(empName为null或者空串),就不需要名字作为条件
 * @param empName
 * @return
 */
List findEmployeeByName(String empName);
  //    List findEmployeeByName(@Param("ename") String empName);

/**
 * 根据姓名与电话查询员工
 * 姓名可以传也可以不传
 * 电话可以传也可以不传,如果传的话,要以所传的内容作为开头
 * @param empPara
 * @return
 */
List findEmployeeByBean(EmpParames empPara);

/**
 * 根据id,name,tel进行查询
 * 如果id不为0或者null,则根据id查询
 * 如果id为0或为null且name不为null与空串,则根据name进行模糊查询
 * 如果以上都不符合,那么就查询出电话不为空的员工
 * 
 * @param id
 * @param name
 * @param tel
 * @return
 */
List findEmployeeChoose(@Param("id") Integer id,@Param("name") String name);

/**
 * 更新员工信息
 * @param emp
 * @return
 */
int updateEmployee(Employee emp);

/**
 * 查询所有员工编号在集合idList中的员工信息
 * @param idList
 * @return
 */
List findEmployeeInList(List idList);

/**
 * 判断出入的字符串是否是'X',
 * 如果是'X'的话,就查询姓名包含X的员工信息,
 * 否则就查询所有员工信息
 * @param str
 * @return
 */
List findEmployeeByStringEqual(String str);}

(3)EmployeeMapper.xml配置文件: 自行甄别





    
    
    
    
    
    
    
    
    
    
    
        
        
    



    



    













    update t_employee
    
        
            real_name= #{realName},
        
        
            sex=#{sex},
        

        
            birthday=#{birthday},
        
        
            mobile=#{mobile},
        

        
            email=#{email},
        
        
            positon=#{positon},
        
        
            note=#{note}
        
    
    where id=#{id}







(4) 向上写 Service层,EmployeeService.java

package com.gb.service;

import com.gb.pojo.Employee;

public interface EmployeeService {
Employee findEmployeeById(Integer id);
int updateEmployee(Employee emp);
}

service接口类实现类:EmployeeServiceImpl.java

package com.gb.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.gb.mapper.EmployeeMapper;
import com.gb.pojo.Employee;

@Component
public class EmployeeServiceImpl implements EmployeeService {

@Autowired
private EmployeeMapper employeeMapper;


public void setEmployeeMapper(EmployeeMapper employeeMapper) {
    this.employeeMapper = employeeMapper;
}



@Transactional(readOnly=true,propagation=Propagation.REQUIRED, isolation=Isolation.READ_UNCOMMITTED)
@Override
public Employee findEmployeeById(Integer id) {
    return employeeMapper.findEmployeeById(id);
}


@Transactional
@Override
public int updateEmployee(Employee emp) {
    return employeeMapper.updateEmployee(emp);
}
    }

(5):Controller层, EmployeeController.java

package com.gb.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.gb.pojo.Employee;
import com.gb.service.EmployeeService;

@Component
public class EmployeeController {

@Autowired
private EmployeeService employeeService;

public void setEmployeeService(EmployeeService employeeService) {
    this.employeeService = employeeService;
}


public Employee getEmployeeById(Integer id) {
    return employeeService.findEmployeeById(id);
}

public int updateEmployee(Employee emp){
    return employeeService.updateEmployee(emp);
}
    }

(6)spring-context.xml配置文件










    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    



    
    




    



    






(7)mybatis-config.xml配置













 





 









(8)main方法

    private static void updateEmployee(){
    AbstractApplicationContext ac = new ClassPathXmlApplicationContext("spring-context.xml");

    EmployeeController ec = ac.getBean(EmployeeController.class);
   //       Employee emp = ec.getEmployeeById(2);
    
    Employee emp = (Employee) ac.getBean("emp");
  //        System.out.println(emp.getRealName());
    
    
    emp.setId(1);
    emp.setRealName("tuyiuouoih");
    
    int count = ec.updateEmployee(emp);
    System.out.println(count + "======================================");
    ac.close();
}

你可能感兴趣的:(spring之事务管理)