您好,登錄后才能下訂單哦!
使用SpringBoot對mybatis進行整合需要注意哪些問題?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
1.NoSuchBeanDefinitionException
2.'..Service' that could not be found service找不到
3.port 80 was already in use 端口號被占用
4.TemplateInputException 模板解析異常或找不到模板
1.檢查模板所在的目錄是否與配置的前綴目錄相同
2.檢查返回的模板是否存在,返回值類型是否一致
3.檢查配置前綴時是否以"/"斜杠結尾
4.控制層的url與客戶端的ur是否一致
5. 404異常 訪問資源不存在
6. 500異常 500異常要查看控制臺
1.springboot中添加maven依賴
2.BadSqlGrammarException 錯誤的sql語句
3.BindingException 綁定異常
1.檢查映射文件的路徑配置與實際存儲位置是否一致
2.檢查dao接口的類名是否與映射文件的namespace值相同(不能有空格)
3.檢查dao接口中的方法名是否在映射文件中有對應的id
4.IllegalArgumentException
原因:同樣說我sql映射是否出現了重復性的定義(例如:分別以注解方式和xml配置文件方式進行定義,也就是說在同一個namespace下出現了重復的元素id)
5.SAXParseException xml解析問題
原因:掃描mapper包沒有配置或配置不正確
解決:
方案一:
1. 啟動類加@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; } }
原因: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,引入數據源,sqlSessionFactory,sqlSessionTemplate,事務管理器 */ @Configuration //配置類 @EnableTransactionManagement //允許使用事務管理器 public class MyBatisModelConfig implements TransactionManagementConfigurer { @Autowired private DataSource dataSource; @Bean(name = "sqlSessionFactory") public SqlSessionFactory getSqlSessionFactory(){ SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean(); ssfb.setDataSource(dataSource); //設置數據源 ssfb.setTypeAliasesPackage("com.yx.readingwebsite.model"); //設置掃描模型包【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 //事務管理器 public PlatformTransactionManager annotationDrivenTransactionManager() { return new DataSourceTransactionManager(dataSource); } }
需要注意的是,xml版的mybatis一定要在sqlSessionFactory中指定mapperLocations,即下圖
總結:
兩種配置方案。方案一,使用配置類;方案二,使用配置文件。完整配置如下:
方案一:配置類
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,引入數據源,sqlSessionFactory,sqlSessionTemplate,事務管理器 */ @Configuration //配置類 @EnableTransactionManagement //允許使用事務管理器 public class MyBatisModelConfig implements TransactionManagementConfigurer { @Autowired private DataSource dataSource; @Bean(name = "sqlSessionFactory") public SqlSessionFactory getSqlSessionFactory(){ SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean(); ssfb.setDataSource(dataSource); //設置數據源 ssfb.setTypeAliasesPackage("com.yx.readingwebsite.model"); //設置掃描模型包【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 //事務管理器 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); } }
看完上述內容,你們掌握使用SpringBoot對mybatis進行整合需要注意哪些問題的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。