总结)ibatis 动态传入表名和列名

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

项目中需要根据不同的表名和列名生成不同的select语句,我想ibatis这里也应该是用预编译的形式存储????

不太了解具体原理,反正我知道在用oci库的时候,是可以的;用sprinf("sql","xx","xx"),一下,就可以了。oci库肯定是用预编译的形式干的!

于是,我上网查了查,感觉类似的问题有,可没给出一个完整的例子。想了一想,既然你能动态传入map,map里面存储了相应的查询值,那你

ibatis肯定也能传入其他元数据信息吧。测试了一下,可以!OK,搞定了:)

不多说了,代码如下

  1. Java code :   
  2. // just a releation to the table stored in db  
  3. public class Test {  
  4.     public String name;  
  5.     public Timestamp date;  
  6.     public int id;  
  7. }  
  8. public class TestInsert {  
  9.    // logger class you can replace it to System.out.println  
  10.    static Log logger = LogFactory.getLog(TestInsert.class);  
  11.   
  12.    public static void main(String[] args) {  
  13.        /* 
  14.         * Test test = new Test(); test.date = new 
  15.         * Timestamp(System.currentTimeMillis()); test.name = "fffff"; try { 
  16.         * long num = (Long) EntityManager.getSqlMapper().insertArgs( 
  17.         * "insertOperation", "fffff", new 
  18.         * Timestamp(System.currentTimeMillis())); logger.info("ID is " + num); 
  19.         * } catch (SQLException e) { e.printStackTrace(); } 
  20.         */  
  21.        // try the dynamic table dealation  
  22.        HashMap<String, Object> map = new HashMap<String, Object>();  
  23.        // set the query value   
  24.        map.put("ID""dizhuang");  
  25.        // set the col1 to be selected  
  26.        map.put("col1""*");  
  27.          // set the table name  
  28.        map.put("tablePrefix""testsocevent");  
  29.        // set the col name which you use  
  30.          map.put("col2""NAME");  
  31.        // map.put("ID", 1000);  
  32.        // map.put("id", "1005");  
  33.        try {  
  34.            // why args is error?  
  35.            Test test = (Test) EntityManager.getSqlMapper().queryForObject(  
  36.                "getTest", map);  
  37.            logger.info("id : " + test.id);  
  38.            logger.info("time :" + test.date);  
  39.            logger.info("name : " + test.name);  
  40.        } catch (SQLException e) {  
  41.            logger.error(e.getMessage(), e);  
  42.            // e.printStackTrace();  
  43.        }  
  44.    }  
  45.   
  46. // ibatis sql  
  47. <select id="getTest" parameterClass="java.util.HashMap"  
  48. resultClass="com.neusoft.soc.eventcenter.test.Test"  
  49. remapResults="true">  
  50. SELECT  
  51.      $col1$  
  52. FROM  
  53.      $tablePrefix$  
  54. WHERE  
  55.     $col2$ = #ID#  
  56.  </select>  
  Java code :   // just a releation to the table stored in db  public class Test {      public String name;      public Timestamp date;      public int id;  }  public class TestInsert {     // logger class you can replace it to System.out.println     static Log logger = LogFactory.getLog(TestInsert.class);      public static void main(String[] args) {         /*          * Test test = new Test(); test.date = new          * Timestamp(System.currentTimeMillis()); test.name = "fffff"; try {          * long num = (Long) EntityManager.getSqlMapper().insertArgs(          * "insertOperation", "fffff", new          * Timestamp(System.currentTimeMillis())); logger.info("ID is " + num);          * } catch (SQLException e) { e.printStackTrace(); }          */         // try the dynamic table dealation         HashMap<String, Object> map = new HashMap<String, Object>();         // set the query value          map.put("ID", "dizhuang");         // set the col1 to be selected         map.put("col1", "*");           // set the table name         map.put("tablePrefix", "testsocevent");         // set the col name which you use           map.put("col2", "NAME");         // map.put("ID", 1000);         // map.put("id", "1005");         try {             // why args is error?             Test test = (Test) EntityManager.getSqlMapper().queryForObject(                 "getTest", map);             logger.info("id : " + test.id);             logger.info("time :" + test.date);             logger.info("name : " + test.name);         } catch (SQLException e) {             logger.error(e.getMessage(), e);             // e.printStackTrace();         }     } } // ibatis sql <select id="getTest" parameterClass="java.util.HashMap"  resultClass="com.neusoft.soc.eventcenter.test.Test"  remapResults="true">  SELECT      $col1$  FROM      $tablePrefix$  WHERE      $col2$ = #ID#  </select>

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

版权声明:本文为hfrujhv原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。