修改字符串两个字符颠倒问题,如:123456 --> 563412
Controller类:
package web.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import web.mapping.interfaceDataList;
import web.service.InterfaceDataListService;
import web.service.MybatisBatchInsertionService;
@Controller
public class MybatisBatchInsertionController {
@Autowired
private InterfaceDataListService interfaceDataListService;
@Autowired
private MybatisBatchInsertionService mybatisBatchInsertionService;
/**
* 修改字符串两个字符颠倒问题,如:123456 --> 563412
*/
@RequestMapping(value="/mybatisInsert",method=RequestMethod.GET,produces="text/html;charset=UTF-8")
public String mybatisInsert(){
//首先查询出1万数据
List<interfaceDataList> list = interfaceDataListService.selectAllDataList();
for (interfaceDataList in: list) {
StringBuilder newStr =new StringBuilder();
String str = in.getSensorNumber();
if(str.length()%2==0){
//如果str字符串的长度为偶数
for (int i = 0; i < str.length(); i++) {
if(i%2==0){
String substring = str.substring(str.length()-i-2,str.length()-i);
newStr.append(substring);
}
}
}else{
//如果str字符串的长度为奇数
for (int i = 0; i < str.length(); i++) {
if(i%2==0 && str.length()%2!=0 && i!=str.length()-1){
String substring = str.substring(str.length()-i-2,str.length()-i);
newStr.append(substring);
}
if(i==str.length()-1){
String substring = str.substring(str.length()-i-1,str.length()-i);
newStr.append(substring);
}
}
}
in.setSensorNumber(newStr.toString());
}
//查询1万条数据的方法
mybatisBatchInsertionService.insertMybatis(list);
return "druid/sql";
}
}
Mybatis 的sql xml:
<insert id="insertMybatis" parameterType="java.util.List">
insert into mybatisinsert (sensorName)
values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.sensorNumber,jdbcType=VARCHAR})
</foreach>
</insert>
经过自己试验,此方法7万条数据修改完成的速度还可以。