MyBatis之SessionFactory与Session简介说明
下文笔者讲述MyBatis中SessionFactory与Session简介说明,如下所示
SessionFactory简介
SessionFactory是一个产生session的工厂
SessionFactory简介
public interface SqlSessionFactory { SqlSession openSession(); SqlSession openSession(boolean autoCommit); SqlSession openSession(Connection connection); SqlSession openSession(TransactionIsolationLevel level); SqlSession openSession(ExecutorType execType); SqlSession openSession(ExecutorType execType, boolean autoCommit); SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level); SqlSession openSession(ExecutorType execType, Connection connection); Configuration getConfiguration(); }
SessionFactory模拟实现
public interface SessionFactory { Session openSession(); } 使用XMLConfigBuilder只需将文件名传给它 然后就会生成一个DataSource public class VSessionFactory implements SessionFactory { private DataSource config; public VSessionFactory(String resource) { config = XMLConfigBuilder.build(resource); } public Session openSession() { return new VSession(config); } } //测试代码 public static void main(String args[]) throws Exception { SessionFactory factory = new VSessionFactory("config.xml"); Session session = factory.openSession(); session.exec("select * from users where id>8"); }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。