JFinal框架操做oracle數據庫,須要在configPlugin()方法中配置連接oracle數據庫的相關配置 html
配置JFinal數據庫操做插件,configPlugin方法 sql
這裏我加載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
- 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找不到的異常 app
- /**
- * 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();
- }
操做數據: ide
- 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");
這裏就完成了JFinal框架操做oracle數據庫,刪除和修改就本身去測試了