0

0

JFinal框架操作oracle数据库

php中文网

php中文网

发布时间:2016-06-07 15:36:52

|

2979人浏览过

|

来源于php中文网

原创

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



同徽B2C电子商务软件系统
同徽B2C电子商务软件系统

开发语言:java,支持数据库:Mysql 5,系统架构:J2EE,操作系统:linux/Windows1. 引言 32. 系统的结构 32.1 系统概述 33. 功能模块设计说明 43.1 商品管理 43.1.1 添加商品功能模块 53.1.2 商品列表功能模块 83.1.3 商品关联功能模块 93.

下载
@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 paras = new ArrayList();
		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);//如果不配置忽略大小写,执行到这里会出现异常,虽然可以添加到数据库,但是这里报错,界面还是会显示500错误
			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)) {//执行到这里返回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);//抛出该异常
	}

现在来说说如果不配置,为什么会出现 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{
	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 testList=Users.dao.find("select * from users");


这里就完成了JFinal框架操作oracle数据库,删除和修改就自己去测试了


相关专题

更多
C++ 高级模板编程与元编程
C++ 高级模板编程与元编程

本专题深入讲解 C++ 中的高级模板编程与元编程技术,涵盖模板特化、SFINAE、模板递归、类型萃取、编译时常量与计算、C++17 的折叠表达式与变长模板参数等。通过多个实际示例,帮助开发者掌握 如何利用 C++ 模板机制编写高效、可扩展的通用代码,并提升代码的灵活性与性能。

10

2026.01.23

php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

29

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

13

2026.01.22

PHP特殊符号教程合集
PHP特殊符号教程合集

本专题整合了PHP特殊符号相关处理方法,阅读专题下面的文章了解更多详细内容。

11

2026.01.22

PHP探针相关教程合集
PHP探针相关教程合集

本专题整合了PHP探针相关教程,阅读专题下面的文章了解更多详细内容。

8

2026.01.22

菜鸟裹裹入口以及教程汇总
菜鸟裹裹入口以及教程汇总

本专题整合了菜鸟裹裹入口地址及教程分享,阅读专题下面的文章了解更多详细内容。

55

2026.01.22

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
JFinal在线手册
JFinal在线手册

共69课时 | 41万人学习

PostgreSQL 教程
PostgreSQL 教程

共48课时 | 7.7万人学习

Django 教程
Django 教程

共28课时 | 3.4万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号