IBATIS2.0 instructions (2) - Configuration articles (3) [Original]

xiaoxiao2021-03-06  66

Introduction to the parameters in Statement:

ParameterClass

The value of the ParameterClass property is the full-qualification of the Java class (ie, including the package name). The ParameterClass property is optional, the purpose is to limit the type of input parameter to the specified Java class. Although Parameter-Class properties are optional, it is recommended that you specify parameterclass for each SQL. If the ParameterClass parameter is not specified, any Java bean with appropriate properties (Get / Set method) can be used as an input parameter. If you use ParameterMap, then you don't need to use the parameterclass property.

Here is an example:

example 1:

INSERT INTO AUTHOR (auth_name, auth_age, auth_tel, auth_address) values ​​(#Name #, # age #, # Telephone #, # address #)

In the above statement, you specified ParameterClass = Author, then there are Name, Age, Telephone, and Address properties in your Author class, and have corresponding GET and SET methods

Example 2:

You can use basic types as parameterclass, such as:

Delete from author where auth_id = # id #

Example 3:

You can use Hashmap as parameterclass, such as:

INSERT INTO AUTHOR (auth_name, auth_age, auth_tel, auth_address) values ​​(#Name #, # age #, # Telephone #, # address #)

At this time, when you call INSERTAUTHOR3, you should first assign a value to the incoming MAP object, the call code is as follows:

Hashmap paramap = new hashmap ();

Parammap.put ("Name", "author three");

Parammap.put ("age", new integer (31));

Parammap.put ("Address", "Nanjing");

Parammap.Put ("Telephone", "025-987654321");

SqlmapClient.insert ("INSERTAUTHOR3", Parammap;

2. ParameterMap

ParameterMap Defines a series of sequenceful parameters to match the JDBC value symbol of PreparedStatement.

ParameterMap properties are rarely used, and the value of ParameterMap attributes is equal to the Name property value of the specified parametermap element. Usually (and default) methods are in -line parameters.

note! Dynamic mapped state means only supports Inline Parameter, and Parameter Map is not supported. Regarding the dynamic mapped statement, it will be described later. E.g:

INSERT INTO AUTHOR (auth_name, auth_age, auth_tel, auth_address) VALUES (?,?,?,?)

In the above example, ParameterMap parameters match the value symbols in the SQL statement in order. Therefore, the first "?" Number will be replaced by the value of the "name" attribute, and the second "?" Number will be replaced by the value of the "agn" property, and so on.

Remember: When using parametermap, the parameters in SQL are replaced by "?", And each "?" Order fully matches the definition in ParameterMap; if you use parameterclass, then the parameter in SQL "# parametername # "Instead, if the incoming parameter class is bean, then there is a method of GET and SET this parameter name.

3. Resultclass

The resultClass property allows you to specify a Java class, automatically map it to the JDBC Resultset based on ResultSetMetadata. As long as it is a JavaBean property, the method name, and the column name of the Resultset, the property is automatically assigned value.

example 1:

Select auth_id, auth_name, auth_age, auth_tel, auth_address from author where auth_id = # id #

So in your Author class, there must be auth_id, auth_name, auth_age, auth_tel, auth_address attribute, and have corresponding GET and SET methods.

Example 2:

You can also use basic types as Resultclass, such as:

Example 3:

You can also use Hashmap as Resultclass, such as:

Select auth_id, auth_name, auth_age, auth_tel, auth_address from author where auth_address like #% address% #

or

SELECT * AUTHOR

In the above statement, you specified ResultClass = Author, then there are ID, name, age, telephone, and address properties in your Author class, and have the corresponding GET and SET methods.

With the definition of ResultMap, the resultSet obtained by the query statement is mapped to the Author object. ResultMap Defines the "ID" attribute value will give the "Auth_ID" field value, and the "Telephone" property value will give the "Auth_tel" field value, and push it according to the class. Note: The fields specified in the ResultMap must be the subset of the following SELECT.

In other words, you can't write

Select auth_id, auth_name, auth_age, auth_tel, auth_address from author where auth_address like #% address% #

In this way, you cannot get the value of auth_id.

5. Cachemodeel

Define the cache of the Mapped Statement. Each query mapped statement can use different or identical Cachemodel.

SELECT Auth_id As ID, Auth_name As Name, Auth_age As Age, Auth_tel As Telephone, Auth_address As Address from Author WHERE Auth_Id = # id #

The above SELECT will produce the following XML object:

1

Author 3

31

025-987654321

Nanjing

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

New Post(0)