LD is tigger forever,CG are not brothers forever, throw the pot and shine forever.
Modesty is not false, solid is not naive, treacherous but not deceitful, stay with good people, and stay away from poor people.
talk is cheap, show others the code and KPI, Keep progress,make a better result.
Survive during the day and develop at night。
目录
概 述
下载mybatis 源码
测试问题:
1.创建CreateDB的.sql:
create table users (
id int,
name varchar(20)
);
insert into users (id, name) values(1, ‘User1’);
insert into users (id, name) values(2, ‘User2’);
insert into users (id, name) values(3, ‘User3’);
insert into users (id, name) values(4, ‘User4’);
insert into users (id, name) values(5, ‘User5’);
insert into users (id, name) values(6, ‘User6’);
接口文件:
public interface Mapper {
User getUser(User user);
int countByUserList(List<User> users);
int countByBestFriend(List<User> users);
String selectWithNullItemCheck(List<User> users);
int typoInItemProperty(List<User> users);
int itemVariableConflict(@Param("id") Integer id, @Param("ids") List<Integer> ids, @Param("ids2") List<Integer> ids2);
int indexVariableConflict(@Param("idx") Integer id, @Param("idxs") List<Integer> ids, @Param("idxs2") List<Integer> ids2);
}
Mapper.xml文件:
<insert id="typoInItemProperty">
insert into users (id, name) values
<foreach item="item" collection="list" separator=",">
(#{item.idd}, #{item.name})
</foreach>
</insert>
select count(*) from users where id in #{id}, #{id} or id = #{id} select count(*) from users where id in #{idx}, #{idx} + 2 or id = #{idx}
对应在这里插入代码片的加载配置文件:
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="" value="" />
</transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:foreach" />
<property name="username" value="sa" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/apache/ibatis/submitted/foreach/Mapper.xml" />
</mappers>
</configuration>
对应的List-mapper 文件:
private Integer id;
private String name;
private User bestFriend;
private List<User> friendList;
获取一个指定的ID:
@Test
void shouldGetAUser() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
User testProfile = new User();
testProfile.setId(2);
User friendProfile = new User();
friendProfile.setId(6);
List<User> friendList = new ArrayList<>();
friendList.add(friendProfile);
testProfile.setFriendList(friendList);
User user = mapper.getUser(testProfile);
Assertions.assertEquals("User6", user.getName());
}
}
<select id="indexVariableConflict" resultType="_int">
select count(*) from users where id in
<foreach collection="idxs" index="idx" open="(" close=")" separator=",">
<foreach collection="idxs2" index="idx">
#{idx},
</foreach>
#{idx} + 2
</foreach>
or id = #{idx}
</select>
ID增加:测试:
@Test
void shouldRemoveIndexVariableInTheContext() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
int result = mapper.indexVariableConflict(4, Arrays.asList(6, 7), Arrays.asList(8, 9));
Assertions.assertEquals(4, result);
}
}
小结
参考资料和推荐阅读
1.链接: link.
本文主要探讨了MyBatis的foreach元素在SQL动态拼接中的应用,通过创建DB的SQL语句、接口及Mapper.xml配置来展示其用法。测试涉及了不同场景,如item属性错误、变量冲突等,并给出了相应的测试用例和预期结果。文章最后总结了关键点,并提供了参考资料。
4589

被折叠的 条评论
为什么被折叠?



