(总结)ibatis 动态传入表名和列名

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

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

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

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

不多说了,代码如下

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 map = new HashMap(); // 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   

你可能感兴趣的:(iBatis)