添加freemarker执行工具
parent
e4a9621385
commit
6f57ee9d6e
@ -0,0 +1,83 @@
|
||||
package com.supervision.util.freemark;
|
||||
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import com.supervision.domain.IrKnowledge;
|
||||
import com.supervision.service.IrKnowledgeService;
|
||||
import freemarker.cache.TemplateLoader;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
|
||||
/**
|
||||
* Created by xiaoj on 2016/9/8.
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class DatabaseTemplateLoader implements TemplateLoader {
|
||||
|
||||
private final IrKnowledgeService irKnowledgeService;
|
||||
|
||||
@Override
|
||||
public Object findTemplateSource(String templateId) {
|
||||
try {
|
||||
IrKnowledge irKnowledge = irKnowledgeService.getById(templateId);
|
||||
long lastModified = ObjUtil.isNull(irKnowledge.getUpdateTime()) ? 0L : irKnowledge.getUpdateTime().getNano();
|
||||
return new StringTemplateSource(templateId, irKnowledge.getResultTemplate(),lastModified);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLastModified(Object templateSource) {
|
||||
return ((StringTemplateSource) templateSource).lastModified;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Reader getReader(Object templateSource, String encoding) {
|
||||
return new StringReader(((StringTemplateSource) templateSource).source);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeTemplateSource(Object templateSource) {
|
||||
//do nothing
|
||||
}
|
||||
|
||||
private static class StringTemplateSource {
|
||||
private final String name;
|
||||
private final String source;
|
||||
private final long lastModified;
|
||||
|
||||
StringTemplateSource(String name, String source, long lastModified) {
|
||||
if (name == null) {
|
||||
throw new IllegalArgumentException("name == null");
|
||||
}
|
||||
if (source == null) {
|
||||
throw new IllegalArgumentException("source == null");
|
||||
}
|
||||
if (lastModified < -1L) {
|
||||
throw new IllegalArgumentException("lastModified < -1L");
|
||||
}
|
||||
this.name = name;
|
||||
this.source = source;
|
||||
this.lastModified = lastModified;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof StringTemplateSource) {
|
||||
return name.equals(((StringTemplateSource) obj).name);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.supervision.util.freemark;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
||||
@Configuration
|
||||
public class FreemarkerConfiguration {
|
||||
@Bean("databaseFreemarkerConfiguration")
|
||||
public freemarker.template.Configuration databaseTemplateLoader(DatabaseTemplateLoader databaseTemplateLoader) {
|
||||
freemarker.template.Configuration configuration = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_30);
|
||||
configuration.setTemplateLoader(databaseTemplateLoader);
|
||||
return configuration;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.supervision.util;
|
||||
package com.supervision.util.mybatis;
|
||||
|
||||
import org.apache.ibatis.builder.StaticSqlSource;
|
||||
import org.apache.ibatis.mapping.*;
|
@ -1,5 +1,6 @@
|
||||
package com.supervision.util;
|
||||
package com.supervision.util.mybatis;
|
||||
|
||||
import com.supervision.util.mybatis.RowMapperStatementBuilder;
|
||||
import org.apache.ibatis.exceptions.TooManyResultsException;
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
import org.springframework.stereotype.Component;
|
Loading…
Reference in New Issue