天天看点

mybatis plus 插入生成id_springboot整合MybatisPlus 实现代码生成增删改查

点击上方蓝色字体,选择“标星公众号”

优质文章,第一时间送达

66套java从入门到精通实战课程分享

springboot整合Mybatis-Plus 实现代码生成增删改查

spring boot 2.x

用user_plus表为实例

sql结构

pom文件

application文件

设置Mybatis-Plus分页

代码生成工具,自行改动生成代码的存放位置

     * 读取控制台内容      * 

     */     public static String scanner(String tip) {         Scanner scanner = new Scanner(System.in);         StringBuilder help = new StringBuilder();         help.append("请输入" + tip + ":");         System.out.println(help.toString());         if (scanner.hasNext()) {             String ipt = scanner.next();             if (StringUtils.isNotEmpty(ipt)) {                 return ipt;             }         }         throw new MybatisPlusException("请输入正确的" + tip + "!");     }  //运行,填数据库表名开始生成代码     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("vicente");         gc.setOpen(false);         // service 命名方式         gc.setServiceName("%sService");         // service impl 命名方式         gc.setServiceImplName("%sServiceImpl");         // 自定义文件命名,注意 %s 会自动填充表实体属性!         gc.setMapperName("%sMapper");         gc.setXmlName("%sMapper");         gc.setFileOverride(true);         gc.setActiveRecord(true);         // XML 二级缓存         gc.setEnableCache(false);         // XML ResultMap         gc.setBaseResultMap(true);         // XML columList         gc.setBaseColumnList(false);         // gc.setSwagger2(true); 实体属性 Swagger2 注解         mpg.setGlobalConfig(gc);         // 数据源配置         DataSourceConfig dsc = new DataSourceConfig();         dsc.setUrl("jdbc:mysql://127.0.0.1:3306/springboot-test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true");         dsc.setDriverName("com.mysql.cj.jdbc.Driver");         dsc.setUsername("root");         dsc.setPassword("root");         mpg.setDataSource(dsc);         // 包配置         PackageConfig pc = new PackageConfig();         pc.setParent("cn.theone.tmp.mybatisplus");         pc.setEntity("model");         pc.setService("service");         pc.setServiceImpl("service.impl");         pc.setController("controller");         mpg.setPackageInfo(pc);         // 自定义配置         InjectionConfig cfg = new InjectionConfig() {             @Override             public void initMap() {                 // to do nothing             }         };         // 如果模板引擎是 freemarker         String templatePath = "/templates/mapper.xml.ftl";         // 如果模板引擎是 velocity         // String templatePath = "/templates/mapper.xml.vm";         // 自定义输出配置         List focList = new ArrayList<>();         // 自定义配置会被优先输出         focList.add(new FileOutConfig(templatePath) {             @Override             public String outputFile(TableInfo tableInfo) {                 // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!                 String moduleName = pc.getModuleName() == null ? "" : pc.getModuleName();return projectPath + "/src/main/resources/mybatisplus/mapper/" + moduleName                         + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT + "xml";             }         });         cfg.setFileOutConfigList(focList);         mpg.setCfg(cfg);         // 配置模板         TemplateConfig templateConfig = new TemplateConfig();         templateConfig.setXml(null);         mpg.setTemplate(templateConfig);         // 策略配置         StrategyConfig strategy = new StrategyConfig();         strategy.setNaming(NamingStrategy.underline_to_camel);         strategy.setColumnNaming(NamingStrategy.underline_to_camel);         strategy.setEntityLombokModel(true);         strategy.setRestControllerStyle(true);         strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));         strategy.setControllerMappingHyphenStyle(true);         strategy.setTablePrefix(pc.getModuleName() + "_");         mpg.setStrategy(strategy);         mpg.setTemplateEngine(new FreemarkerTemplateEngine());         mpg.execute();     } }

这是我的完整目录结构,怕出错的朋友可以按照此目录操作

mybatis plus 插入生成id_springboot整合MybatisPlus 实现代码生成增删改查

启动类加上扫描mapper注解

实体类

Mapper类

Service接口和实现类

Mapper.xml类,默认是没有findAll方法的,自行扩展的接口

写测试类测试

默认Mybatis-Plus已经有非常全面的接口了,可以满足大部分要求,有满足不了的需求可以直接扩展接口xml中写sql即可

mybatis plus 插入生成id_springboot整合MybatisPlus 实现代码生成增删改查

至此Spring boot整合Mybatis-Plus 就完毕了

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:

https://blog.csdn.net/zgc55987/article/details/108941909

粉丝福利:108本java从入门到大神精选电子书领取

👇👇👇

mybatis plus 插入生成id_springboot整合MybatisPlus 实现代码生成增删改查
👆长按上方锋哥微信二维码 2 秒备注「1234」即可获取资料以及可以进入java1234官方微信群
           

感谢点赞支持下哈 

mybatis plus 插入生成id_springboot整合MybatisPlus 实现代码生成增删改查

继续阅读