diff --git a/film-MyBatisProject/src/com/ssm/test/TestDeleteDyguanliById.java b/film-MyBatisProject/src/com/ssm/test/TestDeleteDyguanliById.java new file mode 100644 index 0000000..7f635bf --- /dev/null +++ b/film-MyBatisProject/src/com/ssm/test/TestDeleteDyguanliById.java @@ -0,0 +1,179 @@ +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; +import java.util.List; + +/** + * 电影管理模块删除测试类 + * 测试根据ID删除电影信息功能 + */ +public class TestDeleteDyguanliById { + + 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 testDeleteDyguanliById() { + System.out.println("\n========== 测试:根据ID删除电影信息 =========="); + + // 首先查询所有电影,选择一个要删除的电影 + List movieList = dyguanliMapper.selectAllDyguanli(); + + if (movieList == null || movieList.isEmpty()) { + System.out.println("数据库中没有电影信息,无法执行删除测试!"); + return; + } + + // 显示当前所有电影 + System.out.println("删除前的电影列表:"); + for (int i = 0; i < movieList.size(); i++) { + Dyguanli movie = movieList.get(i); + System.out.println((i + 1) + ". ID: " + movie.getId() + " - " + movie.getMovieName()); + } + + // 选择最后一部电影进行删除测试(避免影响重要数据) + Dyguanli movieToDelete = movieList.get(movieList.size() - 1); + Integer deleteId = movieToDelete.getId(); + + System.out.println("\n--- 准备删除电影 ---"); + System.out.println("将要删除的电影ID: " + deleteId); + System.out.println("电影名称: " + movieToDelete.getMovieName()); + + // 执行删除操作 + int rows = dyguanliMapper.deleteDyguanliById(deleteId); + + // 提交事务 + sqlSession.commit(); + + if (rows > 0) { + System.out.println("\n删除成功!影响了 " + rows + " 行"); + + // 再次查询验证删除结果 + List remainingMovies = dyguanliMapper.selectAllDyguanli(); + System.out.println("\n删除后的电影列表:"); + if (remainingMovies != null && !remainingMovies.isEmpty()) { + for (int i = 0; i < remainingMovies.size(); i++) { + Dyguanli movie = remainingMovies.get(i); + System.out.println((i + 1) + ". ID: " + movie.getId() + " - " + movie.getMovieName()); + } + System.out.println("\n剩余电影数量: " + remainingMovies.size() + " 部"); + } else { + System.out.println("数据库中已没有电影信息!"); + } + + // 验证被删除的电影是否真的不存在了 + Dyguanli deletedMovie = dyguanliMapper.selectDyguanliById(deleteId); + if (deletedMovie == null) { + System.out.println("验证成功!ID为" + deleteId + "的电影已被彻底删除!"); + } else { + System.out.println("警告:ID为" + deleteId + "的电影仍然存在!"); + } + } else { + System.out.println("删除失败!"); + sqlSession.rollback(); + } + } + + /** + * 测试删除不存在的ID + */ + @Test + public void testDeleteNonExistentId() { + System.out.println("\n========== 测试:删除不存在的ID =========="); + + Integer nonExistentId = 999; + + // 先验证该ID确实不存在 + Dyguanli movie = dyguanliMapper.selectDyguanliById(nonExistentId); + if (movie == null) { + System.out.println("确认ID为" + nonExistentId + "的电影不存在"); + } + + // 执行删除操作 + int rows = dyguanliMapper.deleteDyguanliById(nonExistentId); + sqlSession.commit(); + + if (rows == 0) { + System.out.println("正确!ID为" + nonExistentId + "的电影不存在,删除影响行数为0"); + } else { + System.out.println("异常!删除了 " + rows + " 行"); + } + } + + /** + * 测试连续删除多个电影 + */ + @Test + public void testDeleteMultipleMovies() { + System.out.println("\n========== 测试:连续删除多个电影 =========="); + + List movieList = dyguanliMapper.selectAllDyguanli(); + + if (movieList == null || movieList.size() < 2) { + System.out.println("数据库中电影数量不足,无法执行批量删除测试!"); + return; + } + + System.out.println("删除前的电影数量: " + movieList.size() + " 部"); + + // 删除最后两个电影 + int deleteCount = Math.min(2, movieList.size()); + int totalRows = 0; + + for (int i = 0; i < deleteCount; i++) { + Integer deleteId = movieList.get(movieList.size() - 1 - i).getId(); + System.out.println("正在删除ID为 " + deleteId + " 的电影..."); + + int rows = dyguanliMapper.deleteDyguanliById(deleteId); + totalRows += rows; + } + + // 提交事务 + sqlSession.commit(); + + System.out.println("\n总共删除了 " + totalRows + " 部电影"); + + // 验证删除结果 + List remainingMovies = dyguanliMapper.selectAllDyguanli(); + System.out.println("删除后的电影数量: " + (remainingMovies != null ? remainingMovies.size() : 0) + " 部"); + } +} diff --git a/film-MyBatisProject/src/com/ssm/test/TestUpdateDyguanliById.java b/film-MyBatisProject/src/com/ssm/test/TestUpdateDyguanliById.java new file mode 100644 index 0000000..1a9244a --- /dev/null +++ b/film-MyBatisProject/src/com/ssm/test/TestUpdateDyguanliById.java @@ -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(); + } + } +}