# SpringBoot-MybatisPlus **Repository Path**: xiaoxiunique/SpringBoot-MybatisPlus ## Basic Information - **Project Name**: SpringBoot-MybatisPlus - **Description**: No description available - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2018-08-04 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # SpringBoot-MybatisPlus **项目介绍** 使用 个人博客后端: SpringBoot + MybatisPlus + MySql [小西博客](http://www.atom.kim) #### 1.MySql数据源配置 ```yaml # MYSQL DATABASE CONFIG datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://www.atom.kim:3306/xiaoxi?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&serverTimezone=GMT username: root password: Welcome_1 type: com.alibaba.druid.pool.DruidDataSource ``` #### 2.MybatisPlus 配置(官方推荐配置) ```yaml #mybatis mybatis-plus: mapper-locations: classpath:/mapper/*Mapper.xml #实体扫描,多个package用逗号或者分号分隔 typeAliasesPackage: com.xx.springboot.entity; com.xx.springboot.Vo # typeEnumsPackage: com.xx.springboot.entity.enums global-config: # 数据库相关配置 db-config: #主键类型 AUTO:"数据库ID自增", INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID"; id-type: auto #字段策略 IGNORED:"忽略判断",NOT_NULL:"非 NULL 判断"),NOT_EMPTY:"非空判断" field-strategy: not_empty #驼峰下划线转换 column-underline: true #数据库大写下划线转换 #capital-mode: true #逻辑删除配置 logic-delete-value: 0 logic-not-delete-value: 1 db-type: mysql #刷新mapper 调试神器 refresh: true # 原生配置 configuration: map-underscore-to-camel-case: true cache-enabled: false ``` #### 3.Excel 导出 **http://localhost:8889/export** 程序入口: ExportController ##### excel 导出步骤 - 在com.xx.springboot.export.tpl 文件下, 创建要导出数据的对应的 xls模板文件, 与tpl文件 - 在ExportController 中 查询到将要导出的数据集合 - 定义好输出文件名字, 模板对应的名字, 然后调用工具类方法 ##### 导出Excel的几个关键类文件 - com.xx.springboot.utils.XlsUtils - 加载配置文件, 读取配置文件信息 - com.xx.springboot.controller.ExportController - 控制层方法, 对外提供调用接口 - com.xx.springboot.export.tpl - 要导出文件的配置文件 #### 4.Shiro + VUE + SpringBoot 前后端分离跨域问题 **解决办法** - 前端请求axios设置 ```js //浏览器请求 携带 cookie axios.defaults.withCredentials = true ``` - SpringBoot 配置跨域请求头 ```java /** *
* 配置跨域访问 *
*/ @Configuration public class WebCorsConfig { @Bean public WebMvcConfigurer corsConfigurer(){ return new WebMvcConfigurerAdapter() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedHeaders("*") .allowedMethods("*") .allowCredentials(true) .allowedOrigins("*"); } }; } } ``` #### 5.jenkins自动化部署 解决jenkins启动完毕后, 会自动删除掉所有的衍生进程, 导致spring boot应用通过 `nohup java -jar project.jar > log &` 命令jenkins自动化部署之后, 会被删除. **解决方案** 在Exec Shell上方添加如下代码 ``` BUILD_ID=DONTKILLME ``` #### 6.解决springboot上传文件的问题 > 描述: springboot每次部署的时候, 都会创建临时目录, 如果这时如果使用相对目录, 那么下一次重新部署之后, 那么文件会找不到 **解决办法** 上传文件的时候上传到固定的问题, 浏览文件的时候, 通过 nginx 代理访问 ```java /** * 上传文件接口 * @param file * @return */ @ResponseBody @RequestMapping(value = "/uploadImage") public ResponseBo uploadImage(@RequestParam("image") MultipartFile file) { if(!file.isEmpty()){ try { Files.copy(file.getInputStream(), Paths.get(ROOT, file.getOriginalFilename())); return ResponseBo.ok(); } catch (IOException e) { e.printStackTrace(); return ResponseBo.error(e.getLocalizedMessage()); } }else { return ResponseBo.error(); } } ``` #### 7.使用@Vaild 进行实体验证 注意事项 > `@NotNull` 和 `@NotEmpty` 和`@NotBlank` 区别 - @NotEmpty 用在集合类上面 - @NotBlank 用在String上面 - @NotNull 用在基本类型上 #### 8.shiro 做认证授权 > 表结构 ```sql -- create role table drop table if exists role; create table role ( id int primary key auto_increment not null, name varchar(11) not null, create_time TIMESTAMP not null ); insert into role(name, create_time) value('superAdmin', now()); insert into role(name, create_time) value('admin', now()); insert into role(name, create_time) value('user', now()); -- 创建用户关系表 drop table if exists user_role; create table user_role ( id int primary key auto_increment not null, user_id int not null, role_id int not null ); insert into user_role(user_id, role_id) value (3, 1); -- create permission table drop table if exists permission; create table permission ( id int primary key auto_increment not null, name varchar(11) not null, create_time TIMESTAMP ); insert into permission(name, create_time) values ('select', now()), ('update', now()), ('insert', now()), ('delete', now()); drop table if exists role_permission; create table role_permission ( id int primary key auto_increment not null, role_id int not null, permission_id int not null ); insert into role_permission(role_id, permission_id) values (1, 1), (1, 2), (1, 3), (1, 4); ``` > mybatis 一对多查询语句 ```xml