我有一个项目,采用的maven多模块,每一个模块都是springboot2 + mybatis + PageHelper。结构如下:
---abc项目
--------admin模块:[dao,service,mapper.xml; 系统配置相关操作]
--------common模块:[util,entity,提供所有实体类]
--------serviceA模块:[dao,service,mapper.xml; 业务A相关操作]
--------serviceB模块:[dao,service,mapper.xml; 业务B相关操作]
--------web模块:[页面展示]
下面是admin模块的数据方面的配置:
@Configuration
public class Admin_MyBatisCfg {
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// 分页插件
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("reasonable", "true");
properties.setProperty("supportMethodsArguments", "true");
properties.setProperty("returnPageInfo", "check");
properties.setProperty("params", "count=countSql");
pageHelper.setProperties(properties);
// 添加插件
bean.setPlugins(new Interceptor[] { pageHelper });
// 添加XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
bean.setConfigLocation(resolver.getResource("classpath:mybatis-conf.xml"));
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Bean(name = "sqlSession")
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean("admin_mapper_scanner_cfg)")
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer config = new MapperScannerConfigurer();
config.setBasePackage("com.sdcm.admin.dao");
return config;
}
}
server:
port: 10002
spring:
datasource:
name: pmpds
url: jdbc:mysql://192.168.0.241:3306/pmpmain?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
username: sdcm
password: Sdcm_123456
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
maxOpenPreparedStatements: 20
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.sdcm.common.entity
mapper:
mappers: com.sdcm.admin.dao
not-empty: false
identity: MYSQL
#pagehelper
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
returnPageInfo: check
现在遇到一些问题:
1. 非微服务,我这样分maven模块合理吗?
2. datasource,SqlSessionFactory是不是在整个abc项目里同一时刻只能存在一份?
3. 我的配置是否正确?