Spring boot 整合mybatisPlus 报错

Spring boot 整合mybatisPlus 报错“Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required”

1. springboot3.0.3 整合 mybatis-plus3.0.5时,启动报错如下:

Caused by: java.lang.IllegalArgumentException: Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required

2.解决 版本问题。

将 springboot版本下调到3.0.0以下,再次启动,正常。

先前版本

<parent>
        <groupId>org.springframework.boot</groupId>
        <version>3.0.3</version>
        <artifactId>spring-boot-starter-parent</artifactId>
    </parent>

改为

<parent>
        <groupId>org.springframework.boot</groupId>
        <version>2.2.4.RELEASE</version>
        <artifactId>spring-boot-starter-parent</artifactId>
    </parent>

网上查到的方法是说SpringBoot 3以后的版本的sqlSessionFactory类代码变红了

如果不是请批评指教。

修改pom.xml文件自动生成的mysql依赖和lombok依赖

可能因为和Spring Boot的版本不匹配,这是个人的配置

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
        </dependency>
​
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>
 



Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userMapper' defined in file [D:\ideaProject\mybatis_plus\target\classes\com\lrz\mapper\UserMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Error: GlobalConfigUtils setMetaData Fail !  Cause:java.sql.SQLSyntaxErrorException: Unknown database 'mybatis-plus'
    

 

看看自己的application.properties文件内是否吧你的URL配错了

此错误因为和自己数据库的mybatis_plus不一样,找了半天错

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的示例: 1. 引入依赖 在 `pom.xml` 文件中引入 `spring-boot-starter-web` 和 `mybatis-plus-boot-starter` 依赖: ```xml <dependencies> <!-- Spring Boot web依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Mybatis-Plus依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> </dependencies> ``` 2. 配置数据源 在 `application.yml` 文件中配置数据源: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver ``` 3. 创建实体类和Mapper接口 创建一个实体类,例如 `User`: ```java @Data public class User { private Long id; private String name; private Integer age; private String email; } ``` 创建一个Mapper接口,例如 `UserMapper`: ```java @Mapper public interface UserMapper extends BaseMapper<User> { } ``` 4. 编写Service层接口和实现 创建一个Service接口,例如 `UserService`: ```java public interface UserService { List<User> list(); User get(Long id); boolean save(User user); boolean update(User user); boolean remove(Long id); } ``` 创建一个Service实现类,例如 `UserServiceImpl`: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public List<User> list() { return userMapper.selectList(null); } @Override public User get(Long id) { return userMapper.selectById(id); } @Override public boolean save(User user) { return userMapper.insert(user) > 0; } @Override public boolean update(User user) { return userMapper.updateById(user) > 0; } @Override public boolean remove(Long id) { return userMapper.deleteById(id) > 0; } } ``` 5. 编写Controller 创建一个Controller,例如 `UserController`: ```java @RestController @RequestMapping("/users") public class UserController { @Autowired private UserService userService; @GetMapping("/") public List<User> list() { return userService.list(); } @GetMapping("/{id}") public User get(@PathVariable Long id) { return userService.get(id); } @PostMapping("/") public boolean save(@RequestBody User user) { return userService.save(user); } @PutMapping("/") public boolean update(@RequestBody User user) { return userService.update(user); } @DeleteMapping("/{id}") public boolean remove(@PathVariable Long id) { return userService.remove(id); } } ``` 6. 测试 启动应用,访问 `http://localhost:8080/users/` 即可查询所有用户,其它接口也可以通过相应的请求方式进行测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值