mybatis-plus

实现

1、mapper接口继承BaseMpper<指定实体类>
2、@MapperScan(指定mapper路径)

实体类上的注解

@TableId 自动递增
@TableField 自动填充
@TableLogic 逻辑删除 等价与在这里插入图片描述
@Version 乐观锁
@TabelName 对应表名

mybatis-plus插件集合

一、自动分页

步骤:①配置分页插件
在这里插入图片描述
②实现分页效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210511104609440.png![在这里插入图片描述](https://img-blog

二、自动填充

步骤:①实体类添加注解@TableField (添加注解的字段一般为创建时间和更新时间)
②自定义一个实现类MyMetaObjectHandler
在这里插入图片描述

三、乐观锁 (版本控制)

步骤:①实体类添加注解@Version
在这里插入图片描述
②配置乐观锁插件
在这里插入图片描述

四、逻辑删除(删除即更新)

步骤:实体类添加注解
在这里插入图片描述
或这是yml文件中配置
在这里插入图片描述

五、性能分析

步骤:https://mp.baomidou.com/guide/p6spy.html

六、条件构造器

https://mp.baomidou.com/guide/wrapper.html#abstractwrapper

七、自动代码生成

一、将必要的依赖导入

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.2</version>
    </dependency>
    <!--代码生成器 依赖-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-generator</artifactId>
        <version>3.4.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity-engine-core</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>com.github.caijh</groupId>
        <artifactId>swagger-spring-boot-starter</artifactId>
        <version>2.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>2.3.31</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

二、将之前配置的乐观锁 分页配置加上,以防自动成后无法生效分页等效果在这里插入图片描述

三、自动生成代码

/**
@author wuwei
@version 1.0
@date 2021/5/11 13:59
*/
//代码自动生成
public class AutoCode {
public static void main(String[] args) {
// 需要构建一个 代码自动生成器 对象
AutoGenerator mpg = new AutoGenerator();
// 配置策略
// 1、全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty(“user.dir”);//获取用户目录
gc.setOutputDir(projectPath + “/src/main/java”);//输出到指定目录下
gc.setAuthor(“XXX”);//设置作者
gc.setOpen(false);//是否打开资源管理器
// 是否覆盖原来生成的
gc.setFileOverride(false);
// 去Service的I前缀
gc.setServiceName("%sService");
gc.setIdType(IdType.AUTO);//ID自动生成
gc.setDateType(DateType.ONLY_DATE);//日期类型
gc.setSwagger2(true);//自动配置swagger
mpg.setGlobalConfig(gc);//全局配置配置到生成器中
//2、设置数据源
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(“jdbc:mysql://localhost:3306/xxx?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8”);
dsc.setDriverName(“com.mysql.cj.jdbc.Driver”);
dsc.setUsername(“root”);
dsc.setPassword(“root”);
dsc.setDbType(DbType.MYSQL);//设置驱动类型
mpg.setDataSource(dsc);//数据库配置配置到生成器中
//3、包的配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(“XXX”);//设置模块名
pc.setParent(“com.xxx”);//生成模块
pc.setEntity(“entity”);//实体名
pc.setMapper(“mapper”);//mapper
pc.setService(“service”);
pc.setController(“controller”);
mpg.setPackageInfo(pc);//配置到生成器中
//4、策略配置
StrategyConfig strategy = new StrategyConfig();
// 设置要映射的表名
strategy.setInclude(“map_hotel”);
strategy.setNaming(NamingStrategy.underline_to_camel);//包命名 下划线转驼峰
strategy.setColumnNaming(NamingStrategy.underline_to_camel);//列名 下划线转驼峰
strategy.setEntityLombokModel(true); // 自动lombok;
strategy.setLogicDeleteFieldName(“del_flag”);//逻辑删除
// 自动填充配置
TableFill gmtCreate = new TableFill(“gmt_create”, FieldFill.INSERT);
TableFill gmtModified = new TableFill(“gmt_modified”, FieldFill.INSERT_UPDATE);
ArrayList tableFills = new ArrayList<>();
tableFills.add(gmtCreate);
tableFills.add(gmtModified);
strategy.setTableFillList(tableFills);//设置自动填充策略
// 乐观锁
strategy.setVersionFieldName(“version”);
strategy.setRestControllerStyle(true);//开启驼峰命名格式
strategy.setControllerMappingHyphenStyle(true);// localhost:8080/hello_id_2
mpg.setStrategy(strategy);
mpg.execute(); //执行
}
}

注意:自动配置的字段名在数据库表中得对应添加

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值