Springboot3.3 + Mybatis / Mybatis-plus
Java17, Springboot 3.3.5, 项目一启动就报错:
Invalid value type for attribute 'factoryBeanObjectType': java.lang.String
我知道问题出现在pom依赖的版本上。但网上对于Springboot3.3的配置资料并不多,且在我代码上实测还是会报错。搞了一天终于解决了,希望能对同样问题的人有帮助。
Springboot + Mybatis
项目结构
我的pom文件
依赖配置是成功的一半。。。其中 mybatis-plus-spring-boot3-starter 是最重要的!
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.3.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>demo</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- mysql驱动依赖 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.28</version></dependency><!-- MyBatisPlus依赖 --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>3.5.5</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>
我的application.properties
spring.application.name=demo
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/数据库名?useSSL=false
spring.datasource.username=root
spring.datasource.password=密码
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
server.port=8080
才知道这种格式和下面这种是不通用的..
spring:application.name:demodatasource:driver-class-name:com.mysql.jdbc.Driverurl:jdbc:mysql://localhost:3306/数据库名?useSSL=false......
我的数据库
navicat里创建表,随便填几个数据
测试一下
配置完这些,可以在测试类里测一下,是否连接成功。这里成功了再接着往下做。
package com.example.demo;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.SQLException;@SpringBootTest
class DemoApplicationTests {@ResourceDataSource dataSource;@Testvoid contextLoads() throws SQLException {System.out.println("获取的数据库连接为:"+ dataSource.getConnection());}
}
我的entity
@Data
// 这里亲测不需要写表名
public class User {private int id;private String name;public int getId() {return id;}public String getName() {return name;}public void setId(int id) {this.id = id;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +'}';}
}
我的Mapper
package com.example.demo.mapper;
import com.example.demo.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import java.util.List;@Mapper
public interface UserMapper{// 在sql语句中指定表名即可@Select("select * from user0")List<User> find();@Select("select * from user0 where id=#{id}")User selectById(int id);
}
我的Controller
package com.example.demo.controller;
import com.example.demo.mapper.UserMapper;
import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
public class UserController {@Autowiredprivate UserMapper userMapper;@GetMapping("/user")public void query(){List<User> list = userMapper.find();System.out.println(list);User user = userMapper.selectById(1);System.out.println(user);}
}
我的Application
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("com.example.demo.mapper") //不要忘记
public class HelloApplication {public static void main(String[] args) {SpringApplication.run(HelloApplication.class, args);}
}
OK,可以运行Application了!结果如下:
Springboot + Mybatis-plus
pom,properties配置,数据库都不用改动。
改动1:entity
如果你的表名和类名不同(不区分大小写),比如我的表名是user0,类名为User。那么需要新增注解。
@Data
@TableName("user0")
public class User {private int id;private String name;......
}
改动2:Mapper
继承BaseMapper以后,不需要我们再去定义方法了,只需要调用BaseMapper里的方法即可。
@Mapper
public interface UserMapper extends BaseMapper<User>{// 空
}
改动3:Controller
@RestController
public class UserController {@Autowiredprivate UserMapper userMapper;@GetMapping("/user")public void query(){// 查询,selectList括号内写条件List<User> list = userMapper.selectList(null);System.out.println(list);// 条件查询QueryWrapper queryWrapper = new QueryWrapper();queryWrapper.eq("name","zhangsan");List<User> user = userMapper.selectList(queryWrapper);System.out.println(user);}
}
再次运行Application即可成功。