关于java:还在手写CRUD代码这款开源框架助你解放双手

31次阅读

共计 14313 个字符,预计需要花费 36 分钟才能阅读完成。

置信很多敌人在我的项目中应用的 ORM 框架都是 MyBatis,如果单用 MyBatis 来操作数据库的话,须要手写很多单表查问的 SQL 实现。这时候咱们往往会抉择一个加强工具来实现这些单表 CRUD 操作,这里举荐一款好用的工具 MyBatis-Plus!

SpringBoot 实战电商我的项目 mall(40k+star)地址:https://github.com/macrozheng/mall

MyBatis-Plus 简介

MyBatis-Plus(简称 MP)是一个 MyBatis 的加强工具,在 MyBatis 的根底上只做加强不做扭转,为简化开发、提高效率而生。MyBatis-Plus 提供了代码生成器,能够一键生成 controller、service、mapper、model、mapper.xml 代码,同时提供了丰盛的 CRUD 操作方法,助咱们解放双手!

MyBatis-Plus 集成

首先咱们须要在 SpringBoot 我的项目中集成 MyBatis-Plus,之后咱们再具体介绍它的应用办法!

  • pom.xml 中增加相干依赖,次要是 MyBatis-Plus、MyBatis-Plus Generator 和 Velocity 模板引擎;
<dependencies>
    <!--Mybatis-Plus 依赖 -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.3.2</version>
    </dependency>
    <!--Mybatis-Plus 代码生成器 -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-generator</artifactId>
        <version>3.3.2</version>
    </dependency>
    <!--Velocity 模板生成引擎 -->
    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity-engine-core</artifactId>
        <version>2.2</version>
    </dependency>
</dependencies>
  • 在 SpringBoot 配置文件 application.yml 增加如下配置,配置好数据源和 MyBatis-Plus;
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    username: root
    password: root

mybatis-plus:
  mapper-locations: classpath:/mapper/**/*.xml #指定 mapper.xml 门路
  global-config:
    db-config:
      id-type: auto #全局默认主键类型设置为自增
  configuration:
    auto-mapping-behavior: partial #只对非嵌套的 resultMap 进行主动映射
    map-underscore-to-camel-case: true #开启主动驼峰命名规定映射
  • 增加 MyBatis-Plus 的 Java 配置,应用 @MapperScan 注解配置好须要扫码的 Mapper 接口门路,MyBatis-Plus 自带分页性能,须要配置好分页插件PaginationInterceptor
/**
 * MyBatis 配置类
 * Created by macro on 2019/4/8.
 */
@Configuration
@MapperScan("com.macro.mall.tiny.modules.*.mapper")
public class MyBatisConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor() {PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }
}

代码生成器

MyBatis-Plus 提供了代码生成器,能够一键生成 controller、service、mapper、model、mapper.xml 代码,十分不便!

  • 首先咱们创立代码生成器类 MyBatisPlusGenerator,间接运行其main 办法即可生成相干代码;
/**
 * MyBatisPlus 代码生成器
 * Created by macro on 2020/8/20.
 */
public class MyBatisPlusGenerator {public static void main(String[] args) {String projectPath = System.getProperty("user.dir") + "/mall-tiny-plus";
        String moduleName = scanner("模块名");
        String[] tableNames = scanner("表名,多个英文逗号宰割").split(",");
        // 代码生成器
        AutoGenerator autoGenerator = new AutoGenerator();
        autoGenerator.setGlobalConfig(initGlobalConfig(projectPath));
        autoGenerator.setDataSource(initDataSourceConfig());
        autoGenerator.setPackageInfo(initPackageConfig(moduleName));
        autoGenerator.setCfg(initInjectionConfig(projectPath, moduleName));
        autoGenerator.setTemplate(initTemplateConfig());
        autoGenerator.setStrategy(initStrategyConfig(tableNames));
        autoGenerator.setTemplateEngine(new VelocityTemplateEngine());
        autoGenerator.execute();}

    /**
     * 读取控制台内容信息
     */
    private static String scanner(String tip) {Scanner scanner = new Scanner(System.in);
        System.out.println(("请输出" + tip + ":"));
        if (scanner.hasNext()) {String next = scanner.next();
            if (StrUtil.isNotEmpty(next)) {return next;}
        }
        throw new MybatisPlusException("请输出正确的" + tip + "!");
    }

