2024年4月26日发(作者:)
public class SqlBatch {
@Autowired
private SqlSessionFactory sqlSessionFactory;
public static final String MAPPER_01 = "a";
public static final String SQL_ADD = "INSERT INTO TEST VALUES(?,?,?,?,?)";
public void batchSelect(List
String sql = "";
SqlSession session = null;
PreparedStatement prepareStatement = null;
try {
if (list == null || y()) {
return;
}
Map
("id", (0).getId());
("name", (0).getName());
("home", (0).getHome());
("tel", (0).getTel());
("path", (0).getPath());
sql = getSql(MAPPER_01, map);
// 或者
// sql=SQL_ADD ;
session = ssion(, true);
prepareStatement = nection().prepareStatement(sql);
for (TestModel model : list) {
ing(1, ());
ing(2, e());
ing(3, e());
ing(4, ());
ing(5, h());
ch();
}
int[] executeBatch = eBatch();
n(executeBatch);
} catch (SQLException e) {
n("批量处理SQL异常:" + e);
} catch (Exception e) {
n("批处理添加数据异常:" + e);
} finally {
try {
();
} catch (SQLException e) {
n("关闭批处理异常:" + e);
}
}
}
// 根据参数从MyBatis中获取对应的sql
private String getSql(String mapper, Map
return figuration().getMappedStatement(mapper).getBoundSql(param).getSql();
}
}
ller类,测试添加数据:
@RestController
public class DemoController {
@Autowired
private TestService testService;
@Autowired
private SqlBatch sqlBatch;
private static final Logger LOGGER = ger();
@RequestMapping("/test")
public String test1() {
addData();
List
List
(t(0, 20));
for (TestModel testModel : newList) {
("数据:{}", ng());
}
();return " spring boot is starting ....";
}
private void addData() {
long start = tTimeMillis();
// 造数据
List
for (int i = 0; i < 50000; i++) {
TestModel model = new TestModel();
(UUID().toString().replace("-", ""));
e("姓名" + i);
e("家庭地址" + i);
("联系电话" + i);
h("URL:" + i);
(model);
}
// 批量添加
elect(list);
long end = tTimeMillis();
("总共耗时:[{}]", (end - start));
}
}


发布评论