简体   繁体   中英

Looping in insert query in mybatis

I have one pojo class like this:

public class Pojo {
    String a;
    String b;
    String c;
    List<Integer>  d;
    public String getA() {
        return a;
    }
    public void setA(String a) {
        this.a = a;
    }
    public String getB() {
        return b;
    }
    public void setB(String b) {
        this.b = b;
    }
    public String getC() {
        return c;
    }
    public void setC(String c) {
        this.c = c;
    }
    public List<Integer> getD() {
        return d;
    }
    public void setD(List<Integer> d) {
        this.d = d;
    }

<insert id="insertPojoval" parameterType="com.Pojo">
insert into abc values(#{a},#{b},#{c},#{d},)
</insert>

How can I insert this pojo values in one insert query using mybatis?

I want that for each values in the list List<Integer> d values of a , b , and c should be inserted. How can I loop for each value in List<Integer> d ?

I am getting problem in looping for each value in list d .

You will need to look into Oracle Objects and mybatis TypeHandler . Have a look at this: How to Pass Java List of Objects to Oracle Stored Procedure Using MyBatis?

You can use mybatis "foreach" tag to insert as to collection. But I'm not sure this is what you want or not. Refer to below answer and hope it help.

<insert id="insertPojoval" parameterType="com.Pojo">
        INSERT INTO 
            abc
        VALUES
            <foreach collection="list" item="d" index="index" open="(" separator="),("  close=")">
                #{a},
                #{b},
                #{c},
                #{d}
            </foreach>      
</insert>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM