love_Java123 2017-09-07 03:30 采纳率: 0%
浏览 23003
已采纳

SpringBoot集成Mybatis遇到问题

小白一个,第一次使用springboot,想用mybatis来做dao层框架,按网上资料进行了一下配置但是出现了一个错误,我不知道我的配置错在哪儿,麻烦大神帮忙指导一下,谢谢:
application.properties配置如下:
mybatis.mapper-locations=classpath:mapper/mapper/*Mapper.xml
mybatis.config-location=classpath:mapper/sqlMapConfig.xml
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
接着是sqlMapConfig.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">





然后是mapp.xml的配置


select * from user


sercive层代码如下:
package com.tky.test.service;

    import java.util.List;
    import javax.annotation.Resource;
    import org.springframework.stereotype.Service;
    import com.tky.test.dao.UserMapper;
    import com.tky.test.emit.User;

    @Service(value="userService")
    public class UserServiceImpl implements UserService{
        @Resource
        private UserMapper userMapper;

        @Override
        public List<User> findUser() {
            List<User> findAll = userMapper.findAll();
            return findAll;
        }

    }

然后是Mapper接口的代码:
package com.tky.test.dao;
import java.util.List;
import com.tky.test.emit.User;

    public interface UserMapper {

        public List<User> findAll();
    }

在下面是controller层的:
@RestController(value="userController")
public class UserController {

    @Resource
    private UserService userService;
    @RequestMapping("my")
    public List<User> getMy(){
        List<User> findUser = userService.findUser();
        return findUser;
    }
}

最后是springboot的主函数代码:
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

    @SpringBootApplication
    @MapperScan(value="com.tky.test.dao")
    public class Driver {
        public static void main(String[] args) {
            //项目入口
            SpringApplication.run(Driver.class,args);
        }
        @Bean
        public SqlSessionFactory sessionFactory() throws Exception{
            SqlSessionFactory sqlSessionFactory = 
                new SqlSessionFactoryBean().getObject();
            return sqlSessionFactory;
    }

}

最后的报错信息很长是这样的:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userMapper' defined in file [D:\work\tky_workspace\Test_SpringBoot\target\classes\com\tky\test\dao\UserMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in com.tky.test.Driver: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is java.lang.IllegalArgumentException: Property 'dataSource' is required

有没有大神能看明白的帮忙指导一下,谢谢

  • 写回答

2条回答 默认 最新

  • 鼠晓 博客专家认证 2017-09-07 03:40
    关注

    给【UserMapper】接口加个注解

     @Component
    @Mapper
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 为什么zynq CAN IP 无法进入config配置模式,XCan_SelfTest函数失效?
  • ¥15 Pycharm中程序直接运行可以但进入调试报错
  • ¥15 MATLAB动图问题
  • ¥15 有段代码不知道怎么理解,const isToken = (config.headers || {}).isToken === false
  • ¥15 我的显卡支持CUDA最高版本是12.3,这个版本也支持VS 2022 17.0这种情况下如果我想下载CUDA11.8,需要下载旧版本的VS2022吗
  • ¥100 采用栈和深度优先算法取消graphics库采用其他方式显示路径打印显示路径取消graphics库
  • ¥15 pointnet2包安装
  • ¥20 射频功率问题,解答者有酬谢!
  • ¥80 构建降雨和积水的预测模型
  • ¥15 #Qt Transform setTransform()在鼠标拖动移动视角是一致在原地不动,无法变换视角(细微观察似乎视图有在原地抖动),无法变换视角(细微观察似乎视图有在原地抖动)