java mvc 批量插入_Spring MVC 批量添加数据怎么做

展开全部

使用List集合形式的参数的批量操作

如果使用List集合来进行批量操作,首先要把62616964757a686964616fe4b893e5b19e31333337623436待添加的数据与实体类对应(一个实体类通常对应数据库里的一张表, 下面的例子中Actor类就是一个实体类, 它对应了数据库中的 "t_actor"数据表).

比如"t_actor"数据表中有id,first_name,last_name三个字段, 那么对应Actor类就要有id, firstName, lastName属性, 且都得有getter, setter方法.

spring jdbc core 包中提供了一个SqlParamterSource 对象,这个对象用于SQL语句参数的设置.

使用SqlParameterSourceUtils.createBatch这个方法,把javabean(即上面说的实体类)的list  转化成array,spring会循环的进行取值;public class JdbcActorDao implements ActorDao {

private NamedParameterTemplate namedParameterJdbcTemplate;

public void setDataSource(DataSource dataSource) {

this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);

}

public int[] batchUpdate(final List actors) {

SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(actors.toArray());

int[] insertCounts = namedParameterJdbcTemplate.batchUpdate(

"INSERT INTO t_actor (id,first_name,last_name) VALUES(:id,:firstName,  :lastName )",batch);

return insertCounts;

}

// ... additional methods

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java 基于springMVC多图片上传,MySQL源码 @Controller @RequestMapping("/upload") public class UploadFileController { @Autowired private UploadFileService uploadFileService; @RequestMapping("/upfile") public String upload(HttpServletRequest request, @RequestParam("designation") String designation, @RequestParam("remark") String remark, //@RequestParam("file") String file, Model model)throws Exception { String str = designation; String[] st=str.split(","); //以逗号为分隔符进行截取 String re = remark; String[] stre = re.split(","); UploadFile uploadFile = new UploadFile(); //转成文件上传请求 MultipartHttpServletRequest murequest = (MultipartHttpServletRequest)request; //在文件上传请求中获取文件,根据file的name List files = murequest.getFiles("image"); if( files !=null && files.size()>0) { for(int i=0; i<files.size(); i++) { String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase()+"_"; String filename = files.get(i).getOriginalFilename(); //System.out.println("filename="+filename); int hCode =filename.hashCode(); String hex = Integer.toHexString(hCode); String mkdir = hex.charAt(0)+"\\"+hex.charAt(1)+"\\"; String path = request.getServletContext().getRealPath("/images/")+mkdir; //System.out.println("path="+path); File filepath = new File(path,filename); if(!filepath.getParentFile().exists()) { filepath.getParentFile().mkdirs(); } //将上传文件保存到目标文件中 files.get(i).transferTo(new File(path+File.separator+filename)); //获取数据库存储路径 String root = request.getContextPath(); String mkdirsql = hex.charAt(0)+"/"+hex.charAt(1)+"/"; String sqlpath = root+"/images/"+mkdirsql+filename; String imgpath = sqlpath; //图片保存到数据库的路径 uploadFile.setDesignation(st[i]); uploadFile.setRemark(stre[i]); uploadFile.setFile(filename); uploadFile.setImgpath(sqlpath); uploadFileService.addUploadFile(uploadFile); } return "success"; } return "404"; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值