2.12.整合sqllite
2.12.1.创建sqllite数据库
sqllite是一个嵌入式的数据库
使用Navicat Premium创建sqllite文件
创建表,插入数据
CREATE TABLE "student" ( "id" integer NOT NULL, "name" text NOT NULL, "grade" textNOT NULL, PRIMARY KEY ("id") ); INSERT INTO "student" VALUES (1, '沈', '一年级');
2.12.2.添加依赖
<dependencies> <dependency> <groupId>com.jfinal</groupId> <artifactId>jfinal-undertow</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>com.jfinal</groupId> <artifactId>jfinal</artifactId> <version>4.9.15</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--桥接器:告诉slf4j使用Log4j2 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.1</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.23</version> </dependency> <!-- sqlite-jdbc --> <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <version>3.7.2</version> </dependency> </dependencies>
2.12.3.生成model文件
添加代码如下
package com.litongjava.jfinal.db.gen; import javax.sql.DataSource; import com.jfinal.kit.PathKit; import com.jfinal.kit.Prop; import com.jfinal.kit.PropKit; import com.jfinal.kit.StrKit; import com.jfinal.plugin.activerecord.dialect.MysqlDialect; import com.jfinal.plugin.activerecord.generator.Generator; import com.jfinal.plugin.druid.DruidPlugin; public class JFinalGenerator { public static String configFileName = "undertow.properties"; public static Prop p; public static void main(String[] args) { // model 所使用的包名 (MappingKit 默认使用的包名) String modelPackageName = "com.litongjava.jfinal.demo.model"; // base model 所使用的包名 String baseModelPackageName = modelPackageName + ".base";// "com.litong.db.monitoring.common.model.base"; // base model 文件保存路径 String baseModelOutputDir = getBaseModelOutputDir(baseModelPackageName); // model 文件保存路径 (MappingKit 与 DataDictionary 文件默认保存路径) String modelOutputDir = baseModelOutputDir + "/.."; // 创建生成器 Generator generator = new Generator(getDataSource(), baseModelPackageName, baseModelOutputDir, modelPackageName, modelOutputDir); // 配置是否生成备注 generator.setGenerateRemarks(true); // 设置数据库方言 generator.setDialect(new MysqlDialect()); // 设置是否生成链式 setter 方法 generator.setGenerateChainSetter(false); // 添加不需要生成的表名 // generator.addExcludedTable("t_db_connect_info"); // 设置是否在 Model 中生成 dao 对象 generator.setGenerateDaoInModel(true); // 设置是否生成字典文件 generator.setGenerateDataDictionary(false); // 设置需要被移除的表名前缀用于生成modelName。例如表名 "osc_user",移除前缀 // "osc_"后生成的model名为"User"而非OscUser generator.setRemovedTableNamePrefixes("t_"); // 生成 generator.generate(); } public static String getBaseModelOutputDir(String modelPackageName) { String replace = modelPackageName.replace('.', '/'); return PathKit.getWebRootPath() + "/src/main/java/" + replace; } private static void loadConfig() { if (p == null) { p = PropKit.use(configFileName); } } public static DruidPlugin createDruidPlugin() { loadConfig(); String url = p.get("jdbc.url").trim(); String user = null; try { user = p.get("jdbc.user"); }catch(Exception e) { e.printStackTrace(); } String pswd = p.get("jdbc.pswd"); if (!StrKit.isBlank(pswd)) { pswd = pswd.trim(); } return new DruidPlugin(url, user, pswd); } public static DataSource getDataSource() { DruidPlugin druidPlugin = createDruidPlugin(); druidPlugin.start(); return druidPlugin.getDataSource(); } }
生成的文件如下
2.12.4.配置文件
undertow.properties文件如下
undertow.devMode=true undertow.port=11020 undertow.host=127.0.0.1 undertow.contextPath=/ee-jfinal-4.9-sqllite undertow.resourcePath = #jdbc-mysql jdbc.url=jdbc:sqlite:D:/sqllite/test01.db jdbc.user= jdbc.pswd= jdbc.showSql=true
2.12.5.启动类
package com.litongjava.jfinal; 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.server.undertow.UndertowServer; import com.jfinal.template.Engine; import com.litongjava.jfinal.config.DbConfig; import com.litongjava.jfinal.utils.PropKitUtils; import lombok.extern.slf4j.Slf4j; @Slf4j public class Application extends JFinalConfig { private static String configFileName = PropKitUtils.configFileName; public static void main(String[] args) { long start = System.currentTimeMillis(); UndertowServer.create(Application.class, configFileName).start(); long end = System.currentTimeMillis(); System.out.println("启动完成,共使用了" + (end - start) + "ms"); } public void configConstant(Constants me) { me.setInjectDependency(true); me.setInjectSuperClass(true); // } public void configRoute(Routes me) { me.setMappingSuperClass(true); me.scan("com.litongjava.jfinal.demo.controller."); } @Override public void configEngine(Engine me) { } @Override public void configPlugin(Plugins me) { log.info("开始配置插件"); DbConfig.config(me); } @Override public void configInterceptor(Interceptors me) { } @Override public void configHandler(Handlers me) { } }
2.12.6.配置数据库
package com.litongjava.jfinal.config; import com.jfinal.config.Plugins; import com.jfinal.plugin.activerecord.ActiveRecordPlugin; import com.jfinal.plugin.activerecord.dialect.Sqlite3Dialect; import com.jfinal.plugin.druid.DruidPlugin; import com.litongjava.jfinal.demo.model._MappingKit; import com.litongjava.jfinal.utils.PropKitUtils; import lombok.extern.slf4j.Slf4j; /** * @author create by ping-e-lee on 2021年7月9日 下午1:26:18 * @version 1.0 * @desc */ @Slf4j public class DbConfig { public static void config(Plugins me) { DruidPlugin dp = initDruidPlugin(); ActiveRecordPlugin ar = initActiveRecordPlugin(dp); me.add(dp); me.add(ar); log.info("添加主数据源完成"); } public static ActiveRecordPlugin initActiveRecordPlugin(DruidPlugin dp) { ActiveRecordPlugin arp = new ActiveRecordPlugin(dp); arp.setDialect(new Sqlite3Dialect()); arp.setShowSql(PropKitUtils.getBoolean("jdbc.showSql")); _MappingKit.mapping(arp); return arp; } public static DruidPlugin initDruidPlugin() { String jdbcUrl = PropKitUtils.get("jdbc.url"); String jdbcUser = PropKitUtils.get("jdbc.user"); String jdbcPswd = PropKitUtils.get("jdbc.pswd"); /** * druid会自动检测数据库驱动 */ DruidPlugin dp = new DruidPlugin(jdbcUrl, jdbcUser, jdbcPswd); return dp; } }
2.12.7.工具类获取读取配置文件
package com.litongjava.jfinal.utils; import com.jfinal.kit.Prop; import com.jfinal.kit.PropKit; public class PropKitUtils { public static String configFileName = "undertow.properties"; public static Prop prop; static { loadConfig(); } public static void loadConfig() { if (prop == null) { prop = PropKit.use(configFileName); } } public static int getInt(String key) { return prop.getInt(key); } public static String get(String key) { return prop.get(key); } public static Boolean getBoolean(String key) { return prop.getBoolean(key, null); } }
2.12.8.controller
编写一个controller,测试查询
package com.litongjava.jfinal.demo.controller; import java.util.List; import com.jfinal.core.Controller; import com.jfinal.core.Path; import com.litongjava.jfinal.demo.model.Student; @Path("student") public class StudentContorller extends Controller{ public void list() { List<Student> studentList = Student.dao.findAll(); renderJson(studentList); } }
启动服务测试,测试成功
代码提交地址