AD

ibatis batch processing + multi-table queries Relevance

ibatis batch operations

ibatis batch operation there are two ways:
One is directly in the code to loop operation, the other is in the configuration file for loop operations.
(1) loop in the configuration file:
1. The situation one: more input parameters Asymmetric cycles:
Approach: a new JAVABEAN, the parameters will be assigned as part of its property. In the configuration file, get its value, for each cycle.
The following example: To update the value of the field Opr only one, and there are more than the value of ID.
Code:
view plaincopy to clipboardprint?
public void batchClientAppOperation (String [] appDevIds, String operation) throws Exception
{
try
{
AppOperation appOpr = new AppOperation ();
appOpr.setOperation (operation);
appOpr.setAppDevIds (appDevIds);

this.getSqlMapClientTemplate (). update ("Device.ClientAppOperation", appOpr);
}
catch (DataAccessException ex)
{
throw new Exception (
Constants.ERROR_CODE_DELETE_USER_BY_ID.getLongValue (),
ex);
}
}
public void batchClientAppOperation (String [] appDevIds, String operation) throws Exception
{
try
{
AppOperation appOpr = new AppOperation ();
appOpr.setOperation (operation);
appOpr.setAppDevIds (appDevIds);

this.getSqlMapClientTemplate (). update ("Device.ClientAppOperation", appOpr);
}
catch (DataAccessException ex)
{
throw new Exception (
Constants.ERROR_CODE_DELETE_USER_BY_ID.getLongValue (),
ex);
}
}
ibatis configuration file:
view plaincopy to clipboardprint?
<update parameterClass="AppOperation">
update T_Device_App_R_Info
set Opr = # operation #
where ID in
<iterate conjunction="," open="(" close=")" property="appDevIds">
# AppDevIds [] #
</ Iterate>
</ Update>
<update parameterClass="AppOperation">
update T_Device_App_R_Info
set Opr = # operation #
where ID in
<iterate conjunction="," open="(" close=")" property="appDevIds">
# AppDevIds [] #
</ Iterate>
</ Update>

2. Situation II: multiple input parameters the number of cycles is symmetric:
Approach: create a hashmap, the various parameter name as key, parameter value as the value. In the configuration file, for each key-value, for each cycle.
The following example: the circular insert / update the column name as key, the column value as the value placed in the hashmap. (Column names and column values are one to one, that is the cycle number, etc.)
view plaincopy to clipboardprint?
<! -
Insert a new record
Get the value of $ and # is the same way, but $ for the corresponding parameter value, the value of # will get quotes, into a string type. Therefore, the general use of $ to get the table names, column names, use the # with access to insert values.
->
<insert parameterClass="customPO">
INSERT INTO $ moduleTable $ (parentID
<iterate property="fieldValueList" prepend="," conjunction=","> - fieldValueList is a property customPO
$ FieldValueList []. Key $ - Cycle fieldValueList [] the array, because each object in this array is a map, get map's key.
</ Iterate>
)
VALUES (# parentID #
<iterate property="fieldValueList" prepend="," conjunction=",">
# FieldValueList []. Value #
</ Iterate>
)
<selectKey resultClass="int" keyProperty="id">
SELECT last_insert_id ()
</ SelectKey>
</ Insert>

<! - Update ->
<update parameterClass="customPO">
UPDATE $ moduleTable $ SET
<iterate property="fieldValueList" conjunction=",">
$ FieldValueList []. Key $ = # fieldValueList []. Value #
</ Iterate>
WHERE id = # id #
</ Update>
<! -
Insert a new record
Get the value of $ and # is the same way, but $ for the corresponding value of the parameter value # will get quotes, into a string type. Therefore, the general use of $ to get the table names, column names, use the # with access to insert values.
->
<insert parameterClass="customPO">
INSERT INTO $ moduleTable $ (parentID
<iterate property="fieldValueList" prepend="," conjunction=","> - fieldValueList is a property customPO
$ FieldValueList []. Key $ - Cycle fieldValueList [] the array, because each object in this array is a map, get map's key.
</ Iterate>
)
VALUES (# parentID #
<iterate property="fieldValueList" prepend="," conjunction=",">
# FieldValueList []. Value #
</ Iterate>
)
<selectKey resultClass="int" keyProperty="id">
SELECT last_insert_id ()
</ SelectKey>
</ Insert>

