栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot+Mybatis-plus配置流程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot+Mybatis-plus配置流程

文章目录
  • SpringBoot+Mybatis-plus配置流程
    • 1.新建项目
    • 2.添加Model
    • 3.改pom
    • 4.写yml
    • 5.生成代码
    • 6.主启动加注解
    • 7.写config

SpringBoot+Mybatis-plus配置流程 1.新建项目 2.添加Model 3.改pom
		
		
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            1.18.4
        
        
            mysql
            mysql-connector-java
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.3.2
        
        
            com.baomidou
            mybatis-plus-generator
            3.4.1
        
        
        
            org.apache.velocity
            velocity-engine-core
            2.3
        
        
            junit
            junit
            test
        
		
        
            io.springfox
            springfox-swagger2
            2.9.2
        
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        
4.写yml
server:
  port: 8080

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springboot?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: 123456
    
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath:com/nuc/springboot/mapper/xml/*.xml
  type-aliases-package: com.nuc.springboot.entity
5.生成代码
@Test
    void contextLoads() {
        // 1、创建代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 2、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        //工程绝对路径 + 工程的类路径
        gc.setOutputDir("E:\program\workspeaceIDEA\springboot-course\librarty_management" + "/src/main/java");
        gc.setAuthor("高靖奇");
        gc.setOpen(false); //生成后是否打开资源管理器
        gc.setFileOverride(true); //重新生成时文件是否覆盖
        gc.setServiceName("%sService");    //去掉Service接口的首字母I
        gc.setIdType(IdType.ASSIGN_UUID); //主键策略
        gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
        gc.setSwagger2(true);//开启Swagger2模式

        mpg.setGlobalConfig(gc);

        // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/springboot?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        // 4、包配置
        PackageConfig pc = new PackageConfig();
        //生成之后的包结构为: com.museum.account
        //pc.setModuleName("cloud-provider-payment8001"); //模块名
        pc.setParent("com.nuc.springboot");
        pc.setController("controller");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();
        //表名
        strategy.setInclude("books");
        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();
    }
6.主启动加注解

!!!很重要!!!非常重要!!!

主启动类上加@MapperScan(basePackages = {“com.nuc.springboot.mapper”})

7.写config
  • 用于swagger的配置
@Configuration
@EnableSwagger2//开启swagger
public class SwaggerConfig {
    @Bean
    public Docket docket(Environment environment) {
        //获取项目环境
//        Profiles profiles = Profiles.of("dev","test");
//        boolean flag = environment.acceptsProfiles(profiles);

        //配置Swagger类型
        Docket docket = new Docket(documentationType.SWAGGER_2);
        //开启swagger
        docket.enable(true);
//        docket.enable(flag);
        //配置分组名
        docket.groupName("librarty_management");
        //配置api信息
        docket.apiInfo(apiInfo());
        //配置搜索包
        docket
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.nuc"))
                .build();
        return docket;
    }
    //配置Swagger信息=apiInfo
    private ApiInfo apiInfo(){
        return new ApiInfo(
                "天天天天天天...才"
                , "天天天天天天...才"
                , "1.0"
                , "urn:tos",
                contact()
                , "Apache 2.0"
                , "http://www.apache.org/licenses/LICENSE-2.0"
                , new ArrayList());
    }
    
    public Contact contact() {

        return new Contact("天天天天天天...才", "www.baidu.com", "X15234435771@163.com");
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/295542.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号