博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jfinal数据库方言之H2Database
阅读量:6582 次
发布时间:2019-06-24

本文共 5527 字,大约阅读时间需要 18 分钟。

hot3.png

不同的数据库,其sql语句也有不同的差异,Jfinal中,数据库方言是根据不同的数据库类型,生成对应数据库的sql语句,所有的方言类都必须要继承自抽象类com.jfinal.plugin.activerecord.dialect.Dialect,并且重写抽象方法,

以下为H2Database 数据库方言类

package com.cpy.jfinal.plugin.activerecord.dialect;import java.sql.PreparedStatement;import java.sql.SQLException;import java.util.List;import java.util.Map;import java.util.Set;import java.util.Map.Entry;import com.jfinal.plugin.activerecord.Record;import com.jfinal.plugin.activerecord.Table;import com.jfinal.plugin.activerecord.dialect.Dialect;/** * H2Dialect. */public class H2Dialect extends Dialect {		public String forTableBuilderDoBuild(String tableName) {		return "select * from " + tableName + " where rownum < 1";	}		public String forPaginate(int pageNumber, int pageSize, String select, String sqlExceptSelect) {		int start = (pageNumber - 1) * pageSize;		int end = pageNumber * pageSize;		StringBuilder ret = new StringBuilder();		ret.append("select * from ( select row_.*, rownum rownum_ from (  ");		ret.append(select).append(" ").append(sqlExceptSelect);		ret.append(" ) row_ where rownum <= ").append(end).append(") table_alias");		ret.append(" where table_alias.rownum_ > ").append(start);		return ret.toString();	}		public String forModelFindById(Table table, String columns) {		StringBuilder sql = new StringBuilder("select ").append(columns).append(" from ");		sql.append(table.getName());		sql.append(" where ");		String[] pKeys = table.getPrimaryKey();		for (int i=0; i
0) { sql.append(" and "); } sql.append(pKeys[i]).append(" = ?"); } return sql.toString(); } public String forModelDeleteById(Table table) { String[] pKeys = table.getPrimaryKey(); StringBuilder sql = new StringBuilder(45); sql.append("delete from "); sql.append(table.getName()); sql.append(" where "); for (int i=0; i
0) { sql.append(" and "); } sql.append(pKeys[i]).append(" = ?"); } return sql.toString(); } // insert into table (id,name) values(seq.nextval, name) public void forModelSave(Table table, Map
attrs, StringBuilder sql, List
paras) { sql.append("insert into ").append(table.getName()).append("("); StringBuilder temp = new StringBuilder(") values("); String[] pKeys = table.getPrimaryKey(); int count = 0; for (Entry
e: attrs.entrySet()) { String colName = e.getKey(); if (table.hasColumnLabel(colName)) { if (count++ > 0) { sql.append(", "); temp.append(", "); } sql.append(colName); Object value = e.getValue(); if (value instanceof String && isPrimaryKey(colName, pKeys) && ((String)value).endsWith(".nextval")) { temp.append(value); } else { temp.append("?"); paras.add(value); } } } sql.append(temp.toString()).append(")"); } public void forModelUpdate(Table table, Map
attrs, Set
modifyFlag, StringBuilder sql, List
paras) { sql.append("update ").append(table.getName()).append(" set "); String[] pKeys = table.getPrimaryKey(); for (Entry
e : attrs.entrySet()) { String colName = e.getKey(); if (modifyFlag.contains(colName) && !isPrimaryKey(colName, pKeys) && table.hasColumnLabel(colName)) { if (paras.size() > 0) { sql.append(", "); } sql.append(colName).append(" = ? "); paras.add(e.getValue()); } } sql.append(" where "); for (int i=0; i
0) { sql.append(" and "); } sql.append(pKeys[i]).append(" = ?"); paras.add(attrs.get(pKeys[i])); } } public String forDbFindById(String tableName, String[] pKeys) { tableName = tableName.trim(); trimPrimaryKeys(pKeys); StringBuilder sql = new StringBuilder("select * from ").append(tableName).append(" where "); for (int i=0; i
0) { sql.append(" and "); } sql.append(pKeys[i]).append(" = ?"); } return sql.toString(); } public String forDbDeleteById(String tableName, String[] pKeys) { tableName = tableName.trim(); trimPrimaryKeys(pKeys); StringBuilder sql = new StringBuilder("delete from ").append(tableName).append(" where "); for (int i=0; i
0) { sql.append(" and "); } sql.append(pKeys[i]).append(" = ?"); } return sql.toString(); } public void forDbSave(String tableName, String[] pKeys, Record record, StringBuilder sql, List
paras) { tableName = tableName.trim(); trimPrimaryKeys(pKeys); sql.append("insert into "); sql.append(tableName).append("("); StringBuilder temp = new StringBuilder(); temp.append(") values("); int count = 0; for (Entry
e: record.getColumns().entrySet()) { String colName = e.getKey(); if (count++ > 0) { sql.append(", "); temp.append(", "); } sql.append(colName); Object value = e.getValue(); if (value instanceof String && isPrimaryKey(colName, pKeys) && ((String)value).endsWith(".nextval")) { temp.append(value); } else { temp.append("?"); paras.add(value); } } sql.append(temp.toString()).append(")"); } public void forDbUpdate(String tableName, String[] pKeys, Object[] ids, Record record, StringBuilder sql, List
paras) { tableName = tableName.trim(); trimPrimaryKeys(pKeys); sql.append("update ").append(tableName).append(" set "); for (Entry
e: record.getColumns().entrySet()) { String colName = e.getKey(); if (!isPrimaryKey(colName, pKeys)) { if (paras.size() > 0) { sql.append(", "); } sql.append(colName).append(" = ? "); paras.add(e.getValue()); } } sql.append(" where "); for (int i=0; i
0) { sql.append(" and "); } sql.append(pKeys[i]).append(" = ?"); paras.add(ids[i]); } } public boolean isH2() { return true; } public void fillStatement(PreparedStatement pst, List
paras) throws SQLException { for (int i=0, size=paras.size(); i

 

转载于:https://my.oschina.net/u/580298/blog/674380

你可能感兴趣的文章
HP-ux AIX Linux扩展lv的方法
查看>>
Linux用户管理(八)Shell编程基础
查看>>
ps -aux僵尸进程
查看>>
StringUtils方法全集介绍
查看>>
springmvc 配置详解
查看>>
性能调校
查看>>
VMware workstation虚拟网卡类型介绍
查看>>
HTML:target=_blank
查看>>
linux 一天一个命令之'cut'
查看>>
HDTune硬盘检测工具
查看>>
马哥运维学习作业(六)
查看>>
Python自动化运维之高级函数
查看>>
Win8 Metro(C#)数字图像处理--3.5图像形心计算
查看>>
linux basics
查看>>
市场分享竞品分析-Android
查看>>
统一管理MOSS2010用户头像
查看>>
scp命令不需要输入用户密码传输
查看>>
Jenkins学习笔记 系列3--trigger插件配置
查看>>
Linux的企业-docker简介及操作
查看>>
/lib/ld-linux.so.2: bad ELF interpreter: 没有那个文件或目录
查看>>