当前位置:   article > 正文

从零开始,搭建springboot+Mybatis-plus+vue3+sqlServer系统_mybatisplus整合sqlserver

mybatisplus整合sqlserver

一些依赖:

mybatis-plus的相关依赖

  1. <dependency>
  2. <groupId>com.baomidou</groupId>
  3. <artifactId>mybatis-plus-boot-starter</artifactId>
  4. <version>3.5.1</version>
  5. </dependency>
  6. <!-- MP代码生成器依赖-->
  7. <dependency>
  8. <groupId>com.baomidou</groupId>
  9. <artifactId>mybatis-plus-generator</artifactId>
  10. <version>3.5.1</version>
  11. </dependency>
  12. <!-- 模板引擎依赖-->
  13. <dependency>
  14. <groupId>org.apache.velocity</groupId>
  15. <artifactId>velocity-engine-core</artifactId>
  16. <version>2.3</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.freemarker</groupId>
  20. <artifactId>freemarker</artifactId>
  21. <version>2.3.28</version>
  22. </dependency>

swagger的相关依赖:

  1. <dependency>
  2. <groupId>io.springfox</groupId>
  3. <artifactId>springfox-swagger2</artifactId>
  4. <version>2.8.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>io.springfox</groupId>
  8. <artifactId>springfox-swagger-ui</artifactId>
  9. <version>2.8.0</version>
  10. </dependency>

spring-security的相关依赖:

  1. <dependency>
  2. <groupId>org.springframework.security</groupId>
  3. <artifactId>spring-security-test</artifactId>
  4. <scope>test</scope>
  5. </dependency>

安装插件:mybatisx

包结构

连接数据库sqlserver

yml配置:

  1. spring:
  2. datasource:
  3. driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
  4. url: jdbc:sqlserver://localhost:1433;databaseName=demo01;encrypt=true;trustServerCertificate=true;
  5. username: sa
  6. password: 123456

代码生成器(新):

  1. public static void Generation() {
  2. FastAutoGenerator.create("jdbc:sqlserver://localhost:1433;database=demo01;encrypt=true;trustServerCertificate=true", "sa", "123456")
  3. .globalConfig(builder -> {
  4. builder.author("CLONG") // 设置作者
  5. .enableSwagger() // 开启 swagger 模式
  6. .fileOverride() // 覆盖已生成文件
  7. .outputDir("C:\\Users\\073828\\Downloads\\demo\\src\\main\\java"); // 指定输出目录
  8. })
  9. .packageConfig(builder -> {
  10. builder.parent("com.example.demo") // 设置父包名
  11. // .moduleName("system") // 设置父包模块名
  12. .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "C:\\Users\\073828\\Downloads\\demo\\src\\main\\resources\\mapper")); // 设置mapperXml生成路径
  13. })
  14. .strategyConfig(builder -> {
  15. builder.addInclude("test") // 设置需要生成的表名
  16. .addTablePrefix("t_", "c_"); // 设置过滤表前缀
  17. })
  18. .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
  19. .execute();
  20. }

在com.example.demo.mapper下面的mapper文件中使用mybatisx快速开发

 生成:

 测试:

 集成Swagger

swagger配置文件

  1. package com.example.demo.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  5. import springfox.documentation.builders.ApiInfoBuilder;
  6. import springfox.documentation.builders.ParameterBuilder;
  7. import springfox.documentation.builders.PathSelectors;
  8. import springfox.documentation.builders.RequestHandlerSelectors;
  9. import springfox.documentation.schema.ModelRef;
  10. import springfox.documentation.service.ApiInfo;
  11. import springfox.documentation.service.Contact;
  12. import springfox.documentation.service.Parameter;
  13. import springfox.documentation.spi.DocumentationType;
  14. import springfox.documentation.spring.web.plugins.Docket;
  15. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. @Configuration
  19. @EnableSwagger2
  20. @EnableWebMvc
  21. public class Swagger {
  22. @Bean
  23. public Docket customDocket() {
  24. return new Docket(DocumentationType.SWAGGER_2)
  25. .apiInfo(apiInfo())
  26. .select()
  27. .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))//扫描的包路径
  28. .build();
  29. }
  30. private ApiInfo apiInfo() {
  31. return new ApiInfoBuilder()
  32. .title("CLONG")//文档说明
  33. .version("1.0.0")//文档版本说明
  34. .build();
  35. }
  36. }

发现404,配置路径:

  1. package com.example.demo.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  5. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  6. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  7. @Configuration
  8. public class WebMvcConfig implements WebMvcConfigurer {
  9. @Override
  10. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  11. registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
  12. registry.addResourceHandler("swagger-ui.html")
  13. .addResourceLocations("classpath:/META-INF/resources/");
  14. registry.addResourceHandler("/webjars/**")
  15. .addResourceLocations("classpath:/META-INF/resources/webjars/");
  16. }
  17. }

测试控制层:

  1. package com.example.demo.controller;
  2. import io.swagger.annotations.Api;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RestController;
  7. /**
  8. * <p>
  9. * 前端控制器
  10. * </p>
  11. *
  12. * @author CLONG
  13. * @since 2023-04-03
  14. */
  15. @Api("hello测试api")
  16. @RestController
  17. @RequestMapping("/test")
  18. public class TestController {
  19. @GetMapping("/hello")
  20. public String hello(String name){
  21. return "hello:"+name;
  22. }
  23. }

效果:

 成功!

下班了

点赞催更,未完待续.......

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/520961
推荐阅读
相关标签
  

闽ICP备14008679号