1 public class MyBatisUtil { 2 private static SqlSessionFactory factor = null; 3 4 static { 5 try { 6 factor = new SqlSessionFactoryBuilder() 7 .build(Resources.getResourceAsStream("mybatis.xml")); 8 } catch (IOException e) { 9 e.printStackTrace(); 10 } 11 } 12 13 public static SqlSession getSession(){ 14 SqlSession session = null; 15 if(factor!=null) { 16 session = factor.openSession(); 17 } 18 return session; 19 } 20 21 public static void commit(int num,SqlSession session){ 22 if(num>0){ 23 // 提交事务 24 session.commit(); 25 System.out.println("SUCCESS!"); 26 }else { 27 // 回滚事务 28 session.rollback(); 29 System.out.println("FAILED!"); 30 } 31 } 32 }