# Jfinal的Sql模板高级用法

Jfinal的Sql模板高级用法

  1. 配置Jfinal
@SpringBootConfiguration
 public static ActiveRecordPlugin arp = null;
    public static Engine engine= null;
    /**
     * 配置数据源头
     */
    @Bean
    public ActiveRecordPlugin initActiveRecordPlugin() {
        try {
            Prop prop = PropKit.use("db.config");
            String url = prop.get("dburl");
            String username = prop.get("dbusername");
            String password = prop.get("dbpassword");
            DruidPlugin druidPlugin = new DruidPlugin(url, username, password);
            //加强数据库安全
            WallFilter wallFilter = new WallFilter();
            wallFilter.setDbType("mysql");
            druidPlugin.addFilter(wallFilter);
            //添加startFilter才会有数据
            druidPlugin.addFilter(new StatFilter());
            druidPlugin.start();
            arp = new ActiveRecordPlugin("mysql", druidPlugin);
            arp.setShowSql(true);
            //配置缓存
            arp.setCache(new IcacherForJfinal());
//            _MappingKit.mapping(arp);
            arp.setDevMode(true);
            // 设置sql模板位置
            engine=arp.getEngine();
            //设置模板的位置为Resource下的"/sql"
            engine.setBaseTemplatePath("/sql");
            arp.start();
            log.info("The ActiveRecordPlugin is start!");
            return arp;
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return null;
        }
    }
  1. 配置临时模板的读取
    jfinal创建模板源码
	/**
	 * Get template by string content and do not cache the template
	 */
	public Template getTemplateByString(String content) {
		return getTemplateByString(content, false);
	}
	/**
	 * Get template by string content
	 * 
	 * 重要:StringSource 中的 cacheKey = HashKit.md5(content),也即 cacheKey
	 *     与 content 有紧密的对应关系,当 content 发生变化时 cacheKey 值也相应变化
	 *     因此,原先 cacheKey 所对应的 Template 缓存对象已无法被获取,当 getTemplateByString(String)
	 *     的 String 参数的数量不确定时会引发内存泄漏
	 *     
	 *     当 getTemplateByString(String, boolean) 中的 String 参数的
	 *     数量可控并且确定时,才可对其使用缓存 
	 *     
	 * @param content 模板内容
	 * @param cache true 则缓存 Template,否则不缓存
	 */
	public Template getTemplateByString(String content, boolean cache) {
		if (!cache) {
			return buildTemplateBySource(new StringSource(content, cache));
		}
		
		String cacheKey = HashKit.md5(content);
		Template template = templateCache.get(cacheKey);
		if (template == null) {
			template = buildTemplateBySource(new StringSource(content, cache));
			templateCache.put(cacheKey, template);
		} else if (devMode) {
			if (template.isModified()) {
				template = buildTemplateBySource(new StringSource(content, cache));
				templateCache.put(cacheKey, template);
			}
		}
		return template;
	}
  1. 在工具类中写如下三个方法完成sql模板的使用,调用方法时候继承该工具类即可
/**
     * 读取sql
     */
public String getSqlbyFile(String file, HashMap<String, Object> map) {
        return readSql(getTmplSql(file), map);
}
    /**
     * 读取/sql文件夹下的sql文件
     * */
private String getTmplSql(String file) {
try {
  InputStream inputStream =this.getClass().getResourceAsStream("/sql/" + file + ".sql");
  BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
  String line = "";
  StringBuffer stringBuffer=new StringBuffer();
  while ((line = br.readLine()) != null) {
      System.out.println(line);
      stringBuffer.append(line+" ");
  }
 	 return String.valueOf(stringBuffer);
  } catch (Exception e) {
  e.printStackTrace();
  return null;
  }
 }
    /**
     * 使用sql模板
     * */
public static String readSql(String file, HashMap<String, Object> map) {
 return ActiveRecordPluginConfig.engine.getTemplateByString(file).
 renderToString(map);
}
  1. 使用模板查询
 String sql = getSqlbyFile("test", hashMap);   
 Db.find(sql);
发布了123 篇原创文章 · 获赞 9 · 访问量 4003

猜你喜欢

转载自blog.csdn.net/qq_37248504/article/details/103285676
今日推荐