mybatisplus接入springcloud实例

最近构建项目,使用到了mybatisplus。

本来是用的easycode 生成了代码,后来发现按mybatis-plus的执行流程,easycode生成的代码目录并不适合,而后使用mybatis-plus自带的代码生成

代码如下,package生成还有些问题需要注意一下

代码生成器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.libary.libaryeurekamaterial;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Scanner;

public class CodeGenerator {
final static String dirPath = "E:\\project\\example-springcloud\\libary\\libary-eureka-material\\src\\main";


public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();

// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(dirPath);
gc.setAuthor("zhb");
gc.setFileOverride(true); //是否覆盖
gc.setOpen(false);//生成完成后不弹出文件框
gc.setControllerName("%sController");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");
// gc.setSwagger2(true); 实体属性 Swagger2 注解
mpg.setGlobalConfig(gc);

// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
dsc.setUrl("jdbc:mysql://localhost:3306/libary?useUnicode=true&useSSL=false&characterEncoding=utf8");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
mpg.setDataSource(dsc);




// 包配置
PackageConfig pc = new PackageConfig();

pc.setParent("java.com.libary");
pc.setModuleName("libaryeurekamaterial");
pc.setController("controller");
pc.setService("service");
pc.setServiceImpl("service.impl");
pc.setMapper("mapper");
pc.setEntity("entity");
pc.setXml("xml");
mpg.setPackageInfo(pc);

//
// 策略配置
StrategyConfig strategy = new StrategyConfig();
String hh="t_materials_kind,t_materials_kind_relevance,t_materials_tags";
strategy.setInclude( hh.split(","));
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setEntityBuilderModel(true);
strategy.setSuperControllerClass("com.libary.libaryeurekamaterial.controller.BaseController");
strategy.setSuperEntityClass("com.libary.libaryeurekamaterial.entity.BaseEntity");
mpg.setStrategy(strategy);

mpg.execute();
}
}

复杂查询

放一下maven的依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
!--mysql-plus支持-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>


<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.7.1</version>
<exclusions>
<exclusion>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>


<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.4.1</version>
<scope>compile</scope>
</dependency>

<!--sql打印-->
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>3.8.7</version>
</dependency>

由于mybatisplus的baseMapper已经吧大部分常用查询封装,所以我们日常开发的重心再与特殊的定制化sql语句.

这里我展示一下 一个复杂的分页查询,封装的就不放了

TMaterialsMapper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import com.libary.libaryeurekamaterial.entity.TMaterials;
import java.util.List;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.libary.libaryeurekamaterial.entity.vo.MaterialsViewVo;
import org.apache.ibatis.annotations.Select;

public interface TMaterialsMapper extends BaseMapper<TMaterials> {
@Select("select m.id, m.materials_name, m.file_name, m.size, m.materials_type, m.add_time," +
" m.materials_district, m.materials_preview, m.materials_md5," +
"mk.kind_name,mt.tags_name from t_materials m" +
" left join t_materials_kind_relevance kr " +
"on kr.materials_id = m.id " +
"left join t_materials_kind mk " +
"on kr.kind_id = mk.id " +
" left join t_materials_tags mt on kr.tags_id = mt.id")
List<MaterialsViewVo> getMaterialsPageList(Page page);
}

这里直接用select注解 xml 拜拜了您嘞

TMaterialsService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import com.libary.libaryeurekamaterial.entity.TMaterials;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.libary.libaryeurekamaterial.entity.vo.MaterialsViewVo;

public interface TMaterialsService extends IService<TMaterials> {

/**
* 分页接口
* @param page
* @return
*/
Page<MaterialsViewVo> getMateroalsPageList(Page<MaterialsViewVo> page);
}

TMaterialsServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import com.libary.libaryeurekamaterial.entity.TMaterials;
import com.libary.libaryeurekamaterial.mapper.TMaterialsMapper;
import com.libary.libaryeurekamaterial.service.TMaterialsService;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.libary.libaryeurekamaterial.entity.vo.MaterialsViewVo;
import org.springframework.stereotype.Service;

@Service
public class TMaterialsServiceImpl extends ServiceImpl<TMaterialsMapper, TMaterials> implements TMaterialsService {

@Override
public Page<MaterialsViewVo> getMateroalsPageList(Page<MaterialsViewVo> page) {
return page.setRecords(this.baseMapper.getMaterialsPageList(page));
}



}

Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* 分页接口-web
* @param materialsViewVo
* @param offset
* @param limit
* @return
*/
@GetMapping("/getMaterialsPageList")
public AjaxResult getMaterialsListPage(@RequestBody MaterialsViewVo materialsViewVo){

AjaxResult ajaxResult = AjaxResult.success();

try{
Page<MaterialsViewVo> page = new Page<>(materialsViewVo.getOffset(), materialsViewVo.getLimit());

Page<MaterialsViewVo> materialsViewVoPage = tMaterialsService.getMateroalsPageList(page);

ajaxResult = AjaxResult.success(materialsViewVoPage);

}catch (Exception e){
logger.error("查询出错",e);
ajaxResult = AjaxResult.error("数据查询出错");
}

return ajaxResult;
}

分页直接去使用mybatisplus自带的分页插件 将offset和limit注入自动完成查询,关于条件,只需要注入到MaterialsViewVo 条件查询类 他自动判断参数是否null 进而去组合条件

sql打印

利用了 p6spy实现

https://mp.baomidou.com/guide/p6spy.html

只适合开发环境 上线不适合

主要的spy.properties文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory
# 自定义日志打印
logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger
#日志输出到控制台
appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger
# 使用日志系统记录 sql
#appender=com.p6spy.engine.spy.appender.Slf4JLogger
# 设置 p6spy driver 代理
deregisterdrivers=true
# 取消JDBC URL前缀
useprefix=true
# 配置记录 Log 例外,可去掉的结果集有error,info,batch,debug,statement,commit,rollback,result,resultset.
excludecategories=info,debug,result,commit,resultset
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 实际驱动可多个
#driverlist=org.h2.Driver
# 是否开启慢SQL记录
outagedetection=true
# 慢SQL记录标准 2 秒
outagedetectioninterval=2

文档直接上官网看就行,不是外网的。


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!