Ibatis OR ing

Source: Internet
Author: User

Compared with ORM implementations such as Hibernate, ibatis's ing configuration is more concise and straightforward. Below is a typical configuration file.

   
 
  
  
  
    
   
   
  
  
       select * from user where id = #id# 
  
   
Insert into user values (# id #,# name #,# age #)
  
  
   
Update user set name = # name # where id = # id #
  
 
As you can see, the ing file is mainly divided into two parts: module configuration and Statement configuration.

I. module configuration includes:

TypeAlias node:

The alias in this ing file is defined to avoid repeated Writing of long variable values. In this example, the typeAlias node is used as the class "com. itmyhome. User" to define an individual

Name "user". In this way, when you reference the "com. itmyhome. User" class in other parts of this configuration file, you only need to replace it with its alias.

CacheModel Node

Defines the Cache mechanism used in this ing File

 
   
  
  
 
Here we declare a cacheModel named "userCache", which can be referenced in the Statement declaration.

This indicates that cacheModel "userCache" is used to cache the data obtained through Select statement with id "getUser.Then, if the program uses this Statement to query data again, it will directly read the query results from the cache without the need to query the database.CacheModel mainly has the following configuration points:FlushInterval: Sets the cache validity period. If this value is exceeded, the cache of this CacheModel is cleared.Size: Maximum number of data objects in this CacheModel.FlushOnExecute: Indicates that the cache is cleared when a specific Statement is executed. For example, the updateUser operation will changeThe user information in the new database, which will lead to the Data Objects in the cache and the actualData is deviated, so the cache must be cleared to avoid dirty data.Ii. Statement ConfigurationThe Statement configuration contains several SQL Statement-related nodes:1) statement2) insert3) delete4) update5) select6) procedureAmong them, statement is the most common and can replace all other nodes. Nodes except statement correspond toOperations with the same name (procedure corresponds to the stored procedure). Using statement to define all operations can certainly achieve the goal, but it lacks intuition,In actual development, we recommend that you specify the corresponding node names based on the purpose of the operation. On the one hand, make the configuration fileIt is more intuitive. On the other hand, you can also use the DTD to perform more targeted checks on node declarations to avoid configuration errors.The main configuration items are as follows:Select * from user where id = # id #select * from user where id = #id# 
 
  
Insert into user values (# id #,# name #,# age #)
 
 
  
Update user set name = # name # where id = # id #
 
 
  
Delete user where id = # id #
 
Parameters Description
ParameterClass Parameter class. specifies the complete Class Name of the parameter (including the package path ).
You can use aliases to avoid repeated lengthy class names.
ResultClass Result class. Complete class name (including package path) of the specified result type)
You can use aliases to avoid repeated lengthy class names.
ParameterMap Parameter ing, which must be defined based on the parameterMap node.
We recommend that you use statement other than stored procedures.
ParameterClass is used as the parameter configuration method, which avoids
Parameter ing configurator provides better performance.
ResultMap Result ing. The resing relationship must be defined based on the resultMap node.
CacheModel The Cache module corresponding to the statement.
For Parameter definition, try to use parameterClass, that is, directly use POJO as the statement call parameter,

In this way, the attributes of POJO can be directly set as parameters in SQL, such:

 
  alter table user set name = #name# where id = #id#
 
After the com. itmyhome. User class is set as the updateUser parameter, we can use the # name #

Reference the attributes of POJO. As follows:

set name = #name# where id = #id#
At runtime, ibatis obtains the corresponding parameter value by calling the getName and getId methods of the User object and uses it as the SQL parameter.

If parameterClass is set to a simple object type in jdk, such as String and Integer, ibatis directly uses it as a parameter value in SQL.

We can also pass the Map object containing the parameter data to Statement, for example:

 
  update  user set name = #name# where id = #id#
 Map
 
   map = new HashMap
  
   ();map.put("id", "1");map.put("name", "hello");sqlMap.update("updateUser",map);
  
 
The input parameter is a Map object. ibatis extracts the corresponding parameter values from key "id" and "name ".

When setting a parameter name in SQL, you can specify the parameter type at the same time, for example:

update  user set name = #name:VARCHAR# where id = #id:NUMBERIC#



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.