tableName为表名,自定义使用
@Autowired
private JdbcTemplate jdbcTemplate;
private static final String CREATE_TABLE_SQL =
"CREATE TABLE IF NOT EXISTS aaa_%s "
+ " (`id` bigint(20) NOT NULL AUTO_INCREMENT, "
+ " `created_at` datetime(6), "
+ " `updated_at` datetime(6), "
+ " PRIMARY KEY (`id`) "
+ " ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;";
private static final String SAVE_SQL =
"INSERT INTO aaa_%s "
+ " (id,created_at, updated_at) "
+ " VALUES (?,now(),now()) ";
private static final String SELECT_SQL =
" SELECT * FROM aaa_%s " + " ORDER BY created_at Desc LIMIT 1 ";
private static final String SELECT_DEV =
"select * from device_management where device_id = ?";
public void create(String tableName) {
jdbcTemplate.execute(String.format(CREATE_TABLE_SQL, tableName));
}
public void save(String tableName, LiveDTO liveDTO) {
jdbcTemplate.update(
String.format(SAVE_SQL, tableName),
testDTO.getId());
}
public List<testDTO> selectLatest(String tableName) {
return jdbcTemplate.query(String.format(SELECT_SQL, tableName),new BeanPropertyRowMapper<>(testDTO.class));
}
public void selectDevice(String devID) {
LiveDTO liveDTO= jdbcTemplate.queryForObject(SELECT_DEV, new Object[]{devID},new BeanPropertyRowMapper<>(LiveDTO .class) );
System.out.println(liveDTO.toString());