項目中須要根據不一樣的表名和列名生成不一樣的select語句,我想ibatis這裏也應該是用預編譯的形式存儲???? java
不太瞭解具體原理,反正我知道在用oci庫的時候,是能夠的;用sprinf("sql","xx","xx"),一下,就能夠了。oci庫確定是用預編譯的形式乾的! sql
因而,我上網查了查,感受相似的問題有,可沒給出一個完整的例子。想了一想,既然你能動態傳入map,map裏面存儲了相應的查詢值,那你 app
ibatis確定也能傳入其餘元數據信息吧。測試了一下,能夠!OK,搞定了:) 測試
很少說了,代碼以下 spa
- 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>