开发依赖 | 版本 |
---|---|
Spring Boot | 3.0.6 |
Mybatis-Plus | 3.5.3.1 |
JDK | 20 |
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
dependency>
<dependency>
<groupId>com.h2databasegroupId>
<artifactId>h2artifactId>
dependency>
dependencies>
debug: true
logging:
level:
root: debug
spring:
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:tcp://localhost/D:/ProgramFiles/h2database/data/test;MODE=MYSQL;
username:
password:
mybatis-plus:
# 所有实体类所在包路径
type-aliases-package: com.wen3.**.po
# mapper.xmml文件路径,多个使用逗号分隔
mapper-locations: classpath*:resources/mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
@MapperScan
注解的作用是用来指定Mapper
接口的位置,示例如下
@MapperScan(basePackages = "com.wen3.demo.mybatisplus.dao")
@SpringBootApplication
public class DemoMybatisplusApplication {
public static void main(String[] args) {
SpringApplication.run(DemoMybatisplusApplication.class, args);
}
}
basePackages
一定要指定Mapper
接口的包路径,启动的时候Mybatis-Plus
会把这个包及子包下的所有接口当做Mapper- 比如
basePackages
的值指定为com.wen3
,则会把Service
接口也当做Mapper
,在调用方法的时候就会报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
package com.wen3.demo.mybatisplus.po;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
/**
*
*
*
*
* @author tangheng
* @since 2023-05-21
*/
@Getter
@Setter
@Accessors(chain = true)
@TableName("USER")
public class UserPo {
/**
* 用户id
*/
@TableId(value = "USER_ID", type = IdType.AUTO)
private String userId;
/**
* 用户姓名
*/
@TableField("USER_NAME")
private String userName;
/**
* 用户性别
*/
@TableField("USER_SEX")
private String userSex;
/**
* 用户邮箱
*/
@TableField("USER_EMAIL")
private String userEmail;
/**
* 用户账号
*/
@TableField("USER_ACCOUNT")
private String userAccount;
/**
* 用户地址
*/
@TableField("USER_ADDRESS")
private String userAddress;
/**
* 用户密码
*/
@TableField("USER_PASSWORD")
private String userPassword;
/**
* 用户城市
*/
@TableField("USER_CITY")
private String userCity;
/**
* 用户状态
*/
@TableField("USER_STATUS")
private Integer userStatus;
/**
* 用户区县
*/
@TableField("USER_SEAT")
private String userSeat;
}
package com.wen3.demo.mybatisplus.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wen3.demo.mybatisplus.po.UserPo;
import java.util.Map;
public interface UserMapper extends BaseMapper<UserPo> {
}
package com.wen3.demo.mybatisplus.service;
import com.wen3.demo.mybatisplus.po.UserPo;
import java.util.Map;
public interface UserService {
UserPo getById(String userId);
}
package com.wen3.demo.mybatisplus.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wen3.demo.mybatisplus.po.UserPo;
import com.wen3.demo.mybatisplus.dao.UserMapper;
import com.wen3.demo.mybatisplus.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPo> implements UserService {
@Override
public UserPo getById(String userId) {
LambdaQueryWrapper<UserPo> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(UserPo::getUserId, userId);
return baseMapper.selectOne(queryWrapper);
}
}
package com.wen3.demo.mybatisplus.service;
import com.wen3.demo.mybatisplus.MybatisPlusSpringbootTestBase;
import com.wen3.demo.mybatisplus.po.UserPo;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
class UserServiceTest extends MybatisPlusSpringbootTestBase {
@Resource
private UserService userService;
@Test
void getById() {
String userId = "U11902a8436094248a45dd3821b07e82a1651108484889";
UserPo testResult = userService.getById(userId);
log.info("testResult: {}", testResult);
}
}
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
- 重启后日志没打印出来,连控制台也没有输出SQL日志
- 使用slf4j后,日志输出级别就由SpringBoot接管了,SpringBoot默认是INFO级别
- 修改Mybatis-Plus下的日志为DEBUG,比如:
logging.level.com.baomidou.mybatisplus=DEBUG
- 重启后就能看到日志了
Mybatis-Plus
完全兼容Mybatis
,如果项目曾经使用的是Mybatis
,想要引入Mybatis-Plus
,是完成可以的,仅仅是在数据源的处理上有稍微细节的处理,本人在处理Mybatis
和Mybatis-Plus
方面具有丰富经验,如有需要,欢迎交流!