标签:cep == trace turn final actor cal UI factory
基于Mybatis框架
特点:给予MyBatis框架半成品 站在巨人肩膀上 连接访问数据库,封装成工具类 方便调用连接 减少代码的冗余 提高效率 支持多线程并发访问
public class BatisUtil {
private static final ThreadLocal<SqlSession> thl = new ThreadLocal<SqlSession>();
private static SqlSessionFactoryBuilder builder =null;
private static Reader reader =null;
//静态读取代码块mybatils的核心配置文件
static {
try {
// 1 builder
builder = new SqlSessionFactoryBuilder();
// 2 factory
reader = Resources.getResourceAsReader("mybatis-config.xml");
} catch (Exception e) {
e.printStackTrace();
}
}
public static SqlSession getSession(){
SqlSession session =null;
try {
session=thl.get();
if(session==null){
SqlSessionFactory factory = builder.build(reader);
//3 获取session
session = factory.openSession();
thl.set(session);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return session;
}
public static void release(SqlSession session){
i f(session!=null){
session.close();
thl.remove();
}
}
链接别的数据库只需要更改相应的配置文件即可 不需要再重新更改源码文件
例如:MySQL
URL:“jdbc:mysql://localhost:3306/userdb”
dirver: com.mysql.jdbc.Driver
SQL Server 2008
URL : "jdbc:sqlserver://localhost:1433"
driver:com.microsoft.jdbc.sqlserver.SQLServerDriver
标签:cep == trace turn final actor cal UI factory
原文地址:https://www.cnblogs.com/nicklin/p/9000107.html