环境
IDEA+SpringBoot+Maven+MySQL
原先有user表,使用Mybatis对该表CURD
使用Mybatis时,application.properties有
# MybatisProperties
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.demo.entity
mybatis.configuration.useGeneratedKeys=true
mybatis.configuration.mapUnderscoreToCamelCase=true
src\main\resources\mapper下有user-mapper.xml,
src\main\java\com\example\demo\dao下有UserMapper.java,
可以在xml文件里写sql语句实现对user表的CURD。
新增rank_follower表,使用MybatisPlus对该表CURD
偷懒小技巧:
1、利用idea快速生成实体类-百度经验
2、【狂神说Java】MyBatisPlus最新完整教程通俗易懂_哔哩哔哩_bilibili
IDEA连接数据库时:
报错:Server returns invalid timezone. Need to set ‘serverTimezone’ property.
解决:点开最右侧 Advanced,找到 serverTimezone,在右侧value处填写 GMT,保存即可!(或填写 Asia/Shanghai)
数据库里新增表rank_follower,IDEA里新增实体类RankFollower。
pom.xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
RankFollowerMapper.java
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.RankFollower;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Repository
@Mapper
public interface RankFollowerMapper extends BaseMapper<RankFollower> {
}
RankService.java
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.dao.*;
import com.example.demo.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class RankService {
@Autowired
private RankFollowerMapper rankFollowerMapper;
public List<RankFollower> selectRankFollowers(int offset, int limit) {
// return rankFollowerMapper.selectList(null);
return rankFollowerMapper.selectList(new QueryWrapper<RankFollower>().between("id",offset,offset+limit-1));
}
}
报错与解决
报错
此时,使用MybatisPlus进行CURD的表可以CURD,使用Mybatis进行CURD的表无法CURD,报错如下:
1、未在application.properties里配置mybatis-plus.mapper-locations,报该错:
# IDEA里点击运行后不报错,走到执行`selectByName`方法时才报该错误
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.demo.dao.UserMapper.selectByName
2、在application.properties里配置了mybatis-plus.mapper-locations,但未在application.properties里配置mybatis-plus.type-aliases-package,报该错:
# IDEA里点击运行后就报该错
# bean没有办法获取正确路径的报错,这个报错只会出现在java web spring框架运行的时候,由于这个报错并不会在eclipse内显示,只有运行的时候才会出现
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userMapper';
解决
application.properties,注释掉Mybatis的配置,写上MybatisPlus的配置,如下:
# MybatisProperties
#mybatis.mapper-locations=classpath:mapper/*.xml
#mybatis.type-aliases-package=com.example.demo.entity
#mybatis.configuration.useGeneratedKeys=true
#mybatis.configuration.mapUnderscoreToCamelCase=true
# MybatisPlusProperties
mybatis-plus.mapper-locations=classpath:mapper/*.xml
mybatis-plus.type-aliases-package=com.example.demo.entity
mybatis-plus.configuration.use-generated-keys=true
mybatis-plus.configuration.map-underscore-to-camel-case=true
此时,使用MybatisPlus进行CURD的表可以CURD,使用Mybatis进行CURD的表可以CURD。
在IDEA+SpringBoot项目中,原先使用Mybatis操作`user`表,后引入MybatisPlus操作`rank_follower`表。遇到因缺少配置导致的 CURD 报错。解决方案是在`application.properties`中注释Mybatis配置,添加MybatisPlus配置,包括mapper-locations和type-aliases-package。完成配置后,两个库均可正常CURD。
1099

被折叠的 条评论
为什么被折叠?



