sqlsession.close()

它的close()干了啥?

<?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.example.mapper.StudentMapper"> <!-- 多条件查询 --> <select id="queryStudentByConditions" parameterType="map" resultType="student"> select * from student <where> <if test="name != null and name != ''"> name = #{name} </if> <if test="name == null or name == '' and major != null and major != ''"> major = #{major} </if> <if test="name == null or name == '' and major == null or major == ''"> student_number is not null </if> </where> </select> <!-- 单条件查询 --> <select id="queryStudentByIdLessThanFive" resultType="student"> select * from student where id < 5 </select> <!-- 查询选修了数学、化学、物理的学生 --> <select id="queryStudentsByCourses" resultType="student"> select s.* from student s join student_course sc on s.id = sc.student_id where sc.course_name in ('数学', '化学', '物理') group by s.id having count(distinct sc.course_name) = 3 </select> <!-- 用set元素修改张飞的专业为计算机 --> <update id="updateMajorForZhangFei"> update student <set> major = '计算机' </set> where name = '张飞' </update> </mapper> import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.HashMap; import java.util.List; import java.util.Map; public class StudentTest { @Test public void queryStudentByConditionsTest() { SqlSession sqlSession = MyBatisUtil.getSqlSession(); try { // 模拟不同的查询条件 Map<String, Object> params = new HashMap<>(); // 条件1:只输入姓名 params.put("name", "张三"); List<Student> students1 = sqlSession.getMapper(StudentMapper.class).queryStudentByConditions(params); // 条件2:只输入专业 params.clear(); params.put("major", "计算机科学"); List<Student> students2 = sqlSession.getMapper(StudentMapper.class).queryStudentByConditions(params); // 条件3:姓名和专业都为空 params.clear(); List<Student> students3 = sqlSession.getMapper(StudentMapper.class).queryStudentByConditions(params); // 输出结果 System.out.println("条件1查询结果:" + students1); System.out.println("条件2查询结果:" + students2); System.out.println("条件3查询结果:" + students3); } finally { sqlSession.close(); } } @Test public void queryStudentByIdLessThanFiveTest() { SqlSession sqlSession = MyBatisUtil.getSqlSession(); try { List<Student> students = sqlSession.getMapper(StudentMapper.class).queryStudentByIdLessThanFive(); System.out.println("id小于5的学生信息:" + students); } finally { sqlSession.close(); } } @Test public void queryStudentsByCoursesTest() { SqlSession sqlSession = MyBatisUtil.getSqlSession(); try { List<Student> students = sqlSession.getMapper(StudentMapper.class).queryStudentsByCourses(); System.out.println("选修了数学、化学、物理的学生:" + students); } finally { sqlSession.close(); } } @Test public void updateMajorForZhangFeiTest() { SqlSession sqlSession = MyBatisUtil.getSqlSession(); try { int count = sqlSession.getMapper(StudentMapper.class).updateMajorForZhangFei(); System.out.println("受影响的行数:" + count); sqlSession.commit(); } finally { sqlSession.close(); } } } 在上述代码运行成功后生成在idea里边运行代码成功的截图
最新发布
03-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值