上一节:【分享】测试JFinal对接国产达梦数据库-安装达梦
本节我们主要来使用JFinal去完成达梦数据库的CRUD操作。
我们Java应用项目如果要使用数据库服务,需要使用JDBC标准去连接和使用。
Mysql Oracle等国外产品都有对应的JDBC驱动实现。
国产达梦数据库也有自己的JDBC实现,但是没有再maven仓库提交,安装后在安装目录下能找到各种语言的实现。
找到安装目录下的drivers目录
里面就有我们需要的JDBC的实现。
我们用的就是这个DmJdbcDriver18.jar
上面还有一个方言dialect的实现包。
里面看了一下都是Hibernate相关的方言,暂且不理会。
下面开始JFinal的集成。
我没有创建Java Web工程,创建的是一个普通java工程。
需要的依赖Jar就这么几个:
写一个DmTest.java去测试
package com.jfinalxueyuan.test; import java.util.List; import com.jfinal.kit.JsonKit; import com.jfinal.plugin.activerecord.ActiveRecordPlugin; import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory; import com.jfinal.plugin.activerecord.Db; import com.jfinal.plugin.activerecord.Record; import com.jfinal.plugin.activerecord.dialect.AnsiSqlDialect; import com.jfinal.plugin.druid.DruidPlugin; /** * JFinal对接达梦数据库初步测试CRUD * 使用Db+Record模式 * * @ClassName: DmTest * @author: JFinal学院-小木 QQ:909854136 * @date: 2020年9月13日 * */ public class DmTest { String url = "jdbc:dm://localhost:5236"; String username = "SYSDBA"; String password = "xiaomu2020"; DruidPlugin druidPlugin=null; ActiveRecordPlugin arp=null; public boolean start() { druidPlugin=new DruidPlugin(url, username, password); arp=new ActiveRecordPlugin(druidPlugin); arp.setDialect(new AnsiSqlDialect()); arp.setShowSql(true); arp.setDevMode(true); arp.setContainerFactory(new CaseInsensitiveContainerFactory(true)); boolean success=druidPlugin.start(); if(success) { success=arp.start(); } return success; } public static void main(String[] args) { DmTest test = new DmTest(); //启动连接 boolean success=test.start(); if(success) { try { // 插入数据 System.out.println("--- 插入 ---"); test.insertTable(); //查询 System.out.println("--- 显示插入结果 ---"); test.queryTable(); //修改 System.out.println("--- 修改 ---"); test.updateTable(); //查询 System.out.println("--- 显示修改结果 ---"); test.queryTable(); //删除 System.out.println("--- 删除 ---"); test.deleteTable(); //在删除后查询 System.out.println("--- 在删除后查询 ---"); test.queryTable(); } finally { test.stop(); } } } /** * 结束 */ private void stop() { arp.stop(); druidPlugin.stop(); } /** * 删除数据 */ private void deleteTable() { System.out.println("=====DOING:执行数据删除====="); int count=Db.update("delete from DMHR.CITY where CITY_ID='DY'"); if(count==1) { System.out.println("=====SUCCESS:数据删除成功====="); }else { System.out.println("=====ERROR:数据删除失败====="); } } /** * 修改更新数据 */ private void updateTable() { System.out.println("=====DOING:执行数据更新====="); int count=Db.update("update DMHR.CITY set CITY_NAME='东营_修改后' where CITY_ID='DY'"); if(count==1) { System.out.println("=====SUCCESS:数据修改成功====="); }else { System.out.println("=====ERROR:数据修改失败====="); } } /** * 查询数据 */ private void queryTable() { System.out.println("=====DOING:执行数据查询====="); List<Record> records=Db.find("select * from DMHR.CITY"); if(records!=null&&records.size()>0) { System.out.println("=====SUCCESS:数据查询成功====="); int index=0; for(Record record:records) { index++; System.out.println(index+":"+JsonKit.toJson(record)); } }else { System.out.println("=====RESULT:暂无数据====="); } } /** * 插入数据 */ private void insertTable() { System.out.println("=====DOING:执行数据插入====="); Record record=new Record(); record.set("CITY_ID", "DY"); record.set("CITY_NAME", "东营"); record.set("REGION_ID", 1); boolean success=Db.save("DMHR.CITY", record); if(success) { System.out.println("=====SUCCESS:数据插入成功====="); System.out.println(record.toJson()); }else { System.out.println("=====ERROR:数据插入失败====="); } } }
使用JFinal去连接达梦数据库做了CRUD操作,其中配置的DruidPlugin和ActiveRecordPlugin目前来看和Mysql的数据库配置暂时没有特别差异,标准的JDBC实现。
那么,方言这里就单独设置了一下,大小写不敏感设置一下。
最后执行Main方法,测试一下CRUD:整个日志输出正常无误!!!
九月 13, 2020 1:47:59 下午 com.alibaba.druid.pool.DruidDataSource info 信息: {dataSource-1} inited --- 插入 --- =====DOING:执行数据插入===== Sql: insert into DMHR.CITY(city_id, city_name, region_id) values(?, ?, ?) =====SUCCESS:数据插入成功===== {"city_id":"DY","city_name":"东营","id":15,"region_id":1} --- 显示插入结果 --- =====DOING:执行数据查询===== Sql: select * from DMHR.CITY =====SUCCESS:数据查询成功===== 1:{"city_id":"BJ ","city_name":"北京","region_id":1} 2:{"city_id":"SJZ","city_name":"石家庄","region_id":1} 3:{"city_id":"SH ","city_name":"上海","region_id":2} 4:{"city_id":"NJ ","city_name":"南京","region_id":2} 5:{"city_id":"GZ ","city_name":"广州","region_id":3} 6:{"city_id":"HK ","city_name":"海口","region_id":3} 7:{"city_id":"WH ","city_name":"武汉","region_id":4} 8:{"city_id":"CS ","city_name":"长沙","region_id":4} 9:{"city_id":"SY ","city_name":"沈阳","region_id":5} 10:{"city_id":"XA ","city_name":"西安","region_id":6} 11:{"city_id":"CD ","city_name":"成都","region_id":7} 12:{"city_id":"DY ","city_name":"东营","region_id":1} --- 修改 --- =====DOING:执行数据更新===== Sql: update DMHR.CITY set CITY_NAME='东营_修改后' where CITY_ID='DY' =====SUCCESS:数据修改成功===== --- 显示修改结果 --- =====DOING:执行数据查询===== Sql: select * from DMHR.CITY =====SUCCESS:数据查询成功===== 1:{"city_id":"BJ ","city_name":"北京","region_id":1} 2:{"city_id":"SJZ","city_name":"石家庄","region_id":1} 3:{"city_id":"SH ","city_name":"上海","region_id":2} 4:{"city_id":"NJ ","city_name":"南京","region_id":2} 5:{"city_id":"GZ ","city_name":"广州","region_id":3} 6:{"city_id":"HK ","city_name":"海口","region_id":3} 7:{"city_id":"WH ","city_name":"武汉","region_id":4} 8:{"city_id":"CS ","city_name":"长沙","region_id":4} 9:{"city_id":"SY ","city_name":"沈阳","region_id":5} 10:{"city_id":"XA ","city_name":"西安","region_id":6} 11:{"city_id":"CD ","city_name":"成都","region_id":7} 12:{"city_id":"DY ","city_name":"东营_修改后","region_id":1} --- 删除 --- =====DOING:执行数据删除===== Sql: delete from DMHR.CITY where CITY_ID='DY' =====SUCCESS:数据删除成功===== --- 在删除后查询 --- =====DOING:执行数据查询===== Sql: select * from DMHR.CITY =====SUCCESS:数据查询成功===== 1:{"city_id":"BJ ","city_name":"北京","region_id":1} 2:{"city_id":"SJZ","city_name":"石家庄","region_id":1} 3:{"city_id":"SH ","city_name":"上海","region_id":2} 4:{"city_id":"NJ ","city_name":"南京","region_id":2} 5:{"city_id":"GZ ","city_name":"广州","region_id":3} 6:{"city_id":"HK ","city_name":"海口","region_id":3} 7:{"city_id":"WH ","city_name":"武汉","region_id":4} 8:{"city_id":"CS ","city_name":"长沙","region_id":4} 9:{"city_id":"SY ","city_name":"沈阳","region_id":5} 10:{"city_id":"XA ","city_name":"西安","region_id":6} 11:{"city_id":"CD ","city_name":"成都","region_id":7} 九月 13, 2020 1:47:59 下午 com.alibaba.druid.pool.DruidDataSource info 信息: {dataSource-1} closing ... 九月 13, 2020 1:47:59 下午 com.alibaba.druid.pool.DruidDataSource info 信息: {dataSource-1} closed
OK,JFinal也可以玩儿国产数据库了!!
有问题 加我微信:
mumengmeng