关于后端:Mybatis系列教材-三-基础-MyBatis-多条件查询和模糊查询实例


步骤1:先运行,看到成果,再学习
步骤2:模拟和排错
步骤3:在前一步的根底上进行
步骤4:含糊查问
步骤5:多条件查问

步骤 1 : 先运行,看到成果,再学习

老规矩,先下载下载区(点击进入)的可运行我的项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的成果。

步骤 2 : 模拟和排错

在确保可运行我的项目可能正确无误地运行之后,再严格照着教程的步骤,对代码模拟一遍。
模拟过程不免代码有出入,导致无奈失去冀望的运行后果,此时此刻通过比拟正确答案 ( 可运行我的项目 ) 和本人的代码,来定位问题所在。
采纳这种形式,学习有成果,排错有效率,能够较为显著地晋升学习速度,跨过学习路上的各个槛。

举荐应用diffmerge软件,进行文件夹比拟。把你本人做的我的项目文件夹,和我的可运行我的项目文件夹进行比拟。
这个软件很牛逼的,能够晓得文件夹里哪两个文件不对,并且很显著地标记进去
这里提供了绿色装置和应用教程:diffmerge 下载和应用教程

步骤 3 : 在前一步的根底上进行

这一个知识点建设在Mybatis 入门教程的根底上进行。

步骤 4 : 含糊查问

1. 批改Category.xml,提供listCategoryByName查问语句

select * from   category_  where name like concat('%',#{0},'%')

concat(‘%’,#{0},’%’) 这是mysql的写法
如果是oracle,写法是

select * from   category_  where name like '%'||#{0}||'%'

2. 运行测试

List<Category> cs = session.selectList("listCategoryByName","cat");

  • Category.xml
  • TestMybatis.java
<?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.how2java.pojo">
    <select id="listCategoryByName"  parameterType="string" resultType="Category">
            select * from   category_  where name like concat('%',#{0},'%')
        </select>
</mapper>

package com.how2java;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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 com.how2java.pojo.Category;

public class TestMybatis {
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        List<Category> cs = session.selectList("listCategoryByName", "cat");
        for (Category c : cs) {
            System.out.println(c.getName());
        }
        session.commit();
        session.close();

    }
}

步骤 5 : 多条件查问

联合后面的含糊查问,多一个id>多少的条件
1. Category.xml 筹备sql语句

<select id="listCategoryByIdAndName"  parameterType="map" resultType="Category">
    select * from   category_  where id> #{id}  and name like concat('%',#{name},'%')
</select>

2. 测试代码
因为是多个参数,而selectList办法又只承受一个参数对象,所以须要把多个参数放在Map里,而后把这个Map对象作为参数传递进去

Map<String,Object> params = new HashMap<>();
params.put("id", 3);
params.put("name", "cat");
List<Category> cs = session.selectList("listCategoryByIdAndName",params);

  • Category.xml
  • TestMybatis.java
<?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.how2java.pojo">
    <select id="listCategoryByName"  resultType="Category">
            select * from   category_  where name like concat('%',#{0},'%')
        </select>
    <select id="listCategoryByIdAndName"  parameterType="map" resultType="Category">
            select * from   category_  where id> #{id}  and name like concat('%',#{name},'%')
        </select>
</mapper>

package com.how2java;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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 com.how2java.pojo.Category;

public class TestMybatis {

    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();

        Map<String,Object> params = new HashMap<>();
        params.put("id", 3);
        params.put("name", "cat");

        List<Category> cs = session.selectList("listCategoryByIdAndName",params);
        for (Category c : cs) {
            System.out.println(c.getName());
        }
        session.commit();
        session.close();
    }
}

更多内容,点击理解: https://how2j.cn/k/mybatis/mybatis-query/1107.html

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理