前言:
👏作者简介:我是笑霸final,一名热爱技术的在校学生。
📝个人主页:个人主页1 || 笑霸final的主页2
📕系列专栏:后端专栏
📧如果文章知识点有错误的地方,请指正!和大家一起学习,一起进步👀
🔥如果感觉博主的文章还不错的话,👍点赞👍 + 👀关注👀 + 🤏收藏🤏
创建过程就不用多说 我们来详细说一下 pom.xml 文件
pom
这里统一放版本号
@SpringBootApplication
public class EduApplication {public static void main(String[] args) {SpringApplication.run(EduApplication.class,args);}
}
导入坐标
com.baomidou mybatis-plus-boot-starter ${mybatis-plus.version} org.apache.velocity velocity-engine-core ${velocity.version}
代码生成器
详细请看另一博客:点此跳转
public static void main(String[] args) {// 1、创建代码生成器AutoGenerator mpg = new AutoGenerator();// 2、全局配置// 全局配置GlobalConfig gc = new GlobalConfig();String projectPath = System.getProperty("user.dir");//gc.setOutputDir(projectPath + "/src/main/java");//代码生成的路径E:\学习\学习项目\硅谷课堂\ggkt_parentgc.setOutputDir("E:\\programming\\java\\学习项目\\guli_paret\\service\\service_edu\\src\\main\\java");gc.setServiceName("%sService"); //去掉Service接口的首字母Igc.setAuthor("xbfinal");gc.setOpen(false);mpg.setGlobalConfig(gc);// 3、数据源配置DataSourceConfig dsc = new DataSourceConfig();dsc.setUrl("jdbc:mysql://localhost:3306/guli?serverTimezone=GMT%2B8");dsc.setDriverName("com.mysql.cj.jdbc.Driver");dsc.setUsername("root");//数据库账号dsc.setPassword("123456");//数据库密码dsc.setDbType(DbType.MYSQL);mpg.setDataSource(dsc);// 4、包配置PackageConfig pc = new PackageConfig();pc.setParent("com.xbfinal");pc.setModuleName("eduservice"); //模块名pc.setController("controller");pc.setEntity("entity");pc.setService("service");pc.setMapper("mapper");mpg.setPackageInfo(pc);// 5、策略配置StrategyConfig strategy = new StrategyConfig();strategy.setInclude("edu_teacher");strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作strategy.setRestControllerStyle(true); //restful api风格控制器strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符mpg.setStrategy(strategy);// 6、执行mpg.execute();}
运行上面代码就能生成相应的结构了
配置类
注意
在配置类加上@MapperScan("mapper路径")
才能识别出mapper的位置
逻辑删除需要配置bean
@Configuration
@MapperScan("com.xbfinal.eduservice.mapper")
public class EduConfig {/*** 逻辑删除插件*/@Beanpublic ISqlInjector sqlInjector(){return new LogicSqlInjector();}
}
- 在配置类加上分页插件
/*** mp分页插件*/@Beanpublic PaginationInterceptor paginationInterceptor(){return new PaginationInterceptor();}
- 编写分页接口的方法
//创建page对象 参数1页码 参数2条数Page page=new Page<>(current,limit);//调用Service.page 方法实现分页 参数1 page对象 参数2 条件IPage page1 = eduTeacherService.page(page, null);
插件使用方法查看官网 ===>分页插件PaginationInnerInterceptor
带
有条件查询
的分页功能
- 第一步 把条件值传递到接口里面(把条件封装到对象中 vo)
- 编写方法
//由于博客笔记原因 代码我就都写controller了@PostMapping("pageTeacherCondition/{current}/{limit}")public Result pageTeacherCondition(@ApiParam(name = "current",value = "当前页码")@PathVariable("current") Long current,@ApiParam(name = "limit",value = "条数")@PathVariable("limit") Long limit ,@RequestBody(required = false) TeacherQuery teacherQuery){//1.创建page对象Page page=new Page<>(current,limit);//构建条件wrapperQueryWrapper wrapper = new QueryWrapper<>();//多条件组合查询String name=teacherQuery.getName();if (!StrUtil.isEmpty(name)){//模糊查询 参数1 数据库中字段的名称 参数2 条件wrapper.like("name",name);}//调用Service.page 方法实现分页 参数1 page对象 参数2 条件IPage page1 = eduTeacherService.page(page,wrapper );return Result.ok().addData(page1);}
基本情况请看 :swagger入门
注意事项:需要在 配置类打开注解@EnableSwagger2//开启Swagger配置
访问
http://localhost:当前服务模块的端口号/swagger-ui.html
进入ui界面
例如http://localhost:8001/swagger-ui.html
注解
@Api(value = " ")
用于类;
@ApiOperation()
用于方法;表示一个http请求的操作
@ApiParam(name = "id" ,value = "讲师的id",required = true)
用于方法的参数列表,参数,字段说明;表示对参数的添加元数据(说明或是否必填等)
AccessKey
配置文件
#阿里云 OSS
#服务器地址
aliyun.oss.file.endpoint=oss-cn-beijing.aliyuncs.com #(服务器地址)
aliyun.oss.file.keyid=AccessKey ID
aliyun.oss.file.keysecret=AccessKey Secret
#bucket可以在控制台创建,也可以用java创建aliyun.oss.file.bucketname=xxxx # xxxx是 bucket的name
出现问题
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
exclude = DataSourceAutoConfiguration.class
- common模块,也是公共模块,和父工程有着较多的相似性,其较大的作用在于为其他模块提供 共用 类,但不牵扯到其他模块的具体业务逻辑。
- 需要在各个模块引入common模块jar包。完成对项目工程的改造。. 在前后端分离的微服务中,公共模块,大多承担的角色是,根据前端所需返回的数据类型,定义出一个或几个共同的类,满足前端类型所需
- 最后需要在业务模块启动类上加上注解 来扫描到common模块中的组件
- 项目中我们会将响应封装成
json
返回,一般我们会将所有接口的数据格式统一, 使前端(iOS Android, Web)对数据的操作更一致、轻松。- 一般情况下,统一返回数据格式没有固定的格式,只要能描述清楚返回的数据状态以及要返回的具体数据就可以。但是一般会包含
状态码
、返回消息
、返回数据
这几部分内容
interface
来定义成功和失败的状态码/*** @author 笑霸final*/
public interface ResultCode {/*** 自定义成功的状态码*/public static Integer SUCCESS=20000;/*** 自定义失败的状态码*/public static Integer ERROR=20001;
}
Result
@Data
public class Result {@ApiModelProperty(value = "是否成功")private Boolean success;@ApiModelProperty(value = "状态码")private Integer code;@ApiModelProperty(value = "消息")private String message;@ApiModelProperty(value = "返回的数据")private T data;@ApiModelProperty(value = "动态数据")private Map map = new HashMap();/*** 防止直接new*/private Result(){};/*** 成功的静态方法*/public static Result ok( ){Result r=new Result();r.success=true;r.code=ResultCode.SUCCESS;r.message="成功";return r;}/*** 失败的静态方法*/public static Result failed( ){Result r=new Result();r.success=false;r.code=ResultCode.ERROR;r.message="失败";return r;}public Result addCode(Integer code){this.setCode(code);return this;}public Result addMessage(String message){this.setMessage(message);return this;}public Result addData(T data){this.setData(data);return this;}/*** 用来操作 map = new HashMap(); 动态数据* @param key* @param value* @return*/public Result add(String key, Object value) {this.map.put(key, value);return this;}}
先给需要自动填充的数据加上注解@TableField(fill = FieldFill.INSERT)
或者@TableField(fill = FieldFill.INSERT_UPDATE)
编写MyMetaObjectHandler
类去实现MetaObjectHandler
接口
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {@Overridepublic void insertFill(MetaObject metaObject) {//属性名称,不是字段名称this.setFieldValByName("gmtCreate", new Date(), metaObject);this.setFieldValByName("gmtModified", new Date(), metaObject);}@Overridepublic void updateFill(MetaObject metaObject) {this.setFieldValByName("gmtModified", new Date(), metaObject);}
}
- 定义一个异常类
GlobalExceptionHandler
并加上注解@ControllerAdvice
- 异常处理类编写具体的对异常处理方法,在方法上添加注解
@ExceptionHandler
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {//全局异常处理@ResponseBody//返回json数据@ExceptionHandler(Exception.class)public Result error(Exception e){log.info("当前异常信息\n",e);return Result.fail("全局异常处理");}
}
一:创建自定义异常处理类,继承
RuntimeException
类
public class GgktException extends RuntimeException{
}
二:在自定义异常处理类里面创建属性
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GgktException extends RuntimeException{private Integer code;//状态码private String msg;//异常信息}
三:在全局异常处理类上添加上自定义的异常处理方法
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {@ResponseBody//返回json数据@ExceptionHandler(Exception.class) //全局异常处理Exception.classpublic Result error(Exception e){log.info("当前异常信息\n",e);return Result.fail("全局异常处理");}@ResponseBody@ExceptionHandler(GgktException.class) //自定义的异常处理方法public Result divError(GgktException e){log.info("当前异常信息\n",e.getMsg());return Result.fail("自定义的异常处理");}
四:手动抛出自定义异常
public Result> findAllTeacher(){try{int i=1/0;}catch (Exception e){//手动抛出异常throw new GgktException(201,"执行了自定义异常处理");}List teacherList = teacherService.list();return Result.ok(teacherList);}
在配置文件设置日志级别
logging.level.root=warn
把日志输出到文件中使用日志工具
Logback
用法和log4j
差不多
- 1.删除 配置文件的日志配置
- 2.resource文件中创建
logback-spring.xml
粘贴以下代码即可
代码查看线路1 =====>点击查看代码
代码查看线路2 =====>点击查看代码
持续更新中 欢迎大家补充留言