ibatis 動態傳入表名

項目中須要根據不一樣的表名和列名生成不一樣的select語句,我想ibatis這裏也應該是用預編譯的形式存儲???? java

不太瞭解具體原理,反正我知道在用oci庫的時候,是能夠的;用sprinf("sql","xx","xx"),一下,就能夠了。oci庫確定是用預編譯的形式乾的! sql

因而,我上網查了查,感受相似的問題有,可沒給出一個完整的例子。想了一想,既然你能動態傳入map,map裏面存儲了相應的查詢值,那你 app

ibatis確定也能傳入其餘元數據信息吧。測試了一下,能夠!OK,搞定了:) 測試

很少說了,代碼以下 spa

[java] view plain copy
  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> 
相關文章
相關標籤/搜索