MyBatis 中使用 對象數組作為參數進行傳遞查詢MYSQL (15)


MyBatis 中使用 對象數組作為參數進行傳遞查詢MYSQL。既:對象數組定義類型為: Person[] persons={Person0,person1,person2}

應用舉例

1、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 操作 將多個元素放在對象數組中 Person[] persons={Person0,person1,person2}-->
    <!-- 使用對象數組做為參數的時候:parameterType 需要用"Object[]", item 中需要用對象"com.mybatis03.bean.Person"-->
    <select id="queryPersonWithObjectArray" parameterType="Object[]" resultType="com.mybatis03.bean.Person">
        select * from  t_person_01
        <where>
            <if test="array !=null and array.length >0 ">
                <foreach collection="array" open=" and id in(" close=")" item="com.mybatis03.bean.Person" separator=",">
                    #{com.mybatis03.bean.Person.id}
                </foreach>
            </if>
        </where>
    </select>
</mapper>

2、接口類

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

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 {
@Test
    public void queryPersonWithObjectArray() throws Exception{
        Reader reader = Resources.getResourceAsReader("mybatis-03.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);

        SqlSession session = sessionFactory.openSession();

        Person person01 = new Person();
        person01.setId(1001);
        Person person02 = new Person();
        person02.setId(1002);
        Person person03 = new Person();
        person03.setId(1003);
        Person[] persons = new Person[] {person01,person02,person03};

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

5、測試數據

 

 

6、實行測試結果截圖

對象數組作為參數查詢結果為:[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