One of the DAO part of Ibatis

xiaoxiao2021-03-06  58

Ibatis authors don't like to explain before class, criticize.

First look at the package com.ibatis.dao.client 1 Define a declarative interface DAO2 a daomanager interface, which has a factory method for creating a Dao instance: public Dao getdao (class type); is to write a DAO interface written by users Implementation with SQLMAP, by specifying both of DAO.xml. Public Daotransaction GetTransaction (DAO DAO); This is a class that returns a transaction management (daotransaction is also a declarative interface) In addition, there are three operations regarding transaction, start submitting rollback. 3 DaomanagerBuilder Factory class, is responsible for creating a Daomanager object from an XML configuration file.

With the problem, look down at the com.ibatis.dao.Engine.Impl package 1 class StandardDaManager aggregated a MAP object of a DAOCONText type. Every Daocontext corresponds to the map content of a Context node in DAO.XML, which is very understandable. Class 2 DAOCONTEXT, a dictionary class, store the DAO interface with a map (TypedaoImplmap) variable, not, etc. In this MAP variable, the user's specific SQLMAPDAO class should have been replaced with DaoImpl class. Another important responsibility of Daocontext is to process the transaction, you should notice that the TransactionManager node included in the DAO.xml every Context node. Look at the following methods: public Dao getdao (class ifce) {DAOIMPL IMPL = (DAOIMPL) TYPEDAOIMPLMAP.GET (IFACE); if (IMPL == Null) {throw new daoException ("there is no Dao Implementation Found for" IFACE "in this context.");} Return Impl.getProxy ();

The specific implementation of the factory class XMLDaomanagerbuilder starts traceability:

Private DaoIMPL Parsedao {...

// The specific SQLMAPDAO subclass is typically inherited from SQLMAPDAOTEMPLATE, and its constructor uses the DaOManager instance as a parameter, while DaOTemplate implements a DAO declaration interface. Constructor constructor = daoClass.getConstructor (new Class [] {DaoManager.class}); Dao dao = (Dao) constructor.newInstance (new Object [] {daoManager}); ... daoImpl.setDaoInstance (dao);

DaoImpl.initProxy ();

RETURN DAOIMPL;} In parsing DAO.XML, use the dynamic agent to match the user's DAO implementation class as a DaoImpl class instance (for management transaction), it seems to pay attention to the DaoImpl class method initproxy ():

. Public void initProxy () {proxy = DaoProxy.newInstance (this);} then the tracked DaoProxy class: public static Dao newInstance (DaoImpl daoImpl) {return (Dao) Proxy.newProxyInstance (daoImpl.getDaoInterface () getClassLoader (), new Class [] {DAO.CLASS, DAOIMPL.GETDAOINTERFACE ()}, New DaoImpl);} Ibatis uses dynamic agents. Generate an Proxy instance that implements the iBATI Declaration DAO and the DAO dual interface defined by the user yourself. The data access method written by each user will be wrapped on the transaction mechanism when called by Proxy. It can be seen that the method in each DAO subclass is a transaction. I have a question to this, well known that the transaction should be placed in the service layer. If it is placed in the DAO layer, it will cause the performance of the performance, but I think that Ibatis will definitely think of this, there should be a lot of registration in the Service layer. DAO's affairs avoids nested transactions. Note: Dynamic proxy, start support from JDK1.3, "Dynamic Agent" is such a class, which can be implemented at a set of interfaces specified at runtime. The method call to the proxy class is assigned to the calling handler, and the calling handler is specified when the agent class is generated. It has a limit that the object to be packaged / extended must implement an interface. This interface defines all methods preparing to use in the wrapper, we all know that the interface should be programmed, which should not be over. Proxy.newProxyInstance () method There are three parameters: dynamic proxy defined class loader ClassLoader; Class array, contains all interfaces to be implemented, and the calling handler called by the processing method.

Here, Spring, in Spring, based on the Dynamic Proxy mechanism provided by JDK1.3, you can configure this generated subclass to proxy method calls for the original target class. Subclasses are placed in Decorator design mode.

Let's go here, it is a little dizziness. Experience: 1 Declaration interface (ie there is no specific interface method), good design is based on an interface, easy to expand. 3 Dynamic agent usage.

转载请注明原文地址:https://www.9cbs.com/read-83703.html

New Post(0)