MyBatis-Plus:创建动态数据源
简述工具类注意事项
简述
由于一些业务场景需要,我们可能需要使用MyBatis-Plus动态连接数据库,进行数据交互。
工具类
import com
.baomidou
.mybatisplus
.generator
.config
.DataSourceConfig
;
import com
.github
.pagehelper
.PageHelper
;
import com
.github
.pagehelper
.PageInterceptor
;
import org
.apache
.ibatis
.datasource
.pooled
.PooledDataSource
;
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
.TransactionFactory
;
import org
.apache
.ibatis
.transaction
.jdbc
.JdbcTransactionFactory
;
import javax
.sql
.DataSource
;
import java
.sql
.SQLException
;
import java
.util
.Properties
;
public class JdbcSessionFactory {
private static JdbcSessionFactory instance
= null
;
private SqlSessionFactory sqlSessionFactory
;
private DataSourceConfig dataSourceConfig
;
private JdbcSessionFactory() {
}
public static JdbcSessionFactory
tryGetInstance() {
if (instance
== null
) {
throw new RuntimeException();
}
return instance
;
}
public static JdbcSessionFactory
getInstance(String jdbcUrl
, String name
, String password
) {
if (instance
== null
) {
instance
= new JdbcSessionFactory();
instance
.createJdbcSessionFactory(jdbcUrl
, "com.mysql.jdbc.Driver", name
, password
);
}
return instance
;
}
public static Boolean
sqlTest(String jdbcUrl
, String name
, String password
) {
return sqlTest(jdbcUrl
, "com.mysql.jdbc.Driver", name
, password
);
}
public SqlSession
getSqlSession() {
return this.sqlSessionFactory
.openSession();
}
public DataSourceConfig
getDataSourceConfig() {
return this.dataSourceConfig
;
}
private Boolean
createJdbcSessionFactory(String jdbcUrl
, String driver
, String name
, String password
) {
if (!sqlTest(jdbcUrl
, "com.mysql.jdbc.Driver", name
, password
)) {
throw new RuntimeException("db connect time out");
}
dataSourceConfig
= new DataSourceConfig();
dataSourceConfig
.setDriverName(driver
);
dataSourceConfig
.setUrl(jdbcUrl
);
dataSourceConfig
.setUsername(name
);
dataSourceConfig
.setPassword(password
);
DataSource dataSource
=new PooledDataSource(driver
,jdbcUrl
,name
,password
);
TransactionFactory transactionFactory
= new JdbcTransactionFactory();
Environment environment
= new Environment("development", transactionFactory
, dataSource
);
Configuration configuration
= new Configuration(environment
);
configuration
.setDefaultStatementTimeout(8);
PageInterceptor pageInterceptor
= new PageInterceptor();
pageInterceptor
.plugin(getPageHelper());
configuration
.addInterceptor(pageInterceptor
);
configuration
.addMappers("com.cj.tools.mybatisplus.mapper");
SqlSessionFactory sqlSessionFactory
= new SqlSessionFactoryBuilder().build(configuration
);
this.sqlSessionFactory
= sqlSessionFactory
;
return true;
}
private PageHelper
getPageHelper(){
PageHelper pageHelper
=new PageHelper();
Properties properties
=new Properties();
properties
.setProperty("offsetAsPageNum", "true");
properties
.setProperty("rowBoundsWithCount", "true");
properties
.setProperty("reasonable", "true");
pageHelper
.setProperties(properties
);
return pageHelper
;
}
private static Boolean
sqlTest(String jdbcUrl
, String driver
, String name
, String password
) {
DataSource dataSource
=new PooledDataSource(driver
,jdbcUrl
,name
,password
);
try {
dataSource
.setLoginTimeout(5);
dataSource
.getConnection().getClientInfo();
} catch (SQLException e
) {
return false;
}
return true;
}
}
注意事项
1)如果进行多次连接,每次连接时注意进行sqlTest,否则可能出现连接不上问题。 2)分页插件如果不需要,可自行删除