Ibatis.Net Series (4) Ibatisnet API Foundation

xiaoxiao2021-03-31  201

After some understanding of the iBATIS configuration system, you will first come and find out that iBATIS is to call the SQL statement of the mapping file. This has a great help to our next understanding.

For simple IBATIS applications, I think most of them are concentrated in dealing with SQLMAPper objects. This class should be a tool class, because we generally call this class method to perform quid operations, but it is not really doing these things. Because there are many classes within Ibatis, the relationship between objects is very complicated. If customers go directly to use their internal methods, there is no doubt that there is a lot of redundancy code. So here it uses the design mode, encapsulates the complex operation of IBATIS executing database access by SQLMAPPER classes, including opening a session (session), gets returns the IMAPPedStateMent object instance, perform database access, shut down the connection, etc. related operations. In this way, we can do everything very simple when using the Ibatis API, you can do all things. For example, Query Interface Public IList QueryforList (String StatementName, Object ParameterObject), the internal implementation code is like this.

IList List1;

Bool

Flag1

=

False

IDalSession session1

=

THIS

._sessionHolder.localsession;

IF

(session1

==

NULL

) {Session1

=

New

SQLMapSession

THIS

.Datasource; session1.openconnection (); FLAG1

=

True

ImappedStatement Statement1

=

THIS

.GetmappedStatement (StatementName);

Try

{List1

=

Statement1.executeQueryforList (session1, parameterObject);

Catch

{

Throw

}

Finally

{

IF

(flag1) {session1.closeConnection ();}}

Return

List1;

These codes can be implemented directly in the customer code, and how big will the workload will be. And it is also guaranteed to be correct.

The above simply saw a SQLMAPper role, how should I instantiate this object? Instantiation it is also a very simple thing. In the ibatisnet, the SQLMAPPER object is an implementation of a single mode by default. An SQLMAPper object is instantiated by the static instance property of the Mapper class. This design may be partially considered in performance. Because in initializing the SQLMAPPER object, you need to initially restructure the environment configuration, read, and initially resolve the included map files, so when you call the Ibatisnet API for the first time when the system is run, it is necessary to process this configuration. The implementation of the mapper.instance property is as follows:

public

Static

SQLMAPPER Instance () {

IF

(Mapper._mapper

==

NULL

) {

LOCK

(

Typeof

(SQLMAPPER)) {IF

(Mapper._mapper

==

NULL

) {Mapper.initmapper ();}}}

Return

Mapper._mapper;}

So when using the API, you can be as simple as the following:

Mapper.instance (). INSERT ("ContentObject_Defaultinsert", P_DataObject);

Of course, if you are willing and if necessary, you can use yourself to instantiate this object, we can use DomsqlmapBuilder directly, it provides us with this extension ability, can return to SQLMapper objects through its multiple instance methods: Build , Configure, ConfigureAndwatch. This method is very useful under the occasion of multiple databases or multiple different database types.

Note: When using an interface, use StatementName to be in the corresponding type of Statement type. For example, when using the INSERT interface, if you specify a SELECT type configuration statement, it will throw an exception. Because each STATEMENT type corresponds to a type, such as the SELECT type configuration statement corresponding to the SelectMappedStatement class, it is inherited from mappedStatement, and its ExcuteInsert method is implemented.

public

Override

Object

ExecuteInsert (iDalSession Session,

Object

ParameterObject) {

Throw

New

DataMappeRexception

"

Update Statements Cannot Be Executed As a Query Insert.

"

}

This ensures that each statement type is clear.

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

New Post(0)