SpringBoot整合Mybatis遇到的问题,已解决

① 启动报错Failed to configure a DataSource: ‘url’ attribute is not specified and …

根据报错日志分析是在springboot项目启动的时候没有找到database 数据库连接地址,我们知道在spring boot 启动的时候会默认加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration这个类,而DataSourceAutoConfiguration类使用了@Configuration注解向spring注入了dataSource bean,又因为项目中并没有关于dataSource相关的配置信息,所以当spring创建dataSource bean时因缺少相关的信息就会报错。

  • 检查pom.xml 项目数据库jar 是否引用;
  • 查看**.properties或**.yml 配置文件是否配置数据库链接池;
  • 查看spring - datasource - url 配置的地址格式错误需要转义等;
  • yml或者properties文件可能没有被扫描到(情况比较少,如果按照标准命名都会被默认扫描);

方案、如果项目不需要数据库相关信息就排除此类的autoconfig

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application, args);
	}
}
②Field userMapper in com.example.demo.controller.UserController required a bean of type ‘com.example.demo.mapper.UserMapper’ that could not be found.

检查@MapperScan和@Mapper是否使用

  • 可以@Mapper
  • 也可以@MapperScan+@Repository
③ Error creating bean with name ‘xxxxxController’: Unsatisfied dependency expressed through field…

可以看到黑体部分的信息,意思是创建helloController这个bean出错了,嵌套异常是userService不满足依赖,容器里面没有com.cloud.xp.manager.service.UserService 的 实例

既然是userService这个对象的问题,那么我们就首先来分析,userService对象是怎么添加到容器里的,首先我想到的就是注解,这个时候我们可以先检查一下UserService这个接口的实现类上又没加上Service注解

④ SpringBoot+MyBatis整合中的坑以及Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required错误

解决方案添加DruidDataSource配置文件

⑤springboot整合mybatis错误 Invalid bound statement (not found): com.yuan.mapper.UserMapper.getUserList

解决方案:
1.把映射文件 放到resources 目录下 结构目录一模一样
2.在pom.xml中导入以下代码

<build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值