    /**
     * 初始化全局配置
     */
    private static GlobalConfig initGlobalConfig(String projectPath) {GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(projectPath + "/src/main/java");
        globalConfig.setAuthor("macro");
        globalConfig.setOpen(false);
        globalConfig.setSwagger2(true);
        globalConfig.setBaseResultMap(true);
        globalConfig.setFileOverride(true);
        globalConfig.setDateType(DateType.ONLY_DATE);
        globalConfig.setEntityName("%s");
        globalConfig.setMapperName("%sMapper");
        globalConfig.setXmlName("%sMapper");
        globalConfig.setServiceName("%sService");
        globalConfig.setServiceImplName("%sServiceImpl");
        globalConfig.setControllerName("%sController");
        return globalConfig;
    }

    /**
     * 初始化数据源配置
     */
    private static DataSourceConfig initDataSourceConfig() {Props props = new Props("generator.properties");
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl(props.getStr("dataSource.url"));
        dataSourceConfig.setDriverName(props.getStr("dataSource.driverName"));
        dataSourceConfig.setUsername(props.getStr("dataSource.username"));
        dataSourceConfig.setPassword(props.getStr("dataSource.password"));
        return dataSourceConfig;
    }

    /**
     * 初始化包配置
     */
    private static PackageConfig initPackageConfig(String moduleName) {Props props = new Props("generator.properties");
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setModuleName(moduleName);
        packageConfig.setParent(props.getStr("package.base"));
        packageConfig.setEntity("model");
        return packageConfig;
    }

    /**
     * 初始化模板配置
     */
    private static TemplateConfig initTemplateConfig() {TemplateConfig templateConfig = new TemplateConfig();
        // 能够对 controller、service、entity 模板进行配置
        //mapper.xml 模板需独自配置
        templateConfig.setXml(null);
        return templateConfig;
    }

    /**
     * 初始化策略配置
     */
    private static StrategyConfig initStrategyConfig(String[] tableNames) {StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setRestControllerStyle(true);
        // 当表名中带 * 号时能够启用通配符模式
        if (tableNames.length == 1 && tableNames[0].contains("*")) {String[] likeStr = tableNames[0].split("_");
            String likePrefix = likeStr[0] + "_";
            strategyConfig.setLikeTable(new LikeTable(likePrefix));
        } else {strategyConfig.setInclude(tableNames);
        }
        return strategyConfig;
    }

    /**
     * 初始化自定义配置
     */
    private static InjectionConfig initInjectionConfig(String projectPath, String moduleName) {
        // 自定义配置
        InjectionConfig injectionConfig = new InjectionConfig() {
            @Override
            public void initMap() {// 可用于自定义属性}
        };
        // 模板引擎是 Velocity
        String templatePath = "/templates/mapper.xml.vm";
        // 自定义输入配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输入
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名,如果你 Entity 设置了前后缀、此处留神 xml 的名称会跟着发生变化!!return projectPath + "/src/main/resources/mapper/" + moduleName
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;}
        });
        injectionConfig.setFileOutConfigList(focList);
        return injectionConfig;
    }

}
  • 而后在 resources 目录下增加配置文件generator.properties,增加代码生成器的数据源配置及寄存业务代码的根底包名称;
dataSource.url=jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
dataSource.driverName=com.mysql.cj.jdbc.Driver
dataSource.username=root
dataSource.password=root
package.base=com.macro.mall.tiny.modules
  • 仔细的敌人能够发现 MyBatisPlusGenerator 中很多配置代码都没增加正文,其实 MyBatis-Plus 源码中的中文正文十分欠缺,只需查看源码即可,这里摘抄一段 DataSourceConfig 中的源码;
/**
 * 数据库配置
 *
 * @author YangHu, hcl
 * @since 2016/8/30
 */
@Data
@Accessors(chain = true)
public class DataSourceConfig {

    /**
     * 数据库信息查问
     */
    private IDbQuery dbQuery;
    /**
     * 数据库类型
     */
    private DbType dbType;
    /**
     * PostgreSQL schemaName
     */
    private String schemaName;
    /**
     * 类型转换
     */
    private ITypeConvert typeConvert;
    /**
     * 关键字处理器
     * @since 3.3.2
     */
    private IKeyWordsHandler keyWordsHandler;
    /**
     * 驱动连贯的 URL
     */
    private String url;
    /**
     * 驱动名称
     */
    private String driverName;
    /**
     * 数据库连贯用户名
     */
    private String username;
    /**
     * 数据库连贯明码
     */
    private String password;
    
    // 省略若干代码......

}
  • 代码生成器反对两种模式,一种生成单表的代码,比方只生成 pms_brand 表代码能够先输出pms,后输出pms_brand

  • 生成单表代码构造一览;

  • 另一种间接生成整个模块的代码,须要带通配符 *,比方生成ums 模块代码能够先输出ums,后输出ums_*

  • 生成整个模块代码构造一览。

自定义生成模板