<! - Update ->
<update parameterClass="customPO">
UPDATE $ moduleTable $ SET
<iterate property="fieldValueList" conjunction=",">
$ FieldValueList []. Key $ = # fieldValueList []. Value #
</ Iterate>
WHERE id = # id #
</ Update>

(2)
Batch processing in the code:
That is, startBatch () and executeBatch () calls between the cyclic operation of the database. As follows:
view plaincopy to clipboardprint?
public void batchUpdate (final String [] ids, final String appId) throws IEPGMException
{
try
{
final HashMap <String, String> map = new HashMap <String, String> ();

this.getSqlMapClientTemplate (). execute (new SqlMapClientCallback ()
{

public Object doInSqlMapClient (SqlMapExecutor executor) throws SQLException
{
executor.startBatch ();
executor.delete ("Application.deleteAppDevTypeMapById", appId);
for (int i = 0; i <ids.length; i + +)
{
/ / In the map key is appId, ids, so the assignment after each cycle, map and only two in the key, that is, appId, ids, its value will be constantly updated.
map.put ("appId", appId);
map.put ("ids", ids [i]);
executor.insert ("Application.insertAppDevTypeMap", map);
}
executor.executeBatch ();
return null;
}
});
}
catch (DataAccessException ex)
{
throw new IEPGMException (
Constants.ERROR_CODE_DELETE_USER_BY_ID.getLongValue (),
ex);
}
}
public void batchUpdate (final String [] ids, final String appId) throws IEPGMException
{
try
{
final HashMap <String, String> map = new HashMap <String, String> ();

this.getSqlMapClientTemplate (). execute (new SqlMapClientCallback ()
{

public Object doInSqlMapClient (SqlMapExecutor executor) throws SQLException
{
executor.startBatch ();
executor.delete ("Application.deleteAppDevTypeMapById", appId);
for (int i = 0; i <ids.length; i + +)
{
/ / In the map key is appId, ids, so the assignment after each cycle, map and only two in the key, that is, appId, ids, its value will be constantly updated.
map.put ("appId", appId);
map.put ("ids", ids [i]);
executor.insert ("Application.insertAppDevTypeMap", map);
}
executor.executeBatch ();
return null;
}
});
}
catch (DataAccessException ex)
{
throw new IEPGMException (
Constants.ERROR_CODE_DELETE_USER_BY_ID.getLongValue (),
ex);
}
}

ibatis configuration file:
view plaincopy to clipboardprint?
<delete parameterClass="java.lang.String">
DELETE FROM T_App_Spce_R_Info WHERE App_ID = # appId #;
</ Delete>

<insert parameterClass="java.util.HashMap">
INSERT INTO T_App_Spce_R_Info (App_ID, Spec_Code) VALUES (# appId #, # ids #);
</ Insert>
<delete parameterClass="java.lang.String">
DELETE FROM T_App_Spce_R_Info WHERE App_ID = # appId #;
</ Delete>

<insert parameterClass="java.util.HashMap">
INSERT INTO T_App_Spce_R_Info (App_ID, Spec_Code) VALUES (# appId #, # ids #);
</ Insert>

How to define multi-table query
1)
Add Guojia.java class, just an ordinary javabean, the fields corresponding to guojia table, and the setter, getter, not as set as the hibernate mapping.
2)
Add guojia.xml, simply add:

view plaincopy to clipboardprint?
<sqlMap namespace="Guo">
<typeAlias type="com.coship.dhm.iepgm.admin.entity.Guojia" alias="Guo" />
<resultMap>
<result column="gid" property="gid" />
<result column="userid" property="userid" />
<result column="gname" property="gname" />
</ ResultMap>
</ SqlMap>
<sqlMap namespace="Guo">
<typeAlias type="com.coship.dhm.iepgm.admin.entity.Guojia" alias="Guo" />
<resultMap>
<result column="gid" property="gid" />
<result column="userid" property="userid" />
<result column="gname" property="gname" />
</ ResultMap>
</ SqlMap>

