搭建Jfinal+maven專案及若干問題總結
阿新 • • 發佈:2021-11-03
專案準備
在idea環境中,新建maven型別專案,專案型別選擇:maven-archetype-webapp,完成後在pom檔案中新增如下內容:
<dependency> <groupId>com.jfinal</groupId> <artifactId>jfinal-undertow</artifactId> <version>1.9</version> </dependency> <dependency> <groupId>com.jfinal</groupId> <artifactId>jfinal</artifactId> <version>4.7</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.14</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency>
一次性把mysql驅動、連線池及執行伺服器引入,這裡選擇undertow伺服器。
新建專案結構如下所示:
在[project structure]選單中,設定好專案程式碼目錄和資源目錄。然後在資源目錄中引入app.properties檔案,作為配置檔案。
配置檔案內容如下所示:
jdbcUrl=jdbc:mysql://localhost:3306/col?user=root&password=root&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=falseusername= password=
專案配置
新建啟動配置檔案:Democonfig.java
public class DemoConfig extends JFinalConfig { public static Prop prop = PropKit.use("app.properties"); static DruidPlugin getDruidPlugin() { return new DruidPlugin(prop.get("jdbcUrl"), prop.get("username"), prop.get("password").trim()); } public static void main(String[] args) { UndertowServer.start(DemoConfig.class, 80, true); } public void configConstant(Constants me) { me.setDevMode(true); } @Override public void configRoute(Routes me) { me.add("/name", nameController.class); } public void configEngine(Engine me) { } @Override public void configPlugin(Plugins me) { DruidPlugin druidPlugin = getDruidPlugin(); druidPlugin.setMinIdle(1); druidPlugin.setMaxActive(1); // 新增StatFilter druidPlugin.addFilter(new StatFilter()); me.add(druidPlugin); ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin); me.add(arp); arp.setDialect(new MysqlDialect()); _MappingKit.mapping(arp); arp.setShowSql(prop.getBoolean("devMode", false)); //配置SQL模板引擎預設檔案路徑 arp.getEngine().setSourceFactory(new ClassPathSourceFactory()); } public void configInterceptor(Interceptors me) { } public void configHandler(Handlers me) { } }
注意,需要在configPlugin函式中引入ActiveRecordPlugin例項,否則會報錯:
十一月 03, 2021 2:25:04 下午 com.jfinal.core.ActionHandler error 嚴重: *****.controller.nameController.select() : /name/select java.lang.NullPointerException at com.jfinal.plugin.activerecord.Model.find(Model.java:692) at com.jfinal.plugin.activerecord.Model.find(Model.java:703) at com.jfinal.plugin.activerecord.Model.find(Model.java:710) at *****.controller.nameController.select(nameController.java:20)
配置檔案中引用了nameController,其內容如下:
public class nameController extends Controller { public void select() { renderText("Welcome To The JFinal World (^_^)"); } public void add() { Test test = new Test(); test.set("name", "w2323"); test.save(); renderText("hello"); } public void edit() { renderText("hello"); } public void delete() { renderText("hello"); } }
執行
在Democonfig檔案中,點選main方法執行,在瀏覽器中檢視即可:
程式碼生成
新建檔案:ModelGenerator.java,使用jfinal自帶的plugin元件生成實體程式碼。
public class ModelGenerator {
private static DataSource getDataSource() {
DruidPlugin druidPlugin = DemoConfig.getDruidPlugin();
druidPlugin.start();
return druidPlugin.getDataSource();
}
public static void main(String[] args) {
// base model 所使用的包名
String baseModelPackageName = "****.***.***.model.base";
// base model 檔案儲存路徑
String baseModelOutputDir = PathKit.getWebRootPath() + "/src/main/java/cn/***/model/base";
// model 所使用的包名 (MappingKit 預設使用的包名)
String modelPackageName = "****.collect.comoneo.model";
// model 檔案儲存路徑 (MappingKit 與 DataDictionary 檔案預設儲存路徑)
String modelOutputDir = baseModelOutputDir + "/..";
// 建立生成器
Generator gen = new Generator(getDataSource(), baseModelPackageName, baseModelOutputDir, modelPackageName, modelOutputDir);
// 設定資料庫方言
gen.setDialect(new MysqlDialect());//Sqlite3Dialect MysqlDialect
// 設定 BaseModel 是否生成鏈式 setter 方法
gen.setGenerateChainSetter(true);
// 設定是否在 Model 中生成 dao 物件
gen.setGenerateDaoInModel(true);
// 設定是否生成字典檔案
gen.setGenerateDataDictionary(true);
// 配置是否生成欄位備註
gen.setGenerateRemarks(true);
// 設定需要被移除的表名字首用於生成modelName。例如表名 "sys_user",移除字首 "sys_"後生成的model名為 "User"而非 OscUser
// gernerator.setRemovedTableNamePrefixes("sys_");
// 生成
gen.generate();
}
}
如資料庫結構發生變化,執行此檔案main即可,注意:程式碼生成及此處配置使用了MysqlDialect資料庫方言,如果連線其他型別的資料庫,請修改。
備註
在新增時,jfinal框架會在save後自動引入新增實體,不用再次查詢,屬於自返回型別。在業務編碼上確實方便了不少。