MyBatis-Plus 应用模板引擎来生成代码,反对 Velocity(默认)、Freemarker、Beetl 模板引擎,这里以 Velocity 为了来介绍下如何自定义生成模板。

  • 首先咱们能够从 MyBatis-Plus Generator 依赖包的源码中找到默认模板,拷贝到我的项目的 resources/templates 目录下;

  • MyBatisPlusGenerator 类中对 TemplateConfig 进行配置,配置好各个模板的门路;
/**
 * MyBatisPlus 代码生成器
 * Created by macro on 2020/8/20.
 */
public class MyBatisPlusGenerator {
    
    /**
     * 初始化模板配置
     */
    private static TemplateConfig initTemplateConfig() {TemplateConfig templateConfig = new TemplateConfig();
        // 能够对 controller、service、entity 模板进行配置
        templateConfig.setEntity("templates/entity.java");
        templateConfig.setMapper("templates/mapper.java");
        templateConfig.setController("templates/controller.java");
        templateConfig.setService("templates/service.java");
        templateConfig.setServiceImpl("templates/serviceImpl.java");
        //mapper.xml 模板需独自配置
        templateConfig.setXml(null);
        return templateConfig;
    }
}
  • 对模板进行定制,在定制过程中咱们能够发现很多内置变量,用于输入到模板中去,这里以 service.java.vm 模板为例子,比方 packagetable 这些变量;
package ${package.Service};

import ${package.Entity}.${entity};
import ${superServiceClassPackage};

/**
 * <p>
 * $!{table.comment} 服务类
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
#if(${kotlin})
interface ${table.serviceName} : ${superServiceClass}<${entity}>
#else
public interface ${table.serviceName} extends ${superServiceClass}<${entity}> {

}
#end
  • 搞懂这些变量从哪来的,对咱们定制模板很有帮忙,其实这些变量都来着于 AbstractTemplateEnginegetObjectMap办法,具体变量作用能够参考源码。
/**
 * 模板引擎抽象类
 *
 * @author hubin
 * @since 2018-01-10
 */
public abstract class AbstractTemplateEngine {
        /**
         * 渲染对象 MAP 信息
         *
         * @param tableInfo 表信息对象
         * @return ignore
         */
        public Map<String, Object> getObjectMap(TableInfo tableInfo) {Map<String, Object> objectMap = new HashMap<>(30);
            ConfigBuilder config = getConfigBuilder();
            if (config.getStrategyConfig().isControllerMappingHyphenStyle()) {objectMap.put("controllerMappingHyphenStyle", config.getStrategyConfig().isControllerMappingHyphenStyle());
                objectMap.put("controllerMappingHyphen", StringUtils.camelToHyphen(tableInfo.getEntityPath()));
            }
            objectMap.put("restControllerStyle", config.getStrategyConfig().isRestControllerStyle());
            objectMap.put("config", config);
            objectMap.put("package", config.getPackageInfo());
            GlobalConfig globalConfig = config.getGlobalConfig();
            objectMap.put("author", globalConfig.getAuthor());
            objectMap.put("idType", globalConfig.getIdType() == null ? null : globalConfig.getIdType().toString());
            objectMap.put("logicDeleteFieldName", config.getStrategyConfig().getLogicDeleteFieldName());
            objectMap.put("versionFieldName", config.getStrategyConfig().getVersionFieldName());
            objectMap.put("activeRecord", globalConfig.isActiveRecord());
            objectMap.put("kotlin", globalConfig.isKotlin());
            objectMap.put("swagger2", globalConfig.isSwagger2());
            objectMap.put("date", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            objectMap.put("table", tableInfo);
            objectMap.put("enableCache", globalConfig.isEnableCache());
            objectMap.put("baseResultMap", globalConfig.isBaseResultMap());
            objectMap.put("baseColumnList", globalConfig.isBaseColumnList());
            objectMap.put("entity", tableInfo.getEntityName());
            objectMap.put("entitySerialVersionUID", config.getStrategyConfig().isEntitySerialVersionUID());
            objectMap.put("entityColumnConstant", config.getStrategyConfig().isEntityColumnConstant());
            objectMap.put("entityBuilderModel", config.getStrategyConfig().isEntityBuilderModel());
            objectMap.put("chainModel", config.getStrategyConfig().isChainModel());
            objectMap.put("entityLombokModel", config.getStrategyConfig().isEntityLombokModel());
            objectMap.put("entityBooleanColumnRemoveIsPrefix", config.getStrategyConfig().isEntityBooleanColumnRemoveIsPrefix());
            objectMap.put("superEntityClass", getSuperClassName(config.getSuperEntityClass()));
            objectMap.put("superMapperClassPackage", config.getSuperMapperClass());
            objectMap.put("superMapperClass", getSuperClassName(config.getSuperMapperClass()));
            objectMap.put("superServiceClassPackage", config.getSuperServiceClass());
            objectMap.put("superServiceClass", getSuperClassName(config.getSuperServiceClass()));
            objectMap.put("superServiceImplClassPackage", config.getSuperServiceImplClass());
            objectMap.put("superServiceImplClass", getSuperClassName(config.getSuperServiceImplClass()));
            objectMap.put("superControllerClassPackage", verifyClassPacket(config.getSuperControllerClass()));
            objectMap.put("superControllerClass", getSuperClassName(config.getSuperControllerClass()));
            return Objects.isNull(config.getInjectionConfig()) ? objectMap : config.getInjectionConfig().prepareObjectMap(objectMap);
        }
}

CRUD 操作

MyBatis-Plus 的弱小之处不止在于它的代码生成性能,还在于它提供了丰盛的 CRUD 办法,让咱们实现单表 CRUD 简直不必手写 SQL 实现!

