Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use SimpleFramework Framework to realize data access

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to use SimpleFramework framework to realize data access". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

The Simple data access layer is based on Spring JDBC.

The core concept of Simple data access layer is "entity manager", all access to data is generated through different "entity manager", which makes Simple access to data easier, more consistent and more secure.

Simple provides three Entity Managers:

table entity manager

Table Entity Manager with Cache (or Distributed Cache)

Query Entity Manager

table entity manager

The table entity manager provides basic operations (select, insert, update, delete) on a single physical table. The table entity manager can be obtained in the following ways:

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

Here are some examples of manipulating table data through ITableEntityManager:

Query single row records with id equal to 1

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

Query multi-row records with id greater than 1

IQueryEntitySet qs = tem.query(new ExpressionValue("id>1")); or IQueryEntitySet qs2 = tem.query(new ExpressionValue("id>1", TestBean.class));

insert record with id equal to 2

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

Update records with id equal to 2

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

Delete records with id equal to 2

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

Manipulating multiple physical tables

In a non-transactional environment, operating multiple tables requires only defining different table entity managers:

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

In a transactional environment, operating multiple tables requires defining listeners:

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

Table Entity Manager with Cache (or Distributed Cache)

The table entity manager with cache (or distributed cache) inherits from the table entity manager, and its usage is the same as that of the table entity manager. The difference lies in that the result object of the query is directly created from the database and obtained from the cache device. Simple uses EHCahce to manage the cache by default.

Query Entity Manager

Different from the table entity manager, query entity manager is SQL based. The result set is obtained by passing SQL statements, and the result set object exists in the form of Map. The table entity manager can be obtained by the following methods:

IQueryEntityManager qem = DataObjectManagerFactory.getQueryEntityManager(dataSource);

Query single or multi-row records for specified criteria

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

IQueryEntitySet

IQueryEntitySet is an efficient and reliable result set with design criteria:

There is status information

Dynamic and paging acquisition

Scroll back and forth

Customizable cache

Here is sample code for accessing the result set:

TestBean bean; while((bean = qs.next()) != null) { System.out.println(bean.getId()); } qs.move(2); System.out.println(qs.next ());"How to use SimpleFramework framework to achieve data access" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report