JFinal极速开发框架使用笔记(四) _JFinalDemoGenerator实体类生成及映射自动化

运行_JFinalDemoGenerator生成实体类和映射之前的项目结构:

运行之后:

生成和改变的文件:

实体类:

package com.wazn.model;

import com.wazn.model.base.BaseStudent;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class Student extends BaseStudent<Student> {
    public static final Student dao = new Student().dao();
}
package com.wazn.model.base;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.IBean;

/**
 * Generated by JFinal, do not modify this file.
 */
@SuppressWarnings({"serial", "unchecked"})
public abstract class BaseStudent<M extends BaseStudent<M>> extends Model<M> implements IBean {

    public M setStudentid(java.lang.Integer studentid) {
        set("studentid", studentid);
        return (M)this;
    }
    
    public java.lang.Integer getStudentid() {
        return getInt("studentid");
    }

    public M setStudentname(java.lang.String studentname) {
        set("studentname", studentname);
        return (M)this;
    }
    
    public java.lang.String getStudentname() {
        return getStr("studentname");
    }

}
package com.wazn.model;

import com.wazn.model.base.BaseBlog;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class Blog extends BaseBlog<Blog> {
    public static final Blog dao = new Blog().dao();
}
package com.wazn.model.base;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.IBean;

/**
 * Generated by JFinal, do not modify this file.
 */
@SuppressWarnings({"serial", "unchecked"})
public abstract class BaseBlog<M extends BaseBlog<M>> extends Model<M> implements IBean {

    public M setId(java.lang.Integer id) {
        set("id", id);
        return (M)this;
    }
    
    public java.lang.Integer getId() {
        return getInt("id");
    }

    public M setTitle(java.lang.String title) {
        set("title", title);
        return (M)this;
    }
    
    public java.lang.String getTitle() {
        return getStr("title");
    }

    public M setContent(java.lang.String content) {
        set("content", content);
        return (M)this;
    }
    
    public java.lang.String getContent() {
        return getStr("content");
    }

    public M setDate(java.lang.String date) {
        set("date", date);
        return (M)this;
    }
    
    public java.lang.String getDate() {
        return getStr("date");
    }

}

自动写入映射:

package com.wazn.model;

import com.jfinal.plugin.activerecord.ActiveRecordPlugin;

/**
 * Generated by JFinal, do not modify this file.
 * <pre>
 * Example:
 * public void configPlugin(Plugins me) {
 *     ActiveRecordPlugin arp = new ActiveRecordPlugin(...);
 *     _MappingKit.mapping(arp);
 *     me.add(arp);
 * }
 * </pre>
 */
public class _MappingKit {
    
    public static void mapping(ActiveRecordPlugin arp) {
        arp.addMapping("blog", "id", Blog.class);
        arp.addMapping("student", "studentid", Student.class);
    }
}

这里需要注意的是,JFinal中的这个自动映射文件_MappingKit因为在继承JFinalConfig类的主文件中需要指定,所以生成后会在该文件中写入,但是,如在 _JFinalDemoGenerator

中指定的 _MappingKit路径和原来的_MappingKit文件路径不一致,将会在新的指定的路径生成一个新的_MappingKit文件。

接下来是主文件:

package com.wazn.common;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;
import com.wazn.model._MappingKit;

public class GeneratorConfig extends JFinalConfig{
    
    public static void main(String[] args) {
        JFinal.start("src/main/webapp", 80, "/", 5);
    }

    @Override
    public void configConstant(Constants me) {
        // 加载少量必要配置,随后可用PropKit.get(...)获取值
        PropKit.use("a_little_config.txt");
        me.setDevMode(PropKit.getBoolean("devMode", false));
        
    }

    @Override
    public void configEngine(Engine me) {

        
    }

    @Override
    public void configHandler(Handlers arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void configInterceptor(Interceptors arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void configPlugin(Plugins me) {
        // 配置 druid 数据库连接池插件
        DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
        me.add(druidPlugin);
        
        // 配置ActiveRecord插件
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        // 所有映射在 MappingKit 中自动化搞定
        _MappingKit.mapping(arp);
        me.add(arp);
        
    }

    @Override
    public void configRoute(Routes arg0) {
        // TODO Auto-generated method stub
        
    }

    public static DruidPlugin createDruidPlugin() {
        return new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
    }

}

这里因为只是测试使用_JFinalDemoGenerator生成实体类,所以只有相关的代码,没有其他的代码配置。

_JFinalDemoGenerator生成器:

package com.wazn.model;

import javax.sql.DataSource;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.generator.Generator;
import com.jfinal.plugin.druid.DruidPlugin;
import com.wazn.common.GeneratorConfig;

public class _JFinalDemoGenerator {
    
    public static DataSource getDataSource() {
        PropKit.use("a_little_config.txt");
        DruidPlugin druidPlugin = GeneratorConfig.createDruidPlugin();
        druidPlugin.start();
        return druidPlugin.getDataSource();
    }
    
    public static void main(String[] args) {
        // base model 所使用的包名
        String baseModelPackageName = "com.wazn.model.base";
        // base model 文件保存路径
        String baseModelOutputDir = PathKit.getWebRootPath() + "/src/main/java/com/wazn/model/base";
        
        // model 所使用的包名 (MappingKit 默认使用的包名)
        String modelPackageName = "com.wazn.model";
        // model 文件保存路径 (MappingKit 与 DataDictionary 文件默认保存路径)
        String modelOutputDir = baseModelOutputDir + "/..";
        
        // 创建生成器
        Generator generator = new Generator(getDataSource(), baseModelPackageName, baseModelOutputDir, modelPackageName, modelOutputDir);
        // 设置是否生成链式 setter 方法
        generator.setGenerateChainSetter(false);
        // 添加不需要生成的表名
        generator.addExcludedTable("adv");
        // 设置是否在 Model 中生成 dao 对象
        generator.setGenerateDaoInModel(true);
        // 设置是否生成链式 setter 方法
        generator.setGenerateChainSetter(true);
        // 设置是否生成字典文件
        generator.setGenerateDataDictionary(false);
        // 设置需要被移除的表名前缀用于生成modelName。例如表名 "osc_user",移除前缀 "osc_"后生成的model名为 "User"而非 OscUser
        generator.setRemovedTableNamePrefixes("t_");
        // 生成
        generator.generate();
    }
}

 

posted @ 2018-01-27 15:12  谁将新樽辞旧月,今月曾经照古人  阅读(1159)  评论(0编辑  收藏  举报