mybatisforeach 等测试

mybatisforeach 等测试

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.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值