zoukankan      html  css  js  c++  java
  • MyBatis 中实现SQL语句中in的操作 (11)

    MyBatis 中实现SQL语句中in的操作

    概括:应用myBatis实现SQL查询中IN的操作

    1、数据库结构及其数据

     2、mapper.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="com.mybatis03.mapper.PersonMapper">   
     <!-- foreach 操作 -->
        <select id="queryPersonWithInIDs" parameterType="com.mybatis03.bean.PersonIDs" resultType="com.mybatis03.bean.Person">
            select * from  t_person_01
            <!-- where 只会处理第一个条件 -->
    <!--   
        1、PersonIDs 为集合类
        2、personNos  为PersonIDs集合类中的属性
        3、personNo 为遍历personNos  的临时变量(相当于 for循环中的i)
        4、separator 作用为 select * from  t_person_01 where in(1001,1002,1003) 中的分割符号逗号,既:“,”
      -->
            <where>
                <if test="personNos !=null and personNos.size >0 ">
                    <foreach collection="personNos" open=" and id in(" close=")" item="personNo" separator=",">
                      #{personNo}
                    </foreach>
                </if>
            </where>
        </select>
    
    </mapper>

    3、接口类

    /**
     * @author :jack.zhao
     * @Describe: 操作mybatis接口
     * @date :2021-10-16 22:55
     */
    public interface PersonMapper {   
    // 人员编号集合(根据人员id集合查询)select * from t_person_01 where id in(1001,1002,1003);
        List<Person> queryPersonWithInIDs(PersonIDs personIDs);
    }

    4、作为查询条件集合类

    package com.mybatis03.bean;
    
    import java.util.List;
    
    /**
     * @author :jack.zhao
     * @Describe: 查询条件集合类
     * @date :2021-10-16 22:55
     */
    public class PersonIDs {
    
        public List<Integer> getPersonNos() {
            return personNos;
        }
    
        public void setPersonNos(List<Integer> personNos) {
            this.personNos = personNos;
        }
    
        // 人员编号集合(根据人员id集合查询)select * from t_person_01 where id in(1001,1002,1003);
        private List<Integer> personNos;
    }

    5、测试类中测试方法

    /**
     * @author :jack.zhao
     * @Describe: 测试类
     * @date :2021-10-16 22:55
     */
    public class test03 {
     //  // 人员编号集合(根据人员id集合查询)select * from t_person_01 where id in(1001,1002,1003);
        @Test
        public void queryPersonWithInIDs() throws Exception{
            Reader reader = Resources.getResourceAsReader("mybatis-03.xml");
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
    
            SqlSession session = sessionFactory.openSession();
            PersonIDs personIDs = new PersonIDs();
    
            List<Integer> lists = new ArrayList<>();
            lists.add(1001);
            lists.add(1002);
            lists.add(1003);
            personIDs.setPersonNos(lists);
    
    
            // 动态代理
            PersonMapper personMapper = session.getMapper(PersonMapper.class);
            List<Person> personList = personMapper.queryPersonWithInIDs(personIDs);
            System.out.println("动态SQL查询结果为:"+personList);
            session.close();
        }
    }

    查询结果:

    "C:Program FilesJavajdk1.8.0_25injava" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:Users
    ewsoftAppDataRoamingJetBrainsIntelliJ IDEA 2017.3.5libidea_rt.jar=1182:C:Users
    ewsoftAppDataRoamingJetBrainsIntelliJ IDEA 2017.3.5in" -Dfile.encoding=UTF-8 -classpath "C:Users
    ewsoftAppDataRoamingJetBrainsIntelliJ IDEA 2017.3.5libidea_rt.jar;C:Users
    ewsoftAppDataRoamingJetBrainsIntelliJ IDEA 2017.3.5pluginsjunitlibjunit-rt.jar;C:Users
    ewsoftAppDataRoamingJetBrainsIntelliJ IDEA 2017.3.5pluginsjunitlibjunit5-rt.jar;D:downloadlibmavenTollTransfermic-repositoryorgjunitplatformjunit-platform-launcher1.5.2junit-platform-launcher-1.5.2.jar;D:downloadlibmavenTollTransfermic-repositoryorgapiguardianapiguardian-api1.1.0apiguardian-api-1.1.0.jar;D:downloadlibmavenTollTransfermic-repositoryorgjunitplatformjunit-platform-engine1.5.2junit-platform-engine-1.5.2.jar;D:downloadlibmavenTollTransfermic-repositoryorgopentest4jopentest4j1.2.0opentest4j-1.2.0.jar;D:downloadlibmavenTollTransfermic-repositoryorgjunitplatformjunit-platform-commons1.5.2junit-platform-commons-1.5.2.jar;D:downloadlibmavenTollTransfermic-repositoryorgjunitjupiterjunit-jupiter-engine5.5.2junit-jupiter-engine-5.5.2.jar;D:downloadlibmavenTollTransfermic-repositoryorgjunitjupiterjunit-jupiter-api5.5.2junit-jupiter-api-5.5.2.jar;D:downloadlibmavenTollTransfermic-repositoryorgjunitvintagejunit-vintage-engine5.5.2junit-vintage-engine-5.5.2.jar;D:downloadlibmavenTollTransfermic-repositoryjunitjunit4.12junit-4.12.jar;D:downloadlibmavenTollTransfermic-repositoryorghamcresthamcrest-core1.3hamcrest-core-1.3.jar;C:Program FilesJavajdk1.8.0_25jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_25jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_25jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_25jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_25jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_25jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_25jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_25jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_25jrelibext
    ashorn.jar;C:Program FilesJavajdk1.8.0_25jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_25jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_25jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_25jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_25jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_25jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_25jrelibjce.jar;C:Program FilesJavajdk1.8.0_25jrelibjfr.jar;C:Program FilesJavajdk1.8.0_25jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_25jrelibjsse.jar;C:Program FilesJavajdk1.8.0_25jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_25jrelibplugin.jar;C:Program FilesJavajdk1.8.0_25jrelib
    esources.jar;C:Program FilesJavajdk1.8.0_25jrelib
    t.jar;D:ideaworkspaceProjectStudymybatis-02	argetclasses;D:downloadlibmavenTollTransfermic-repositoryorghamcresthamcrest-core2.1hamcrest-core-2.1.jar;D:downloadlibmavenTollTransfermic-repositoryorghamcresthamcrest2.1hamcrest-2.1.jar;D:downloadlibmavenTollTransfermic-repositoryorgmybatismybatis3.4.5mybatis-3.4.5.jar;D:downloadlibmavenTollTransfermic-repositorymysqlmysql-connector-java5.1.44mysql-connector-java-5.1.44.jar;D:downloadlibmavenTollTransfermic-repositorycglibcglib3.3.0cglib-3.3.0.jar;D:downloadlibmavenTollTransfermic-repositoryorgow2asmasm7.1asm-7.1.jar;D:downloadlibmavenTollTransfermic-repository
    etlogstashlogbacklogstash-logback-encoder5.3logstash-logback-encoder-5.3.jar;D:downloadlibmavenTollTransfermic-repositorycomfasterxmljacksoncorejackson-databind2.10.3jackson-databind-2.10.3.jar;D:downloadlibmavenTollTransfermic-repositorycomfasterxmljacksoncorejackson-annotations2.10.3jackson-annotations-2.10.3.jar;D:downloadlibmavenTollTransfermic-repositorycomfasterxmljacksoncorejackson-core2.10.3jackson-core-2.10.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit5 com.mybatis03.test.test03,queryPersonWithInIDs
    动态SQL查询结果为:[Person{id=1001, name='zhangsan', age=27, sex=true, address=null}, Person{id=1002, name='chenqi', age=29, sex=true, address=null}, Person{id=1003, name='maliu', age=16, sex=true, address=null}]
    
    Process finished with exit code 0
  • 相关阅读:
    [ZJOI 2012]数列
    [JSOI 2011]柠檬
    sonar错误及修改方案
    mysql 查询该字段不包含中文
    service 事务
    mysql 不等于和null
    java文件编译为class文件
    主,副时间排序
    MySQL 按首字母排序
    excle导入
  • 原文地址:https://www.cnblogs.com/northeastTycoon/p/15422965.html
Copyright © 2011-2022 走看看