免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1791 | 回复: 0
打印 上一主题 下一主题

Jfinal Mysql迁移至Oracle 基础Model [复制链接]

论坛徽章:
1
2015亚冠之卡尔希纳萨夫
日期:2015-10-16 14:12:46
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-06-17 09:29 |只看该作者 |倒序浏览
本帖最后由 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了
基础代码
  1. package com.v246.jfinal;

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

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

  7. /**
  8. * Created by aquaqu on 2015-06-08.
  9. */
  10. public abstract class Model<M extends Model> extends com.jfinal.plugin.activerecord.Model<M> {
  11.     private static final long serialVersionUID = -992334519496260591L;
  12.     protected static  String oracleSequenceName = null;
  13.     protected static   String primaryKey = null;
  14.     /**
  15.      * Get attribute of any mysql type
  16.      */
  17.     public <T> T get(String attr) {
  18.         if(DbKit.getConfig().getDialect().isOracle())
  19.             return super.get(attr.toLowerCase());
  20.         else
  21.             return super.get(attr);
  22.     }

  23.     /**
  24.      * Get attribute of any mysql type. Returns defaultValue if null.
  25.      */
  26.     public <T> T get(String attr, Object defaultValue) {
  27.         if(DbKit.getConfig().getDialect().isOracle())
  28.             return super.get(attr.toLowerCase(), defaultValue);
  29.         else
  30.             return super.get(attr);
  31.     }

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

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

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

  59.     /**
  60.      * Get attribute of mysql type: unsigned bigint
  61.      */
  62.     public java.math.BigInteger getBigInteger(String attr) {
  63.         if(DbKit.getConfig().getDialect().isOracle())
  64.             return super.getBigInteger(attr.toLowerCase());
  65.         else
  66.             return getBigInteger(attr);
  67.     }

  68.     /**
  69.      * Get attribute of mysql type: date, year
  70.      */
  71.     public java.util.Date getDate(String attr) {
  72.         if(DbKit.getConfig().getDialect().isOracle())
  73.             return super.getDate(attr.toLowerCase());
  74.         else
  75.             return super.getDate(attr);
  76.     }

  77.     /**
  78.      * Get attribute of mysql type: time
  79.      */
  80.     public java.sql.Time getTime(String attr) {
  81.         if(DbKit.getConfig().getDialect().isOracle())
  82.             return super.getTime(attr.toLowerCase());
  83.         else
  84.             return super.getTime(attr);
  85.     }

  86.     /**
  87.      * Get attribute of mysql type: timestamp, datetime
  88.      */
  89.     public java.sql.Timestamp getTimestamp(String attr) throws RuntimeException{
  90.         java.sql.Timestamp re = null;
  91.         if(DbKit.getConfig().getDialect().isOracle()){
  92.             Object obj = super.get(attr.toLowerCase());
  93.             if (obj == null) {
  94.                 return re;
  95.             }
  96.             if(obj instanceof oracle.sql.TIMESTAMP) {
  97.                 try {
  98.                     re =  ((oracle.sql.TIMESTAMP) obj).timestampValue();
  99.                 } catch (Exception e) {
  100.                     throw (new RuntimeException(e));
  101.                 }
  102.             }
  103.         }else {
  104.             re =  super.getTimestamp(attr);
  105.         }
  106.         return re;
  107.     }

  108.     /**
  109.      * Get attribute of mysql type: real, double
  110.      */
  111.     public Double getDouble(String attr) {
  112.         if(DbKit.getConfig().getDialect().isOracle())
  113.             return super.getNumber(attr.toLowerCase()).doubleValue();
  114.         else
  115.             return super.getDouble(attr);
  116.     }

  117.     /**
  118.      * Get attribute of mysql type: float
  119.      */
  120.     public Float getFloat(String attr) {
  121.         if(DbKit.getConfig().getDialect().isOracle())
  122.             return super.getNumber(attr.toLowerCase()).floatValue();
  123.         else
  124.             return super.getFloat(attr);
  125.     }

  126.     /**
  127.      * Get attribute of mysql type: bit, tinyint(1)
  128.      */
  129.     public Boolean getBoolean(String attr) {
  130.         if(DbKit.getConfig().getDialect().isOracle())
  131.             return super.getBoolean(attr.toLowerCase());
  132.         else
  133.             return super.getBoolean(attr);
  134.     }

  135.     /**
  136.      * Get attribute of mysql type: decimal, numeric
  137.      */
  138.     public java.math.BigDecimal getBigDecimal(String attr) {
  139.         if(DbKit.getConfig().getDialect().isOracle())
  140.             return super.getBigDecimal(attr.toLowerCase());
  141.         else
  142.             return super.getBigDecimal(attr);
  143.     }

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

  153.     /**
  154.      * Get attribute of any type that extends from Number
  155.      */
  156.     public Number getNumber(String attr) {
  157.         if(DbKit.getConfig().getDialect().isOracle())
  158.             return super.getNumber(attr.toLowerCase());
  159.         else
  160.             return super.getNumber(attr);
  161.     }
  162.     public boolean save(){
  163.         if(DbKit.getConfig().getDialect().isOracle()) {
  164.             if (oracleSequenceName != null) {
  165.                 if (primaryKey != null) {
  166.                     super.set(primaryKey, oracleSequenceName + ".nextval");
  167.                 }else{
  168.                     super.set("id", oracleSequenceName + ".nextval");
  169.                 }
  170.             }
  171.         }
  172.         return super.save();
  173.     }
  174.     public M set(String attr, Object value) {
  175.         if(DbKit.getConfig().getDialect().isOracle()) {
  176.             if (value instanceof java.util.Date) {
  177.                 java.util.Date dt = (java.util.Date)value;
  178.                 value = new Timestamp(((Date) value).getTime());
  179.             }
  180.         }
  181.         return super.set(attr, value);
  182.     }

  183. }
复制代码
使用方法
  1. package com.v246.project.model;

  2. import com.v246.jfinal.Model;

  3. /**
  4. * Created by aquaqu on 2014/11/8.
  5. */
  6. public class SYS_LOGINSModel extends Model<SYS_LOGINSModel>{
  7.     static{
  8.         primaryKey = null;
  9.         oracleSequenceName  = "SYS_LOGINS_sequence";
  10.     }
  11.     public static final SYS_LOGINSModel dao = new SYS_LOGINSModel();
  12. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP