时间:2021-07-01 10:21:17 帮助过:52人阅读
JFinal框架操作oracle数据库,需要在configPlugin()方法中配置链接oracle数据库的相关配置 配置JFinal数据库操作插件,configPlugin方法 这里我加载jdbc.properties配置文件实在configConstant加载的 @Overridepublic void configConstant(Constants me) {lo
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); }
arp.setContainerFactory(new CaseInsensitiveContainerFactory());//忽略大小写
如果不需要对数据库进行增加操作,则必须配置忽略大小写,如果不配置忽略大小写,在保存源代码的该段代码中会出现属性id找不到的异常
/** * Save model. */ public boolean save() { Config config = getConfig(); Table table = getTable(); StringBuilder sql = new StringBuilder(); List
getModifyFlag().clear(); return result >= 1; } catch (Exception e) { throw new ActiveRecordException(e); } finally { config.close(pst, conn); } }
getGeneratedKey()源代码部分
/** * 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 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) {
if (getTable().hasColumnLabel(attr)) {//执行到这里返回false
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);//抛出该异常
}
实体类:
package com.tenghu.core.model; import com.jfinal.plugin.activerecord.Model; public class Users extends Model{ 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(); ListtestList=Users.dao.find("select * from users");