SpringBoot 2.1.x整合MybatisPlus代码示例

资源相关

  • boot-mp项目结构图【已经过逆向工程代码生成】
    SpringBoot 2.1.x整合MybatisPlus代码示例_第1张图片
  • boot-mp项目源码【推荐】

核心代码

  • pom.xml
<dependencies>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.1.10version>
        dependency>


        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.2.0version>
        dependency>

        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-generatorartifactId>
            <version>3.2.0version>
        dependency>
        
        <dependency>
            <groupId>org.apache.velocitygroupId>
            <artifactId>velocity-engine-coreartifactId>
            <version>2.1version>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.42version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>

        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.4version>
        dependency>

        
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.9.2version>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.9.2version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>
  • application.yml
mybatis-plus:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.hf.mp.entity
  #全局配置
  configuration:
    map-underscore-to-camel-case: true
    call-setters-on-nulls: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    cache-enabled: true

spring:
  application:
    name: boot-mp
  datasource:
    username: root
    password: admin
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.44.121:3306/mp_task_01
    type: com.alibaba.druid.pool.DruidDataSource
    #schema: classpath:sql/task_db.sql
    druid:
      #初始化大小
      initial-size: 5
      min-idle: 5
      max-active: 20
      #等待超时时间
      max-wait: 60000
      #心跳检测
      time-between-eviction-runs-millis: 60000
      #配置连接在池的最小时间
      min-evictable-idle-time-millis: 300000
      validation-query: SELCET 1
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      #打开PSCache
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      #配置监控统计连接的filters,去掉后监控sql无法统计
      filters: stat,wall,slf4j
      connection-properties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
      #配置DruidStatFilter
      web-stat-filter:
        enabled: true
        url-pattern: "/*"
        exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
      #配置DruidStatViewServlet
      stat-view-servlet:
        url-pattern: "/druid/*"
        #IP白名单
        allow: 127.0.0.1,192.168.44.121
        #IP黑名单
        deny: 192.168.15.85
        reset-enable: false
        login-username: admin
        login-password: 123456

  • MybatisPlusConfig.java
package com.hf.mp.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @Copyright (C), 2017-2019
 * @FileName: MybatisPlusConfig
 * @Author: hf
 * @Date: 2019/9/26 0:58
 * @Description: Mybatis Plus插件配置类
 */
@EnableTransactionManagement
@Configuration
@MapperScan("com.hf.mp.dao")
public class MybatisPlusConfig {

    //分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

  • CodeGenerator.java
package com.hf.mp.util;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * @Copyright (C), 2017-2019
 * @FileName: CodeGenerator
 * @Author: hf
 * @Date: 2019/9/26 1:03
 * @Description: Mybatis plus逆向工程代码生成器
 */
public class CodeGenerator {

    public static void main(String[] args) {

        //1、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "\\boot-mp\\src\\main\\java")//文件输出路径
                .setAuthor("hf") //作者
                .setOpen(false) //生成完成是否打开文件夹
                .setFileOverride(true) //是否覆写
                .setIdType(IdType.AUTO) //主键策略
                .setEnableCache(false) //是否启用缓存
                .setServiceName("%sService") //生成service前缀不带I
                .setBaseResultMap(true) //生成result 映射
                .setSwagger2(true) //是否启动swagger
                .setBaseColumnList(true); //生成公众的sql字段

        //2、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL)
                .setDriverName("com.mysql.jdbc.Driver")
                .setUrl("jdbc:mysql://192.168.44.121:3306/mp_task_01")
                .setUsername("root")
                .setPassword("admin");

        //3、策略配置
        StrategyConfig sc = new StrategyConfig();
        sc.setCapitalMode(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setColumnNaming(NamingStrategy.underline_to_camel)
                .setEntityLombokModel(true)
                .setTablePrefix("tb_")
                .setRestControllerStyle(true);

        //4、包名策略配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.hf.mp")
                .setMapper("dao")
                .setService("service")
                .setController("controller")
                .setEntity("entity")
                .setXml("mapper");

        //5、 整合配置
        AutoGenerator ag = new AutoGenerator();
        ag.setGlobalConfig(gc)
                .setDataSource(dsc)
                .setStrategy(sc)
                .setPackageInfo(pc)
                .execute();
    }
}

测试相关

  • 运行CodeGenerator即会自动生成其它目录结构
  • 测试查询
    SpringBoot 2.1.x整合MybatisPlus代码示例_第2张图片

你可能感兴趣的:(SpringBoot)