Mybatis
Posted Lev1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis相关的知识,希望对你有一定的参考价值。
@Test public void test01() { SqlSession sqlSession = myBatisUntil.getSqlSession(); Bill bill = new Bill(); bill.setProductName("日用"); bill.setProviderId(13); bill.setIsPayment(2); Bill billByOther = sqlSession.getMapper(ProviderMapper.class).getBillByOther(bill); if (billByOther == null){ System.out.println("查无信息"); }else { System.out.println("订单编码:"+billByOther.getBillCode()+" 商品名称"+billByOther.getProductName() +" 供应商名称"+billByOther.getProviderName()+" 账单金额"+billByOther.getTotalPrice() +" 是否付款"+billByOther.getIsPayment()+" 创建时间"+billByOther.getCreationDate()); } } /** * 实现根据供应商id修改供应商信息 */ @Test public void test02(){ SqlSession sqlSession = myBatisUntil.getSqlSession(); Provider provider = new Provider(); provider.setId(15); provider.setProContact("冯宝宝"); provider.setProPhone("18060975170"); int count = sqlSession.getMapper(ProviderMapper.class).updateProviderByid(provider); sqlSession.commit(); System.out.println("添加:"+count); } /** * 实现根据供应商id删除供应商信息 */ @Test public void test03(){ SqlSession sqlSession = myBatisUntil.getSqlSession(); int count = sqlSession.getMapper(ProviderMapper.class).delProviderByid("16"); sqlSession.commit(); System.out.println("添加:"+count); } /** * 根据供应商id获取供应商及其订单列表 */ @Test public void test04(){ SqlSession sqlSession = myBatisUntil.getSqlSession(); Provider providerBillList = sqlSession.getMapper(ProviderMapper.class).getProviderBillList("1"); sqlSession.commit(); System.out.println("供应商id:"+providerBillList.getId()+"供应商编码:" +providerBillList.getProCode()+"供应商联系人:" +providerBillList.getProContact()+"供应商联系电话:" +providerBillList.getProPhone()); List<Bill> billList = providerBillList.getBillList(); for (Bill bill : billList) { System.out.println("订单编码:"+bill.getBillCode()+"商品名称:"+bill.getProductName() +"订单金额:"+bill.getTotalPrice()+"是否付款(1、未付款 2、已付款):"+bill.getIsPayment()); } } /** * 根据商品名称(模糊查询)、供应商id、是否付款查询订单以及供应商信息 */ @Test public void test05(){ SqlSession sqlSession = myBatisUntil.getSqlSession(); Bill bill = new Bill(); bill.setProductName("皂"); bill.setProviderId(13); bill.setIsPayment(2); Bill billProviderByBill = sqlSession.getMapper(ProviderMapper.class).getBillProviderByBill(bill); System.out.println("账单编码:"+billProviderByBill.getBillCode()+"商品名称:" +billProviderByBill.getProductName()+"供应商编码:" +billProviderByBill.getProvider().getProCode()+"供应商名称:" +billProviderByBill.getProvider().getProName()+"供应商联系人:" +billProviderByBill.getProvider().getProContact()+"联系电话:"+billProviderByBill.getProvider().getProPhone() +"总金额:"+billProviderByBill.getTotalPrice()+"是否支付(1、未付款 2、已付款):"+billProviderByBill.getIsPayment()); } /** * 增加供货商信息 */ @Test public void test06(){ SqlSession sqlSession = myBatisUntil.getSqlSession(); Provider provider = new Provider(); provider.setId(16); provider.setProCode("ZJ_GYS002"); provider.setProName("乐摆日用品厂"); provider.setProDesc("长期合作伙伴,主营产品:各种中、高档塑料杯,塑料乐扣水杯(密封杯)、保鲜杯(保鲜盒)、广告杯、礼品杯"); provider.setProContact("王佳欣"); provider.setProPhone("18060975170"); provider.setProAddress("湖里区"); provider.setProFax("0579-34452321"); provider.setCreatedBy(1); provider.setCreationDate("2019-10-21 10:01:30"); int count = sqlSession.getMapper(ProviderMapper.class).addProvide(provider); sqlSession.commit(); System.out.println("添加:"+count); }
/** * 实现按条件查询订单表,查询条件如下 * 商品名称(模糊查询) * 供应商(供应商id) * 是否付款 * @param bill * @return */ public Bill getBillByOther(Bill bill); /** * 增加供货商信息 * @param provider 供货商实体类 * @return */ public int addProvide(Provider provider); /** * 实现根据供应商id修改供应商信息 * @param provider * @return */ public int updateProviderByid(Provider provider); /** * 实现根据供应商id删除供应商信息 * @param id * @return */ public int delProviderByid(@Param("id") String id); /** * 根据供应商id获取供应商及其订单列表 * @param id * @return */ public Provider getProviderBillList(String id); /** * 根据商品名称(模糊查询)、供应商id、是否付款查询订单以及供应商信息 * @param bill * @return */ public Bill getBillProviderByBill(Bill bill); 接口类
<!-- 实现按条件查询订单表(无内部嵌套)--> <select id="getBillByOther" resultMap="BillProviderResult" parameterType="Bill"> SELECT b.*,p.proName FROM smbms_bill b,smbms_provider p WHERE b.providerId = p.id and b.providerId = #{providerId} and isPayment = #{isPayment} and productName LIKE CONCAT(‘%‘,#{productName},‘%‘) </select> <resultMap id="BillProviderResult" type="Bill"> <result property="billCode" column="billCode"></result> <result property="productName" column="productName"></result> <result property="totalPrice" column="totalPrice"></result> <result property="isPayment" column="isPayment"></result> <result property="creationDate" column="creationDate"></result> <result property="providerName" column="providerName"></result> </resultMap> <!-- 实现根据供应商id修改供应商信息--> <update id="updateProviderByid" parameterType="Provider"> update smbms_provider set proContact=#{proContact},proPhone=#{proPhone} where id=#{id} </update> <!-- 实现根据供应商id删除供应商信息--> <delete id="delProviderByid" parameterType="String"> delete from smbms_provider where id=#{id} </delete> <!-- 根据供应商id获取供应商及其订单列表--> <select id="getProviderBillList" resultMap="providerBillList" parameterType="String"> select p.*,b.billCode,b.productName,b.totalPrice,b.isPayment from smbms_bill b,smbms_provider p where b.providerId = p.id and p.id = #{id} </select> <resultMap id="providerBillList" type="Provider"> <id property="id" column="id"></id> <result property="proCode" column="proCode"></result> <result property="proName" column="proName"></result> <result property="proContact" column="proContact"></result> <result property="proPhone" column="proPhone"></result> <collection property="billList" ofType="Bill" resultMap="billList"></collection> </resultMap> <resultMap id="billList" type="Bill"> <result property="billCode" column="billCode"></result> <result property="productName" column="productName"></result> <result property="totalPrice" column="totalPrice"></result> <result property="isPayment" column="isPayment"></result> </resultMap> <!-- 根据商品名称(模糊查询)、供应商id、是否付款查询订单以及供应商信息--> <select id="getBillProviderByBill" resultMap="billProviderByBill" parameterType="Bill"> select b.*,p.proCode,p.proName,p.proContact,p.proPhone from smbms_bill b,smbms_provider p where b.providerId = p.id and productName LIKE CONCAT(‘%‘,#{productName},‘%‘) and providerId = #{providerId} and isPayment=#{isPayment} </select> <resultMap id="billProviderByBill" type="Bill"> <result property="billCode" column="billCode"></result> <result property="productName" column="productName"></result> <result property="totalPrice" column="totalPrice"></result> <result property="isPayment" column="isPayment"></result> <association property="provider" javaType="provider" resultMap="providerList"></association> </resultMap> <resultMap id="providerList" type="Provider"> <result property="proCode" column="proCode"></result> <result property="proName" column="proName"></result> <result property="proContact" column="proContact"></result> <result property="proPhone" column="proPhone"></result> </resultMap> Mapper.xml的配置
以上是关于Mybatis的主要内容,如果未能解决你的问题,请参考以下文章
SSM-MyBatis-05:Mybatis中别名,sql片段和模糊查询加getMapper
MYBATIS05_ifwherechoosewhentrimsetforEach标签sql片段