mybatis-plus

介绍

MyBatis-Plus (简称 MP)是一个 MyBatis 的增强工具,对于简单表查询无需再写sql,提供了丰富了API, 大大提高开发效率。开发人员只关注于业务,繁琐的分层一键生成,无需在这些分层上浪费时间。
下面一起来构建一个项目

数据库

DROP TABLE IF EXISTS user;
CREATE TABLE user
(
    id BIGINT(20) NOT NULL COMMENT '主键ID',
    name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
    age INT(11) NULL DEFAULT NULL COMMENT '年龄',
    email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
    RIMARY KEY (id)
);
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');


依赖

<!--springboot -->
<parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>2.2.6.RELEASE</version>
     <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
   <dependency>
       <groupId>com.baomidou</groupId>
       <artifactId>mybatis-plus-boot-starter</artifactId>
       <version>3.0.5</version>
   </dependency>
  <dependency>
       <groupId>org.apache.velocity</groupId>
       <artifactId>velocity-engine-core</artifactId>
       <version>2.2</version>
   </dependency>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter</artifactId>
   </dependency>
  <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
 <dependency>
       <groupId>io.springfox</groupId>
       <artifactId>springfox-swagger2</artifactId>
       <version>2.9.2</version>
   </dependency>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-devtools</artifactId>
       <scope>runtime</scope>
       <optional>true</optional>
   </dependency>
 <!--spring2.x 使用8以上数据库-->
  <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <scope>runtime</scope>
   </dependency>
 
<!-- IDEA中需要安装lombok插件!  -->
   <dependency>
       <groupId>org.projectlombok</groupId>
       <artifactId>lombok</artifactId>
       <optional>true</optional>
   </dependency>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-test</artifactId>
   </dependency>
</dependencies>

配置文件

application.properties

#server-port
server.port=8000
#datasource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test_mp?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root

自动生成配置

public void autoConfig(){

   // 模块名
    String moduleName = "test";

    // 1、代码生成器
    AutoGenerator mpg = new AutoGenerator();

    // 规则的配置
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir");// 获取当前项目的路径
    gc.setOutputDir(projectPath + "/项目模块名称/src/main/java");
    gc.setAuthor("guanzc");
    gc.setOpen(false);
    gc.setFileOverride(false); // 是否覆盖之前文件!
    gc.setServiceName("%sService");
    gc.setIdType(IdType.ID_WORKER_STR); // 主键策略
    gc.setDateType(DateType.ONLY_DATE); // 日期类型
    gc.setSwagger2(true); // 自动开启Swagger配置!
    mpg.setGlobalConfig(gc);


    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://localhost:3306/test_"+moduleName+"?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("root");
    dsc.setDbType(DbType.MYSQL);
    mpg.setDataSource(dsc);


    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setModuleName(moduleName);
    pc.setParent("com.guan");
    pc.setController("controller");
    pc.setService("service");
    pc.setEntity("entity");
    pc.setMapper("mapper");
    mpg.setPackageInfo(pc);

    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    // 要生成哪一个表对应的类
    strategy.setInclude(moduleName+"_\\w*"); // 自动生成以test 开头的表映射
    strategy.setNaming(NamingStrategy.underline_to_camel); // 数据库表生成到实体类的策略
    strategy.setTablePrefix(pc.getModuleName()+"_"); //实体排除表的 test_ 前缀

    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    strategy.setEntityLombokModel(true); // 自动生成lombok注解

     // 乐观锁
    strategy.setVersionFieldName("version");
    strategy.setRestControllerStyle(true); // restful api
    strategy.setControllerMappingHyphenStyle(true); // 使用_连接驼峰!

    mpg.setStrategy(strategy);

    // 2、执行代码生成器
    mpg.execute();
}

在这里插入图片描述

测试

@Autowired
  private UserServer userServer;

  @Test
  public void getUers() {
      List<User> list = userServer.list(null);
      System.out.print(list.size());
  }

userService:提供了很多API, 根据业务需求条用不同接口。
要针对单表查询,多表查询,需要写自定义接口,sql ,mybatis-plus 提了mapper.xml支持

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值