日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

oracle19c方言,JFinal框架操作oracle数据库

發(fā)布時(shí)間:2023/12/2 数据库 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 oracle19c方言,JFinal框架操作oracle数据库 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

JFinal框架操作oracle數(shù)據(jù)庫,需要在configPlugin()方法中配置鏈接oracle數(shù)據(jù)庫的相關(guān)配置

配置JFinal數(shù)據(jù)庫操作插件,configPlugin方法

這里我加載jdbc.properties配置文件實(shí)在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=newDruidPlugin(url,?username,?password,?driver);

me.add(dp);

arp=newActiveRecordPlugin(dp);//設(shè)置數(shù)據(jù)庫方言

arp.setDialect(new?OracleDialect());

arp.setContainerFactory(new?CaseInsensitiveContainerFactory());//忽略大小寫

me.add(new?EhCachePlugin());

arp.addMapping("users",?"id",Users.class);

me.add(arp);

}

需要注意一點(diǎn)的是,由于oracle數(shù)據(jù)庫中在創(chuàng)建表時(shí),會(huì)自動(dòng)的將所有的字段自動(dòng)轉(zhuǎn)為大寫,因此在避免后面操作的時(shí)候出現(xiàn)大小寫錯(cuò)誤的相關(guān)異常,這里需要配置忽略大小寫的功能

arp.setContainerFactory(new?CaseInsensitiveContainerFactory());//忽略大小寫

如果不需要對(duì)數(shù)據(jù)庫進(jìn)行增加操作,則必須配置忽略大小寫,如果不配置忽略大小寫,在保存源代碼的該段代碼中會(huì)出現(xiàn)屬性id找不到的異常

/**

*?Save?model.

*/

public?boolean?save()?{

Config?config=getConfig();

Table?table=getTable();

StringBuilder?sql=newStringBuilder();

Listparas=newArrayList();

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();

getGeneratedKey(pst,?table);//如果不配置忽略大小寫,執(zhí)行到這里會(huì)出現(xiàn)異常,雖然可以添加到數(shù)據(jù)庫,但是這里報(bào)錯(cuò),界面還是會(huì)顯示500錯(cuò)誤

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()源代碼部分

/**

*?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))?{//執(zhí)行到這里返回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);//拋出該異常

}

現(xiàn)在來說說如果不配置,為什么會(huì)出現(xiàn) The attribute name is not exists:這個(gè)異常,這是因?yàn)閛racle中的字段是大寫的,而set方法中傳入的attr屬性的值是小寫,而getTable()中的屬性對(duì)應(yīng)的就是oracle字段,這些屬性則是大寫,因此這里使用getTable().hasColumnLabel(attr)判斷是否存在該字段,就會(huì)找不到,這時(shí)就會(huì)拋出該異常,因此就必須配置忽略大小寫的方法,就不會(huì)出現(xiàn)該異常

實(shí)體類:

package?com.tenghu.core.model;

import?com.jfinal.plugin.activerecord.Model;

public?class?Users?extends?Model{

public?static?Users?dao=newUsers();

}

操作數(shù)據(jù):

Usersusers=newUsers();

users.set("id",?"users_sequence.nextval");

users.set("username",?"張三");

users.set("pwd",?"sdfsdfs");

users.save();

ListtestList=Users.dao.find("select?*?from?users");

這里就完成了JFinal框架操作oracle數(shù)據(jù)庫,刪除和修改就自己去測(cè)試了

總結(jié)

以上是生活随笔為你收集整理的oracle19c方言,JFinal框架操作oracle数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。