版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/linzhaoliangyan/article/details/88740416
* 有问题可以参加Java技术交流群:839366464
* 前期准备
* Student,Teacher,Classes对象没有变
* 在SM集成的基础上
* dao
public interface ClassesMapper {
/**
* 查询教html班级的信息(包含老师)
* @param cname
* @return
*/
public Classes queryClassesFromName(String cname);
/**
* cno : 班级号
* isAdd:不是增加学生就是减少学生,true为增加学生,false为减少学生
* @param map
*/
public void updateClasses(HashMap map);
}
package com.hx.hx02.dao;
import com.hx.hx02.bean.Student;
import java.util.HashMap;
import java.util.List;
/**
* @author xiaozhao
*/
public interface StudentMapper {
/**
* 查询某班级某学号学生信息
* @param map
* sid:学生姓名
* cno:班级名称
* @return map
*/
Student queryStudent(HashMap map);
/**
* 更新学生的班级号
* @param student
*/
void updateStudent(Student student);
}
<?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.hx.hx02.dao.ClassesMapper">
<select id="queryClassesFromName" parameterType="string" resultMap="rstc">
SELECT * FROM classes c,teacher t,student s WHERE c.tid=t.tid AND c.cno=s.cno AND c.cname=#{c.cname}
</select>
<resultMap id="rstc" type="Classes">
<id column="cno" property="cno"/>
<result column="cname" property="cname"/>
<result column="cnum" property="cnum"/>
<result column="cdes" property="cdes"/>
<association property="teacher" javaType="Teacher">
<id column="tid" property="tid"/>
<result column="tname" property="tname"/>
<result column="tsex" property="tsex"/>
<result column="tage" property="tage"/>
</association>
<collection property="students" ofType="Student">
<id column="sid" property="sid"/>
<result column="cno" property="cno"/>
<result column="sname" property="sname"/>
<result column="ssex" property="ssex"/>
<result column="sage" property="sage"/>
<result column="colleage" property="colleage"/>
</collection>
</resultMap>
<!-- 方式一 -->
<!--<select id="queryClassesFromName" parameterType="string" resultMap="rstc">
SELECT * FROM classes c WHERE c.cname=#{cname}
</select>
<resultMap id="rstc" type="Classes">
<id column="cno" property="cno"/>
<result column="cname" property="cname"/>
<result column="cnum" property="cnum"/>
<result column="cdes" property="cdes"/>
<association property="teacher" column="tid" select="getTeacher"></association>
</resultMap>
<select id="getTeacher" parameterType="int" resultType="Teacher">
SELECT * FROM teacher t WHERE t.tid=#{tid}
</select>-->
<update id="updateClasses" parameterType="hashMap">
UPDATE classes
<set>
<choose>
<when test="isAdded==true">
cnum=cnum+1
</when>
<otherwise>
cnum=cnum-1
</otherwise>
</choose>
</set>
<where>
<if test="cno!=null">
cno=#{cno}
</if>
</where>
</update>
</mapper>
<?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.hx.hx02.dao.StudentMapper">
<select id="queryStudent" parameterType="Student" resultType="Student">
SELECT sid,cno,sname,ssex,sage,colleage FROM student WHERE sid=#{sid} AND cno=#{cno}
</select>
<update id="updateStudent" parameterType="Student">
UPDATE student SET cno=#{cno} WHERE sid=#{sid}
</update>
</mapper>
* 业务层传递数据的实体
/**
* @author xiaozhao
*/
public class TransferDataVo {
private int sid;
private int oldCno;
private int newCno;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public int getOldCno() {
return oldCno;
}
public void setOldCno(int oldCno) {
this.oldCno = oldCno;
}
public int getNewCno() {
return newCno;
}
public void setNewCno(int newCno) {
this.newCno = newCno;
}
@Override
public String toString() {
return "TransferDataVo{" +
"sid=" + sid +
", oldCno=" + oldCno +
", newCno=" + newCno +
'}';
}
}
* 业务层
public interface TransferService {
/**
* 某个学生转班
* @param dataVo
* @return
*/
public boolean transfer(TransferDataVo dataVo);
}
package com.hx.hx02.service.impl;
import com.hx.hx02.bean.Student;
import com.hx.hx02.bean.vo.TransferDataVo;
import com.hx.hx02.dao.ClassesMapper;
import com.hx.hx02.dao.StudentMapper;
import com.hx.hx02.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
/**
* @author xiaozhao
*/
@Service
public class TransferServiceImpl implements TransferService {
@Autowired
private ClassesMapper mClassesMapper;
@Autowired
private StudentMapper mStudentMapper;
@Override
public boolean transfer(TransferDataVo dataVo) {
HashMap map=new HashMap();
map.put("cno",dataVo.getOldCno());
map.put("sid",dataVo.getSid());
Student student = mStudentMapper.queryStudent(map);
if(student!=null){
// 设置学生为新班级
student.setCno(dataVo.getNewCno());
mStudentMapper.updateStudent(student);
// 更新需要转班班级的人数
map.put("cno",dataVo.getOldCno());
map.put("isAdded",false);
mClassesMapper.updateClasses(map);
// 更新被转班班级的人数
map.put("cno",dataVo.getNewCno());
map.put("isAdded",true);
mClassesMapper.updateClasses(map);
return true;
}else{
System.out.println(dataVo.getOldCno()+"班级没有存在"+dataVo.getSid()+"学生");
}
return false;
}
}
* 测试
@Autowired
private TransferService mTransferService;
@Test
public void test6(){
TransferDataVo transferDataVo=new TransferDataVo();
transferDataVo.setSid(100001);
transferDataVo.setOldCno(1001);
transferDataVo.setNewCno(1002);
mTransferService.transfer(transferDataVo);
}
* 结果可以正确的更新数据
* 模拟异常,继续测试
* 添加事务管理
* 添加事务的命名工具
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
* 添加事务管理器
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="c3p0"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
* 配置
@Transactional
* 测试ok
* 有问题可以参加Java技术交流群:839366464