java代码_批量导入_SpringMVC_JSON传值_Oracle数据库_id使用序列自增_示例

来源:互联网 发布:薛冰是怎么死的 知乎 编辑:程序博客网 时间:2024/06/03 19:51
/** * @param request  Controller * @return -1:失败,插入条数:成功 */@RequestMapping(value="/addXXX", produces = "application/json")@ResponseBodypublic int addXXX(HttpServletRequest request){    Entity entity= null;    List<Entity> list = new ArrayList<Entity>();    try {        String jsonValue = request.getParameter("...");        JSONArray jsonArray = JSONArray.fromObject(jsonValue);        int iSize = jsonArray.size();        for (int i = 0; i < iSize; i++)        {            JSONObject jsonObj = jsonArray.getJSONObject(i);
              entity = new MicArticleTag();
entity.setId(String.valueOf(jsonObj.get("id")));
              entity.setName(String.valueOf(jsonObj.get("name")));
              list.add(entity);
        }        return newsService.addXXX(list);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        return -1;
    }}
/**
 * @param list  Service * @return * @throws Exception */public int  addXXX(List<Entity> list) throws Exception{    return newsDao.addXXX(list);}
    /**     * Dao     */    public int addXXX (List<Entity> list) throws Exception {        final List<Entity> temList = list;        String sql = "insert into table(ID, NAME) values(SEQ_ID.NEXTVAL,?)";        try{            int[] ii = this.getJdbcTemplate().batchUpdate(sql, new EntityBatchSetter(temList));            return ii.length;        }catch (Exception e){            logger.error(e.getMessage(), e);            return -1;        }    }
/** * Mapper */public class EntityBatchSetter implements BatchPreparedStatementSetter {    final List<Entity> temList;    public EntityBatchSetter(List<Entity> list){        temList = list;    }    public int getBatchSize() {        return temList.size();    }    public void setValues(PreparedStatement ps, int i)            throws SQLException {        Entity entity = temList.get(i);        ps.setString(1, entity.getName());    }}

0 0
原创粉丝点击