package com.saddam.service;
import com.saddam.pojo.Stu;
public interface StuService {
public Stu getStuInfo(int id);
public void saveStu();
public void updateStu(int id);
public void deleteStu(int id);
}
impl
java复制代码
package com.saddam.service.impl;
import com.saddam.mapper.StuMapper;
import com.saddam.pojo.Stu;
import com.saddam.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
public class StuServiceImpl implements StuService {
@Autowired
public StuMapper stuMapper;
@Transactional(propagation = Propagation.SUPPORTS)
@Override
public Stu getStuInfo(int id) {
return stuMapper.selectByPrimaryKey(id);
}
@Transactional(propagation = Propagation.REQUIRED)
@Override
public void saveStu() {
Stu stu = new Stu();
stu.setName("jack");
stu.setAge(19);
stuMapper.insert(stu);
}
@Transactional(propagation = Propagation.REQUIRED)
@Override
public void updateStu(int id) {
Stu stu = new Stu();
stu.setId(id);
stu.setName("lucy");
stu.setAge(20);
stuMapper.updateByPrimaryKey(stu);
}
@Transactional(propagation = Propagation.REQUIRED)
@Override
public void deleteStu(int id) {
stuMapper.deleteByPrimaryKey(id);
}
}
pojo
java复制代码
package com.saddam.pojo;
import javax.persistence.Id;
public class Stu {
@Id
private Integer id;
private String name;
private Integer age;
/**
* @return id
*/
public Integer getId() {
return id;
}
/**
* @param id
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return name
*/
public String getName() {
return name;
}
/**
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* @return age
*/
public Integer getAge() {
return age;
}
/**
* @param age
*/
public void setAge(Integer age) {
this.age = age;
}
}