温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用SimpleFramework框架实现数据访问

发布时间:2022-01-06 17:59:48 来源:亿速云 阅读:106 作者:iii 栏目:编程语言

本篇内容介绍了“怎么用SimpleFramework框架实现数据访问”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

Simple数据访问层基于Spring JDBC。

Simple数据访问层的核心概念为“实体管理器”,所有对数据的访问都是通过不同的“实体管理器”产生的,这使得Simple对数据的访问更加简单、一致、也更为安全。

怎么用SimpleFramework框架实现数据访问

Simple提供了三种“实体管理器”,分别为:

表实体管理器

具有缓存(或分布式缓存)的表实体管理器

查询实体管理器

表实体管理器

表实体管理器提供了对单张物理表的基本操作(select,insert,update、delete),可以通过如下方式获取表实体管理器:

ITableEntityManager tem =       DataObjectManagerFactory.getTableEntityManager(dataSource, new Table("simple_test", "id"));

下面演示一些通过ITableEntityManager操作表数据的例子:

查询id等于1的单行记录

Map data = tem.queryForMap(new UniqueValue(1));  或则  TestBean bean = tem.queryForObject(new UniqueValue(1), TestBean.class);

查询id大于1的多行记录

IQueryEntitySet<Map> qs = tem.query(new ExpressionValue("id>1"));  或则  IQueryEntitySet<TestBean> qs2 = tem.query(new ExpressionValue("id>1", TestBean.class));

 

插入id等于2的记录

Map data2 = new HashMap();  data2.put("id", 2);  data2.put("f1", "value1");  data2.put("f2", true);  tem.insert(data2);   或则  TestBean bean2 = new TestBean();  bean2.setId(2);  bean2.setF1("value1");  bean2.setF2(true);  tem.insert(bean2);

更新id等于2的记录

data2.put("f1", "value1_update");  tem.update(data2);  或则  bean2.setF1("value1_update");  tem.update(bean2);

删除id等于2的记录

tem.delete(new ExpressionValue("id=2"));

操作多个物理表

在非事务环境下,操作多张表只需定义不同的表实体管理器:

TestBean bean = new TestBean();  bean.set&hellip;  tem.insert(bean);  ITableEntityManager tem2 =      DataObjectManagerFactory.getTableEntityManager(dataSource, new Table("simple_test2", "id"));  Test2Bean bean2 = new Test2Bean();  bean2.set&hellip;  tem2.insert(bean2);  &hellip;

在事务环境下,操作多张表需要定义监听器:

tem.insertTransaction (bean, new TableEntityAdapter() {      public void afterInsert(final ITableEntityManager manager,                   final Object object, final SQLValue sqlValue) throws EntityException {          &hellip;          tem2.insert(bean2);      }  });

具有缓存(或分布式缓存)的表实体管理器

具有缓存(或分布式缓存)的表实体管理器继承自表实体管理器,其用法和表实体管理器一样,区别在于查询的结果对象,一个是从数据库直接创建的,一个是从缓存设备中获取的,Simple默认采用EHCahce来管理缓存。

查询实体管理器

和表实体管理器不同,查询实体管理器是基于SQL的,通过传递SQL语句来获取结果集,并且结果集对象以Map形式存在,可以通过如下方式获取表实体管理器:

IQueryEntityManager qem = DataObjectManagerFactory.getQueryEntityManager(dataSource);

查询指定条件的单行或多行记录

Map data = qem.queryForMap(      new SQLValue("select * from table1 t1, table2 t2 where t1.c1=t2.c2 and t2.c3=?",       new Object[] {1}));   IQueryEntitySet<Map> qs = qem.query(      new SQLValue("select * from table1 t1, table2 t2 where t1.c1=t2.c2"));

结果集(IQueryEntitySet)

IQueryEntitySet是一个高效、可靠的结果集,其设计准则:

有状态信息

动态及分页获取

可前后滚动

可定制缓存

下面是访问结果集的示例代码:

TestBean bean;  while((bean = qs.next()) != null) {      System.out.println(bean.getId());  }  qs.move(2);  System.out.println(qs.next());

“怎么用SimpleFramework框架实现数据访问”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI