(總結)ibatis 動態傳入表名和列名

來源:互聯網
上載者:User

項目中需要根據不同的表名和列名產生不同的select語句,我想ibatis這裡也應該是用先行編譯的形式儲存????

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

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

ibatis肯定也能傳入其他中繼資料資訊吧。測試了一下,可以!OK,搞定了:)

不多說了,代碼如下

Java code :<br /> // just a releation to the table stored in db<br /> public class Test {<br /> public String name;<br /> public Timestamp date;<br /> public int id;<br /> }<br /> public class TestInsert {<br /> // logger class you can replace it to System.out.println<br /> static Log logger = LogFactory.getLog(TestInsert.class);</p><p> public static void main(String[] args) {<br /> /*<br /> * Test test = new Test(); test.date = new<br /> * Timestamp(System.currentTimeMillis()); test.name = "fffff"; try {<br /> * long num = (Long) EntityManager.getSqlMapper().insertArgs(<br /> * "insertOperation", "fffff", new<br /> * Timestamp(System.currentTimeMillis())); logger.info("ID is " + num);<br /> * } catch (SQLException e) { e.printStackTrace(); }<br /> */<br /> // try the dynamic table dealation<br /> HashMap<String, Object> map = new HashMap<String, Object>();<br /> // set the query value<br /> map.put("ID", "dizhuang");<br /> // set the col1 to be selected<br /> map.put("col1", "*");<br /> // set the table name<br /> map.put("tablePrefix", "testsocevent");<br /> // set the col name which you use<br /> map.put("col2", "NAME");<br /> // map.put("ID", 1000);<br /> // map.put("id", "1005");<br /> try {<br /> // why args is error?<br /> Test test = (Test) EntityManager.getSqlMapper().queryForObject(<br /> "getTest", map);<br /> logger.info("id : " + test.id);<br /> logger.info("time :" + test.date);<br /> logger.info("name : " + test.name);<br /> } catch (SQLException e) {<br /> logger.error(e.getMessage(), e);<br /> // e.printStackTrace();<br /> }<br /> }<br /> }<br />  // ibatis sql<br />  <select id="getTest" parameterClass="java.util.HashMap"<br />   resultClass="com.neusoft.soc.eventcenter.test.Test"<br />   remapResults="true"><br />   SELECT<br />   $col1$<br />   FROM<br />   $tablePrefix$<br />   WHERE<br /> $col2$ = #ID#<br />   </select>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.