局部
全局
clazz 指定主键的类型,IdType必须设置,且不能是AUTO,后面附有源码
@TableName("VZ_FAIL_ORDER_MESSAGE")
@KeySequence(value = "SEQ_VZ_FAIL_ORDER_MESSAGE", clazz = String.class)
public class FailOrderMessage extends BaseModel {
private static final long serialVersionUID = -8043398680425490355L;
@TableId(value = "ID_", type = IdType.INPUT)
private String id;
@TableField("PRO_ID")
private String proId;
...
/**
*
* 注入插入 SQL 语句
*
*
* @param selective 是否选择插入
* @param mapperClass
* @param modelClass
* @param table
*/
protected void injectInsertOneSql(boolean selective, Class mapperClass, Class modelClass, TableInfo table) {
/*
* INSERT INTO table
* xx, #{xx},
*
*/
KeyGenerator keyGenerator = new NoKeyGenerator();
StringBuilder fieldBuilder = new StringBuilder();
StringBuilder placeholderBuilder = new StringBuilder();
SqlMethod sqlMethod = selective ? SqlMethod.INSERT_ONE : SqlMethod.INSERT_ONE_ALL_COLUMN;
fieldBuilder.append("\n\n");
placeholderBuilder.append("\n\n");
String keyProperty = null;
String keyColumn = null;
// 表包含主键处理逻辑,如果不包含主键当普通字段处理
if (StringUtils.isNotEmpty(table.getKeyProperty())) {
if (table.getIdType() == IdType.AUTO) {
/* 自增主键 */
keyGenerator = new Jdbc3KeyGenerator();
keyProperty = table.getKeyProperty();
keyColumn = table.getKeyColumn();
} else {
if (null != table.getKeySequence()) {
keyGenerator = TableInfoHelper.genKeyGenerator(table, builderAssistant, sqlMethod.getMethod(), languageDriver);
keyProperty = table.getKeyProperty();
keyColumn = table.getKeyColumn();
fieldBuilder.append(table.getKeyColumn()).append(",");
placeholderBuilder.append("#{").append(table.getKeyProperty()).append("},");
} else {
/* 用户输入自定义ID */
fieldBuilder.append(table.getKeyColumn()).append(",");
// 正常自定义主键策略
placeholderBuilder.append("#{").append(table.getKeyProperty()).append("},");
}
}
}
// 是否 IF 标签判断
boolean ifTag;
List fieldList = table.getFieldList();
for (TableFieldInfo fieldInfo : fieldList) {
// 在FieldIgnore,INSERT_UPDATE,INSERT 时设置为false
ifTag = !(FieldFill.INSERT == fieldInfo.getFieldFill()
|| FieldFill.INSERT_UPDATE == fieldInfo.getFieldFill());
if (selective && ifTag) {
fieldBuilder.append(convertIfTagIgnored(fieldInfo, false));
fieldBuilder.append(fieldInfo.getColumn()).append(",");
fieldBuilder.append(convertIfTagIgnored(fieldInfo, true));
placeholderBuilder.append(convertIfTagIgnored(fieldInfo, false));
placeholderBuilder.append("#{").append(fieldInfo.getEl()).append("},");
placeholderBuilder.append(convertIfTagIgnored(fieldInfo, true));
} else {
fieldBuilder.append(fieldInfo.getColumn()).append(",");
placeholderBuilder.append("#{").append(fieldInfo.getEl()).append("},");
}
}
fieldBuilder.append("\n ");
placeholderBuilder.append("\n ");
String sql = String.format(sqlMethod.getSql(), table.getTableName(), fieldBuilder.toString(),
placeholderBuilder.toString());
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
this.addInsertMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource, keyGenerator, keyProperty,
keyColumn);
}
/**
*
* Oracle Key Sequence 生成器
*
*
* @author hubin
* @Date 2017-05-08
*/
public class OracleKeyGenerator implements IKeyGenerator {
@Override
public String executeSql(String incrementerName) {
StringBuilder sql = new StringBuilder();
sql.append("SELECT ").append(incrementerName);
sql.append(".NEXTVAL FROM DUAL");
return sql.toString();
}
}
CREATE SEQUENCE SEQ_VZ_FAIL_ORDER_MESSAGE 1 MAXVALUE 9999999999999999999999999999 START WITH 1 INCREMENT BY 1 CACHE 100;