iBATIS SQLMap API编程如何进行呢?首先我们配置好sqlmap-config.xml文件以后,需要对他进行解析,例如:
iBATIS SQLMap的Java代码
- public void insert(Person p){
- String xml = "com/Jdnis/ibatis/map/sql-map-config.xml";
- SqlMapClient sqlMap = null;
- try {
- Reader reader = Resources.getResourceAsReader(xml);
- sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
- sqlMap.startTransaction();
- sqlMap.insert("insertPerson",p);
- sqlMap.commitTransaction();
- System.out.println("数据插入成功");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- System.out.println("主键 "+p.getId()+" 冲突!无法插入数据");
- }finally{
- try {
- sqlMap.endTransaction();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
iBATIS SQLMap API编程批处理:
Java代码
- sqlMap.startBatch();
- //...execute statements in between
- sqlMap.executeBatch();
iBATIS SQLMap API编程就讲解到这里,希望对你有所帮助。
【编辑推荐】