时间:2021-05-20
1.NoSuchBeanDefinitionException
2.'..Service' that could not be found service找不到
3.port 80 was already in use 端口号被占用
4.TemplateInputException 模板解析异常或找不到模板
5. 404异常 访问资源不存在
6. 500异常 500异常要查看控制台
1.springboot中添加maven依赖
2.BadSqlGrammarException 错误的sql语句
3.BindingException 绑定异常
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层 */ @Configurationpublic 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/*.xmlpackage 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常见问题(小结)的文章就介绍到这了,更多相关SpringBoot整合mybatis问题内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
SpringBoot整合mybatis分页操作SpringBoot整合Mybatis进行分页操作,这里需要使用Mybatis的分页插件:pageHelper,关
前言本篇文章主要讲述的是springboot整合mybatis、druid和pagehelper并实现多数据源和分页。其中springboot整合mybatis
因为实习用的是MyBatis框架,所以写一篇关于SpringBoot整合MyBatis框架的总结。一,Pom文件
最近刚接触springboot,正是因为他的及简配置方便开发,促使我下定决心要用它把之前写的项目重构,那么问题来了,springboot怎么整合mybatis呢
mybatis我们常用的判空操作,出现了常见问题:错误写法:iftest=”status==‘Y'”结果:抛异常NumberFormatException异常!