在使用Mybatis-plus过程中,有许多步骤不是很明确,需要整理下,留着日后自用
1.依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
2.配置Config
如下是3.4+版本的新版配置项
@Configuration
public class MybatisPlusConfig {
//分页查询 mybatis-plus
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
paginationInnerInterceptor.setDbType(DbType.MYSQL);
paginationInnerInterceptor.setOverflow(true);
interceptor.addInnerInterceptor(paginationInnerInterceptor);
return interceptor;
}
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(false);
}
}
datasource配置
@Configuration
@MapperScan(basePackages = MysqlDataSource.PACKAGE_NAME ,sqlSessionFactoryRef = "streamBookSqlSessionFactory")
public class MysqlDataSource {
static final String PACKAGE_NAME="com.xxxx.xxx.dao";
private static final String STREAMBOOK_MAPPER_LOCATION = "classpath:mappers/*.xml";
@Autowired
private MybatisPlusInterceptor mybatisPlusInterceptor;
@Primary
@Bean("streamBookDataSourceProperties")
@Qualifier("streamBookDataSourceProperties")
@ConfigurationProperties(prefix = "spring.datasource.mysql")
public DataSourceProperties streamBookDataSourceProperties() {
return new DataSourceProperties();
}
@Primary
@Bean("streamBookDataSource")
@Qualifier("streamBookDataSource")
@ConfigurationProperties(prefix = "spring.datasource.mysql")
public DataSource streamBookDataSource() {
return new DruidDataSource();
}
@Primary
@Bean("streamBookTransactionManager")
@Qualifier("streamBookTransactionManager")
public DataSourceTransactionManager streamBookTransactionManager(@Qualifier("streamBookDataSource")
DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean("streamBookSqlSessionFactory")
@Qualifier("streamBookSqlSessionFactory")
public SqlSessionFactory streamBookSqlSessionFactory(@Qualifier("streamBookDataSource") DataSource dataSource)
throws Exception {
final MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
initMybatisPlusConfiguration(sqlSessionFactoryBean);
sqlSessionFactoryBean.setDataSource(dataSource);
//分页插件注册
sqlSessionFactoryBean.setPlugins(mybatisPlusInterceptor);
sqlSessionFactoryBean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources(STREAMBOOK_MAPPER_LOCATION));
return sqlSessionFactoryBean.getObject();
}
private void initMybatisPlusConfiguration(MybatisSqlSessionFactoryBean factoryBean) {
// mybatis plus 配置, 参见{@link https://mp.baomidou.com/config/#configuration-2}
MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setMapUnderscoreToCamelCase(true);
factoryBean.setConfiguration(configuration);
// 全局默认,参见{@link https://mp.baomidou.com/config/#dbconfig-2}
GlobalConfig globalConfig = new GlobalConfig();
GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
dbConfig.setIdType(IdType.AUTO);
globalConfig.setDbConfig(dbConfig);
factoryBean.setGlobalConfig(globalConfig);
}
@Primary
@Bean("streamBookSqlSessionTemplate")
@Qualifier("streamBookSqlSessionTemplate")
public SqlSessionTemplate streamBookSqlSessionTemplate(@Qualifier("streamBookSqlSessionFactory")
SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
3.代码编写
mapper.xml
<select id="selectUserPage" resultType="com.xx.xxx.model.pojo.xxxx">
select
<include refid="Base_Column_List"/>
from t_data_exploration_info ${ew.customSqlSegment}
</select>
dao.java
//自定义sql分页
IPage<xxx> selectUserPage(Page<xxx> page, @Param(Constants.WRAPPER) Wrapper<xxx> wrapper);
impl.java
Page<xxx> infoPage = new Page<>(currentPage, pageSize);
QueryWrapper<xxx> queryWrapper = new QueryWrapper<>();
if (StringUtils.isNotEmpty(expName)){
if (expName.matches("\\d+")){
long id = Long.parseLong(expName);
queryWrapper.eq("id", id);
}else {
queryWrapper.eq("exploration_name", expName);
}
}
if (StringUtils.isNotEmpty(dataSource)){
queryWrapper.eq("data_source",dataSource);
}
if (StringUtils.isNotEmpty(userId)){
queryWrapper.eq("user_id",userId);
}
IPage<xxx> xxxx = mapper.selectUserPage(infoPage, queryWrapper);
System.out.println("总条数: " + xxx.getTotal());
System.out.println("数据条数: " + xxx.getRecords());
xxVo xxvo = new xxVo();
xxvo.setCurrentPage(xx.getCurrent());
xxvo.setPageSize(xx.getSize());
xxvo.setTotalSize(xx.getPages());
xxvo.setRecords(xx.getTotal());
xxvo.setInfoList(xxx.getRecords());
return xxvo;
4.结果