最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot整合mybatis常見問題(小結(jié))

 更新時(shí)間:2020年12月25日 09:54:27   作者:哇啦哇啦哇  
這篇文章主要介紹了SpringBoot整合mybatis常見問題(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

Spring中常見問題

1.NoSuchBeanDefinitionException

2.'..Service' that could not be found service找不到

3.port 80 was already in use 端口號(hào)被占用

4.TemplateInputException 模板解析異?;蛘也坏侥0?/p>

  • 1.檢查模板所在的目錄是否與配置的前綴目錄相同
  • 2.檢查返回的模板是否存在,返回值類型是否一致
  • 3.檢查配置前綴時(shí)是否以"/"斜杠結(jié)尾
  • 4.控制層的url與客戶端的ur是否一致

5. 404異常 訪問資源不存在

6. 500異常 500異常要查看控制臺(tái)

Mybatis中常見問題

1.springboot中添加maven依賴

2.BadSqlGrammarException 錯(cuò)誤的sql語句

3.BindingException 綁定異常

  • 1.檢查映射文件的路徑配置與實(shí)際存儲(chǔ)位置是否一致
  • 2.檢查dao接口的類名是否與映射文件的namespace值相同(不能有空格)
  • 3.檢查dao接口中的方法名是否在映射文件中有對(duì)應(yīng)的id

4.IllegalArgumentException

原因:同樣說我sql映射是否出現(xiàn)了重復(fù)性的定義(例如:分別以注解方式和xml配置文件方式進(jìn)行定義,也就是說在同一個(gè)namespace下出現(xiàn)了重復(fù)的元素id)

5.SAXParseException xml解析問題

補(bǔ)充

問題一:Mapper類 autowired失敗

原因:掃描mapper包沒有配置或配置不正確

解決:

方案一:

1. 啟動(dòng)類加@MapperScan("mapperPackagePath")

方案二:

增加配置類:

package com.yx.readingwebsite.config;
 
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
/**
 * MapperScannerConfigurer 配置DAO層
 */
 
 
@Configuration
public class MyBatisMapperScannerConfig {
  @Bean
  public MapperScannerConfigurer getMapperScannerConfigurer(){
    MapperScannerConfigurer msc = new MapperScannerConfigurer();
    msc.setSqlSessionFactoryBeanName("sqlSessionFactory");
    msc.setBasePackage("com.yx.readingwebsite.mapper");
    return msc;
  }
}

問題二:Mapper掃描成功后,繼續(xù)報(bào)錯(cuò),org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

原因:xml的mapper SQL 和 Mapper接口沒有綁定

解決:

方案一:全局配置文件application.yml增加mybatis配置【xml mapper包在resource目錄下】

mybatis:
 mapper-locations: classpath:mapper/*.xml

方案二:增加配置類

package com.yx.readingwebsite.config;
 
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
 
import javax.sql.DataSource;
 
/**
 * 配置MyBatis,引入數(shù)據(jù)源,sqlSessionFactory,sqlSessionTemplate,事務(wù)管理器
 */
 
@Configuration //配置類
@EnableTransactionManagement //允許使用事務(wù)管理器
public class MyBatisModelConfig implements TransactionManagementConfigurer {
 
  @Autowired
  private DataSource dataSource;
 
  @Bean(name = "sqlSessionFactory")
  public SqlSessionFactory getSqlSessionFactory(){
    SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
    ssfb.setDataSource(dataSource); //設(shè)置數(shù)據(jù)源
    ssfb.setTypeAliasesPackage("com.yx.readingwebsite.model");  //設(shè)置掃描模型包【po】
    try {
      Resource[] resources = new PathMatchingResourcePatternResolver()
          .getResources("classpath:mapper/*.xml");
      ssfb.setMapperLocations(resources);
      return ssfb.getObject();
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException();
    }
  }
 
  @Bean  //獲得Session 模板,從而獲得Session
  public SqlSessionTemplate getSqlSessionTemplate(SqlSessionFactory sqlSessionFactory){
    return new SqlSessionTemplate(sqlSessionFactory);
  }
 
  @Override  //事務(wù)管理器
  public PlatformTransactionManager annotationDrivenTransactionManager() {
    return new DataSourceTransactionManager(dataSource);
  }
}

需要注意的是,xml版的mybatis一定要在sqlSessionFactory中指定mapperLocations,即下圖

總結(jié):
兩種配置方案。方案一,使用配置類;方案二,使用配置文件。完整配置如下:

方案一:配置類

package com.yx.readingwebsite.config;
 
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
 
import javax.sql.DataSource;
 
/**
 * 配置MyBatis,引入數(shù)據(jù)源,sqlSessionFactory,sqlSessionTemplate,事務(wù)管理器
 */
 
@Configuration //配置類
@EnableTransactionManagement //允許使用事務(wù)管理器
public class MyBatisModelConfig implements TransactionManagementConfigurer {
 
  @Autowired
  private DataSource dataSource;
 
  @Bean(name = "sqlSessionFactory")
  public SqlSessionFactory getSqlSessionFactory(){
    SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
    ssfb.setDataSource(dataSource); //設(shè)置數(shù)據(jù)源
    ssfb.setTypeAliasesPackage("com.yx.readingwebsite.model");  //設(shè)置掃描模型包【po】
    try {
      Resource[] resources = new PathMatchingResourcePatternResolver()
          .getResources("classpath:mapper/*.xml");
      ssfb.setMapperLocations(resources);
      return ssfb.getObject();
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException();
    }
  }
 
  @Bean  //獲得Session 模板,從而獲得Session
  public SqlSessionTemplate getSqlSessionTemplate(SqlSessionFactory sqlSessionFactory){
    return new SqlSessionTemplate(sqlSessionFactory);
  }
 
  @Override  //事務(wù)管理器
  public PlatformTransactionManager annotationDrivenTransactionManager() {
    return new DataSourceTransactionManager(dataSource);
  }
}
package com.yx.readingwebsite.config;
 
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
/**
 * MapperScannerConfigurer 配置DAO層
 */
 
 
@Configuration
@AutoConfigureAfter(MyBatisModelConfig.class)
public class MyBatisMapperScannerConfig {
  @Bean
  public MapperScannerConfigurer getMapperScannerConfigurer(){
    MapperScannerConfigurer msc = new MapperScannerConfigurer();
    msc.setSqlSessionFactoryBeanName("sqlSessionFactory");
    msc.setBasePackage("com.yx.readingwebsite.mapper");
    return msc;
  }
}

方案二:配置文件 application.yml

spring:
 datasource:
  url: jdbc:mysql://127.0.0.1:3306/readingWebsite?useUnicode=true&characterEncoding=utf-8
  username:
  password:
  driver-class-name: com.mysql.jdbc.Driver
  max-active: 100
  max-idle: 10
  max-wait: 10000
  default-auto-commit: false
  time-between-eviction-runs-millis: 30000
  min-evictable-idle-time-millis: 30000
  test-while-idle: true
  test-on-borrow: true
  test-on-return: true
  validation-query: SELECT 1
 
mybatis:
 mapper-locations: classpath:mapper/*.xml
package com.yx.readingwebsite;
 
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
@MapperScan("com.yx.readingwebsite")
public class ReadingWebsiteApplication {
 
  public static void main(String[] args) {
    SpringApplication.run(ReadingWebsiteApplication.class, args);
  }
 
}

到此這篇關(guān)于SpringBoot整合mybatis常見問題(小結(jié))的文章就介紹到這了,更多相關(guān)SpringBoot整合mybatis問題內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 如何使用Java 8 中的 Stream 遍歷樹形結(jié)構(gòu)

    如何使用Java 8 中的 Stream 遍歷樹形結(jié)構(gòu)

    這篇文章主要介紹了使用Java 8中的Stream遍歷樹形結(jié)構(gòu),我們可以使用Java8中的Stream流一次性把數(shù)據(jù)查出來,然后通過流式處理,我們一起來看看,代碼實(shí)現(xiàn)為了實(shí)現(xiàn)簡(jiǎn)單,就模擬查看數(shù)據(jù)庫所有數(shù)據(jù)到List里面,需要的朋友可以參考下
    2023-08-08
  • 新手學(xué)習(xí)Java對(duì)Redis簡(jiǎn)單操作

    新手學(xué)習(xí)Java對(duì)Redis簡(jiǎn)單操作

    這篇文章主要介紹了新手學(xué)習(xí)Java對(duì)Redis簡(jiǎn)單操作,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Java之多個(gè)線程順序循環(huán)執(zhí)行的幾種實(shí)現(xiàn)

    Java之多個(gè)線程順序循環(huán)執(zhí)行的幾種實(shí)現(xiàn)

    這篇文章主要介紹了Java之多個(gè)線程順序循環(huán)執(zhí)行的幾種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 在SpringBoot中靜態(tài)資源訪問方法

    在SpringBoot中靜態(tài)資源訪問方法

    這篇文章給大家介紹了在SpringBoot中靜態(tài)資源訪問方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-11-11
  • java char數(shù)據(jù)類型原理解析

    java char數(shù)據(jù)類型原理解析

    這篇文章主要介紹了java char數(shù)據(jù)類型原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Java多線程中Lock鎖的使用小結(jié)

    Java多線程中Lock鎖的使用小結(jié)

    這篇文章主要介紹了Java多線程中Lock鎖的使用小結(jié),本節(jié)主要講了它的基本使用,大家可以舉一反三,試試什么條件下會(huì)導(dǎo)致死鎖,需要的朋友可以參考下
    2022-06-06
  • 詳解SpringBoot之添加單元測(cè)試

    詳解SpringBoot之添加單元測(cè)試

    本篇文章主要介紹了詳解SpringBoot之添加單元測(cè)試,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07
  • Java線程池的簡(jiǎn)單使用方法實(shí)例教程

    Java線程池的簡(jiǎn)單使用方法實(shí)例教程

    線程的使用在java中占有極其重要的地位,在jdk1.4極其之前的jdk版本中,關(guān)于線程池的使用是極其簡(jiǎn)陋的,在jdk1.5之后這一情況有了很大的改,這篇文章主要給大家介紹了關(guān)于Java線程池的簡(jiǎn)單使用方法,需要的朋友可以參考下
    2021-10-10
  • jpa異常No entity found for query問題解決

    jpa異常No entity found for query問題解決

    這篇文章主要為大家介紹了jpa異常之No entity found for query的異常問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • SpringBoot使用Scheduling實(shí)現(xiàn)定時(shí)任務(wù)的示例代碼

    SpringBoot使用Scheduling實(shí)現(xiàn)定時(shí)任務(wù)的示例代碼

    Spring Boot提供了一種方便的方式來實(shí)現(xiàn)定時(shí)任務(wù),即使用Spring的@Scheduled注解,通過在方法上添加@Scheduled注解,我們可以指定方法在何時(shí)執(zhí)行,本文我們就給大家介紹一下SpringBoot如何使用Scheduling實(shí)現(xiàn)定時(shí)任務(wù),需要的朋友可以參考下
    2023-08-08

最新評(píng)論

克东县| 美姑县| 沙河市| 刚察县| 尼勒克县| 横山县| 溆浦县| 丰原市| 马关县| 德惠市| 长春市| 康保县| 禹州市| 肥东县| 小金县| 阿勒泰市| 宣化县| 永济市| 右玉县| 永年县| 东阳市| 密山市| 道真| 仪陇县| 卫辉市| 长治市| 德格县| 五河县| 德化县| 邯郸市| 临朐县| 密云县| 若尔盖县| 苏尼特右旗| 扶余县| 瓮安县| 山阴县| 大丰市| 岐山县| 当涂县| 新郑市|