Function Auto-Increment Properties
10/27/25Less than 1 minutesql
easy-query supports generating column values with database functions, such as implementing the generation of corresponding id columns when inserting through a custom nextId() function, rather than ordinary object property columns.
Custom Function to Create Database Auto-Generated Columns
Database object, must set generatedKey to true, indicating it is automatically generated. If generatedSQLColumnGenerator is not added, it will be treated as an auto-increment column and will not be included in the insert statement, generated by the database
@Data
@Table("custom_increment")
public class CustomIncrement {
@Column(primaryKey = true,generatedKey = true, generatedSQLColumnGenerator = MyDatabaseIncrementSQLColumnGenerator.class)
private String id;
private String name;
private String address;
}Custom insert column function
public class MyDatabaseIncrementSQLColumnGenerator implements GeneratedKeySQLColumnGenerator {
@Override
public void configure(TableAvailable table, ColumnMetadata columnMetadata, SQLPropertyConverter sqlPropertyConverter, QueryRuntimeContext runtimeContext) {
sqlPropertyConverter.sqlNativeSegment("mysqlNextId()");
}
}Assuming I defined a mysql function mysqlNextId to automatically generate primary key id, implemented by mysql database function
CustomIncrement customIncrement=new CustomIncrement();
//customIncrement.setId();//Whether or not it is set, mysqlNextId will be used as the insert function
customIncrement.setName("name");
customIncrement.setAddress("address");
easyQuery.insertable(customIncrement)
.executeRows();
INSERT INTO `custom_increment` (`id`,`name`,`address`) VALUES (mysqlNextId(),?,?)Use Case Scenario
pgsql geo data addition or custom database function generated columns, only called when adding
Contributors
只是我