JFinal操作框架oracle数据库。在需求configPlugin()方法来配置链路oracle配置数据库
组态JFinal数据库操作窗口小部件,configPlugin方法
在这里,我打开jdbc.properties它分析configConstant载入的
@Override public void configConstant(Constants me) { loadPropertyFile("jdbc.properties");//载入配置文件 me.setDevMode(getPropertyToBoolean("config.devModel", false)); me.setViewType(ViewType.JSP); me.setEncoding("UTF-8"); }
jdbc.properites配置文件
oracle.driver=oracle.jdbc.driver.OracleDriver oracle.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl oracle.username=scott oracle.password=xiaohu config.devModel=true
@Override public void configPlugin(Plugins me) { ActiveRecordPlugin arp=null; String driver=getProperty("oracle.driver"); String url=getProperty("oracle.url"); String username=getProperty("oracle.username"); String password=getProperty("oracle.password"); DruidPlugin dp=new DruidPlugin(url, username, password, driver); me.add(dp); arp=new ActiveRecordPlugin(dp);//设置数据库方言 arp.setDialect(new OracleDialect()); arp.setContainerFactory(new CaseInsensitiveContainerFactory());//忽略大写和小写 me.add(new EhCachePlugin()); arp.addMapping("users", "id",Users.class); me.add(arp); }
须要注意一点的是,因为oracle数据库中在创建表时。会自己主动的将全部的字段自己主动转为大写。因此在避免后面操作的时候出现大写和小写错误的相关异常,这里须要配置忽略大写和小写的功能
arp.setContainerFactory(new CaseInsensitiveContainerFactory());//忽略大写和小写
假设不须要对数据库进行添加操作。则必须配置忽略大写和小写。假设不配置忽略大写和小写,在保存源码的该段代码中会出现属性id找不到的异常
/** * Save model. */ public boolean save() { Config config = getConfig(); Table table = getTable(); StringBuilder sql = new StringBuilder(); List<Object> paras = new ArrayList<Object>(); config.dialect.forModelSave(table, attrs, sql, paras); // if (paras.size() == 0) return false; // The sql "insert into tableName() values()" works fine, so delete this line // -------- Connection conn = null; PreparedStatement pst = null; int result = 0; try { conn = config.getConnection(); if (config.dialect.isOracle()) pst = conn.prepareStatement(sql.toString(), new String[]{table.getPrimaryKey()}); else pst = conn.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS); config.dialect.fillStatement(pst, paras); result = pst.executeUpdate(); <span style="color:#ff0000;">getGeneratedKey(pst, table);//假设不配置忽略大写和小写。运行到这里会出现异常,尽管能够加入到数据库,可是这里报错。界面还是会显示500错误</span>
getModifyFlag().clear(); return result >= 1; } catch (Exception e) { throw new ActiveRecordException(e); } finally { config.close(pst, conn); } }
<span style="color:#ff0000;">getGeneratedKey()源码部分</span>
/** * Get id after save method. */ private void getGeneratedKey(PreparedStatement pst, Table table) throws SQLException { String pKey = table.getPrimaryKey(); if (get(pKey) == null || getConfig().dialect.isOracle()) { ResultSet rs = pst.getGeneratedKeys(); if (rs.next()) { Class colType = table.getColumnType(pKey); if (colType == Integer.class || colType == int.class) set(pKey, rs.getInt(1)); else if (colType == Long.class || colType == long.class) set(pKey, rs.getLong(1)); else set(pKey, rs.getObject(1)); // It returns Long object for int colType rs.close(); } } }
set()源码部分
/** * Set attribute to model. * @param attr the attribute name of the model * @param value the value of the attribute * @return this model * @throws ActiveRecordException if the attribute is not exists of the model */ public M set(String attr, Object value) { <span style="color:#ff0000;">if (getTable().hasColumnLabel(attr)) {//运行到这里返回false</span> attrs.put(attr, value); getModifyFlag().add(attr); // Add modify flag, update() need this flag. return (M)this; } throw new ActiveRecordException("The attribute name is not exists: " + attr);//抛出该异常 }
如今来说说假设不配置,为什么会出现 The attribute name is not exists:这个异常,这是由于oracle中的字段是大写的,而set方法中传入的attr属性的值是小写,而getTable()中的属性相应的就是oracle字段。这些属性则是大写。因此这里使用getTable().hasColumnLabel(attr)推断是否存在该字段,就会找不到,这时就会抛出该异常,因此就必须配置忽略大写和小写的方法,就不会出现该异常
实体类:
package com.tenghu.core.model; import com.jfinal.plugin.activerecord.Model; public class Users extends Model<Users>{ public static Users dao=new Users(); }
操作数据:
Users users=new Users(); users.set("id", "users_sequence.nextval"); users.set("username", "张三"); users.set("pwd", "sdfsdfs"); users.save(); List<Users> testList=Users.dao.find("select * from users");