public static getSqlSessionFactory() {
try {
FileInputStream in = new FileInputStream("xx.xml");//我的xml是动态生成的,如果你的是内置的,自己百度吧
UnpooledDataSource ds= new UnpooledDataSource();
ds.setUrl(AppCoreInitService.mysql_url);
ds.setUsername(Router.decrypt(AppCoreInitService.mysql_name));//解密
ds.setPassword(Router.decrypt(AppCoreInitService.mysql_pass));//把解密之后设置给ds
ds.setDefaultNetworkTimeout(30000);
ds.setDriver("com.mysql.cj.jdbc.Driver");
Environment env = new Environment("development", new JdbcTransactionFactory(), ds);
Reader cfgReader = new InputStreamReader(in);
XMLConfigBuilder parser = new XMLConfigBuilder(cfgReader, null, null);
Configuration cfg = parser.parse();
cfg.setEnvironment(env);
SqlSessionFactory f = new SqlSessionFactoryBuilder().build(cfg);
return f;
} catch (Exception e) {
logger.error("GetSqlSessionFactor,[" + mark + "] " + resource + " , error=" + CommonUtils.getStackTrace(e)
+ "!");
}
return null;
}
使用:
SqlSession session = getSqlSessionFactory().openSession(true);
maper = session.getMapper(Mapers.class);
|