欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis遷移到Mybatis-Plus的實(shí)現(xiàn)方法

 更新時(shí)間:2020年08月28日 14:19:21   作者:海鹽老伍  
這篇文章主要介紹了Mybatis遷移到Mybatis-Plus的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

由于原來(lái)項(xiàng)目中已有很多功能和包,想遷移到Mybatis-Plus,舊的還是繼續(xù)用 Mybatis和PageHelper,新的準(zhǔn)備全部用Mybatis-Plus。遷移遇到了各種錯(cuò)誤,記錄一下,特別是這個(gè)錯(cuò)誤:mybatis-plus org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):,花了差不多一天時(shí)間,都差點(diǎn)準(zhǔn)備撤子模塊了,將舊的一個(gè)模塊,新的一個(gè)模塊。

一、Mybatis-Plus依賴

后面還準(zhǔn)備新建對(duì)象,把代碼生成器也加進(jìn)來(lái)了。

<!-- 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>

在這兒遇到第一個(gè)問(wèn)題,原模板有Velocity 1.7版本,在代碼生成器中有需要用velocity-engine-core,這兩個(gè)不能同時(shí)引用,會(huì)有沖突。將Velocity引用去掉,在服務(wù)器監(jiān)控程序有一個(gè)下面語(yǔ)句不能用,注釋掉,好像沒(méi)有什么影響。
p.setProperty(Velocity.OUTPUT_ENCODING, Constants.UTF8);

二、創(chuàng)建代碼生成器

參考官方文檔,找個(gè)單獨(dú)的包,創(chuàng)建代碼生成器。在原來(lái)的模塊上增加后,各種不能使用,沒(méi)有辦法,新建了一個(gè)全新的文件,生產(chǎn)對(duì)象代碼,創(chuàng)建測(cè)試對(duì)象,可以運(yùn)行,到了原來(lái)的程序上好多問(wèn)題,后檢查大部分是引用包之間版本沖突造成,主要是:
1、不要保留Mybatis的依賴,用最新的Mybatis-plus-boot-start就行
2、Mybatis-plus版本也會(huì)不影響,我用的是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
// 執(zhí)行 main 方法控制臺(tái)輸入模塊表名回車自動(dòng)生成對(duì)應(yīng)項(xiàng)目目錄中
public class MpGenerator {
 /**
  * <p>
  * 讀取控制臺(tái)內(nèi)容
  * </p>
  */
 public static String scanner(String tip) {
  Scanner scanner = new Scanner(System.in);
  StringBuilder help = new StringBuilder();
  help.append("請(qǐng)輸入" + tip + ":");
  System.out.println(help.toString());
  if (scanner.hasNext()) {
   String ipt = scanner.next();
   if (StringUtils.isNotEmpty(ipt)) {
    return ipt;
   }
  }
  throw new MybatisPlusException("請(qǐng)輸入正確的" + 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);
  //實(shí)體屬性 Swagger2 注解
  gc.setSwagger2(true);
  mpg.setGlobalConfig(gc);
  // 數(shù)據(jù)源配置
  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("模塊名"));
  **//生成對(duì)模塊數(shù)據(jù)對(duì)像的代碼保存地**
  pc.setParent("com.xiyou.project");
  **//pojo對(duì)象缺省是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<>();
  // 自定義配置會(huì)被優(yōu)先輸出
  focList.add(new FileOutConfig(templatePath) {
   @Override
   public String outputFile(TableInfo tableInfo) {
    // 自定義輸出文件名 , 如果你 Entity 設(shè)置了前后綴、此處注意 xml 的名稱會(huì)跟著發(fā)生變化?。?
    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) {
    // 判斷自定義文件夾是否需要?jiǎng)?chuàng)建
    checkDir("調(diào)用默認(rèn)方法創(chuàng)建的目錄");
    return false;
   }
  });
  */
  cfg.setFileOutConfigList(focList);
  mpg.setCfg(cfg);
  // 配置模板
  TemplateConfig templateConfig = new TemplateConfig();
  // 配置自定義輸出模板
  //指定自定義模板路徑,注意不要帶上.ftl/.vm, 會(huì)根據(jù)使用的模板引擎自動(dòng)識(shí)別
  // 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("表名,多個(gè)英文逗號(hào)分割").split(","));
  strategy.setControllerMappingHyphenStyle(true);
  //xml文件名前再增加模塊名,不需要,加了重復(fù)了
  //strategy.setTablePrefix(pc.getModuleName() + "_");
  mpg.setStrategy(strategy);
  //mpg.setTemplateEngine(new FreemarkerTemplateEngine());
  mpg.execute();
 }

