- 新增.ibatis的新增有专门的标签insert.新增的时候可以简化下代码量,values前面跟后面都可以用include的方式写,这样代码看起来更简洁明了。
INSERT INTO tables ( ) VALUES ( ) - 批量新增,批量的时候传入的参数是List类型,然后iterate循环遍历的添加
INSERT INTO ka06 (BAZ001, AAZ001, AAZ164, AKA120, AKA121, AKA174, AKA122, AKA020, AKA021, AAE030, AAE031, AAE100, BAE011, BAE036, BAE034, AAE011, AAE036,AAB034,BAB301,AAA027) - 删除
- 批量删除
DELETE FROM KF01 WHERE BAZ001 IN #list[].BAZ001# - 修改
UPDATE kf01 BKE301 = #BKE301# - 批量修改
package com.paic.mhis.smcs.yits.common.util;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.util.List;import com.paic.mhis.smcs.yits.common.exception.BusinessException;/** * * TODO(动态生成更新sql语句) * @author EX-LIAOKUNLIN001 * @createDate 2015-8-12 */public class GenerateSqlMapUtils { /** * 需要转换成TO_DATE的字符串 */ private final static String DATE_TYPE = "AKC266,AKC221,AKC194,BAE036,AAE036,AAE030,AAE031,AAE127"; /** * 在实体类中需要过滤的字段 */ private final static String FILTER_FIELD = "BKZ001,serialVersionUID,flag,state,dateNum,kf01Flag,PERSIONS,AKB021,BKC042,AKB082,AKB069,BKB007," + "BKB060,BKB009,AKB100,AKB101,BKB020,BKB012,ZYCS,TOTALMON,JRTCFY,TCBX,ZF,GRFD,MZQFX,GRXJ,DATASTATE,BKA121,AKC050,AKC185,FHYBFY,MZTC,"+ "YJMZQFX,EJMZQFX,SJMZQFX,ISEDITTAG,BKA200"; /** * TODO(动态生成批量更新语句) * @author EX-LIAOKUNLIN001 * @param list * @param isNotNull 是否null也保存 * @param 数据库的主键 * @return String 返回sqlMap * @createDate 2015-8-11 */ @SuppressWarnings("rawtypes") public static String generateSqlMap(List list, String key, String keyExt, boolean isNotNull) throws BusinessException { StringBuilder sql = new StringBuilder(); Object columnValue = null; for (Object obj : list) { String entity = obj.getClass().getSimpleName().replace("DTO", ""); sql.append("UPDATE "+ entity +" SET "); try { // 获得KC22中的所有字段 Field[] fields = obj.getClass().getDeclaredFields(); for (Field field : fields) { // 获得字段名称 String columnName = field.getName(); if (FILTER_FIELD.indexOf(columnName) < 0) { columnValue = obj.getClass().getMethod("get" + columnName, new Class[] {}).invoke(obj, new Object[] {}); columnValue = columnValue == null ? "" : columnValue; if (isNull(columnValue, field.getType().getName(), isNotNull)) { if (DATE_TYPE.indexOf(columnName) >= 0) { sql.append(columnName + " = TO_DATE('"+ columnValue +"','yyyy-MM-dd HH24:mi:ss'),"); } else { sql.append(columnName + " = '" + columnValue + "',"); } } } } Object pk_key = obj.getClass().getMethod("get" + key, new Class[] {}).invoke(obj, new Object[] {}); sql = new StringBuilder(sql.substring(0, sql.lastIndexOf(","))); String keyValue = ""; Object pkExt_key = null; if (null != keyExt && !"".equals(keyExt)) { pkExt_key = obj.getClass().getMethod("get" + keyExt, new Class[] {}).invoke(obj, new Object[] {}); keyValue += " AND " + keyExt + " = '" + pkExt_key + "'"; } if ("KC22".equals(entity)) { Object bkz001 = obj.getClass().getMethod("getBKZ001", new Class[] {}).invoke(obj, new Object[] {}); sql.append(" WHERE BKZ001 = '"+ bkz001 +"' AND " + key + " = '" + pk_key + "'"+ keyValue +";\n"); } else { sql.append(" WHERE " + key + " = '" + pk_key + "'"+ keyValue +";\n"); } } catch (IllegalArgumentException e) { throw new BusinessException("反射方法错误:" + e.getMessage()); } catch (SecurityException e) { throw new BusinessException("反射方法错误:" + e.getMessage()); } catch (IllegalAccessException e) { throw new BusinessException("反射方法错误:" + e.getMessage()); } catch (InvocationTargetException e) { throw new BusinessException("反射方法错误:" + e.getMessage()); } catch (NoSuchMethodException e) { throw new BusinessException("反射方法错误:在类中没有找到该字段。" + e.getMessage()); } } return sql.toString(); } private static boolean isNull(Object param, Object fieldTypeName, boolean isNotNull) { if (!isNotNull) { if (param == null || "".equals(param)) { return false; } } return true; }}
GenerateSqlMapUtils.generateSqlMap(List,主键,null,false); String updatSql = GenerateSqlMapUtils.generateSqlMap(ka06Dto_update, "BAZ001", null, false);
ka06Dao.updateKA60(updatSql);
<!-- 批量更新操作 -->
<statement id="ka06.batchUpdateKA06" parameterClass="java.lang.String"> BEGIN $updatSql$ END; </statement> - 查询
第一种:简单的查询
- 总结:批量的增删改效率更高一些。