由于原来项目中已有很多功能和包,想迁移到Mybatis-Plus,旧的还是继续用 Mybatis和PageHelper,新的准备全部用Mybatis-Plus。迁移遇到了各种错误,记录一下,特别是这个错误:mybatis-plus org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):,花了差不多一天时间,都差点准备撤子模块了,将旧的一个模块,新的一个模块。
一、Mybatis-Plus依赖
后面还准备新建对象,把代码生成器也加进来了。
<!-- SpringBoot集成mybatis plus框架 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis.plus.version}</version> </dependency> <!-- mybatis-plus-generator 代码生成器 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>${mybatis.plus.generator.version}</version> </dependency> <!-- mybatis plus生成代码的模板引擎 --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>${velocity.engine.version}</version> </dependency>
在这儿遇到第一个问题,原模板有Velocity 1.7版本,在代码生成器中有需要用velocity-engine-core,这两个不能同时引用,会有冲突。将Velocity引用去掉,在服务器监控程序有一个下面语句不能用,注释掉,好像没有什么影响。
p.setProperty(Velocity.OUTPUT_ENCODING, Constants.UTF8);
二、创建代码生成器
参考官方文档,找个单独的包,创建代码生成器。在原来的模块上增加后,各种不能使用,没有办法,新建了一个全新的文件,生产对象代码,创建测试对象,可以运行,到了原来的程序上好多问题,后检查大部分是引用包之间版本冲突造成,主要是:
1、不要保留Mybatis的依赖,用最新的Mybatis-plus-boot-start就行
2、Mybatis-plus版本也会不影响,我用的是3.3.1
3、包的位置影响很大,接口文件一定要在@MapperScan(“com.xiyou.project.**.mapper”)包含的目录下,
4、配置文件要正确,生成代码要在配置文件包含下:
# MyBatis-plus配置 mybatis-plus: # 搜索指定包别名 type-aliases-package: com.xiyou.project.**.domain # 配置mapper的扫描,找到所有的mapper.xml映射文件 mapper-locations: classpath*:mybatis/**/*Mapper.xml
// 执行 main 方法控制台输入模块表名回车自动生成对应项目目录中 public class MpGenerator { /** * <p> * 读取控制台内容 * </p> */ 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("wu jize"); gc.setOpen(false); //实体属性 Swagger2 注解 gc.setSwagger2(true); mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:33306/xiyou?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8"); // dsc.setSchemaName("public"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("123123"); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); pc.setModuleName(scanner("模块名")); **//生成对模块数据对像的代码保存地** pc.setParent("com.xiyou.project");<b style="color:transparent">本文来源gao@!dai!ma.com搞$$代^@码网*</b> **//pojo对象缺省是entity目录,为了与以前的一致,改为domain** pc.setEntity("domain"); 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<FileOutConfig> focList = new ArrayList<>(); // 自定义配置会被优先输出 focList.add(new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!! return projectPath + "/src/main/resources/mybatis/" + pc.getModuleName() + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } }); /* cfg.setFileCreate(new IFileCreate() { @Override public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) { // 判断自定义文件夹是否需要创建 checkDir("调用默认方法创建的目录"); return false; } }); */ cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // 配置模板 TemplateConfig templateConfig = new TemplateConfig(); // 配置自定义输出模板 //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别 // templateConfig.setEntity("templates/entity2.java"); // templateConfig.setService(); // templateConfig.setController(); 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.setSuperControllerClass("com.xiyou.framework.web.controller.BaseController"); //strategy.setSuperEntityClass("com.xiyou.framework.web.domain.BaseEntity"); // 写于父类中的公共字段 //strategy.setSuperEntityColumns("id"); strategy.setInclude(scanner("表名,多个英文逗号分割").split(",")); strategy.setControllerMappingHyphenStyle(true); //xml文件名前再增加模块名,不需要,加了重复了 //strategy.setTablePrefix(pc.getModuleName() + "_"); mpg.setStrategy(strategy); //mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); }