三、 Invalid bound statement (not found)問(wèn)題

這個(gè)問(wèn)題搞的時(shí)間最長(zhǎng),比較復(fù)雜,在官網(wǎng)上是如下描述,比較簡(jiǎn)單:

  • 檢查是不是引入 jar 沖突 檢查 Mapper.java 的掃描路徑 檢查是否指定了主鍵?如未指定,則會(huì)導(dǎo)致 selectById 相關(guān);
  • ID 無(wú)法操作,請(qǐng)用注解 @TableId 注解表 ID 主鍵。當(dāng)然 @TableId 注解可以沒(méi)有!但是你的主鍵必須叫
  • id(忽略大小寫) SqlSessionFactory不要使用原生的,請(qǐng)使用MybatisSqlSessionFactory
  • 檢查是否自定義了SqlInjector,是否復(fù)寫了getMethodList()方法,該方法里是否注入了你需要的方法

上面方法一遍又一遍查找,沒(méi)有發(fā)現(xiàn)問(wèn)題,我在全新模塊測(cè)試對(duì)比沒(méi)有發(fā)現(xiàn)任何問(wèn)題,后來(lái)從第參考文章的看到SqlSessionFactory問(wèn)題得到啟發(fā),重點(diǎn)研究這個(gè)問(wèn)題,查然解決了。
 原來(lái)的程序有Mybatis的配置文件Bean,需要替換為Mybatis-Plus的Bean,代碼如下:

@Configuration
//@MapperScan(basePackages = "com.xiyou.project.map.mapper")
public class MybatisPlusConfig {
 @Autowired
 private DataSource dataSource;

 @Autowired
 private MybatisPlusProperties properties;

 @Autowired
 private ResourceLoader resourceLoader = new DefaultResourceLoader();

 @Autowired(required = false)
 private Interceptor[] interceptors;

 @Autowired(required = false)
 private DatabaseIdProvider databaseIdProvider;

 @Autowired
 private Environment env;
 /**
  * * mybatis-plus分頁(yè)插件
  *  
  */
 @Bean
 public PaginationInterceptor paginationInterceptor() {
  PaginationInterceptor page = new PaginationInterceptor();
  page.setDialect(new MySqlDialect());
  return page;
 }

 /**
  * * 這里全部使用mybatis-autoconfigure 已經(jīng)自動(dòng)加載的資源。不手動(dòng)指定 配置文件和mybatis-boot的配置文件同步
  * * 
  * * @return
  * * @throws IOException
  * 
  */
 @Bean
 public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() throws IOException {
  MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
  mybatisPlus.setDataSource(dataSource);
  mybatisPlus.setVfs(SpringBootVFS.class);
  String configLocation = this.properties.getConfigLocation();
  if (StringUtils.isNotBlank(configLocation)) {
   mybatisPlus.setConfigLocation(this.resourceLoader.getResource(configLocation));
  }
  mybatisPlus.setConfiguration(properties.getConfiguration());
  mybatisPlus.setPlugins(this.interceptors);
  MybatisConfiguration mc = new MybatisConfiguration();
  mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
  // 數(shù)據(jù)庫(kù)和java都是駝峰,就不需要,
  //mc.setMapUnderscoreToCamelCase(false);
  mybatisPlus.setConfiguration(mc);
  if (this.databaseIdProvider != null) {
   mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider);
  }
  mybatisPlus.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
  mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
  mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations());
  // 設(shè)置mapper.xml文件的路徑
  String mapperLocations = env.getProperty("mybatis-plus.mapper-locations");
  ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
  Resource[] resource = resolver.getResources(mapperLocations);
  mybatisPlus.setMapperLocations(resource);
  return mybatisPlus;
 }
}

