mybatis使⽤forupdate,对数据进⾏⾏级锁定与Spring Date JPA中使⽤⾏级锁⼀样,都需要加上事务,并在查询的时候加上for update。直接上代码:
数据库表:
create table t_pub_student(
id int PRIMARY key auto_increment,
code VARCHAR(50) COMMENT '学⽣CODE',
name VARCHAR(50) COMMENT '学⽣名字'
)
create table t_course_detail(
id int PRIMARY key auto_increment,
name VARCHAR(50) COMMENT '课程名称',
teacher_name VARCHAR(50) COMMENT '教师名字',
elective_total int COMMENT '可选总数',
elective_num int COMMENT '已选数量'
)
create table t_course_detail(
id int PRIMARY key auto_increment,
student_code varchar(50) COMMENT '学⽣code',
course_id int COMMENT '课程ID'
)
代码⽬录结构:
定义实体:
ity;
import batisplus.annotations.TableId; import batisplus.annotations.TableName; import ums.IdType;
import javax.persistence.*;
/**
* Created by Xichuan on 2018-10-31.
*/
@TableName("t_pub_student")
public class Student {
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
@Column(name = "code")
private String code;
@Column(name = "name")
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
ity;
import batisplus.annotations.TableId; import batisplus.annotations.TableName; import ums.IdType;
import javax.persistence.*;
/**
* Created by Xichuan on 2018-10-31.
*/
@TableName("t_pub_course")
public class Course {
@TableId(value = "id",type = IdType.AUTO)
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
@Column(name = "name")jpa mybatis
private String name;
@Column(name = "teacher_name")
private String teacherName;
@Column(name = "elective_total")
private Integer electiveTotal;
@Column(name = "elective_num")
private Integer electiveNum;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) { acherName = teacherName;
}
public Integer getElectiveTotal() {
return electiveTotal;
}
public void setElectiveTotal(Integer electiveTotal) { this.electiveTotal = electiveTotal;
}
public Integer getElectiveNum() {
return electiveNum;
}
public void setElectiveNum(Integer electiveNum) { this.electiveNum = electiveNum;
}
}
import batisplus.annotations.TableName; import ums.IdType;
import javax.persistence.*;
/**
* Created by Xichuan on 2018-10-31.
*/
@TableName("t_course_detail")
public class CourseDetail {
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
@Column(name = "course_id")
private Integer courseId;
@Column(name = "student_code")
private String studentCode;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getCourseId() {
return courseId;
}
public void setCourseId(Integer courseId) {
}
public String getStudentCode() {
return studentCode;
}
public void setStudentCode(String studentCode) {
this.studentCode = studentCode;
}
}
DAO层代码:
package course.mapper;
import batisplus.mapper.BaseMapper; ity.Student;
import org.apache.ibatis.annotations.Mapper;
/**
* Created by XiChuan on 2018-10-31.
*/
@Mapper
public interface StudentMapper extends BaseMapper<Student> { }
ity.CourseDetail;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface CourseDetailMapper extends BaseMapper<CourseDetail> {
}
package course.mapper;
import batisplus.mapper.BaseMapper;
ity.Course;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
/**
* Created by Xichuan on 2018-10-31.
*/
public interface CourseMapper extends BaseMapper<Course> {
/**将此⾏数据进⾏加锁,当整个⽅法将事务提交后,才会解锁*/
@Select(value = "select t from t_pub_course t where t.id = #{courseId} for update")
Course queryAllById(@Param("courseId") Integer courseId);
/**将course表中的electiveNum进⾏加1操作*/
@Update("update t_pub_course t set t.elective_num = t.elective_num + 1 where t.id = #{courseId}") void addElectiveNumByCourseId(@Param("courseId") Integer courseId);
}
定义接⼝:
ller;
import course.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by XiChuan on 2018-10-31.
*/
@RestController
public class CourseController {
@Autowired
CourseService courseService;
@PostMapping("/course/choose")
public Object chooseCourse(@RequestParam("student_code")String studentCode,
@RequestParam("course_id")Integer courseId){
return courseService.chooseCourse(studentCode,courseId);
}
}
service层代码:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论