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 parse iBATIS DAO Framework

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

How to analyze the iBATIS DAO framework, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

The iBATIS DAO framework is easy to write, and the project adopts the following shorthand conventions:

◆ Transaction:Tx

◆ Manager:Mgr

◆ Context:Ctx

◆ Interface:Iface

The iBATIS DAO framework is shown below:

The core of iBATIS DAO is that the creation code of DaoManager,DaoManager is as follows:

Reader reader = Resources.getResourceAsReader ("dao.xml"); DaoManager daoMngr = DaoManagerBuilder.buildDaoManager (reader)

DaoManager is the interface, and looking at the DaoManagerBuilder source code, you can see that its buildDaoManager method returns an instance of StandardDaoManager. The buildDaoManager method calls the buildDaoManager method of the XmlDaoManagerBuilder class, which does the following:

1. Create a StandardDaoManager instance stdDaoMgr

2. Create a Properties object that collects various property globally (from the resource file pointed to by the element or from all levels of elements in the current dao.xml)

3. Parse the elements in the dao.xml file (it is recommended to refer to a dao.xml file when reading this article, such as the dao.xml given in the JGameStore application), and add the corresponding property

4. Parse the elements in the dao.xml file to get a DaoContext instance daoCtx (4.1), and call the stdDaoMgr.addContext method to add daoCtx to the stdDaoMgr (4.2):

4.1 the process of parsing the elements of the dao.xml file to get daoCtx is as follows:

Instantiate a DaoContext object daoCtx

Set its daoManager field to our stdDaoMgr

If there is an id attribute, take the id field of daoCtx to the value of this property

Parsed child elements:

4.1.1 parse the child elements and get the DaoTxMgr API instance txMgr, which is set to the corresponding field of daoCtx. The parsing process is as follows:

Instantiate a corresponding DaoTxMgr instance txMgr based on the type attribute of the child element

Parsed child elements, add the resulting property to properties

Configure txMgr according to properties (that is, call the txMgr.configure method)

4.1.2 parse the child elements, get an instance of DaoImpl class daoImpl, and then add it to daoCtx

4.1.2.1 the parsing process is:

4.1.2.1.1 instantiate an instance of DaoImpl class daoImpl

4.1.2.1.2 set the daoMgr field of daoImpl to our stdDaoMgr

4.1.2.1.3 set the daoCtx field of daoImpl to our daoCtx

4.1.2.1.4 set the daoIface field of daoImpl to the class corresponding to the value of the iface attribute

4.1.2.1.5 set the daoImplementation field of daoImpl to the class corresponding to the value of the implementation attribute

4.1.2.1.6 instantiate a DAO implementation class according to the implementation attribute and set it to the value of the daoInstance field. Note that the instance must be an instance of the Dao interface, because any one inherits from DaoTemplate, and DaoTemplate implements the Dao interface.

4.1.2.1.7 create a proxy of the current DAO implementation class, set to the proxy field value of daoImpl, which calls the daoCtx.startTx method before calling the delegate method when the explicit transaction is enabled, and calls the daoCtx.startTx method and the commitTx method before and after the delegate method is called (the daoCtx.endTx method is also called in the finally block) when using an implicit transaction.

4.1.2.2 the process of adding daoImpl to daoCtx is to populate a table from daoIface to DaoImpl instance with the current daoImpl

4.2 the process of calling the stdDaoMgr.addContext method to add daoCtx to stdDaoMgr is as follows:

4.2.1 populate a table from id to DaoCtx instance with the current daoCtx

4.2.2 iterate through all the daoImpl stored in daoCtx, populating a table from daoIface to daoCtx and a table from Dao interface instances (that is, proxy and daoInstance in daoImpl) to daoCtx

5. The process for a customer to get a DaoIface implementation class instance xxxYyyDao by calling the DaoMgr.getDao method with a DaoIface is as follows:

StdDaoMgr looks up its table from daoIface to daoCtx, gets the daoCtx where the current daoIface resides, and then calls the daoCtx.getDao method:

DaoCtx looks up its table from daoIface to DaoImpl instance, gets daoImpl, and returns its proxy field

6. Implicit transaction:

In implicit transactions, every time a customer calls a method in xxxYyyDao, it is a complete transaction, because xxxYyyDao is obtained by calling the DaoMgr.getDao method, while according to 5, xxxYyyDao is actually a proxy, and according to 4.1.2.1.7, the agent "calls the daoCtx.startTx method and the commitTx method respectively before and after calling its delegate method (and also calls the daoCtx.endTx method in the finally block)."

The daoCtx.startTx method calls the txMgr.startTx method of its txMgr field, which returns an instance of DaoTx daoTx,daoCtx and puts it in a thread variable

6.2 in the DaoIface implementation class, because it must inherit from a DaoTemplate to call the database access method in it, and these database access methods call the getTx method of daoMgr with itself as a parameter; this method looks up the table from the Dao interface instance to daoCtx mentioned in 4.2.2, gets a daoCtx, and then calls daoCtx.getTx;daoCtx.getTx to return the daoTx instance stored in the thread variable

6.3 the daoTx instance contains the key elements required for database operation. For example, for SqlMapDaoTx, it contains a SqlMapClient instance. The database access methods in SqlMapDaoTemplate (such as insert,queryForList, etc.) first call daoMgr.getTx to get the daoTx instance, force it into a SqlMapDaoTx instance, then call its getSqlMap method to get the SqlMapClient instance, and then call the corresponding method in the SqlMapClient instance. For example, in the case of JDBC, the corresponding DaoTx is ConnectionDaoTx, and this class contains one. Each time the getConnection method of the JdbcDaoTemplate method is called, the method first calls daoMgr.getTx, obtains the daoTx instance, forcibly converts it into a ConnectionDaoTx instance, and then calls its getConnection method to get the Connection instance, and then calls the corresponding method.

The daoCtx.commitTx method calls the txMgr.commitTx (daoTx) method of its txMgr field to complete the commit of the transaction.

The daoCtx.endTx method calls the txMgr.endTx (daoTx) method of its txMgr field to end the transaction.

7. Explicit transaction:

An explicit transaction usually consists of three steps: first, call daoMgr.startTx, then call the method in xxxYyyDao, and * call daoMgr.commitTx.

The job of 7.1daoMgr.startTx is very simple, just to set the fields in stdDaoMgr that mark explicit transactions

When calling methods in xxxYyyDao, daoCtx.startTx will be called first because of the proxy. This procedure is the same as 6.1s

7.3.When calling daoMgr.commitTx, the method finally calls daoCtx.commitTx. Please refer to

Let's conclude this article with the implementation of a question: if I were to implement Hibernate support in iBATIS's DAO framework, how should we implement it?

The core of Hibernate is Session, and all database operations can be done by calling the corresponding methods on Session. All DaoTx implementations considered to support Hibernate should be a wrapper of Session with a getSession method that returns the current Session (including commit and rollback methods, of course). Similarly, the configure method of the DaoTxMgr implementation class is responsible for completing the configuration of a Session instance (session), and the startTx method is responsible for returning a DaoTx instance that wraps the current session instance. The commitTx method forcibly converts the incoming daoTx instance and calls the commit method on the daoTx. The rollbackTx method forcibly converts the incoming daoTx instance and calls the rollback method on the daoTx. The key of the HibernateDaoTemplate class lies in the getSession method of its protected, which first calls daoMgr.getTx to get the current daoTx instance, and then calls the getSession method on the daoTx after forced conversion.

Query the source code of iBATIS and find that the idea is exactly the same as above.

After reading the above, have you mastered how to parse the iBATIS DAO framework? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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