您的位置:首页 > 数据库 > Oracle

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

2016-05-17 10:43 423 查看
/**
* @param request  Controller
* @return -1:失败,插入条数:成功
*/
@RequestMapping(value="/addXXX", produces = "application/json")
@ResponseBody
public 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());
}
}

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: