栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatisforeach 等测试

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 users);

  int countByBestFriend(List users);

  String selectWithNullItemCheck(List users);

  int typoInItemProperty(List users);

  int itemVariableConflict(@Param("id") Integer id, @Param("ids") List ids, @Param("ids2") List ids2);

  int indexVariableConflict(@Param("idx") Integer id, @Param("idxs") List ids, @Param("idxs2") List ids2);
}

Mapper.xml文件:

 
    insert into users (id, name) values
    
        (#{item.idd}, #{item.name})
    
  

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}

对应在这里插入代码片的加载配置文件:



    
        
            
                
            
            
                
                
                
            
        
    

    
        
    


对应的List-mapper 文件:

  private Integer id;
  private String name;
  private User bestFriend;
  private List 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 friendList = new ArrayList<>();
      friendList.add(friendProfile);
      testProfile.setFriendList(friendList);
      User user = mapper.getUser(testProfile);
      Assertions.assertEquals("User6", user.getName());
    }
  }

    select count(*) from users where id in
    
      
        #{idx},
      
      #{idx} + 2
    
    or id = #{idx}
  

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.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/821367.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号