在maven的velocity项目中将resources下的模板资源文件打包。
将velocity模块化引入到其它项目时报Unable to find resource '';Velocity.getTemplate获取资源文件出错!
VelocityContext velocityContext = new VelocityContext(context.toMap());
Template template = Velocity.getTemplate("mybatis\Model.java.vm", "UTF-8")
try (StringWriter writer = new StringWriter()) {
template.merge(velocityContext, writer);
return writer.toString();
} catch (IOException e) {
e.printStackTrace();
}
解决方案1:
配置maven的build(可选择其它方式配置)
src/main/resources
即可。
解决方案2:
使用变量编译模板。
final String templateContent = "package com.sicex.db.entity.${table.classPackage};n" +
"n" +
"import com.sicex.db.annotation.Column;n" +
"import com.sicex.db.annotation.Key;n" +
"import com.sicex.db.annotation.Table;n" +
"import com.sicex.db.type.KeyType;n" +
"import lombok.Data;n" +
"import org.apache.ibatis.type.JdbcType;n" +
"n" +
"import java.util.Date;n" +
"n" +
"n" +
"@Datan" +
"@Table(name = "${table.tableCode}")n" +
"public class ${table.classCode} {n" +
"#foreach (${column} in ${table.columns})n" +
" n" +
" #if (${column.primaryKey})n" +
"@Key(${column.primaryRule})n" +
" #endn" +
"@Column(name = "${column.columnCode}",jdbcType = ${column.jdbcType})n" +
" private ${column.fieldType} ${column.fieldCode};n" +
"#endn" +
"}n";
编译方式:
VelocityContext velocityContext = new VelocityContext(context.toMap());
VelocityEngine velocityEngine = new VelocityEngine();
try (StringWriter writer = new StringWriter()) {
velocityEngine.evaluate(velocityContext,writer,"",templateContent);
return writer.toString();
} catch (IOException e) {
e.printStackTrace();
}



