ibatis 动态传入表名

原文链接:https://my.oschina.net/u/1398304/blog/372613

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

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

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

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

不多说了,代码如下

[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> 

转载于:https://my.oschina.net/u/1398304/blog/372613

展开阅读全文
博主设置当前文章不允许评论。

求助,ibatis动态表及参数传入

11-02

网上看到这么一个帖子http://blog.csdn.net/a9529lty/article/details/6000726rn内容如下:rnJava code : rn// just a releation to the table stored in db rnpublic class Test rn public String name; rn public Timestamp date; rn public int id; rn rnpublic class TestInsert rn // logger class you can replace it to System.out.println rn static Log logger = LogFactory.getLog(TestInsert.class); rn rn public static void main(String[] args) rn /* rn * Test test = new Test(); test.date = new rn * Timestamp(System.currentTimeMillis()); test.name = "fffff"; try rn * long num = (Long) EntityManager.getSqlMapper().insertArgs( rn * "insertOperation", "fffff", new rn * Timestamp(System.currentTimeMillis())); logger.info("ID is " + num); rn * catch (SQLException e) e.printStackTrace(); rn */ rn // try the dynamic table dealation rn HashMap map = new HashMap(); rn // set the query value rn map.put("ID", "dizhuang"); rn // set the col1 to be selected rn map.put("col1", "*"); rn // set the table name rn map.put("tablePrefix", "testsocevent"); rn // set the col name which you use rn map.put("col2", "NAME"); rn // map.put("ID", 1000); rn // map.put("id", "1005"); rn try rn // why args is error? rn Test test = (Test) EntityManager.getSqlMapper().queryForObject( rn "getTest", map); rn logger.info("id : " + test.id); rn logger.info("time :" + test.date); rn logger.info("name : " + test.name); rn catch (SQLException e) rn logger.error(e.getMessage(), e); rn // e.printStackTrace(); rn rn rn rn// ibatis sql rn rn  SELECT rn   $col1$ rn  FROM rn   $tablePrefix$ rn  WHERE rn $col2$ = #ID# rn rnrn如果我的map里面value是一个对象,如:rnpublic class Userrnprivate int id;rnprivate String name;rnrn那么Ibatis的sql怎么写呢?rn如果是value是List users;rn那么Ibatis的sql怎么写呢?rn谢谢! 论坛

求助,ibatis动态表及参数传入问题,在线等

11-04

网上看到这么一个帖子http://blog.csdn.net/a9529lty/article/details/6000726rn内容如下:rnJava code : rn// just a releation to the table stored in db rnpublic class Test rn public String name; rn public Timestamp date; rn public int id; rn rnpublic class TestInsert rn // logger class you can replace it to System.out.println rn static Log logger = LogFactory.getLog(TestInsert.class); rn rn public static void main(String[] args) rn /* rn * Test test = new Test(); test.date = new rn * Timestamp(System.currentTimeMillis()); test.name = "fffff"; try rn * long num = (Long) EntityManager.getSqlMapper().insertArgs( rn * "insertOperation", "fffff", new rn * Timestamp(System.currentTimeMillis())); logger.info("ID is " + num); rn * catch (SQLException e) e.printStackTrace(); rn */ rn // try the dynamic table dealation rn HashMap map = new HashMap(); rn // set the query value rn map.put("ID", "dizhuang"); rn // set the col1 to be selected rn map.put("col1", "*"); rn // set the table name rn map.put("tablePrefix", "testsocevent"); rn // set the col name which you use rn map.put("col2", "NAME"); rn // map.put("ID", 1000); rn // map.put("id", "1005"); rn try rn // why args is error? rn Test test = (Test) EntityManager.getSqlMapper().queryForObject( rn "getTest", map); rn logger.info("id : " + test.id); rn logger.info("time :" + test.date); rn logger.info("name : " + test.name); rn catch (SQLException e) rn logger.error(e.getMessage(), e); rn // e.printStackTrace(); rn rn rn rn// ibatis sql rn rn  SELECT rn   $col1$ rn  FROM rn   $tablePrefix$ rn  WHERE rn $col2$ = #ID# rn rnrn如果我的map里面value是一个对象,如:rnpublic class Userrnprivate int id;rnprivate String name;rnrn那么Ibatis的sql怎么写呢?rn如果是value是List users;rn那么Ibatis的sql怎么写呢?rn谢谢! 论坛

没有更多推荐了,返回首页