赞
踩
mybatis-plus的相关依赖
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>3.5.1</version>
- </dependency>
- <!-- MP代码生成器依赖-->
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-generator</artifactId>
- <version>3.5.1</version>
- </dependency>
- <!-- 模板引擎依赖-->
- <dependency>
- <groupId>org.apache.velocity</groupId>
- <artifactId>velocity-engine-core</artifactId>
- <version>2.3</version>
- </dependency>
- <dependency>
- <groupId>org.freemarker</groupId>
- <artifactId>freemarker</artifactId>
- <version>2.3.28</version>
- </dependency>
swagger的相关依赖:
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger2</artifactId>
- <version>2.8.0</version>
- </dependency>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger-ui</artifactId>
- <version>2.8.0</version>
- </dependency>
spring-security的相关依赖:
- <dependency>
- <groupId>org.springframework.security</groupId>
- <artifactId>spring-security-test</artifactId>
- <scope>test</scope>
- </dependency>
安装插件:mybatisx
yml配置:
- spring:
- datasource:
- driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
- url: jdbc:sqlserver://localhost:1433;databaseName=demo01;encrypt=true;trustServerCertificate=true;
- username: sa
- password: 123456
代码生成器(新):
- public static void Generation() {
- FastAutoGenerator.create("jdbc:sqlserver://localhost:1433;database=demo01;encrypt=true;trustServerCertificate=true", "sa", "123456")
- .globalConfig(builder -> {
- builder.author("CLONG") // 设置作者
- .enableSwagger() // 开启 swagger 模式
- .fileOverride() // 覆盖已生成文件
- .outputDir("C:\\Users\\073828\\Downloads\\demo\\src\\main\\java"); // 指定输出目录
- })
- .packageConfig(builder -> {
- builder.parent("com.example.demo") // 设置父包名
- // .moduleName("system") // 设置父包模块名
- .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "C:\\Users\\073828\\Downloads\\demo\\src\\main\\resources\\mapper")); // 设置mapperXml生成路径
- })
- .strategyConfig(builder -> {
- builder.addInclude("test") // 设置需要生成的表名
- .addTablePrefix("t_", "c_"); // 设置过滤表前缀
- })
- .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
- .execute();
- }
在com.example.demo.mapper下面的mapper文件中使用mybatisx快速开发
生成:
测试:
- package com.example.demo.config;
-
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.config.annotation.EnableWebMvc;
- import springfox.documentation.builders.ApiInfoBuilder;
- import springfox.documentation.builders.ParameterBuilder;
- import springfox.documentation.builders.PathSelectors;
- import springfox.documentation.builders.RequestHandlerSelectors;
- import springfox.documentation.schema.ModelRef;
- import springfox.documentation.service.ApiInfo;
- import springfox.documentation.service.Contact;
- import springfox.documentation.service.Parameter;
- import springfox.documentation.spi.DocumentationType;
- import springfox.documentation.spring.web.plugins.Docket;
- import springfox.documentation.swagger2.annotations.EnableSwagger2;
-
- import java.util.ArrayList;
- import java.util.List;
-
- @Configuration
- @EnableSwagger2
- @EnableWebMvc
- public class Swagger {
- @Bean
- public Docket customDocket() {
- return new Docket(DocumentationType.SWAGGER_2)
- .apiInfo(apiInfo())
- .select()
- .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))//扫描的包路径
- .build();
- }
-
- private ApiInfo apiInfo() {
- return new ApiInfoBuilder()
- .title("CLONG")//文档说明
- .version("1.0.0")//文档版本说明
- .build();
- }
- }
发现404,配置路径:
- package com.example.demo.config;
-
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
- import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
-
- @Configuration
- public class WebMvcConfig implements WebMvcConfigurer {
-
-
- @Override
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
- registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
- registry.addResourceHandler("swagger-ui.html")
- .addResourceLocations("classpath:/META-INF/resources/");
- registry.addResourceHandler("/webjars/**")
- .addResourceLocations("classpath:/META-INF/resources/webjars/");
- }
- }
测试控制层:
- package com.example.demo.controller;
-
-
- import io.swagger.annotations.Api;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RestController;
-
- /**
- * <p>
- * 前端控制器
- * </p>
- *
- * @author CLONG
- * @since 2023-04-03
- */
- @Api("hello测试api")
- @RestController
- @RequestMapping("/test")
- public class TestController {
- @GetMapping("/hello")
- public String hello(String name){
- return "hello:"+name;
- }
- }
效果:
成功!
下班了
点赞催更,未完待续.......
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。