1. 引入maven依赖
com.baomidou
mybatis-plus
3.5.6
com.baomidou
mybatis-plus-extension
3.5.6
com.zaxxer
HikariCP
4.0.3
mysql
mysql-connector-java
org.slf4j
slf4j-nop
1.7.2
org.projectlombok
lombok
1.18.32
provided
2. 编写初始化数据源单例 DataSourceInitializer
private static DataSourceInitializer instance = new DataSourceInitializer();
public static DataSourceInitializer getInstance() {
return instance;
}
private SqlSession session;
private DataSourceInitializer() {
try {
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//这是mybatis-plus的配置对象,对mybatis的Configuration进行增强
MybatisConfiguration configuration = new MybatisConfiguration();
//初始化配置
initConfiguration(configuration);
//这是初始化连接器,如mybatis-plus的分页插件
configuration.addInterceptor(initInterceptor());
//配置日志实现
configuration.setLogImpl(Slf4jImpl.class);
//扫描mapper接口所在包
configuration.addMappers("com.example.mapper");
//构建mybatis-plus需要的globalconfig
GlobalConfig globalConfig = GlobalConfigUtils.getGlobalConfig(configuration);
//此参数会自动生成实现baseMapper的基础方法映射
globalConfig.setSqlInjector(new DefaultSqlInjector());
//设置id生成器
globalConfig.setIdentifierGenerator(new DefaultIdentifierGenerator());
//设置超类mapper
globalConfig.setSuperMapperClass(BaseMapper.class);
//设置数据源
Environment environment = new Environment("1", new JdbcTransactionFactory(), initDataSource());
configuration.setEnvironment服务器托管(environment);
this.registryMapperXml(configuration, "mapper");
//构建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = builder.build(configuration);
//创建session
this.session = sqlSessionFactory.openSession();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
3. 初始化配置方法
/**
* 初始化配置
*
* @param configuration
*/
private void initConfiguration(MybatisConfiguration configuration) {
//开启驼峰大小写转换
configuration.setMapUnderscoreToCamelCase(true);
//配置添加数据自动返回数据主键
configuration.setUseGeneratedKeys(true);
}
/**
* 初始化数据源
*
* @return
*/
private DataSource initDataSource() {
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:sqlite:db/database.db");
dataSource.setDriverClassName("org.sqlite.JDBC");
dataSource.setIdleTimeout(60000);
dataSource.setAutoCommit(true);
dataSource.setMaximumPoolSize(5);
dataSource.setMinimumIdle(1);
dataSource.setMaxLifetime(60000 * 10);
dataSource.setConnectionTestQuery("SELECT 1");
return dataSource;
}
/**
* 初始化拦截器
*
* @return
*/
private Interceptor initInterceptor() {
//创建mybatis-plus插件对象
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//构建分页插件
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
paginationInnerInterceptor.setDbType(DbType.SQLITE);
paginationInnerInterceptor.setOverflow(true);
paginationInnerInterceptor.setMaxLimit(500L);
interceptor.addInnerInterceptor(paginationInnerInterceptor);
return interceptor;
}
/**
* 解析mapper.xml文件
*
* @param configuration
* @param classPath
* @throws IOException
*/
private void registryMapperXml(MybatisConfiguration configuration, String classPath) throws IOException {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
Enumeration mapper = contextClassLoader.getResources(classPath);
while (mapper.hasMoreElements()) {
URL url = mapper.nextElement();
if (url.getProtocol().equals("file")) {
String path = url.getPath();
File file = new File(path);
File[] files = file.listFiles();
for (File f : files) {
FileInputStream in = new FileInputStream(f);
XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(in, configuration, f.getPath(), configuration.getSqlFragments());
xmlMapperBuilder.parse();
in.close();
}
} else {
JarURLConnection urlConnection = (JarURLConnection) url.openConnection();
JarFile jarFile = urlConnection.getJarFile();
Enumeration entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry jarEntry = entries.nextElement();
if (jarEntry.getName().endsWith(".xml")) {
InputStream in = jarFile.getInputStream(jarEntry);
XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(in, configuration, jarEntry.getName(), configuration.getSqlFragments());
xmlMapperBuilder.parse();
in.close();
}
}
}
服务器托管 }
}
xml文件放在 resources/mapper 下面
4. 其他方法
/**
* 获取mapper类
* @param type
* @return
* @param
*/
public T getMapper(Class type) {
return this.session.getMapper(type);
}
/**
* 销毁数据库连接
*/
public void destroy() {
this.session.commit();
this.session.close();
}
5. 调用Mapper
UserMapper mapper = DataSourceInitializer.getInstance().getMapper(UserMapper .class);
List res= mapper.selectList(null);
System.out.println(res);
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
相关推荐: 应用Druid解析SQL获取查询表字段、参数信息
业务需求:输入一条SQL,解析出对应的结果表字段列表与参数列表。 select t.id,t.name,date_format(update_time,’%Y-%m’) as update_month from t_user t where t.account…