項目中需要根據不同的表名和列名產生不同的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>