本文共计256个文字,预计阅读时间需要2分钟。

Mybatis框架+封装的工具类+主要功能是调用接口和配置数据库建立连接+public class MyHelper { // 将属性私有化 private SqlSessionFactory sf; private SqlSession ss; private Class e; public MyHelper() { InputStream i=; // ... } }

mybatis框架 封装的工具类 主要功能是调用接口和配置和数据库建立联系
public class MyHelper
{
//把属性私用
private SqlSessionFactory sf;
private SqlSession ss;
private Class e;
public MyHelper(){
InputStream i = null;
try {
i = Resources.getResourceAsStream("mybatis-config.xml");
this.sf = new SqlSessionFactoryBuilder().build(i);
this.ss = this.sf.openSession();
} catch (IOException e) {
e.printStackTrace();
}
}
public SqlSessionFactory getSqlSessionFactory(){
return this.sf;
}
public Connection getConn(){
return this.ss.getConnection();
}
public MyHelper(Class e){
InputStream i = null;
try {
i = Resources.getResourceAsStream("mybatis-config.xml");
this.sf = new SqlSessionFactoryBuilder().build(i);
this.ss = this.sf.openSession();
this.e = e;
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
//获取session
public SqlSession getSession(){
return this.ss;
}
//无参数是调用的配置走的 StudentMapper.xml配置
public E getMapper(){
return (E)this.ss.getMapper(this.e);
}
//有参的是调用的配置走的 StudentMapper接口
public E getMapper(Class c){
return (E)this.ss.getMapper(c);
}
//先提交后关闭session回话,之后无法再吊方法
public void close(){
if(this.ss!=null){
this.ss.commit();
this.ss.close();
}
}
}