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