通过注解的形式实现数据库的批量插入
/** * @author Holley * @Description 请输入一句话进行描述 * @create 2018-07-16 12:09 **/ @Component @Mapper public interface TagOfPatientDao { @InsertProvider(type = TagOfPatientProvider.class,method = "insertAll") void insertAll(@Param("list") List<TagOfPatient>list); }
/** * @author Holley * @Description 批量插入 * @create 2018-07-16 13:21 **/ public class TagOfPatientProvider { public String insertAll(@Param("list") List<TagOfPatient> list){ StringBuilder sb = new StringBuilder(); sb.append("insert into tag_of_patient"); sb.append("(pid,tagid,status,created)"); sb.append("values"); MessageFormat mf = new MessageFormat("(#‘{‘list[{0}].pid},#‘{‘list[{0}].tagid},0,now())"); for(int i = 0;i < list.size();i++){ sb.append(mf.format(new Object[]{i})); if(i < (list.size() - 1)){ sb.append(","); } } return sb.toString(); } }
注意:在dao层向provider类中传参时,如果参数只有一个map类型,则此时参数前一定不要使用@Param("map"),否则,在provider类中获取到的map不是当前的map,而是在当前map上又封装了一层map。(具体原因不是很清楚,如果有哪位翻出了源码,请指教下)
参考
http://f0rb.iteye.com/blog/1207384
https://www.cnblogs.com/fjdingsd/p/5143625.html
原文地址:https://www.cnblogs.com/zhlblogs/p/9579863.html
时间: 2024-10-25 06:44:46