  • 咱们之前生成的 PmsBrandMapper 接口因为继承了 BaseMapper 接口,间接领有了各种 CRUD 办法;
/**
 * <p>
 * 品牌表 Mapper 接口
 * </p>
 *
 * @author macro
 * @since 2020-08-20
 */
public interface PmsBrandMapper extends BaseMapper<PmsBrand> {}
  • 咱们来看下 BaseMapper 中的办法,是不是根本能够满足咱们的日常所需了;

  • 咱们之前生成的 PmsBrandService 接口因为继承了 IService 接口,也领有了各种 CRUD 办法;
/**
 * <p>
 * 品牌表 服务类
 * </p>
 *
 * @author macro
 * @since 2020-08-20
 */
public interface PmsBrandService extends IService<PmsBrand> {}
  • 能够看下比 BaseMapper 中的更加丰盛;

  • 有了这些 IServiceBaseMapper中提供的这些办法,咱们单表查问就简直不必手写 SQL 实现了,应用 MyBatis-Plus 实现以前 PmsBrandController 的办法更轻松了!
/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author macro
 * @since 2020-08-20
 */
@Api(tags = "PmsBrandController", description = "商品品牌治理")
@RestController
@RequestMapping("/brand")
public class PmsBrandController {private static final Logger LOGGER = LoggerFactory.getLogger(PmsBrandController.class);

    @Autowired
    private PmsBrandService brandService;

    @ApiOperation("获取所有品牌列表")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsBrand>> getBrandList() {return CommonResult.success(brandService.list());
    }

    @ApiOperation("增加品牌")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult createBrand(@RequestBody PmsBrand pmsBrand) {
        CommonResult commonResult;
        boolean result = brandService.save(pmsBrand);
        if (result) {commonResult = CommonResult.success(pmsBrand);
            LOGGER.debug("createBrand success:{}", pmsBrand);
        } else {commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("createBrand failed:{}", pmsBrand);
        }
        return commonResult;
    }

    @ApiOperation("更新指定 id 品牌信息")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateBrand(@RequestBody PmsBrand pmsBrand) {
        CommonResult commonResult;
        boolean result = brandService.updateById(pmsBrand);
        if (result) {commonResult = CommonResult.success(pmsBrand);
            LOGGER.debug("updateBrand success:{}", pmsBrand);
        } else {commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("updateBrand failed:{}", pmsBrand);
        }
        return commonResult;
    }

    @ApiOperation("删除指定 id 的品牌")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deleteBrand(@PathVariable("id") Long id) {boolean result = brandService.removeById(id);
        if (result) {LOGGER.debug("deleteBrand success :id={}", id);
            return CommonResult.success(null);
        } else {LOGGER.debug("deleteBrand failed :id={}", id);
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("分页查问品牌列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmsBrand>> listBrand(@RequestParam(value = "pageNum", defaultValue = "1")
                                                        @ApiParam("页码") Integer pageNum,
                                                        @RequestParam(value = "pageSize", defaultValue = "3")
                                                        @ApiParam("每页数量") Integer pageSize) {Page<PmsBrand> page = new Page<>(pageNum, pageSize);
        Page<PmsBrand> pageResult = brandService.page(page);
        return CommonResult.success(CommonPage.restPage(pageResult));
    }

    @ApiOperation("获取指定 id 的品牌详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsBrand> brand(@PathVariable("id") Long id) {return CommonResult.success(brandService.getById(id));
    }

}

我的项目源码地址

https://github.com/macrozheng…

本文 GitHub https://github.com/macrozheng/mall-learning 曾经收录,欢送大家 Star!

正文完
 0