Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30467291
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: Java

2011-11-11 13:37:07

  1. /**
  2.  * IbatisTest.java
  3.  * com.isoftstone.cry.ibatisTest
  4.  *
  5.  * Function: TODO
  6.  *
  7.  * ver date         author
  8.  * ──────────────────────────────────
  9.  *          Mar 10, 2011     
  10.  *
  11.  * Copyright (c) 2011, All Rights Reserved.
  12. */

  13. package com.isoftstone.cry.ibatis;

  14. import java.io.IOException;
  15. import java.io.Reader;
  16. import java.sql.SQLException;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import com.ibatis.common.resources.Resources;
  20. import com.ibatis.sqlmap.client.SqlMapClient;
  21. import com.ibatis.sqlmap.client.SqlMapClientBuilder;
  22. import com.isoftstone.cry.pojo.KeyBean;
  23. import com.isoftstone.cry.pojo.LockBean;
  24. /**
  25.  *
  26.  * ClassName:IbatisDemo
  27.  * Project:
  28.  * Company: isoftStone
  29.  *
  30.  * @author
  31.  * @version
  32.  * @since Ver 1.1
  33.  * @Date     2011    Mar 14
  34.  * @see
  35.  */
  36. public class IbatisDemo {
  37.     public static void main(String[] args) throws IOException,SQLException{
  38. //        读取配置文件
  39.         Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
  40.         SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
  41. //        单表的增删改查
  42. //        ①、查询表集合
  43.         /*List lockList = (List)sqlMap.queryForList("lockNameSpace.getLockList");
  44.         for(LockBean lb : lockList){
  45.             System.out.println(
  46.                     lb.getId()+" "+lb.getLockName()+" "+lb.getLockFactory()
  47.             );
  48.         }*/
  49. //        ②、查询表对象 by id
  50.         /*LockBean lb = (LockBean)sqlMap.queryForObject("lockNameSpace.getLockObjectById",new Long(1));
  51.         System.out.println(
  52.                 lb.getId()+" "+lb.getLockName()+" "+lb.getLockFactory()
  53.         );*/
  54. //        ③、查询表对象 by like
  55.         /*List lockList = (List)sqlMap.queryForList("lockNameSpace.getLockObjectByLike",new String("南京"));
  56.         for(LockBean lb : lockList){
  57.             System.out.println(
  58.                     lb.getId()+" "+lb.getLockName()+" "+lb.getLockFactory()
  59.             );
  60.         }*/
  61. //        ④、查询表对象 动态组合查询
  62. //        test 前台http 获取属性值
  63.     /*    LockBean lb = new LockBean();
  64.         lb.setLockFactory("南京");
  65.         lb.setLockType(new Integer(12));
  66.         List lockList = (List)sqlMap.queryForList
  67.         ("lockNameSpace.getLockListByDynamic",lb);
  68.         for(LockBean lock : lockList){
  69.             System.out.println(
  70.                     lock.getId()+" "+lock.getLockName()+" "+lock.getLockFactory()
  71.             );
  72.         }*/
  73. //        ⑤、多表查询 - 一对一 关联查询 ... (所谓“n+1”Select问题)
  74.         /*List lockList = (List)sqlMap.queryForList
  75.         ("lockNameSpace.getLockList");
  76.         for(LockBean lock : lockList){
  77.             System.out.println(
  78.                     lock.getId()+" "+lock.getLockName()+" "+lock.getLockInfo().getLockMoney()
  79.             );
  80.         }*/
  81. //        ⑥、多表查询 - 一对一 (解决所谓“n+1”Select问题)
  82.         /*List lockList = (List)sqlMap.queryForList
  83.         ("lockNameSpace.getLockListOneToOne");
  84.         for(LockBean lock : lockList){
  85.             System.out.println(
  86.                     lock.getId()+" "+lock.getLockName()+" "+lock.getLockInfo().getLockMoney()
  87.             );
  88.         }*/
  89. //        7、 多表查询 一对多(基本解决,有个bug.....)
  90.         /*List lockList = (List)sqlMap.queryForList
  91.         ("lockNameSpace.getLockList");
  92.         System.out.println("size = "+lockList.size());
  93.         for(int i=0;i
  94.         {
  95.             LockBean lb = lockList.get(i);
  96.             System.out.println(lb.getId()+" - ");
  97.             for(int j=0;j
  98.                 KeyBean kb = lb.getKeyList().get(j);
  99.                 System.out.println(kb.getLockId()+" "+kb.getId()+" "+kb.getKeyName());
  100.             }
  101.         }*/
  102. //        8、插入操作 insert
  103.         /*LockBean lb = new LockBean();
  104.         lb.setLockName("lockName");
  105.         lb.setLockFactory("lockFactory");
  106.         lb.setLockColor("lockColor");
  107.         lb.setLockType(new Integer(44));
  108.         sqlMap.insert("lockNameSpace.insertLock",lb);
  109.         System.out.println("insert success .... ");*/
  110.         
  111. //        9、批量插入操作 insert
  112.         /*List lbList = new ArrayList();
  113.         for(int i=0;i<5;i++){
  114.             LockBean lb = new LockBean();
  115.             lb.setLockName("lockName"+i);
  116.             lb.setLockFactory("lockFactory"+i);
  117.             lb.setLockColor("lockColor"+i);
  118.             lb.setLockType(new Integer(44)+i);
  119.             lbList.add(lb);
  120.         }*/
  121. //        结合spring批量处理
  122.         /*SqlMapClientCallback callback = new SqlMapClientCallback() {
  123.             public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
  124.                 executor.startBatch();
  125.                 for (T tObject : memberList) {
  126.                     executor.insert(statement, tObject); // statement在*MapSql.xml一条语句的id
  127.                 }
  128.                 executor.executeBatch();
  129.                 return null;
  130.             }
  131.         };*/
  132. //        ibatis 批量插入 - 删除跟新类似
  133.         /*sqlMap.startTransaction();
  134.         for(int i=0;i
  135.             sqlMap.insert("lockNameSpace.batchInsertLock",lbList.get(i));
  136.         }
  137.         sqlMap.executeBatch();
  138.         sqlMap.commitTransaction();
  139.         sqlMap.endTransaction();
  140.         System.out.println("批量插入完成................");*/
  141.         
  142. //        10、 删除操作 delete
  143.         /*sqlMap.delete("lockNameSpace.deleteLock",100);*/
  144.         
  145. //        11、批量删除
  146.         /*sqlMap.startTransaction();
  147.         for(int i=51;i<100;i++){
  148.             sqlMap.delete("lockNameSpace.deleteLock",i);
  149.         }
  150.         sqlMap.executeBatch();
  151.         sqlMap.commitTransaction();
  152.         sqlMap.endTransaction();
  153.         System.out.println("批量删除完成................");*/
  154.         
  155. //        12、更新操作 update ..
  156.         /*LockBean lb = new LockBean();
  157.         lb.setLockName("updatelockName");
  158.         lb.setLockColor("upColor");
  159.         lb.setId(50);
  160.         sqlMap.update("lockNameSpace.updateLock",lb);
  161.         System.out.println("更新完成。。。。。");*/
  162. //        13、批量更新
  163.         /*List lbList = new ArrayList();
  164.         for(int i=50;i<60;i++){
  165.             LockBean lb = new LockBean();
  166.             lb.setLockName("lockName"+i);
  167.             lb.setLockFactory("lockFactory"+i);
  168.             lb.setLockColor("Color"+i);
  169.             lb.setLockType(new Integer(44)+i);
  170.             lb.setId(i);
  171.             lbList.add(lb);
  172.         }
  173.         
  174.         sqlMap.startTransaction();
  175.         for(int i=0;i
  176.             sqlMap.update("lockNameSpace.updateLock",lbList.get(i));
  177.         }
  178.         sqlMap.executeBatch();
  179.         sqlMap.commitTransaction();
  180.         sqlMap.endTransaction();
  181.         System.out.println("批量更新完成................");*/
  182.     }
  183. }

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE sqlMap
  3. PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
  4. "">

  5. <sqlMap namespace="lockNameSpace">
  6.     <!-- ibatis add,delete,modify,query demo -->
  7.     <!-- 设计映射 -->
  8.     <typeAlias alias="lock" type="com.isoftstone.cry.pojo.LockBean"/>
  9.     <typeAlias alias="lockInformation" type="com.isoftstone.cry.pojo.LockInfoBean"/>
  10.     <typeAlias alias="lockKey" type="com.isoftstone.cry.pojo.KeyBean"/>
  11.     
  12.     <resultMap id="lockResult" class="lock">
  13.         <result property="id" column="id"/>
  14.         <result property="lockName" column="lock_Name"/>
  15.         <result property="lockColor" column="lock_Color"/>
  16.         <result property="lockFactory" column="lock_Factory"/>
  17.         <result property="lockType" column="lock_Type"/>
  18.         <!-- 一对一设置 n+1 select 延时加载-->
  19.         <result property="lockInfo" column="id" select="lockNameSpace.getLockVsInfo"/>
  20.         <!-- 一对多 -->
  21.         <result property="keyList" column="id" select="lockNameSpace.getKeyList"/>
  22.         <!-- <result property="lockInfo" resultMap="resultLockInfo"/>-->
  23.     </resultMap>
  24.     
  25.     <!-- 备注:resultMap 映射机制,resultClass 隐式映射(前提是javaBean属性和表字段column相同) -->
  26.     <!-- query object list -->
  27.     <!-- query all -->
  28.     <select id="getLockList" resultMap="lockResult">
  29.         <![CDATA[
  30.         SELECT * FROM LOCK_
  31.         ]]>
  32.     </select>
  33.     <!-- query by id -->
  34.     <select id="getLockObjectById" resultMap="lockResult">
  35.         SELECT * FROM LOCK_ WHERE ID = #VALUE#
  36.     </select>
  37.     <!-- query by like -->
  38.     <select id="getLockObjectByLike" resultMap="lockResult">
  39.         SELECT * FROM LOCK_ WHERE LOCK_FACTORY LIKE '%$VALUE$%'
  40.     </select>
  41.     <!-- query dynamik -->
  42.     <select id="getLockListByDynamic" resultMap="lockResult">
  43.         SELECT * FROM LOCK_
  44.         <dynamic prepend="WHERE">
  45.             <isNotEmpty prepend="and" property="lockFactory">
  46.                 (lock_factory like '%$lockFactory$%')
  47.             </isNotEmpty>
  48.             <isNotNull prepend="AND" property="lockType">
  49.                 (lock_type = #lockType#)
  50.             </isNotNull>
  51.         </dynamic>
  52.     </select>
  53.     <!-- 多表查询 一对一 -->
  54.     <!-- 关联lockInfo表属性对应数据库字段 -->
  55.     <resultMap id="resultLockInfo" class="lockInformation">
  56.         <result property="id" column="id"/>
  57.         <result property="lockId" column="lock_Id"/>
  58.         <result property="lockWeight" column="lock_Weight"/>
  59.         <result property="lockMoney" column="lock_Money"/>
  60.         <result property="lockRemark" column="lock_Remark"/>
  61.     </resultMap>
  62.     <!-- 一对一查询 SQL -->
  63.     <!--
  64.         在lock_表POJO中增加了 lockInfo对象
  65.         result property="lockInfo" column="id" select="lockNameSpace.getLockVsInfo"
  66.         用2次SQL查询实现 一对一 关联
  67.         也就是所谓“n+1”Select问题(如果lock表有十几万数据,而lockinfo表就几条数据问题)
  68.      -->
  69.     <select id="getLockVsInfo" resultMap="resultLockInfo">
  70.         select * from lock_info where lock_id = #value#
  71.     </select>
  72.     <!--
  73.     解决一对一的 n+1 问题 (待解决...)
  74.     <select id="getLockListOneToOne" parameterClass="int" resultMap="lockResult">
  75.         <![CDATA[
  76.             SELECT l.*,i.* FROM lock_ l,lock_info i WHERE l.id=i.lock_id and l.id=#value#
  77.         ]]>
  78.     </select>
  79.     -->
  80.     <!-- 一对多 (单向关联和双向关联)-->
  81.     <resultMap class="lockKey" id="keyResultMap">
  82.         <result property="id" column="id"/>
  83.         <result property="keyName" column="key_Name"/>
  84.         <result property="lockId" column="lock_Id"/>
  85.     </resultMap>
  86.     <select id="getKeyList" parameterClass="int" resultMap="keyResultMap">
  87.         select * from key_ where LOCK_ID = #id#
  88.     </select>
  89.     <!-- insert -->
  90.     <insert id="insertLock" parameterClass="lock">
  91.         <!-- 获取序列 -->
  92.         <selectKey keyProperty="id" resultClass="long">
  93.             SELECT pagetest_seq.nextval AS id FROM dual
  94.         </selectKey>
  95.         insert into lock_ (id,lock_Name,lock_Color,lock_Factory,lock_Type) values (#id#,#lockName#,#lockColor#,#lockFactory#,#lockType#)
  96.     </insert>
  97.     <!-- batch insert -->
  98.     <insert id="batchInsertLock" parameterClass="lock">
  99.         <!-- 获取序列 -->
  100.         <selectKey keyProperty="id" resultClass="long">
  101.             SELECT pagetest_seq.nextval AS id FROM dual
  102.         </selectKey>
  103.         insert into lock_ (id,lock_Name,lock_Color,lock_Factory,lock_Type) values (#id#,#lockName#,#lockColor#,#lockFactory#,#lockType#)
  104.     </insert>
  105.     <!-- delete -->
  106.     <delete id="deleteLock" parameterClass="int">
  107.         delete from lock_ where id =#id#
  108.     </delete>
  109.     <!-- update -->
  110.     <update id="updateLock" parameterClass="lock">
  111.         update lock_ set lock_name=#lockName# , lock_color=#lockColor# where id=#id#
  112.     </update>
  113.     <!-- page -->
  114.     <!-- batch -->
  115. </sqlMap>

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE sqlMapConfig
  3. PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
  4. "">

  5. <sqlMapConfig>
  6.     <settings
  7.         cacheModelsEnabled="true"
  8.         enhancementEnabled="true"
  9.         lazyLoadingEnabled="true"
  10.         maxRequests="32"
  11.         maxSessions="10"
  12.         maxTransactions="5"
  13.         useStatementNamespaces="true" />
  14.         
  15.     <transactionManager type="JDBC" commitRequired="false">
  16.        <dataSource type="SIMPLE">
  17.          <property name="JDBC.Driver" value="oracle.jdbc.OracleDriver"/>
  18.          <property name="JDBC.ConnectionURL"
  19.               value="jdbc:oracle:thin:@127.0.0.1:1521:ORCL"/>
  20.          <property name="JDBC.Username" value="SYSTEM"/>
  21.          <property name="JDBC.Password" value="suypower"/>
  22.        </dataSource>
  23.     </transactionManager>
  24.     
  25.     <sqlMap resource="com/isoftstone/cry/pojo/lock.xml"/>
  26. </sqlMapConfig>

  1. public class KeyBean implements Serializable{
  2.     private long id ;
  3.     private String keyName ;
  4.     private long lockId ;
  5. 。。。。。。。get、set

  6. ----------------------------
  7. public class LockBean implements Serializable{
  8.     private long id ; // 主键
  9.     private String lockName ; // 锁名称
  10.     private String lockColor ; // 锁颜色
  11.     private String lockFactory ; // 锁出厂公司
  12.     private Integer lockType ; // 锁类型 - 枚举字段 (用封装类型,允许null)
  13.     
  14.     private LockInfoBean lockInfo ; // 一对一
  15.     private List<KeyBean> keyList = new ArrayList<KeyBean>(); // 一对多

  16. get、set
  17. -----------------------------
  18. public class LockInfoBean implements Serializable{
  19.     private long id ;
  20.     private long lockId ;
  21.     private String lockWeight ;
  22.     private String lockMoney ;
  23.     private String lockRemark ;

阅读(3288) | 评论(0) | 转发(0) |
0

上一篇:ibatis 泛型dao设计

下一篇:资源预加载

给主人留下些什么吧!~~