不同的数据库,其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