一、mybatis开发问题

  1. 需要自己写实体
  2. 需要自己写xml文件和对应的xml中的sql

那是不是存在一种对于通用的功能做很好支持的插件功能:mybatis-plus

二、解决的问题:

  1. 代码生成-生成我们需要的control、service、实体、Mapper、mapper.xml等文件
  2. 自带增删改查的支持,不需要太多的自我配置

三、整合

1)xml-只列出了特殊使用的几个jar包,特别注意版本、不同的版本有些代码有差异


      <!-- 代码生成器 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.4.1</version>
        </dependency>
        <!-- 模板引擎 -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.0</version>
        </dependency>

View Code

 

 

2)代码生成器-使用这个可以直接生成 control、service、实体、Mapper、mapper.xml等文件代码

  


public class GeneratorUtils {
//    public static String  PROJECT_PATH = "";


    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        mpg.setGlobalConfig(GeneratorUtils.globalConfig());
        mpg.setDataSource(GeneratorUtils.dataSourceConfig());
        mpg.setPackageInfo(GeneratorUtils.packageConfig("com.baomidou.ant"));
        mpg.setCfg(GeneratorUtils.injectionConfig());
        mpg.setTemplate(GeneratorUtils.templateConfig());
        //参数是表名称,也可以是表名称数组
        mpg.setStrategy(GeneratorUtils.strategyConfig("test_user"));
        mpg.setTemplateEngine(new VelocityTemplateEngine());
        mpg.execute();
    }
    
    public static Scanner scanner = new Scanner(System.in);
    // 获得当前项目的路径,如上图就是 F:stady_program/Mybatis_plus逆向工程
    public final static String PROJECT_PATH = System.getProperty("user.dir");
    public static String partentName = "com.baomidou.ant";

    // 最简单的配置
    public static TemplateConfig templateConfig() {
        TemplateConfig templateConfig = new TemplateConfig();


        return templateConfig.setXml(null);
    }





    // 判断输入的父项目是否存在
    public static boolean parentPackageExits(String project, String parentPackageName){
        StringBuilder path = new StringBuilder(PROJECT_PATH);
        if(!"".equals(project) && project != null){
            path.append("/" + project);
        }
        path.append(("/src/main/java/" + parentPackageName).replace(".","/"));
        File file = new File(path.toString());
        return file.exists();
    }



    public static InjectionConfig injectionConfig(){
        InjectionConfig injectionConfig = new InjectionConfig() {
            @Override
            public void initMap() {
                this.setMap(new HashMap<>()); // 实现InjectionConfig抽象类就需要初始化一个Map集合
            }
        };
        List<FileOutConfig> fileOutConfigList = new ArrayList<>();
        // 根据/templates/mapper.xml.ftl规则在指定的位置生成Mapper文件,可以在多个地方生成。
        String templatePath = "/templates/mapper.xml.vm";
//        String templatePath = "/templates/mapper.xml.ftl";
        fileOutConfigList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 返回Mapper文件的绝对路径
                String path = PROJECT_PATH + "/src/main/resources/mapper/" +
                        tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
                return path;
            }
        });
        // 将对Mapper文件的配置添加到文件输出对象中
        injectionConfig.setFileOutConfigList(fileOutConfigList);
        return injectionConfig;
    }


    public static StrategyConfig strategyConfig (String tableName){
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setCapitalMode(true)// 开启全局大写命名
                .setInclude(tableName)// 设置要映射的表
                .setNaming(NamingStrategy.underline_to_camel)// 下划线到驼峰的命名方式
                .setColumnNaming(NamingStrategy.underline_to_camel)// 下划线到驼峰的命名方式
                .setEntityColumnConstant(true)
                .setEntityTableFieldAnnotationEnable(true) //生成的实体有 对应的注解对应
                .setEntityLombokModel(true)// 是否使用lombok
                .setRestControllerStyle(true)// 是否开启rest风格
                .setTablePrefix("t_") // 去除前缀
                .setControllerMappingHyphenStyle(true); // localhost:8080/hello_a_2

        return strategyConfig;
    }


    public static PackageConfig packageConfig(String fatherPackageName){
        fatherPackageName = partentName;
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent(fatherPackageName) // 配置指定项目中各层的名称
                .setController("controller") // Controller层
                .setEntity("entity") // 实体层(pojo层)
                .setMapper("dao") // Dao 层
                .setService("service") // service层
                .setServiceImpl("service.serviceImpl"); // ServiceImp层

        return packageConfig;
    }


    public static GlobalConfig globalConfig(){
        GlobalConfig globalConfig = new GlobalConfig();
//        GlobalConfig gc = new GlobalConfig();

        // 输出文件路径
        globalConfig.setOutputDir(PROJECT_PATH + "/src/main/java")
                .setAuthor("Time Travel")// 设置作者名字
                .setOpen(false)// 是否打开资源管理器
                .setFileOverride(true)// 是否覆盖原来生成的
                .setIdType(IdType.AUTO)// 主键策略
                .setBaseResultMap(true)// 生成resultMap
                .setDateType(DateType.ONLY_DATE) // 设置时间格式,采用Date
                .setServiceName("%sService");// 生成的service接口名字首字母是否为I,这样设置就没有I
        //setBaseColumnList(true)  XML中生成基础列
        return globalConfig;
    }


    public static DataSourceConfig dataSourceConfig(){
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/test11?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai")
                .setUsername("root1")
                .setPassword("root1")
                .setDriverName("com.mysql.jdbc.Driver").setDbType(DbType.MYSQL);

        return dataSourceConfig;
    }
 
}

View Code

 

原文地址:http://www.cnblogs.com/lean-blog/p/16908148.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性