|
|
|
|
@ -0,0 +1,232 @@
|
|
|
|
|
package com.ssm.test;
|
|
|
|
|
|
|
|
|
|
import com.ssm.entity.Dyguanli;
|
|
|
|
|
import com.ssm.mapper.DyguanliMapper;
|
|
|
|
|
import org.apache.ibatis.io.Resources;
|
|
|
|
|
import org.apache.ibatis.session.SqlSession;
|
|
|
|
|
import org.apache.ibatis.session.SqlSessionFactory;
|
|
|
|
|
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
|
|
|
|
|
import org.junit.After;
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 电影管理模块更新测试类
|
|
|
|
|
* 测试根据ID更新电影信息功能(使用动态SQL)
|
|
|
|
|
*/
|
|
|
|
|
public class TestUpdateDyguanliById {
|
|
|
|
|
|
|
|
|
|
private SqlSession sqlSession;
|
|
|
|
|
private DyguanliMapper dyguanliMapper;
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void setUp() throws Exception {
|
|
|
|
|
// 1. 加载MyBatis配置文件
|
|
|
|
|
InputStream inputStream = Resources.getResourceAsStream("config.xml");
|
|
|
|
|
|
|
|
|
|
// 2. 创建SqlSessionFactory
|
|
|
|
|
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
|
|
|
|
|
|
|
|
|
|
// 3. 创建SqlSession
|
|
|
|
|
sqlSession = sqlSessionFactory.openSession();
|
|
|
|
|
|
|
|
|
|
// 4. 获取Mapper接口实现
|
|
|
|
|
dyguanliMapper = sqlSession.getMapper(DyguanliMapper.class);
|
|
|
|
|
|
|
|
|
|
System.out.println("========== 测试环境准备完成 ==========");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@After
|
|
|
|
|
public void tearDown() {
|
|
|
|
|
// 关闭SqlSession
|
|
|
|
|
if (sqlSession != null) {
|
|
|
|
|
sqlSession.close();
|
|
|
|
|
}
|
|
|
|
|
System.out.println("========== 测试环境清理完成 ==========");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 测试根据ID更新电影信息(全量更新)
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testUpdateDyguanliById() {
|
|
|
|
|
System.out.println("\n========== 测试:根据ID更新电影信息(全量更新) ==========");
|
|
|
|
|
|
|
|
|
|
// 首先查询要更新的电影信息
|
|
|
|
|
Integer updateId = 18;
|
|
|
|
|
Dyguanli existingMovie = dyguanliMapper.selectDyguanliById(updateId);
|
|
|
|
|
|
|
|
|
|
if (existingMovie == null) {
|
|
|
|
|
System.out.println("未找到ID为" + updateId + "的电影信息,无法更新!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.println("更新前的电影信息:");
|
|
|
|
|
System.out.println("ID: " + existingMovie.getId());
|
|
|
|
|
System.out.println("电影名称: " + existingMovie.getMovieName());
|
|
|
|
|
System.out.println("导演: " + existingMovie.getDirector());
|
|
|
|
|
System.out.println("主演: " + existingMovie.getActor());
|
|
|
|
|
System.out.println("上映日期: " + existingMovie.getReleaseDate());
|
|
|
|
|
System.out.println("时长: " + existingMovie.getDuration() + "分钟");
|
|
|
|
|
System.out.println("类型: " + existingMovie.getGenre());
|
|
|
|
|
|
|
|
|
|
// 创建更新后的电影对象
|
|
|
|
|
Dyguanli updateMovie = new Dyguanli();
|
|
|
|
|
updateMovie.setId(updateId);
|
|
|
|
|
updateMovie.setMovieName(existingMovie.getMovieName() + "(已更新)");
|
|
|
|
|
updateMovie.setDirector(existingMovie.getDirector());
|
|
|
|
|
updateMovie.setActor(existingMovie.getActor() + ",新加入演员");
|
|
|
|
|
updateMovie.setReleaseDate(existingMovie.getReleaseDate());
|
|
|
|
|
updateMovie.setDuration(existingMovie.getDuration() + 10);
|
|
|
|
|
updateMovie.setGenre(existingMovie.getGenre());
|
|
|
|
|
|
|
|
|
|
System.out.println("\n--- 执行更新操作 ---");
|
|
|
|
|
// 执行更新操作
|
|
|
|
|
int rows = dyguanliMapper.updateDyguanliById(updateMovie);
|
|
|
|
|
|
|
|
|
|
// 提交事务
|
|
|
|
|
sqlSession.commit();
|
|
|
|
|
|
|
|
|
|
if (rows > 0) {
|
|
|
|
|
System.out.println("更新成功!影响了 " + rows + " 行");
|
|
|
|
|
|
|
|
|
|
// 再次查询验证更新结果
|
|
|
|
|
Dyguanli updatedMovie = dyguanliMapper.selectDyguanliById(updateId);
|
|
|
|
|
System.out.println("\n更新后的电影信息:");
|
|
|
|
|
System.out.println("ID: " + updatedMovie.getId());
|
|
|
|
|
System.out.println("电影名称: " + updatedMovie.getMovieName());
|
|
|
|
|
System.out.println("导演: " + updatedMovie.getDirector());
|
|
|
|
|
System.out.println("主演: " + updatedMovie.getActor());
|
|
|
|
|
System.out.println("上映日期: " + updatedMovie.getReleaseDate());
|
|
|
|
|
System.out.println("时长: " + updatedMovie.getDuration() + "分钟");
|
|
|
|
|
System.out.println("类型: " + updatedMovie.getGenre());
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("更新失败!");
|
|
|
|
|
sqlSession.rollback();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 测试更新不存在的ID
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testUpdateNonExistentId() {
|
|
|
|
|
System.out.println("\n========== 测试:更新不存在的ID ==========");
|
|
|
|
|
|
|
|
|
|
Dyguanli updateMovie = new Dyguanli();
|
|
|
|
|
updateMovie.setId(999);
|
|
|
|
|
updateMovie.setMovieName("不存在的电影");
|
|
|
|
|
updateMovie.setDirector("未知导演");
|
|
|
|
|
updateMovie.setActor("未知演员");
|
|
|
|
|
updateMovie.setReleaseDate("2026-01-01");
|
|
|
|
|
updateMovie.setDuration(120);
|
|
|
|
|
updateMovie.setGenre("剧情");
|
|
|
|
|
|
|
|
|
|
int rows = dyguanliMapper.updateDyguanliById(updateMovie);
|
|
|
|
|
sqlSession.commit();
|
|
|
|
|
|
|
|
|
|
if (rows == 0) {
|
|
|
|
|
System.out.println("正确!ID为999的电影不存在,更新影响行数为0");
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("异常!更新了 " + rows + " 行");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 测试部分字段更新(动态SQL特性)
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testPartialUpdate() {
|
|
|
|
|
System.out.println("\n========== 测试:部分字段更新(动态SQL) ==========");
|
|
|
|
|
|
|
|
|
|
// 首先查询要更新的电影信息
|
|
|
|
|
Integer updateId = 1;
|
|
|
|
|
Dyguanli existingMovie = dyguanliMapper.selectDyguanliById(updateId);
|
|
|
|
|
|
|
|
|
|
if (existingMovie == null) {
|
|
|
|
|
System.out.println("未找到ID为" + updateId + "的电影信息,无法更新!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.println("更新前的电影信息:");
|
|
|
|
|
System.out.println("ID: " + existingMovie.getId());
|
|
|
|
|
System.out.println("电影名称: " + existingMovie.getMovieName());
|
|
|
|
|
System.out.println("导演: " + existingMovie.getDirector());
|
|
|
|
|
System.out.println("主演: " + existingMovie.getActor());
|
|
|
|
|
System.out.println("上映日期: " + existingMovie.getReleaseDate());
|
|
|
|
|
System.out.println("时长: " + existingMovie.getDuration() + "分钟");
|
|
|
|
|
System.out.println("类型: " + existingMovie.getGenre());
|
|
|
|
|
|
|
|
|
|
// 只更新部分字段(只设置要更新的字段,其他字段保持null)
|
|
|
|
|
Dyguanli partialUpdate = new Dyguanli();
|
|
|
|
|
partialUpdate.setId(updateId);
|
|
|
|
|
partialUpdate.setDuration(150); // 只更新时长
|
|
|
|
|
partialUpdate.setGenre("动作/科幻"); // 只更新类型
|
|
|
|
|
// 其他字段保持null,不会被更新
|
|
|
|
|
|
|
|
|
|
System.out.println("\n--- 执行部分字段更新(只更新时长和类型)---");
|
|
|
|
|
int rows = dyguanliMapper.updateDyguanliById(partialUpdate);
|
|
|
|
|
sqlSession.commit();
|
|
|
|
|
|
|
|
|
|
if (rows > 0) {
|
|
|
|
|
System.out.println("部分更新成功!影响了 " + rows + " 行");
|
|
|
|
|
|
|
|
|
|
// 再次查询验证更新结果
|
|
|
|
|
Dyguanli updatedMovie = dyguanliMapper.selectDyguanliById(updateId);
|
|
|
|
|
System.out.println("\n更新后的电影信息:");
|
|
|
|
|
System.out.println("ID: " + updatedMovie.getId());
|
|
|
|
|
System.out.println("电影名称: " + updatedMovie.getMovieName() +
|
|
|
|
|
(updatedMovie.getMovieName().equals(existingMovie.getMovieName()) ? " (未改变)" : ""));
|
|
|
|
|
System.out.println("导演: " + updatedMovie.getDirector() +
|
|
|
|
|
(updatedMovie.getDirector().equals(existingMovie.getDirector()) ? " (未改变)" : ""));
|
|
|
|
|
System.out.println("主演: " + updatedMovie.getActor() +
|
|
|
|
|
(updatedMovie.getActor().equals(existingMovie.getActor()) ? " (未改变)" : ""));
|
|
|
|
|
System.out.println("上映日期: " + updatedMovie.getReleaseDate() +
|
|
|
|
|
(updatedMovie.getReleaseDate().equals(existingMovie.getReleaseDate()) ? " (未改变)" : ""));
|
|
|
|
|
System.out.println("时长: " + updatedMovie.getDuration() + "分钟" +
|
|
|
|
|
(!updatedMovie.getDuration().equals(existingMovie.getDuration()) ? " (已更新)" : ""));
|
|
|
|
|
System.out.println("类型: " + updatedMovie.getGenre() +
|
|
|
|
|
(!updatedMovie.getGenre().equals(existingMovie.getGenre()) ? " (已更新)" : ""));
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("更新失败!");
|
|
|
|
|
sqlSession.rollback();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 测试只更新单个字段
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testSingleFieldUpdate() {
|
|
|
|
|
System.out.println("\n========== 测试:单字段更新 ==========");
|
|
|
|
|
|
|
|
|
|
Integer updateId = 1;
|
|
|
|
|
Dyguanli existingMovie = dyguanliMapper.selectDyguanliById(updateId);
|
|
|
|
|
|
|
|
|
|
if (existingMovie == null) {
|
|
|
|
|
System.out.println("未找到ID为" + updateId + "的电影信息,无法更新!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.println("更新前时长: " + existingMovie.getDuration() + "分钟");
|
|
|
|
|
|
|
|
|
|
// 只更新一个字段
|
|
|
|
|
Dyguanli singleFieldUpdate = new Dyguanli();
|
|
|
|
|
singleFieldUpdate.setId(updateId);
|
|
|
|
|
singleFieldUpdate.setDuration(180); // 只更新时长
|
|
|
|
|
|
|
|
|
|
int rows = dyguanliMapper.updateDyguanliById(singleFieldUpdate);
|
|
|
|
|
sqlSession.commit();
|
|
|
|
|
|
|
|
|
|
if (rows > 0) {
|
|
|
|
|
System.out.println("单字段更新成功!");
|
|
|
|
|
Dyguanli updatedMovie = dyguanliMapper.selectDyguanliById(updateId);
|
|
|
|
|
System.out.println("更新后时长: " + updatedMovie.getDuration() + "分钟");
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("更新失败!");
|
|
|
|
|
sqlSession.rollback();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|