mybatis

您所在的位置:网站首页 苞米豆mybatis的page mybatis

mybatis

2023-05-03 04:21| 来源: 网络整理| 查看: 265

com.baomidou mybatis-plus-boot-starter 3.3.1.tmp

com.baomidou mybatis-plus-generator 3.3.1.tmp org.apache.velocity velocity 1.7

手动创建表后生成

package com.shaohao.tianmaomall;

import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.annotation.FieldFill; 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.po.TableFill; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList; import java.util.List;

public class Main {

public static void main (String [] args) { AutoGenerator autoGenerator = new AutoGenerator(); DataSourceConfig dataSourceConfig = new DataSourceConfig(); dataSourceConfig.setDbType(DbType.MYSQL); dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver"); dataSourceConfig.setUsername("root"); dataSourceConfig.setPassword("12345678"); dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=UTF-8"); autoGenerator.setDataSource(dataSourceConfig); GlobalConfig globalConfig = new GlobalConfig(); globalConfig.setOpen(true); globalConfig.setOutputDir(System.getProperty("user.dir")+"/src/main/java"); globalConfig.setAuthor("shaohao"); globalConfig.setServiceName("%sService"); autoGenerator.setGlobalConfig(globalConfig); PackageConfig packageConfig = new PackageConfig(); packageConfig.setParent("com.shaohao.tianmaomall"); packageConfig.setEntity("entity"); packageConfig.setMapper("mapper"); packageConfig.setController("controller"); packageConfig.setService("service"); packageConfig.setServiceImpl("service.impl"); autoGenerator.setPackageInfo(packageConfig); StrategyConfig strategyConfig = new StrategyConfig(); strategyConfig.setEntityLombokModel(true); strategyConfig.setNaming(NamingStrategy.underline_to_camel); strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel); List list = new ArrayList(); TableFill tableFill = new TableFill("creat_time", FieldFill.INSERT); TableFill tableFill2 = new TableFill("update_time", FieldFill.INSERT_UPDATE); list.add(tableFill); list.add(tableFill2); strategyConfig.setTableFillList(list); autoGenerator.setStrategy(strategyConfig); autoGenerator.execute(); }

}

添加依赖

com.baomidou mybatis-plus-generator 3.4.0

添加 模板引擎 依赖 MyBatis-Plus 支持 Velocity(默认)、Freemarker、Beetl,用户可以选择自己熟悉的模板引擎,如果都不满足您的要求,可以采用自定义模板引擎。我这里就是使用自定义模板

org.apache.velocity velocity-engine-core 2.2 ———————————————— 编写配置

MyBatis-Plus 的代码生成器提供了大量的自定义参数供用户选择,能够满足绝大部分人的使用需求。

public class CodeGenerator {

public static void main(String[] args) { //创建代码生成器 AutoGenerator mpg = new AutoGenerator(); //全局配置 GlobalConfig gc = new GlobalConfig(); //获取当前系统目录 String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/src/main/java"); gc.setAuthor("dong"); //生成作者注释 gc.setOpen(false); //生成后是否打开资源管理器 gc.setFileOverride(false); //重新生成时文件是否覆盖 gc.setServiceName("%sService"); //去掉Service接口的首字母I gc.setIdType(IdType.ID_WORKER); //主键策略 gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型 gc.setSwagger2(false);//开启Swagger2模式 mpg.setGlobalConfig(gc); // 3、数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/mp?serverTimezone=GMT%2B8"); //url dsc.setDriverName("com.mysql.cj.jdbc.Driver"); //数据库驱动 dsc.setUsername("root"); //数据库账号 dsc.setPassword("root"); //数据库密码 dsc.setDbType(DbType.MYSQL); //数据库类型 mpg.setDataSource(dsc); // 4、包配置 PackageConfig pc = new PackageConfig(); pc.setModuleName("test"); //模块名,可以不设置 pc.setParent("cn.dong"); //放在哪个包下 pc.setController("controller"); pc.setEntity("entity"); pc.setService("service"); pc.setMapper("mapper"); mpg.setPackageInfo(pc); // 5、策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setInclude("employee");//对哪一张表生成代码 strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略 strategy.setTablePrefix(pc.getModuleName() + "_"); //生成实体时去掉表前缀 strategy.setLogicDeleteFieldName("deleted"); //逻辑删除字段设置 strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略 strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作 strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符 mpg.setStrategy(strategy); //执行 mpg.execute(); }

} ————————————————



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3