My project's persistence layer uses ibatis:
After the interface of my project is called, it returns a jsonArray string, and then replaces it with a List<Object> object. The insert is to insert the loop List<Object> object in batches. The code diagram is as follows:
Action layer:
@RequestMapping(value = "/test.htm", method = RequestMethod.POST) public String init(HttpServletRequest request ) throws IOException { //Receive interface return data Map<String, String> map = new HashMap<String, String>(); Enumeration<?> fields = request.getParameterNames(); while(fields.hasMoreElements()) { String field = (String) fields.nextElement(); String[] values = request.getParameterValues(field); if (values.length > 1) map.put(field, values.toString()); else map.put(field, values[0]); } System.out.println(map.get("jsonArray")); //自定义测试数据 // String arrayStr ="[{\"amount\":\"9.83\",\"fundCategory\":\"A\",\"postStaus\":\"I\",\"fundChannelCode\":\"WEIXINPAY11601\",\"instOrder\":\"FI116011001082400080288\",\"happenDate\":\"20170824\",\"remarks\":\"20170824扣除入款手续费9.83元\",\"creater\":\"Arvin\",\"createTime\":\"20170907\",\"auditor\":\"\",\"auditorTime\":\"\",\"deleteFlag\":\"N\"},{\"amount\":\"15.77\",\"fundCategory\":\"A\",\"postStaus\":\"I\",\"fundChannelCode\":\"WEIXINPAY11601\",\"instOrder\":\"FI116011001082400080301\",\"happenDate\":\"20170824\",\"remarks\":\"20170824扣除入款手续费15.77元\",\"creater\":\"Arvin\",\"createTime\":\"20170907\",\"auditor\":\"\",\"auditorTime\":\"\",\"deleteFlag\":\"N\"}]"; if(map.get("jsonArray")!=null && map.get("jsonArray"if(map.get("jsonArray")!=null && map.get("jsonArray"if(map.get("jsonArray")!=null && map.get("jsonArray").length()>0){ String arrayStr = map.get( "jsonArray" ); logger .info( "The interface returns jsonArray: " +arrayStr); List<PostAccountVO> postAccountVOList = new ArrayList<PostAccountVO>(); List<Map<String,String>> listObjectSec = JSONArray.parseObject(arrayStr,List.class); Map<String,String> mapList = new HashMap<String, String>(); for(int i=0;i<listObjectSec.size();i++){ mapList = listObjectSec.get(i); JSONObject jsonObject = JSONObject.fromObject(mapList); PostAccountVO postAccountVO = (PostAccountVO) JSONObject. toBean (jsonObject, PostAccountVO. class ); postAccountVOList.add (postAccountVO); } System.out.println( postAccountVOList.size()); //After the received data is transferred to the list object, it is stored in the database GlideSyncResult result = postAccountFacade.getBankHandlingCharge(postAccountVOList); }else{System.out .println ( "The interface is empty!" ); logger .info( "The interface is empty!" ); } return ""; }
service layer:
public GlideSyncResult getBankHandlingCharge(List<PostAccountVO> vo);
Service impl implementation layer:
My situation is: the service layer entity class is VO, the dao layer entity class is DO, so I want to call the dao layer method, and I want to convert the object to the dao layer object
(VO and DO entity classes are the same, but this project is based on this format. I also built VO and DO entity classes respectively. You should probably build an entity class, so there is no need to convert)
@Override public GlideSyncResult getBankHandlingCharge(List<PostAccountVO> vo) { List<PostAccountInfoDO> infoDOList = new ArrayList<PostAccountInfoDO>(); int status=0; if(vo!=null && vo.size()>0){ for(int i=0;i<vo.size();i++){ PostAccountInfoDO infoDO = new PostAccountInfoDO (); infoDO.setAccountSeqNo ( postAccountInfoDAO .getAccountNo ()); infoDO.setAmount(vo.get(i).getAmount()); infoDO.setFundCategory(vo.get(i).getFundCategory()); infoDO.setPostStaus(vo.get(i).getPostStaus()); infoDO.setFundChannelCode(vo.get(i).getFundChannelCode()); infoDO.setHappenDate(vo.get(i).getHappenDate()); infoDO.setRemarks(vo.get(i).getRemarks()); infoDO.setCreater(vo.get(i).getCreater()); infoDO.setCreateTime(vo.get(i).getCreateTime()); infoDO.setAuditor(vo.get(i).getCreater()); infoDO.setAuditorTime(vo.get(i).getCreateTime()); infoDO.setDeleteFlag (vo.get (s) .getDeleteFlag ()); infoDO.setInstOrder(vo.get(i).getInstOrder()); infoDOList.add(infoDO); } status = postAccountInfoDAO.insert(infoDOList); } if (status> 0) { return new GlideSyncResult(GlideSyncResultCode.SUCCESS); } else { return new GlideSyncResult(GlideSyncResultCode.FAILED); } }
dao layers:
public int insert(List<PostAccountInfoDO> infoDOList ) throws DataAccessException;
Dao impl implementation layer:
@Override public int insert(List<PostAccountInfoDO> infoDOList ) throws DataAccessException { if(infoDOList==null){ throw new IllegalArgumentException("Can't insert a null data object into db."); } Integer nums = (Integer)getSqlMapClientTemplate().insert("MS-POST-ACCOUNT-INFO-INSERT", infoDOList); if(nums==null){ return 1; } return 0; }
mapper.xml:
<insert id="MS-POST-ACCOUNT-INFO-INSERT" parameterClass="java.util.List"> insert all <iterate conjunction="" > <![CDATA[ into TB_POST_ACCOUNT_INFO (ACCOUNT_SEQ_NO,AMOUNT,FUND_CATEGORY,POST_STAUS,FUND_CHANNEL_CODE,HAPPEN_DATE,REMARKS,CREATER,CREATE_TIME,AUDITOR,AUDITOR_TIME,DELETE_FLAG,INST_ORDER) values (#infoDOList[].accountSeqNo#,#infoDOList[].amount#,#infoDOList[].fundCategory#,#infoDOList[].postStaus#, #infoDOList[].fundChannelCode#,#infoDOList[].happenDate#,#infoDOList[].remarks#,#infoDOList[].creater#, #infoDOList[].createTime#,#infoDOList[].auditor#,#infoDOList[].auditorTime#,#infoDOList[].deleteFlag#, #infoDOList[].instOrder#) ]]> </iterate> select 1 from dual </insert>
Notice:
If you add data, if there are automatically generated parameters, for example:
<select id="ACCOUNT-INFO-NO" resultClass="String"> <![CDATA[ select seq_post_account_info.nextval from dual ]]> </select>
It is recommended to write a separate SQL query, and then insert the value into the object after finding it, as shown in the following figure:
The method is rather stupid. . .
Moreover, the batch submission quantity of this method should be less than one thousand (size() < 1000)
If you find a better way, please let me know and I will learn it.