MyBatis之动态SQL-2学生信息查询系统

一、需求分析

本案例要求利用本章所学知识完成一个学生信息查询系统,该系统要求实现2个以下功能。

1)单条件查询

查询出所有id值小于5的学生的信息;

2)多条件查询

当用户输入的学生姓名不为空,则只根据学生姓名进行学生信息的查询;

当用户输入的学生姓名为空,而学生专业不为空,则只根据学生专业进行学生信息的查询;

二、资料、视频教程

资料下载链接

学生信息查询系统

三、编码实现

1、搭建环境
1)数据库环境

mybatis数据库,t_student表

2)引入依赖

pom.xml文件

3)数据库连接的配置文件

src/main/resources,数据库连接的配置文件db.properties。

4)MyBatis核心配置文件

src/main/resources,MyBatis的核心配置文件mybatis-config.xml

5)数据封装类

src/main/java,新建com.sw.pojo包,新建Student类

public class Student {
    private Integer id;
    private String name;
    private String major;
    private String sno;
    //get、set
    //tostring
}
6)mapper接口

src/main/java创建com.sw.mapper包,新建StudentMapper接口。

7)映射文件

src/main/resources目录下,新建com/sw/mapper目录,StudentMapper.xml

2、单条件查询

查询出所有id值小于5的学生的信息

1)修改接口StudentMapper

List<Student> getListByIdCondition(List<Integer> ids);

2)修改映射文件CustomerMapper.xml

    <select id="getListByIdCondition" parameterType="java.util.List" resultType="Student">
        select * from t_student where id in
        <foreach collection="list" index="index" open="(" separator="," close=")" item="id">
            #{id}
        </foreach>
    </select>

3)测试

使用工具类MyBatisUtils的getSession方法来获得sqlSession

    public void testGetListByIdCondition() {
        List<Integer> ids = new ArrayList<Integer>();
        for (int i = 1; i < 5; i++) {
            ids.add(i);
        }
        List<Student> studentList = studentMapper.getListByIdCondition(ids);
        for (Student student:studentList) {
            System.out.println(student);
        }
    }
3、多条件查询

当用户输入的学生姓名不为空,则只根据学生姓名进行学生信息的查询;

当用户输入的学生姓名为空,而学生专业不为空,则只根据学生专业进行学生信息的查询;

否则返回学号不为空的学生信息

1)修改接口CustomerMapper

List<Student> getListByStudent(Student student);

2)修改映射文件CustomerMapper.xml

    <select id="getListByStudent" parameterType="Student" resultType="Student">
        select * from t_student
        <where>
            <choose>
                <when test="name !=null and name !=''">
                    and name like concat('%',#{name}, '%')
                </when>
                <when test="major !=null and major !=''">
                    and major= #{major}
                </when>
                <otherwise>
                    and sno is not null
                </otherwise>
            </choose>
        </where>
    </select>

3)测试

    public void testGetListByChoose() {
        Student student = new Student();
//        student.setName("张");
//        student.setMajor("英语");
        List<Student> studentList = studentMapper.getListByChoose(student);
        for (Student s:studentList) {
            System.out.println(s);
        }
    }

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

勇 士 Teacher

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值