替換原來(lái)的Mybatis配置文件Bean后,仍然發(fā)以下兩個(gè)問(wèn)題:

1、我的application.yml配置文件有配置文件選項(xiàng),在上面配置文件中也要加載configration內(nèi)容,存沖突,報(bào)下面錯(cuò)誤。
Property ‘configuration' and ‘configLocation' can not specified with together
將application.yml文件中面來(lái)配置項(xiàng)刪除。
config-Location: classpath:mybatis/mybatis-config.xml

 2、查詢表時(shí),沒(méi)有正確處理字段中駝峰字段名,上面文件中有下面行,注釋掉即可。

//mc.setMapUnderscoreToCamelCase(false);

至此,將原來(lái)mybatis項(xiàng)目成功遷移到了mybatis-plus上。

到此這篇關(guān)于Mybatis遷移到Mybatis-Plus的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)Mybatis遷移到Mybatis-Plus內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解ThreadLocal為什么會(huì)內(nèi)存溢出原理

    詳解ThreadLocal為什么會(huì)內(nèi)存溢出原理

    這篇文章主要為大家介紹了ThreadLocal為什么會(huì)內(nèi)存溢出原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • 詳解spring boot容器加載完后執(zhí)行特定操作

    詳解spring boot容器加載完后執(zhí)行特定操作

    這篇文章主要介紹了詳解spring boot容器加載完后執(zhí)行特定操作,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • 詳解Spring Boot 使用slf4j+logback記錄日志配置

    詳解Spring Boot 使用slf4j+logback記錄日志配置

    本篇文章主要介紹了Spring Boot 使用slf4j+logback記錄日志配置,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05
  • Java實(shí)現(xiàn)異步執(zhí)行的8種方式總結(jié)

    Java實(shí)現(xiàn)異步執(zhí)行的8種方式總結(jié)

    這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)異步執(zhí)行的8種方式,異步編程不會(huì)阻塞程序的執(zhí)行,它將耗時(shí)的操作提交給后臺(tái)線程或其他執(zhí)行環(huán)境,并立即返回,使得程序可以繼續(xù)執(zhí)行其他任務(wù),需要的朋友可以參考下
    2023-09-09
  • Java類的繼承原理與用法分析

    Java類的繼承原理與用法分析

    這篇文章主要介紹了Java類的繼承原理與用法,結(jié)合實(shí)例形式分析了java類的繼承相關(guān)原理、使用方法及操作注意事項(xiàng),需要的朋友可以參考下
    2020-02-02
  • Java Socket聊天室編程(二)之利用socket實(shí)現(xiàn)單聊聊天室

    Java Socket聊天室編程(二)之利用socket實(shí)現(xiàn)單聊聊天室

    這篇文章主要介紹了Java Socket聊天室編程(二)之利用socket實(shí)現(xiàn)單聊聊天室的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-09-09
  • 簡(jiǎn)單談?wù)凾hreadPoolExecutor線程池之submit方法

    簡(jiǎn)單談?wù)凾hreadPoolExecutor線程池之submit方法

    下面小編就為大家?guī)?lái)一篇簡(jiǎn)單談?wù)凾hreadPoolExecutor線程池之submit方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • 詳解mybatis中的if-else的嵌套使用

    詳解mybatis中的if-else的嵌套使用

    本文主要介紹了mybatis中的if-else的嵌套使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Java中Spring擴(kuò)展點(diǎn)詳解

    Java中Spring擴(kuò)展點(diǎn)詳解

    這篇文章主要介紹了Java中Spring技巧之?dāng)U展點(diǎn)的應(yīng)用,下文Spring容器的啟動(dòng)流程圖展開其內(nèi)容的相關(guān)資料,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-06-06
  • Spring條件注解沒(méi)生效該如何解決

    Spring條件注解沒(méi)生效該如何解決

    條件注解相信各位小伙伴都用過(guò),Spring?中的多環(huán)境配置?profile?底層就是通過(guò)條件注解來(lái)實(shí)現(xiàn)的,下面小編就來(lái)為大家介紹一下當(dāng)Spring條件注解沒(méi)生效時(shí)該如何解決,感興趣的可以了解下
    2023-09-09

最新評(píng)論