一、介绍

1.1 概述

  • 本项目创建mybatisPlus动态代理对象bean,并动态注入到spring容器中
  • 实现CRUD的基础,在此基础上可根据各自需求进行扩展
  • CRUD功能将对实体类进行解耦,不需要再维护(以后实体类字段变更的情况)
  • 简化开发步骤,加快开发效率
  •  
  • 在原理和配置上也有很多不清楚的地方,希望大家留言讨论~

1.2 知识

spring boot

mybatisPlus

swagger等

1.3 演示&项目架构

二、项目实现

1、导入依赖包

数据驱动根据自己具体使用的数据库进行修改

        <!--mybatismp-->
        <dependency>
            <groupId>com.github.ice-zero-cat</groupId>
            <artifactId>icezerocat-mybatismp</artifactId>
            <version>0.0.2</version>
        </dependency>

        <!--数据库驱动-->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>runtime</scope>
        </dependency>

2、编辑配置文件

2.1 application.yml

(注:声明实体类包名-此功能不在这编文件讲,但此依赖包需要声明。可以与文章一致或者声明自己的实体类包名)

spring:
  datasource:
    driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
    url: jdbc:sqlserver://127.0.0.1:1433;databaseName=gplatform_szhy
    username: sa
    password: 123456

#手动链接db配置信息
jdbc:
  driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
  url: jdbc:sqlserver://127.0.0.1:1433;databaseName=gplatform_szhy
  username: sa
  password: 123456

#mybatisPlus的相关配置
mybatis-plus:
  global-config:
    db-config:
      id-type: auto
    banner: false
  #mapper配置文件
  mapper-locations: classpath:mapper/*.xml
  #开启驼峰命名
  configuration:
    map-underscore-to-camel-case: true

#声明实体类包名
entity:
  package: com.githup.icezerocat

2.2 启动类

@MapperScan("com.githup.icezerocat.**.mapper")
@SpringBootApplication(scanBasePackages = {"github.com.icezerocat", "com.github.icezerocat"})

注:红色的包声明是固定的,不能修改

1、红色的是依赖包的包名,蓝色的是自己项目的包名

2、声明mapper目录,可以自定义声明多个例如:@MapperScan({"com.githup.icezerocat.**.mapper", "com.**.mapper"})

/**
 * Description: 启动类
 * CreateDate:  2020/9/24 9:54
 *
 * @author zero
 * @version 1.0
 */
@MapperScan("com.githup.icezerocat.**.mapper")
@SpringBootApplication(scanBasePackages = {"github.com.icezerocat", "com.github.icezerocat"})
public class IcezerocatMpdemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(IcezerocatMpdemoApplication.class, args);
    }

}

3、获取实例进行操作-例子

/**
 * Description: mp控制器
 * CreateDate:  2020/9/24 9:54
 *
 * @author zero
 * @version 1.0
 */
@Slf4j
@Api("mp控制器")
@RestController
@RequestMapping("/mp")
public class MpController {

    private final BaseMpBuildService baseMpBuildService;

    public MpController(BaseMpBuildService baseMpBuildService) {
        this.baseMpBuildService = baseMpBuildService;
    }

    /**
     * 注册mapper
     *
     * @param tableName 表单名
     * @return 操作结果
     */
    @ApiOperation("注册mappers")
    @PostMapping("registeredMappers")
    public HttpResult registeredMappers(@RequestParam String tableName) {
        NoahServiceImpl<BaseMapper<Object>, Object> baseMapperObjectNoahService = this.baseMpBuildService.newInstance(tableName);
        if (baseMapperObjectNoahService != null) {
            List<Object> list = baseMapperObjectNoahService.list();
            return HttpResult.ok(list);
        }
        return HttpResult.error();
    }
}

重要代码-获取实例:

this.baseMpBuildService.newInstance(tableName);

tableName数据库表单名

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