ys.artifact-orm.1.0.1.source-code.dao-mysql.btl Maven / Gradle / Ivy
<%
// 字段名
var ALL_COLUMN = "";
for(column in table.columns){
ALL_COLUMN = ALL_COLUMN + column.columnName;
if(!columnLP.last){
ALL_COLUMN = ALL_COLUMN + ", ";
}
}
// 占位符
var ALL_PLA = "";
for(column in table.columns){
ALL_PLA = ALL_PLA + "?";
if(!columnLP.last){
ALL_PLA = ALL_PLA + ",";
}
}
var packages = [];
array.addOnly(packages,"org.artifact.core.db.MySqlDao");
array.addOnly(packages,"org.artifact.core.lang.StrPlus");
array.addOnly(packages,"java.util.List");
array.addOnly(packages,"java.text.MessageFormat");
array.addOnly(packages,templates['BeanTemplate'].package + "." + table.className);
%>
<%for(package in packages){%>
import ${package};
<%}%>
import static cn.hutool.core.util.ObjectUtil.equal;
public abstract class ${table.className+templates['DaoTemplate'].classSuffix} extends MySqlDao<${table.className}>{
protected final String sqlAllColumn = "${ALL_COLUMN}";
protected final String sqlAllPla = "${ALL_PLA}";
<%include("dao-common.btl"){}%>
@Override
public boolean createTable(){
StrPlus plus = StrPlus.b("CREATE TABLE IF NOT EXISTS `${table.tableName}` (");
<%for(column in table.columns){%>
plus.a(" `${column.columnName}` ${column.dbType} NOT NULL COMMENT '${column.remark}',");
<%}%>
<%
var comma = "";
if(!isEmpty(table.indexs)){
comma = ",";
}
%>
plus.a(" PRIMARY KEY (`${table.pk.columnName}`)${comma}");
<%
for(index in table.indexs){
var indexColumns = "";
for(column in index.columns){
indexColumns = indexColumns + "`" + column.columnName + "`";
if(!columnLP.last){
indexColumns = indexColumns + ",";
}
}
if(indexLP.last){
comma = "";
}else{
comma = ",";
}
%>
<%if(index.unique){%>
plus.a(" UNIQUE KEY (${indexColumns})${comma}");
<%}else{%>
plus.a(" KEY (${indexColumns})${comma}");
<%}%>
<%}%>
plus.a(") ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='${table.remark}';");
update(plus.e());
return true;
}
@Override
protected String getSaveSql() {
return MessageFormat.format("REPLACE INTO {0} ({1}) VALUES ({2})",getTableName(),sqlAllColumn,sqlAllPla);
}
@Override
protected String getDeleteSql() {
return MessageFormat.format("DELETE FROM {0} WHERE ${table.pk.columnName} = ?",getTableName());
}
<%
for(index in table.indexs){
var methodSuffix = "";
var parameter = "";
var sqlCondition = "";
var values = "";
var equals = "";
for(column in index.columns){
var ColumnName = uf(column.fieldName);
var columnName = lf(column.fieldName);
methodSuffix = methodSuffix + ColumnName;
parameter = parameter + column.type + " " + columnName;
sqlCondition = sqlCondition + column.columnName + " = ?";
values = values + columnName;
equals = equals + 'equal(t.get'+ColumnName+'(), '+columnName+')';
if(!columnLP.last){
parameter = parameter + " ,";
sqlCondition = sqlCondition + " AND ";
values = values + " ,";
equals = equals + " && ";
}
}
%>
<%if(index.unique){%>
public ${table.className} findBy${methodSuffix}(${parameter}){
String sql = MessageFormat.format("SELECT {0} FROM {1} WHERE ${sqlCondition}",sqlAllColumn,getTableName());
return resultFirst(()->{
return findFirst(sql,new Object[]{${values}});
}, t -> {
return ${equals};
}) ;
}
<%}else{%>
public List<${table.className}> findBy${methodSuffix}(${parameter}){
String sql = MessageFormat.format("SELECT {0} FROM {1} WHERE ${sqlCondition}",sqlAllColumn,getTableName());
return resultList(()->{
return find(sql,new Object[]{${values}});
}, t -> {
return ${equals};
}) ;
}
<%}%>
<%}%>
}