额 额而对方恐龙
接口定义
创建数据库,创建目录结构
- 创建父工程guli(删除src目录)
打包方式设为pom
设置依赖管理(版本和依赖) - 创建子模块service(删除src目录)
设置依赖和打包方式pom - 创建eduservice二级子模块(一定是子子模块)
设置项目配置文件()端口号 数据库连接信息spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/guli?useSLL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=123456
- 设置mp自动代码生成
1t
est引入自动生成类
mp自动生成@Test public void run() { // 1、创建代码生成器 AutoGenerator mpg = new AutoGenerator(); // 2、全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir("E:\\学习\\1尚骨粒\\guli\\service\\eduservice" + "/src/main/java");//输出的路径名 gc.setAuthor("testjava"); gc.setOpen(false); //生成后是否打开资源管理器 gc.setFileOverride(false); //重新生成时文件是否覆盖 gc.setServiceName("%sService"); //去掉Service接口的首字母I gc.setIdType(IdType.ID_WORKER_STR); //主键策略 gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型 gc.setSwagger2(true);//开启Swagger2模式 mpg.setGlobalConfig(gc); // 3、数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/guli?useSLL=true&useUnicode=true&characterEncoding=utf-8&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.liu");//包名 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.setTablePrefix(pc.getModuleName() + "_"); //生成实体时去掉表前缀 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(); }
创建查询全部数据
1在生成的controller中自动注入serviceimpl
2service自动调用mapper层(在配置类中添加扫描mapper注解)
3@GetMapping("/aaa") public List findall(){ List<EduTeacher> list = serviceImpl.list(null); return list; }
4配置文件中返回的格式化时间格式
#设置json全局反回时间格式 spring.jackson.date-format=yyyy-MM-dd HH-mm-ss spring.jackson.time-zone=GMT+8
- 创建主启动
测试@SpringBootApplication @ComponentScan(basePackages = {"com.liu"}) @EnableSwagger2 public class EduApplication { public static void main(String[] args) { SpringApplication.run(EduApplication.class, args); } }
- 实现restful风格的请求
用不同的请求头实现,实现逻辑删除
- 配置swagger实现接口测试(整个项目都可用,建在common公共组件中)
1创建service同级模块commenn(导入依赖,设置打包方式为pom)
2创建service_base模块
3创建swaggeconfigl类
4设置类为配置类开启swagger@Bean public Docket webApiConfig(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("webApi") .apiInfo(webApiInfo()) .select() .paths(Predicates.not(PathSelectors.regex("/admin/.*"))) .paths(Predicates.not(PathSelectors.regex("/error.*"))) .build(); } private ApiInfo webApiInfo(){ return new ApiInfoBuilder() .title("谷粒学院") .description("本文档描述了谷粒学院各个模块的接口信息") .version("1.0") .contact(new Contact("java", "http://liu.com", "1123@qq.com")) .build(); }
-
在主启动类中加载swgger
1在service pom中引入swagge所在模块的pom<!--artifactId:要引入的模块名 groupId:组 --> <dependency> <artifactId>common</artifactId> <groupId>com.liu</groupId> <version>0.0.1-SNAPSHOT</version> </dependency>
2主启动类中添加组件扫描注解,以找到加载模块的组件
3添加开启swagge组件@SpringBootApplication @ComponentScan(basePackages = {"com.liu"}) @EnableSwagger2 public class EduApplication { public static void main(String[] args) { SpringApplication.run(EduApplication.class, args); } }
-
swagge设置对外属性注解
1设置接口名称(类上添加@Api(description = "讲师管理接口")注解)
2设置请求名称(方法上添加@ApiOperation(value = "查询所有讲师")
3设置属性名称(对应属性@ApiParam(name = "id",value = "讲师id",required = true))
)
@RestController @RequestMapping("/eduservice/teacher") @Api(description = "讲师管理接口") public class EduTeacherController { @Autowired private EduTeacherServiceImpl serviceImpl; //查询全部数据 @ApiOperation(value = "查询所有讲师") @GetMapping("/aaa") public List findall(){ List<EduTeacher> list = serviceImpl.list(null); return list; } //通过id删除 @ApiOperation(value = "逻辑删除讲师") @DeleteMapping("{id}") public boolean deleteById(@ApiParam(name = "id",value = "讲师id",required = true) @PathVariable String id){ boolean b = serviceImpl.removeById(id); return b; }