No need to add things such as query and the like.
3)
Add in the current class hwj.java Link query attribute table guojia the list, as follows:
view plaincopy to clipboardprint?
private List <Guojia> guoList;
public List <Guojia> getGuoList () {
return guoList;
}
public void setGuoList (List <Guojia> guoList) {
this.guoList = guoList;
}
private List <Guojia> guoList;
public List <Guojia> getGuoList () {
return guoList;
}
public void setGuoList (List <Guojia> guoList) {
this.guoList = guoList;
}

4)
In the current table hwj.xml the resultMap, add <result property="guoList" resultMap="Guo.GuoResult"/>, as follows:
view plaincopy to clipboardprint?
<resultMap>
<result column="staff_id" property="id" />
<result column="longinName" property="loginName" />
<result column="password" property="password" />
<result column="staff_name" property="username" />
<result column="status" property="status" />
<result column="phone" property="phone" />
<result column="email" property="email" />
<result property="guoList" resultMap="Guo.GuoResult"/>
</ ResultMap>
<resultMap>
<result column="staff_id" property="id" />
<result column="longinName" property="loginName" />
<result column="password" property="password" />
<result column="staff_name" property="username" />
<result column="status" property="status" />
<result column="phone" property="phone" />
<result column="email" property="email" />
<result property="guoList" resultMap="Guo.GuoResult"/>
</ ResultMap>

Note: resultMap = "Guo.GuoResult" corresponds to the above guojia.xml in Guo's namespace, GuoResult name corresponding to the resultMap.
5)
Import guojia.xml in sqlMapClient.xml
Note:
ResultMap listed in the columns, only select the various fields. No more, no less. Otherwise error.
Such as: A table has a, b two columns. Now select a from A. ResultMap only column is a column about a name - attribute, multi-column b, not something else wrong.

6)
Even if the table together for the three queries,
1. For the third set the table with the table above as the second set.
2. Similarly, only in hwj.java, hwj.xml make the same settings.
Description:
Displayed on the page:
Recorded in the hwj loop iterator, add guojia, address a variety of recycling can be.
No need to address embedded in guojia in: because the query is actually hwj check out all records, guojia, address to add the loop, only the circle, take a value.
Set as follows:
view plaincopy to clipboardprint?
<% - Guojia -%>
<td width="20%">
<s:iterator value="#user.guoList" status="index1">
<% - <s:property Value="#index1.index"/> -%>
<s:property value="gname" />
</ S: iterator> </ td>
<% - Address -%>
<td width="20%">
<s:iterator value="#user.addressList" status="index1">
<% - <s:property Value="#index1.index"/> -%>
<s:property value="address" />
</ S: iterator> </ td>
<% - Guojia -%>
<td width="20%">
<s:iterator value="#user.guoList" status="index1">
<% - <s:property Value="#index1.index"/> -%>
<s:property value="gname" />
</ S: iterator> </ td>
<% - Address -%>
<td width="20%">
<s:iterator value="#user.addressList" status="index1">
<% - <s:property Value="#index1.index"/> -%>
<s:property value="address" />
</ S: iterator> </ td>

(3)
resultMap column in correspondence with the javaBean property, only list the fields sql select statement, no more and no less, or do not care about errors .---- sql statement associated with a few tables. as follows:
view plaincopy to clipboardprint?
<resultMap>
<result column="Spec_Code" property="specCode" />
<result column="Spec_Name" property="specName" />
<result column="Vendor" property="vendor" />
<result column="Brand" property="brand" />
<result column="Model" property="model" />
</ ResultMap>

