@Resource
private JdbcTemplate jdbcTemplate;
private Logger log = LoggerFactory.getLogger(this.class);
/**
* 批量插入
*/
public Boolean add() {
List<User> userList = userRepo.findAllByDelFlag(DelFlag.NORMAL);
//TODO 站内信附属表批量添加信息
if (userList.size() > 0) {
StringBuilder insert = new StringBuilder("INSERT INTO `user_info` (`uname`, `uid`) VALUES ");
for (int i = 0; i < userList.size(); i++) {
insert.append("(")
.append(userList.get(i).getUname())
.append(",")
.append(userList.get(i).getUid())
.append(")");
if (i < userList.size() - 1) {
insert.append(",");
}
}
String sql = (String) JSON.toJSON(insert);
log.info("SQL语句:{}", JSON.toJSON(insert));
try {
jdbcTemplate.execute(sql);
} catch (Exception e) {
throw new BaseException(500, "sql解析错误");
}
return true;
} else {
throw new BaseException(500, "该用户类型没有用户");
}
}