Mybatis003

练习一:

1.在接口 BillMapper中添加方法

//根据供应商id ,获取这些对应商的订单列表信息(数组入参)
    public List<Bill> getBillByProviderArray(Integer[] ids);

    //根据供应商id ,获取这些对应商的订单列表信息(List入参)
    public List<Bill> getBillByProviderList(List<Integer> idlist);

 2.在BillMapper.xml文件中添加SQL语句

<!--     遍历数组参数-->
    <select id="getBillByProviderArray" resultMap="billlist">

        select * from smbms_bill where 1=1
        and providerId in
        <foreach collection="array" item="ids" open="(" separator="," close=")">
                #{ids}
        </foreach>


    </select>
<!--遍历list集合-->
    <select id="getBillByProviderList" resultMap="billlist">

        select * from smbms_bill where 1=1
        and providerId in
        <foreach collection="list" item="idlist" open="(" separator="," close=")">
            #{idlist}
        </foreach>


    </select>

 3.在TestBillMapper.java 中测试

    @Test
    public void test1(){
     //测试数组入参
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        Integer[] ids = new Integer[]{1,2};
        List<Bill> billByProviderArray = sqlSession.getMapper(BillMapper.class).getBillByProviderArray(ids);

        for (Bill bill : billByProviderArray){
            System.out.println(bill.getProviderId()+"\t"+bill.getProductName());
        }

        SqlSessionUtil.closeSqlsession(sqlSession);
    }

    @Test
    public void test2(){
        //测试list入参
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        List<Bill> billByProviderArray = sqlSession.getMapper(BillMapper.class).getBillByProviderList(list);

        for (Bill bill : billByProviderArray){
            System.out.println(bill.getProviderId()+"\t"+bill.getProductName());
        }

        SqlSessionUtil.closeSqlsession(sqlSession);
    }

 练习二:

1.在 接口 ProviderMapper 中添加方法

//模糊查询(供应商编码,供应商名称,供应商联系人,) 时间范围(在本年)
    //使用choose(when,otherwise) 完成
    public List<Provider> getProviderByCodeNameContactTime(@Param("proCode")String proCode, @Param("proName")String proName,@Param("proContact")String  proContact,@Param("fromDate")Date date1,@Param("comeDate")Date date2);

 2.在 ProviderMapper.xml 文件中添加

<select id="getProviderByCodeNameContactTime" resultType="com.bean.Provider">
        SELECT * FROM smbms_provider WHERE 1=1
        <choose>
            <when test="proCode!=null">
                AND proCode like concat('%',#{proCode},'%')
            </when>
            <when test="proName != null">
                AND proName like concat('%',#{proName},'%')
            </when>
            <when test="proContact != null">
                and proContact like concat('%',#{proContact},'%')
            </when>
            <when test="fromDate != null and comDate !=null  ">
                and creationDate between #{fromDate} and #{comeDate}
            </when>
            <otherwise>
                AND featured = 1
            </otherwise>
        </choose>
    </select>

 3.在TestProviderMapper中测试

    @Test
    public void test3(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();

        Date fromDate = null;
        Date comeDate = null;
        try {
            fromDate = new SimpleDateFormat("yyyy-MM-dd").parse("2020-01-01");
             comeDate = new SimpleDateFormat("yyyy-MM-dd").parse("2020-07-10");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<Provider> list = sqlSession.getMapper(ProviderMapper.class).getProviderByCodeNameContactTime("B", "北", "张",fromDate,comeDate);

        for (Provider p : list){
            System.out.println(p.toString());
        }
        SqlSessionUtil.closeSqlsession(sqlSession);
    }

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值