<select parameterClass="java.lang.String" resultMap="DevTypeResult">
SELECT distinct d. *
FROM T_App_Info a, T_Device_Spec d, T_App_Spce_R_Info r
WHERE a.App_ID = r.App_ID
AND r.Spec_Code! = D.Spec_Code
AND a.App_ID = # appId #
ORDER BY d.Spec_Code;
</ Select>
<resultMap>
<result column="Spec_Code" property="specCode" />
<result column="Spec_Name" property="specName" />
<result column="Vendor" property="vendor" />
<result column="Brand" property="brand" />
<result column="Model" property="model" />
</ ResultMap>

<select parameterClass="java.lang.String" resultMap="DevTypeResult">
SELECT distinct d. *
FROM T_App_Info a, T_Device_Spec d, T_App_Spce_R_Info r
WHERE a.App_ID = r.App_ID
AND r.Spec_Code! = D.Spec_Code
AND a.App_ID = # appId #
ORDER BY d.Spec_Code;
</ Select>

This article comes from CSDN blog, reproduced, please credit: **blog.csdn**/w40338544/archive/2010/06/28/5699568.aspx
标签: lt, public void, javabean, configuration file, conjunction, two ways, input parameters, constants, string operation, iterate, batch operations, batch operation, relevance, loop operations, table queries, loop operation
分类: Java
时间: 2010-10-12

相关文章

  1. iBatis batch problem - on Oracle's insert into ... select ... wrong to use

    Published the first time :2009-02-17 Need to use iBatis in the development of a batch processing feature large ...
  2. In the Hibernate batch processing in the

    1: Bulk Insert In the project development process, as the project requirements, we often need large quantities ...
  3. [Oracle] efficient PL / SQL Programming (4) - batch processing

    Batch operation is generally used in ETL, ETL behalf extract (extract), convert (transform), load (load), is a ...
  4. Hibernate batch processing (Batch processing)

    Use Hibernate will insert 100,000 records into the database may be a very natural way so Session session = ses ...
  5. Hibernate batch processing application series of six articles

    Technical documentation] [IT168 Abstract: For a long time, Hibernate batch operation as part of the performanc ...
  6. java batch processing

    Received from the Excel data: package com.mobilecn.kys.export; import java.io.File; import java.io.IOException ...
  7. Hibernate cache configuration / batch processing

    Hibernate cache configuration / batch processing Keywords: hibernate Hibernate In addition to automatically co ...
  8. The update sql in multi table updates

    The update sql in multi table updates In the development of the database back and forth exchange, and some key ...
  9. hibernate one to many table queries fetchMode.join generate left outer join up data duplication

    hibernate one to many table queries fetchMode.join generate left outer join up data duplication @ OneToMany (c ...
  10. jdbc transaction processing and batch processing

    [Size = large] [/ size] [/ b] [b] / / use of services import java.sql .*; public class Transaction { public st ...
  11. Hibernate batch processing solution to the problem under

    Many people on Java in the batch processing of data whether it is the right place skeptical of the idea, and b ...
  12. 第 13 章 批量处理(Batch processing)

    第 13 章 批量处理(Batch processing) 使用Hibernate将 100 000 条记录插入到数据库的一个很自然的做法可能是这样的 Session session = sessionFactory.o ...
  13. iBATIS paging processing

    ibatis pagination to achieve the main steps: a. create the Page class. b. create a bean class, and inherits Pa ...
  14. Batch processing time to obtain the last week from the current time format yyyyMMdd

    Need to work each day BCP to import a table the week of data used in this requirement automatically generated ...
  15. oracle multi table join

    Multi-table join: Query a data source is no longer a single table, we could extract more than one table of dat ...
  16. spring + ibatis batch submission of data to improve performance

    public void insertCreditItemBatch( final List< credititem></ credititem> creditItemList) throws Da ...
  17. Batch processing and preparation of directory of PHP file line number and the number of statistical

    Statistics today under the number of lines of code to write (to remove blank lines and comments) and the total ...
  18. Database query optimization (one million record deal on how to improve the processing speed of queries)

    1 the query is optimized, should try to avoid full table scans, we should first consider where and order by th ...
  19. Spring + Ibatis batch performance

    Project using Spring + Ibatis, on how to use transactions, how to enhance the performance of a few summary Tra ...