SpringBoot整合mybatis常见问题(小结)
作者:哇啦哇啦哇 发布时间:2023-07-23 09:50:12
Spring中常见问题
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异常要查看控制台
Mybatis中常见问题
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类 autowired失败
原因:扫描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;
}
}
问题二:Mapper扫描成功后,继续报错,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,引入数据源,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);
}
}
来源:https://segmentfault.com/a/1190000038641171


猜你喜欢
- 目录android_os_MessageQueue.cppLooper.cpp1.epoll基础知识介绍epoll工作流程分析案例2. po
- 一.分类从实现的技术上来分类,目前主要有三种技术(或者说有三种产品):1.Java自带的java.util.Timer类,这个类允许你调度一
- Map是键值对的集合,又叫作字典或关联数组等,是最常见的数据结构之一。在java如何让一个map按value排序呢? 看似简单,但却不容易!
- 分部类(Partial Class)在C#2.0引入,分部方法(Partial Method)在C#3.0引入,这两个语法特性都具有相同的特
- hello,今天给大家带来一道算法题。这道算法题,是我目前为止,见过最难的一道题。那么到底是怎样的一道算法题呢?如下:题目:给定一个数组,
- Android的遮罩效果就是把一张图片盖在另一张图片的上面,通过控制任意一张图片的显示百分比实现遮罩效果。下面我使用两张一样的图片来实现一个
- 文档中添加印章可以起一定的作用,比如,防止文件随意被使用,或者确保文档内容的安全性和权威性。C#添加图片印章其实也有很多实现方法,这里我使用
- let 和 var(a): let 声明的变量只在 let 命令所在的代码块内有效(b): let 是在代码块内有效,var 是在全局范围内
- RestTemplate 请求接收自定义400+ 或500+错误场景当服务端自定义400错误返回体时,使用restTemplate 请求接收
- RandomAccessFileRandomAccessFile 是随机访问文件(包括读/写)的类。它支持对文件随机访问的读取和写入,即我们
- 先看英文意思命名空间using System.Text.RegularExpressions;正则表达式是干什么用的?简单来说就是 检索 数
- 方式一public class Test{ public static void main(String[] args) throws Ex
- 本文将介绍一种通过代码控制ListView上下滚动的方法。先上图:按下按钮会触发ListView滚动或停止。实现该功能并不难,下面给出主要代
- MyBatis 获取子类的属性这里有个model类:基类public class user { pu
- 引言当我们通过@ConfigurationProperties注解实现配置 bean的时候,如果默认的配置属性转换无法满足我们的需求的时候,
- eclipse中改变默然的workspace的方法可以有以下几种:1.在创建project的时候,手动选择使用新的workspace,如创建
- 这篇文章主要介绍了线程池中使用spring aop事务增强,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要
- Beanutils.copyProperties()用法及重写提高效率特别说明本文介绍的是Spring(import org.springf
- 刚开始我以为熔断和降级是一体的,以为他们必须配合使用; 只不过名字不一样而已,但是当我经过思考过后,发现他们其实不是一个东西;降级什么是服务
- FilterInputStream 介绍FilterInputStream 的作用是用来“封装其它的输入流,并为它们提供额外的功能”。它的常