关于循环查询的方式,使用in方式
【资料图】
#{id}
里面的变量,ids代表是一个list的string类型的,id代表循环里面的自定义变量。and business_id代表的是查询语句里面的sql语句。
在可以确定查询的id是多条的情况下,比如说可能是10条以上的话,最好的方式进行in的方式进行查询,避免打开或者关闭数据库的时候浪费大量的时间。
需要特别注意下,这个传参不是实体类而是一个Map类型的,所以在定义方法的时候,要定义这个类型的
/*** @Project:根据业务id查询拓展字段数据* @Module: com.iss.cms.tshd* @Deion:* @author: hycaid* @date: 2021/4/22 10:22*/List queryExpandFormExtByBusinessIds(List ids);
具体的实现类型是这样的,使用Map进行参数的传值
@Overridepublic List queryExpandFormExtByBusinessIds(List ids) { ExpandFormExt expandFormExt = new ExpandFormExt(); Map map=new HashMap<>(); map.put("ids",ids); map.put("tenantId",expandFormExt.getTenantId()); map.put("tableNo",expandFormExt.getTableNo()); return this.sqlSessionTemplate.selectList(NAMESPACE + "queryExpandFormExtByBusinessIds", map);}
通过实现的方式可以看出来,这个map进行动态的赋值操作,比如说,进行三个参数的传参处理。
批量插入的处理代码,sql语句如下:
insert into t_fdrb_bl_contract_balance${tableNo}( id, create_by_code, create_by_name, create_time, update_by_code, update_by_name, update_time, data_version, data_status, contract_code, contract_id, loan_date, balance, contract_type, loan_org_id, loan_org_code, loan_org_name, currency_name, currency_type, tenant_id ) values (#{item.id}, #{item.createByCode}, #{item.createByName}, #{item.createTime}, #{item.updateByCode}, #{item.updateByName}, #{item.updateTime}, #{item.dataVersion}, #{item.dataStatus}, #{item.contractCode}, #{item.contractId}, #{item.loanDate}, #{item.balance}, #{item.contractType}, #{item.loanOrgId}, #{item.loanOrgCode}, #{item.loanOrgName}, #{item.currencyName}, #{item.currencyType}, #{item.tenantId})
对应的接口定义如下:
@Overridepublic void insertBatchContractBalanceByMysql(List list) { ContractBalance contractBalance = new ContractBalance(); Map map = new HashMap<>(); map.put("tableNo",contractBalance.getTableNo()); map.put("list",list); this.sqlSessionTemplate.insert(NAMESPACE + "insertBatchContractBalanceByMysql", map);}
通过上面的两段代码可以看出来,整个map的集合,然后map的第二个key、value传值的是一个list的集合,而不是实体类的形式,这样通过sql拼接的方式实现插入的处理,避免存在多条插入语句异常导致的部分插入失败的情况。
通过上面的两个例子可以延申出来,更新的时候也可以使用拼接sql的形式进行批量更新的操作。
X 关闭
X 关闭