cn.khthink.easyapi.kit.database.mybatis.EasyMybatis Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of EasyApi Show documentation
Show all versions of EasyApi Show documentation
A RESTFUL Framework for JavaWeb
The newest version!
package cn.khthink.easyapi.kit.database.mybatis;
/*
Create by KH at 2017/12/4 13:23
*/
import cn.khthink.easyapi.config.CoreConfig;
import cn.khthink.easyapi.kit.database.hikari.Hikari;
import cn.khthink.easyapi.kit.EasyLogger;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import java.io.IOException;
import java.io.Reader;
/**
* Mybatis
*
* @author kh
*/
public class EasyMybatis {
private SqlSessionFactory sqlSessionFactory;
private static class Mybatis {
private static EasyMybatis instance = new EasyMybatis();
}
private EasyMybatis() {
}
public static EasyMybatis getInstance() {
return Mybatis.instance;
}
public void init() throws IOException {
if ("".equals(CoreConfig.mybatisConfig)) {
initDefault();
} else {
Reader reader = Resources.getResourceAsReader(CoreConfig.mybatisConfig);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
EasyLogger.info("--->初始化Mybatis配置");
}
for (String mapper : CoreConfig.mappers) {
addMappers(mapper);
}
}
private void initDefault() {
Configuration configuration = new Configuration();
JdbcTransactionFactory jdbcTransactionFactory = new JdbcTransactionFactory();
Environment development = new Environment("development", jdbcTransactionFactory, Hikari.getInstance().getDataSource());
configuration.setEnvironment(development);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
EasyLogger.info("--->初始化默认Mybatis配置");
}
/**
* 添加mapper包
*
* @param packageName 包名
*/
public void addMappers(String packageName) {
if (sqlSessionFactory != null) {
sqlSessionFactory.getConfiguration().addMappers(packageName);
}
}
/**
* 添加单个mapper
*
* @param type 类型
* @param 泛型
*/
public void addMapper(Class type) {
if (sqlSessionFactory != null) {
sqlSessionFactory.getConfiguration().addMapper(type);
}
}
/**
* 获取SessionFactory
*
* @return SqlSessionFactory
*/
public SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
/**
* 获取sqlsession
*
* @param isAutoComit 是否自动提交
* @return SqlSession
*/
public SqlSession getSqlSession(boolean isAutoComit) {
return sqlSessionFactory.openSession(isAutoComit);
}
/**
* 获取sqlsession(自动提交)
*
* @return SqlSession
*/
public SqlSession getSqlSession() {
return getSqlSession(true);
}
}