I am inserting object A in database with parameter (A a). The object like that
A {
List<B> b;
List<C> c;
}
In mapper file I use this function:
void register(@Param("a")A a);
In mapper.xml, I wrote the following code:
<insert id="registerB" parameterType="java.util.List">
<foreach>...</foreach>
</insert>
<insert id="registerC" parameterType="java.util.List">
<foreach>...</foreach>
</insert>
<insert id="register">
//TODO include insert registerB and registerC
</insert>
How can i include the inserting of list of B and C into insert query of A?
You can't reuse insert sql snippets easily in the mapper.xml
. The problem is not with sql strings themselves this can be done (like described here for example).
The problem will be with referencing parameters. In insert for registerB
for example the parameter is a list of B
s. And you can't redefine it to use them easily from register
.
You can however use java8 default methods to reuse registerB
and registerC
from java code like this:
interface MyMapper {
void registerB(@Param("b")B b);
void registerC(@Param("c")C c);
default void register(@Param("a")A a) {
registerB(a.getB());
registerC(a.getC());
}
}