Jfinal基础学习(一)

我负责的项目都是Jfinal的了,有必要写一些学习知识,记录下来。

1、PropKit.use("config.txt", "UTF-8");

PropKit 工具类用来操作外部配置文件

public class AppConfig extends JFinalConfig { 
public void configConstant(Constants me) { 
// 第一次使用use加载的配置将成为主配置,可以通过PropKit.get(...)直接取值 
PropKit.use("a_little_config.txt"); 
me.setDevMode(PropKit.getBoolean("devMode")); 
} 
 
public void configPlugin(Plugins me) { 
// 非第一次使用use加载的配置,需要通过每次使用use来指定配置文件名再来取值 
String redisHost = PropKit.use("redis_config.txt").get("host"); 
int redisPort = PropKit.use("redis_config.txt").getInt("port"); 
RedisPlugin rp = new RedisPlugin("myRedis", redisHost, redisPort); 
me.add(rp); 
 
// 非第一次使用 use加载的配置,也可以先得到一个Prop对象,再通过该对象来获取值 
Prop p = PropKit.use("db_config.txt"); 
DruidPlugin  dp  =  new  DruidPlugin(p.get("jdbcUrl"),  p.get("user")…); 
me.add(dp); 
} 
} 

 2、Tx 事务拦截器

以下为事务处理示例:

boolean succeed = Db.tx(new IAtom(){ 
  public boolean run() throws SQLException { 
    int  count = Db.update("update account set cash = cash  -  ? where 
id = ?", 100, 123); 
    int  count2 = Db.update("update account set cash = cash + ? where 
id = ?", 100, 456); 
    return count == 1 && count2 == 1; 
  }}); 
} 

以上两次数据库更新操作在一个事务中执行,如果执行过程中发生异常或者 invoke()方法
返回 false,则自动回滚事务。

3、Jfinal基本配置代码

/**
 * API引导式配置
 */
public class CommonConfig extends JFinalConfig {

    public static Properties p;

    /**
     * 配置常量
     */
    @Override
    public void configConstant(Constants me) {
        p = loadPropertyFile("project.txt"); // 加载少量必要配置,随后可用getProperty(...)获取值
        me.setDevMode(getPropertyToBoolean("devMode", false));
        me.setBaseViewPath("/WEB-INF/jsp/");
        me.setViewType(ViewType.JSP); // 设置视图类型为Jsp,否则默认为FreeMarker
        
        //me.setError404View("/error/error_404.html");
        //me.setError500View("/error/error_500.html");
    }

    /**
     * 配置路由
     */
    @Override
    public void configRoute(Routes me) {
        AutoBindRoutes auto = new AutoBindRoutes();
        me.add(auto);
    }

    /**
     * 配置插件
     */
    @Override
    public void configPlugin(Plugins me) {
        /**
         * // 配置C3p0数据库连接池插件 C3p0Plugin c3p0Plugin = new
         * C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"),
         * getProperty("password").trim()); me.add(c3p0Plugin);
         * 
         * // 配置ActiveRecord插件 ActiveRecordPlugin arp = new
         * ActiveRecordPlugin(c3p0Plugin); me.add(arp); arp.addMapping("blog",
         * Blog.class); // 映射blog 表到 Blog模型
         */

        DruidPlugin druidPlugin = new DruidPlugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password"), getProperty("driverClass"));
        druidPlugin.setValidationQuery("select 1 from dual");
        druidPlugin.setTestOnBorrow(true);
        druidPlugin.setFilters("stat");
        me.add(druidPlugin);
        AutoTableBindPlugin arp = new AutoTableBindPlugin(druidPlugin, SimpleNameStyles.UP_UNDERLINE);

        // arp.setDialect(new MysqlDialect()); //mysql的方言配置
        arp.setDialect(new OracleDialect());
        arp.setContainerFactory(new CaseInsensitiveContainerFactory());
        arp.setShowSql(true);//输出sql
        me.add(arp);
        
        QuartzPlugin quartzPlugin =  new QuartzPlugin("job.properties"); 
        me.add(quartzPlugin);
    }

    /**
     * 配置全局拦截器
     */
    @Override
    public void configInterceptor(Interceptors me) {
        me.add(new TxByRegex("save;delete;submit;update"));
        me.add(new AuthInterceptor());
        //me.add(new ExceptionInterceptor());
    }

    /**
     * 配置处理器
     */
    @Override
    public void configHandler(Handlers me) {
        me.add(new UrlSkipHandler("/hessian.*", false));
        me.add(new BaseUrlHandler());
        me.add(new ContextPathHandler("contextpath"));
        me.add(new DruidStatViewHandler("/druid"));
    }

    /**
     * 建议使用 JFinal 手册推荐的方式启动项目 运行此 main
     * 方法可以启动项目,此main方法可以放置在任意的Class类定义中,不一定要放于此
     */
    public static void main(String[] args) {
        JFinal.start("WebRoot", 80, "/", 5);
    }
}

 

/**
	 * 配置常量
	 */
	@Override
	public void configConstant(Constants me) {
		p = loadPropertyFile("project.txt"); // 加载少量必要配置,随后可用getProperty(...)获取值
		me.setDevMode(getPropertyToBoolean("devMode", false));
		me.setBaseViewPath("/WEB-INF/jsp/");
		me.setViewType(ViewType.JSP); // 设置视图类型为Jsp,否则默认为FreeMarker
	}

  数据库配置信息都这

/WEB-INF/jsp/project.txt 里了。
原文地址:https://www.cnblogs.com/hoge/p/4961257.html