您的位置:首页 > 数据库

可支持多种数据库的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);
2.空间函数
查询语句

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+"'");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