Based on the MyBatis framework
Features: Give MyBatis frame semi-finished products stand on Giants shoulders connect Access database, encapsulate as tool class easy to call connection reduce code redundancy increase efficiency support multithreading concurrent access
Public class Batisutil {
Private Static Final threadlocal<sqlsession> THL = new threadlocal<sqlsession> ();
Private Static Sqlsessionfactorybuilder Builder =null;
Private Static Reader Reader =null;
Static read code block mybatils core configuration file
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 Getting 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 ();
}
}
linking other databases requires only a more Change the corresponding configuration file can not need to re-modify the source file
For example: 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
Link database based on MyBatis framework