SSM框架学习05——MyBatis条件查询

一、对学生表进行条件查询,涉及姓名、性别和年龄。

1、创建学生表

在这里插入图片描述

2、创建学生实体类

在这里插入图片描述

二、创建学生映射文件StudentMapper.xml

在这里插入图片描述

<?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="net.cw.mybatis.mapper.StudentMapper">
    <select id="findByCondition" parameterType="java.util.Map" resultMap="studentResultMap">
        SELECT * FROM student
        <trim prefix="WHERE" prefixOverrides="AND|OR">
            <if test="name != null">
                s_name Like CONCAT(#{name},'%')
            </if>
            <if test="gender != null">
                AND s_gender = #{gender}
            </if>
            <if test="age != null">
                AND s_age = #{age}
            </if>
        </trim>
    </select>
 
    <resultMap id="studentResultMap" type="Student">
        <result column="s_id" property="id"/>
        <result column="s_name" property="name"/>
        <result column="s_gender" property="gender"/>
        <result column="s_age" property="age"/>
        <association property="clazz" column="class_id" javaType="Clazz"
                     select="getClazz"/>
    </resultMap>
 
    <select id="getClazz" resultType="Clazz">
        SELECT c_id id, c_name name FROM class WHERE c_id = #{id};
    </select>
 
</mapper>

三、在mybatis-config.xml文件里配置实体类别名和映射文件

在这里插入图片描述

四、 定义学生映射接口StudentMapper

在这里插入图片描述

package net.cw.mybatis.mapper;

import net.cw.mybatis.bean.Student;

import java.util.List;
import java.util.Map;

public interface StudentMapper {
    List<Student> findByCondition(Map<String, Object> condition);
}

五、 创建测试程序TestStudentMapper

在这里插入图片描述

package net.cw.mybatis.mapper;

import net.cw.mybatis.bean.Student;
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.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;

public class TestStudentMapper {
    private SqlSession session;
    private StudentMapper studentMapper;

    @Before
    public void init() {
        try {
            Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
            session = factory.openSession();
            studentMapper = session.getMapper(StudentMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testFindByCondition() {
        HashMap<String, Object> condition = new HashMap<String, Object>();
        condition.put("gender", "女");
        List<Student> students = studentMapper.findByCondition(condition);
        for (Student student : students) {
            System.out.println(student);
        }

    }

    @After
    public void destroy() {
        session.close();
    }
}

1、 运行testFindByCondition()方法,查看结果

在这里插入图片描述

2、修改查询条件,查询19岁的女生,并运行testFindByCondition()

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值