MyBatisPlus automatically generates the background package and code logic according to the database

Several more important configurations (there are in the code, the focus is here)

1. Code generation path

 gc.setOutputDir(path+"/src/main/java");//  代码生成路劲

2. Declare database parameters

DataSourceConfig dc=new DataSourceConfig();
            dc.setUrl("jdbc:mysql://127.0.0.1:3306/02-tingyu?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai");
            // dsc.setSchemaName("public");
            dc.setDriverName("com.mysql.jdbc.Driver");
            dc.setUsername("root");
            dc.setPassword("123456");

3. Declare the package structure

 PackageConfig pc=new PackageConfig();
            pc.setParent("com.bjsxt")
                    .setMapper("mapper")
                    .setService("service")
                    .setController("controller")
                    .setEntity("pojo")
                    .setXml("mapper");

4. Whether to set the entity class generation format lombok (the configuration in the complete code is generation, if you don’t need it, you can delete it)

 //5.策略生成
            StrategyConfig sc=new StrategyConfig();
            sc.setCapitalMode(true) //全局大写命名
                .setEntityLombokModel(true)//设置实体类生成格式为lombok
                .setNaming(NamingStrategy.underline_to_camel) // 数据库表映射到实体的命名策略
                .setTablePrefix("t_")
                .setInclude("t_admin" ,
                        "t_admin_role" ,
                        "t_company" ,
                        "t_host" ,
                        "t_host_power" ,
                        "t_married_person" ,
                        "t_menu" ,
                        "t_order" ,
                        "t_planner" ,
                        "t_role" ,
                        "t_role_menu"); // 生成的表,多个表继续传递即可,String类型的可变参数

The following is the completion code

 //1. 创建代码生成器对象
            AutoGenerator auto=new AutoGenerator();
        //2.声明全局配置策略
            GlobalConfig gc=new GlobalConfig();
            String path = System.getProperty("user.dir");//动态获取当前项目的路径
            System.out.println(path);
            gc.setFileOverride(false);// 是否覆盖同名文件,默认是false
            gc.setActiveRecord(true);// 不需要ActiveRecord特性的请改为false
            gc.setEnableCache(false);// XML 二级缓存
            gc.setBaseResultMap(true);// XML ResultMap
            gc.setBaseColumnList(true);// XML columList
            gc.setOutputDir(path+"/src/main/java");//  代码生成路劲
            gc.setIdType(IdType.AUTO);//设置主键策略
        //3.声明数据库参数
            DataSourceConfig dc=new DataSourceConfig();
            dc.setUrl("jdbc:mysql://127.0.0.1:3306/02-tingyu?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai");
            // dsc.setSchemaName("public");
            dc.setDriverName("com.mysql.jdbc.Driver");
            dc.setUsername("root");
            dc.setPassword("123456");
        //4.包配置参数
            PackageConfig pc=new PackageConfig();
            pc.setParent("com.huletian")
                    .setMapper("mapper")
                    .setService("service")
                    .setController("controller")
                    .setEntity("pojo")
                    .setXml("mapper");
        //5.策略生成
            StrategyConfig sc=new StrategyConfig();
            sc.setCapitalMode(true) //全局大写命名
                .setEntityLombokModel(true)//设置实体类生成格式为lombok
                .setNaming(NamingStrategy.underline_to_camel) // 数据库表映射到实体的命名策略
                .setTablePrefix("t_")
                .setInclude("t_admin" ,
                        "t_admin_role" ,
                        "t_company" ,
                        "t_host" ,
                        "t_host_power" ,
                        "t_married_person" ,
                        "t_menu" ,
                        "t_order" ,
                        "t_planner" ,
                        "t_role" ,
                        "t_role_menu"); // 生成的表,多个表继续传递即可,String类型的可变参数
        //6.将参数对象注入到代码生成器对象中
           auto.setGlobalConfig(gc);
           auto.setDataSource(dc);
           auto.setPackageInfo(pc);
           auto.setStrategy(sc);
        //7.执行生成
            auto.execute();
        System.out.println("生成成功");

Paste the code into the test test class for execution (note that you need to configure MybatisPlus dependency or jar)

Guess you like

Origin blog.csdn.net/weixin_44613100/article/details/106345184