博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
利用MyBatis实现条件查询
阅读量:3971 次
发布时间:2019-05-24

本文共 2735 字,大约阅读时间需要 9 分钟。

目录

一、查询数据准备

  1. 对学生表进行条件查询,涉及姓名、性别和年龄三个字段。
    在这里插入图片描述
  2. 打开MyBatisDemo项目
    在这里插入图片描述

二、对学生表进行条件查询

1、创建学生映射器配置文件

1.在resources/mapper目录里创建学生映射器配置文件 - StudentMapper.xml

在这里插入图片描述

2、在MyBatis配置文件里注册学生映射器配置文件

  1. 在元素里添加子元素
    在这里插入图片描述

3、创建学生映射器接口

在这里插入图片描述

package net.zjs.mybatis.mapper;/* * 功能:学生映射器接口 * */import net.zjs.mybatis.bean.Student;import java.util.List;import java.util.Map;public interface StudentMapper {    List
findByCondition(Map
condition);}

4、创建测试类TestStudentMapper

在这里插入图片描述

package net.zjs.mybatis.mapper;import net.zjs.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;import java.util.Map;public class TestStudentMapper {    private SqlSession sqlSession; // SQL会话    private StudentMapper studentMapper; // 学生映射器    @Before    public void init() {        try {            // 读取MyBatis配置文件作为字符输入流            Reader reader = Resources.getResourceAsReader("mybatis-config.xml");            // 基于MyBatis配置文件构建SQL会话工厂            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);            // 利用SQL会话工厂获取SQL会话            sqlSession = factory.openSession();            // 利用SQL会话获取学生映射器对象            studentMapper = sqlSession.getMapper(StudentMapper.class);            // 提示用户SQL会话对象创建成功            System.out.println("sqlSession对象已创建。");        } catch (IOException e) {            e.printStackTrace();        }    }    @Test    public void testFindByCondition(){        Map
condition=new HashMap<>(); condition.put("gender","女"); List
students = studentMapper.findByCondition(condition); students.forEach(student -> System.out.println(student)); } @After public void destroy() { // 关闭SQL会话 sqlSession.close(); // 提示用户SQL会话对象关闭 System.out.println("sqlSession对象已关闭。"); }}

1、查询性别为女的记录

  1. 添加测试方法testFindByCondition()
    在这里插入图片描述
  2. 运行测试方法testFindByCondition(),查看结果
    在这里插入图片描述

查询年龄为19岁的女生

  1. 修改测试方法里的查询条件

    在这里插入图片描述

  2. 运行测试方法testFindByCondition(),查看结果

    在这里插入图片描述

查询姓唐的19岁女生

  1. 修改测试方法里的查询条件

在这里插入图片描述

  1. 运行测试方法testFindByCondition(),查看结果
    在这里插入图片描述

查找姓张的19岁女生

  1. 修改测试方法里的查询条件

    在这里插入图片描述

  2. 运行测试方法testFindByCondition(),查看结果

    在这里插入图片描述

转载地址:http://hjtki.baihongyu.com/

你可能感兴趣的文章
P6-c++内存模型和名称空间-02存储连续性、作用域和链接性
查看>>
P9-c++对象和类-02构造函数和析构函数总结
查看>>
P10-c++对象和类-03this指针详细介绍,详细的例子演示
查看>>
ksh 命令分隔符
查看>>
sed 精萃
查看>>
awk 精萃
查看>>
awk 注释
查看>>
GROUPING SETS、ROLLUP、CUBE
查看>>
数据类型和变量
查看>>
表连接(JOIN)
查看>>
游标(Cursor)
查看>>
复合语句(compound statement)
查看>>
DB2 物化查询表
查看>>
IF 语句
查看>>
循环语句
查看>>
DB2 临时表
查看>>
ITERATE、LEAVE、GOTO和RETURN
查看>>
异常处理
查看>>
存储过程
查看>>
动态SQL(Dynamic SQL)
查看>>