sprest 发表于 2015-06-17 09:29

Jfinal Mysql迁移至Oracle 基础Model

本帖最后由 sprest 于 2015-06-17 09:31 编辑

近日将应用从Mysql迁移至ORACLE,,发现了不少问题,特此记录一下!
1:主建自增问题:这个其实可以在数据库层面解决的,建个序列写个触发器就可以了,不过,我还是喜欢在应用层面上来解决,,解决方案,,方法请看代码
2:大小写问题:在Jfinal的Config类中配置为不区分大小写(PS 刚刚想到,即然自封装了Model,还不如直接在这里转成大写得了,配置了这个参数后,Model的put方法也会被jfinal转成小写,这个很蛋疼的!)
3oRACLE木有布尔型:这个,在建表的时候这样写youColumnName CHAR(1) CHECK (youColumnName in(0,1)),经测试,查询和更新都木用问题直接兼容getBoolen和setBoolean
4oracle无int long float等类型的区分:这个用自封装model解决,请查看代码
5:前台显示问题:后台传过去的字段名都是小写的,而前台的js是区分大小写的,这个,你就只能手动将Model转换成区分大小写的HashMap了
基础代码package com.v246.jfinal;

import com.jfinal.plugin.activerecord.DbKit;
import com.jfinal.plugin.activerecord.TableMapping;
import net.sf.ehcache.store.disk.ods.AATreeSet;

import java.sql.Timestamp;
import java.util.Date;

/**
* Created by aquaqu on 2015-06-08.
*/
public abstract class Model<M extends Model> extends com.jfinal.plugin.activerecord.Model<M> {
    private static final long serialVersionUID = -992334519496260591L;
    protected staticString oracleSequenceName = null;
    protected static   String primaryKey = null;
    /**
   * Get attribute of any mysql type
   */
    public <T> T get(String attr) {
      if(DbKit.getConfig().getDialect().isOracle())
            return super.get(attr.toLowerCase());
      else
            return super.get(attr);
    }

    /**
   * Get attribute of any mysql type. Returns defaultValue if null.
   */
    public <T> T get(String attr, Object defaultValue) {
      if(DbKit.getConfig().getDialect().isOracle())
            return super.get(attr.toLowerCase(), defaultValue);
      else
            return super.get(attr);
    }

    /**
   * Get attribute of mysql type: varchar, char, enum, set, text, tinytext, mediumtext, longtext
   */
    public String getStr(String attr) {
      if(DbKit.getConfig().getDialect().isOracle())
            return super.getStr(attr.toLowerCase());
      else
            return super.getStr(attr);
    }

    /**
   * Get attribute of mysql type: int, integer, tinyint(n) n > 1, smallint, mediumint
   */
    public Integer getInt(String attr) {
      if(DbKit.getConfig().getDialect().isOracle())
            return super.getNumber(attr.toLowerCase())==null?null:super.getNumber(attr.toLowerCase()).intValue();
      else
            return super.getInt(attr);
    }

    /**
   * Get attribute of mysql type: bigint, unsign int
   */
    public Long getLong(String attr) {
      if(DbKit.getConfig().getDialect().isOracle())
            return super.getNumber(attr.toLowerCase())==null?null:super.getNumber(attr.toLowerCase()).longValue();
      else
            return super.getLong(attr);
    }

    /**
   * Get attribute of mysql type: unsigned bigint
   */
    public java.math.BigInteger getBigInteger(String attr) {
      if(DbKit.getConfig().getDialect().isOracle())
            return super.getBigInteger(attr.toLowerCase());
      else
            return getBigInteger(attr);
    }

    /**
   * Get attribute of mysql type: date, year
   */
    public java.util.Date getDate(String attr) {
      if(DbKit.getConfig().getDialect().isOracle())
            return super.getDate(attr.toLowerCase());
      else
            return super.getDate(attr);
    }

    /**
   * Get attribute of mysql type: time
   */
    public java.sql.Time getTime(String attr) {
      if(DbKit.getConfig().getDialect().isOracle())
            return super.getTime(attr.toLowerCase());
      else
            return super.getTime(attr);
    }

    /**
   * Get attribute of mysql type: timestamp, datetime
   */
    public java.sql.Timestamp getTimestamp(String attr) throws RuntimeException{
      java.sql.Timestamp re = null;
      if(DbKit.getConfig().getDialect().isOracle()){
            Object obj = super.get(attr.toLowerCase());
            if (obj == null) {
                return re;
            }
            if(obj instanceof oracle.sql.TIMESTAMP) {
                try {
                  re =((oracle.sql.TIMESTAMP) obj).timestampValue();
                } catch (Exception e) {
                  throw (new RuntimeException(e));
                }
            }
      }else {
            re =super.getTimestamp(attr);
      }
      return re;
    }

    /**
   * Get attribute of mysql type: real, double
   */
    public Double getDouble(String attr) {
      if(DbKit.getConfig().getDialect().isOracle())
            return super.getNumber(attr.toLowerCase()).doubleValue();
      else
            return super.getDouble(attr);
    }

    /**
   * Get attribute of mysql type: float
   */
    public Float getFloat(String attr) {
      if(DbKit.getConfig().getDialect().isOracle())
            return super.getNumber(attr.toLowerCase()).floatValue();
      else
            return super.getFloat(attr);
    }

    /**
   * Get attribute of mysql type: bit, tinyint(1)
   */
    public Boolean getBoolean(String attr) {
      if(DbKit.getConfig().getDialect().isOracle())
            return super.getBoolean(attr.toLowerCase());
      else
            return super.getBoolean(attr);
    }

    /**
   * Get attribute of mysql type: decimal, numeric
   */
    public java.math.BigDecimal getBigDecimal(String attr) {
      if(DbKit.getConfig().getDialect().isOracle())
            return super.getBigDecimal(attr.toLowerCase());
      else
            return super.getBigDecimal(attr);
    }

    /**
   * Get attribute of mysql type: binary, varbinary, tinyblob, blob, mediumblob, longblob
   */
    public byte[] getBytes(String attr) {
      if(DbKit.getConfig().getDialect().isOracle())
            return super.getBytes(attr.toLowerCase());
      else
            return super.getBytes(attr);
    }

    /**
   * Get attribute of any type that extends from Number
   */
    public Number getNumber(String attr) {
      if(DbKit.getConfig().getDialect().isOracle())
            return super.getNumber(attr.toLowerCase());
      else
            return super.getNumber(attr);
    }
    public boolean save(){
      if(DbKit.getConfig().getDialect().isOracle()) {
            if (oracleSequenceName != null) {
                if (primaryKey != null) {
                  super.set(primaryKey, oracleSequenceName + ".nextval");
                }else{
                  super.set("id", oracleSequenceName + ".nextval");
                }
            }
      }
      return super.save();
    }
    public M set(String attr, Object value) {
      if(DbKit.getConfig().getDialect().isOracle()) {
            if (value instanceof java.util.Date) {
                java.util.Date dt = (java.util.Date)value;
                value = new Timestamp(((Date) value).getTime());
            }
      }
      return super.set(attr, value);
    }

}使用方法package com.v246.project.model;

import com.v246.jfinal.Model;

/**
* Created by aquaqu on 2014/11/8.
*/
public class SYS_LOGINSModel extends Model<SYS_LOGINSModel>{
    static{
      primaryKey = null;
      oracleSequenceName= "SYS_LOGINS_sequence";
    }
    public static final SYS_LOGINSModel dao = new SYS_LOGINSModel();
}
页: [1]
查看完整版本: Jfinal Mysql迁移至Oracle 基础Model