可支持多种数据库的sql语句的持久层程序介绍
2013-12-01 09:06
549 查看
功能概述
数据库的种类很多,mysql、oracle、postgres、sql server等,访问数据库的持久层真正能做到支持多种数据库还真不容易,目前,hibernate能支持多种数据库方言,但是有些特殊的数据库函数方法都各自不同,比如空间应用的空间函数相差很大,hibernate支持的也不够,有时只能用sql语句实现,而各个数据库sql语句各自不同,我们通过数据库类型判断来对应不同的sql语句,代码中会增加许多的逻辑,重复代码会很多,修改维护时也很费力。所以我考虑了一种较通用的方法,可以将遇到的各种sql语句统一起来调用,这样我们也可以积累下来,不同数据库到底哪些sql语句的函数方法不同,能够做到支持不同的数据库,程序代码也会比较容易维护。实现方法
1.基类public abstract class SQLTemplate { static String dbtype="oracle";//这里写初始化默认的数据库类型,也可以写其他方法从配置文件或其他渠道取得数据库类型 public static String getDbtype(){ return OMConstant.getDbtype_jdbc(); } //这里可以写基类中的一些通用属性、方法,根据实际应用情况去扩展 } |
public class SQL extends SQLTemplate { //sql空间函数ST_AsText,空间字段转换为字符串,如select ST_AsText(geom) as geom public static String ST_AsText(String fieldName){ String sqlResult=""; if ("postgresql".equals(dbtype)){ sqlResult = "ST_AsText("+fieldName+")"; }else if ("oracle".equals(dbtype)){ sqlResult = "SDO_UTIL.TO_WKTGEOMETRY("+fieldName+")";// fieldName+".get_wkt()"; }else if ("mysql".equals(dbtype)){ sqlResult = "ST_AsText("+fieldName+")"; } return sqlResult; } //sql空间函数geometry,将空间字符串转换为空间字段类型,如 "insert into op_require_spatial(..,geometry) values(..,geometry ('"+geometryText+"'))" public static String geometry(String geometryText){ String sqlResult=""; if ("postgresql".equals(dbtype)){ sqlResult = "geometry('"+geometryText+"')"; }else if ("oracle".equals(dbtype)){ sqlResult = "sdo_geometry('"+geometryText+"')";// "SDO_UTIL.from_wktgeometry('"+geometryText+"')"; }else if ("mysql".equals(dbtype)){ } return sqlResult; } //sql语句中,将字符串转换为日期型的语句 public static String to_date(String dateStr){ String sqlResult=""; if ("postgresql".equals(dbtype)){ sqlResult = "'"+dateStr+"'"; }else if ("oracle".equals(dbtype)){ sqlResult = "to_date('"+dateStr+"','YYYY-MM-DD HH24:MI:SS')"; }else if ("mysql".equals(dbtype)){ sqlResult = "'"+dateStr+"'"; } return sqlResult; } //sql语句中,limit用法,适用于mysql、postgresql,放在sql句尾、或者排序后子句后面 public static String limit(int limitval){ String sqlResult=""; if ("postgresql".equals(dbtype)){ sqlResult = "limit "+limitval; }else if ("oracle".equals(dbtype)){ }else if ("mysql".equals(dbtype)){ sqlResult = "limit "+limitval; } return sqlResult; } //sql语句中,rownum用法,适用于oracle,放在where子句条件后 public static String rownum(int limitval){ String sqlResult=""; if ("postgresql".equals(dbtype)){ }else if ("oracle".equals(dbtype)){ sqlResult = " and "+ "rownum="+limitval; }else if ("mysql".equals(dbtype)){ } return sqlResult; } } |
调用范例
1.日期函数JdbcTemplate jdbcTemplate = (JdbcTemplate)SpringHelper.getBean("jt"); String trackId = IDHelper.genUUID(); Date addtime=new Date(); String addtimeStr = DateUtils.dateToStr(addtime, "yyyy-MM-dd HH:mm:ss"); String sql_track = "insert into op_order_track(id,orderid,orderstate,remark,operid,opertime) values('"+trackId+"','"+id+"','"+orderstate+"','"+trackremark+"','"+adder+"',"+SQL.to_date(addtimeStr)+")"; jdbcTemplate.execute(sql_track); |
查询语句
String sql_fid="select SQL.ST_AsText("fs.fgeometry")+" as fgeometryText,"" + " from fids where 1=1"; |
strbuff.append("insert into op_require_spatial(id,spatialtype,spatialtypevalue,geometry) values('"+id+"','"+spatialtype+"','"+spatialtypevalue+"',"+SQL.geometry(geometryText)+")"); |
strbuff.append("update op_require_spatial set spatialtype='"+spatialtype+"',spatialtypevalue='"+spatialtypevalue+"',geometry="+SQL.geometry(geometryText)+" where id='"+id+"'"); |
相关文章推荐
- [数据库测试]强烈推荐一个python ODBC数据源插件,可支持Oracle,Db2,Mysql,Sql-server以及各种数据库版本,附例子和测试程序
- 在程序开发中怎样写SQL语句可以提高数据库的性能
- 数据库表某几列的列名是中文的,Java程序里面编程的SQL语句和Pl/Sql里面写法差异
- jTDS官方介绍(OpenCms v6.2版MS Sqlserver数据库支持所用JDBC驱动)
- asp.net sql语句中where条件存在中文在程序中取不出数据,在数据库中可以正常取值,数据库为oracle
- 数据库中可以执行的sql语句,在程序中执行抛出乱码异常
- SQL语句创建数据库表介绍以及在.net中创建介绍!
- [数据库测试]强烈推荐一个python ODBC数据源插件,可支持Oracle,Db2,Mysql,Sql-server以及各种数据库版本,附例子和测试程序
- [数据库测试]强烈推荐一个python ODBC数据源插件,可支持Oracle,Db2,Mysql,Sql-server以及各种数据库版本,附例子和测试程序
- 数据库基础sql语句总结(以Oracle为主介绍,并与mysql,sql server做对比)一
- c# 数据库访问静态类 支持多条SQL语句事务操作
- 数据库基础sql语句总结(以Oracle为主介绍,并与mysql,sql server做对比)二
- 在程序开发中怎样写SQL语句可以提高数据库的性能
- 以数据库字段分组显示数据的sql语句(详细介绍)
- 以数据库字段分组显示数据的sql语句(详细介绍)
- 支持SQL语句的嵌入式数据库:SQLite
- 数据库查询前N条记录sql语句介绍
- jTDS官方介绍(OpenCms v6.2版MS Sqlserver数据库支持所用JDBC驱动)
- jTDS官方介绍(OpenCms v6.2版MS Sqlserver数据库支持所用JDBC驱动)
- 在程序开发中怎样写SQL语句可以提高数据库的性能