MyBatis 中使用 list作為參數進行傳遞查詢MYSQL (13)


MyBatis 中使用 list作為參數進行傳遞查詢MYSQL,傳遞的list內容為:將多個元素放在List<Integer> 中List 添加的數據值為 add.(1001);add.(1002);add.(1003)

1、mapper.xml 文件內容

    <!-- foreach 操作 將多個元素放在List<Integer> 中List 添加的數據值為 add.(1001);add.(1002);add.(1003)-->
    <!-- 使用 list作為參數進行傳遞時,在mapper.xml中,必須用list -->
    <select id="queryPersonWithList" parameterType="list" resultType="com.mybatis03.bean.Person">
        select * from  t_person_01
        <!-- where 只會處理第一個條件 -->
        <where>
            <if test="list !=null and list.size() >0 ">
                <foreach collection="list" open=" and id in(" close=")" item="personNo" separator=",">
                    #{personNo}
                </foreach>
            </if>
        </where>
    </select>

2、接口類

/**
 * @author :jack.zhao
 * @Describe: 操作mybatis接口
 * @date :2021-10-16 22:55
 */
public interface PersonMapper { 
 List<Person> queryPersonWithList(List<Integer> intList);
}

3、實體類

package com.mybatis03.bean;

/**
 * @author :jack.zhao
 * @Describe: 實體類
 * @date :2021-10-16 22:55
 */
public class  Person {

    private int id;
    private String name;
    private int age;
    private Boolean sex;

    private Address address;

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Person() {
    }

    public Person(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Person(int id, String name, int age, Boolean sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }



    public Boolean getSex() {
        return sex;
    }

    public void setSex(Boolean sex) {
        this.sex = sex;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                ", address=" + address +
                '}';
    }

}

4、測試類

/**
 * @author :jack.zhao
 * @Describe: 測試類
 * @date :2021-10-16 22:55
 */
public class test01 {
//     <!-- foreach 操作 將多個元素放在List<Integer> 中List 添加的數據值為 add.(1001);add.(1002);add.(1003)-->
    @Test
    public void queryPersonWithList() throws Exception{
        Reader reader = Resources.getResourceAsReader("mybatis-03.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);

        SqlSession session = sessionFactory.openSession();

        List<Integer> intList = new ArrayList<>();
        intList.add(1001);
        intList.add(1002);
        intList.add(1003);

        // 動態代理
        PersonMapper personMapper = session.getMapper(PersonMapper.class);
        List<Person> personList = personMapper.queryPersonWithList(intList);
        System.out.println("List作為參數查詢結果為:"+personList);
        session.close();
    }
}

5、pom文件為

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis01"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!-- jack.zhao 加載映射文件 -->
        <mapper resource="com/mybatis03/mapper/personMapper.xml"/>
    </mappers>
</configuration>

6、測試數據

 

 7、測試結果

List作為參數查詢結果為:[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}]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM