接下来我将为您提供关于“SpringBoot集成MybatisPlus报错的解决方案”的完整攻略。
问题描述
在SpringBoot项目中集成MybatisPlus时,可能会遇到以下报错信息:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxxMapper' defined in file ... xxxMapper.java: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
该错误提示“Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required”表明,在配置Mapper时,没有为Mapper添加合适的SessionFactory或SessionTemplate属性。
解决方案
要解决该问题,需要对MybatisPlus和SpringBoot进行相应的配置。
步骤一:添加MybatisPlus的依赖
在项目的pom.xml文件中,添加MybatisPlus的依赖:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.1</version>
</dependency>
步骤二:添加MybatisPlus的配置
在SpringBoot项目的application.yml或者application.properties中,添加MybatisPlus的配置,如下所示:
mybatis:
mapper-locations: classpath*:mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
上述配置中,mapper-locations属性表示Mapper文件所在的位置,一般为classpath:mapper/.xml;configuration属性用于设置Mybatis的基本属性,如日志输出等。
步骤三:添加DataSource和SessionFactory配置
在SpringBoot项目的application.yml或者application.properties中,添加DataSource和SessionFactory配置,示例如下:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${mysql.ip}:${mysql.port}/${mysql.database}?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: ${mysql.username}
password: ${mysql.password}
hikari:
minimum-idle: 5
maximum-pool-size: 100
connection-test-query: SELECT 1
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath*:mapper/**/*.xml
上述配置中,dataSource属性包含了关于数据库连接的一些基本信息,如驱动类、URL、用户名和密码等;SessionFactory属性则由MybatisPlus自动配置。
步骤四:添加Mapper和扫描Mapper配置
在SpringBoot项目的Mapper接口中,添加@Mapper注解;同时,在SpringBoot项目的启动类上,添加@MapperScan注解,如下所示:
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
示例一
下面给出一个完整的示例。
User实体类
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
private String name;
}
UserMapper接口
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
UserService接口
public interface UserService {
List<User> findAll();
}
UserServiceImpl实现类
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper mapper;
@Override
public List<User> findAll() {
return mapper.selectList(null);
}
}
UserController接口
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public List<User> findAll() {
return userService.findAll();
}
}
application.yml配置文件
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: root
hikari:
minimum-idle: 5
maximum-pool-size: 100
connection-test-query: SELECT 1
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:mapper/**/*.xml
数据库表结构
CREATE TABLE `user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增主键',
`name` varchar(20) DEFAULT NULL COMMENT '姓名',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
示例二
下面给出第二个示例。
Book实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Book {
private Long id;
private String name;
}
BookMapper接口
@Mapper
public interface BookMapper extends BaseMapper<Book> {
}
BookController接口
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookMapper mapper;
@GetMapping
public List<Book> findAll() {
return mapper.selectList(null);
}
}
application.yml配置文件
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: root
hikari:
minimum-idle: 5
maximum-pool-size: 100
connection-test-query: SELECT 1
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath*:mapper/**/*.xml
数据库表结构
CREATE TABLE `book` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增主键',
`name` varchar(20) DEFAULT NULL COMMENT '书名',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
以上就是完整的“SpringBoot集成MybatisPlus报错的解决方案”攻略。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:SpringBoot集成MybatisPlus报错的解决方案 - Python技术站