今天使用MybatisPlus,测试时报错Invalid bound statement (not found)
使用自定义的mapper接口中的方法可以执行,而调用MybatisPlus中baseMapper中的方法会报错
因此可以排除是路径配置问题
查询网上各种解决方案依旧无果之后,从头到尾梳理了一下代码,找到了错误
package com.jt.pojo;
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.Data;
import lombok.experimental.Accessors;
import org.springframework.stereotype.Component;
import java.io.Serializable;
//基于ORM思想,属性与表一致
@Data
@Accessors(chain=true)
@TableName//如果表明与类名一致可以省略
public class User implements Serializable {
//自动生成序列化编号,settings-editor-inspections-serializable without serialVersionUID
private static final long serialVersionUID = -7155610581355123677L;
//标识主键,并且主键自增
@TableId(type= IdType.AUTO)
//如果字段属性名称一致,可以省略配置
private Integer id;
//@TableField(value = "name")
private String name;
private Integer age;
private String sex;
}
package com.jt.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jt.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.stereotype.Component;
import java.util.List;
//代理方式:1.JDK代理(默认配置)2.CGLIB代理
//如果被代理者是接口,默认采用JDK代理.规定:jdk代理,必须有接口
//如果被代理者没有接口(没有实现接口)默认采用CGLIB.规定:可以创建任何对象的代理,代理者是目标对象的子类
//@Mapper//将接口交给spring管理,接口并不能创建duixiang,管理的是UserMapper的代理对象
//@MapperScan("com.jt.mapper")将此注解添加到启动类,可以自动扫描mapper包下的所有注解,因此此处不用再添加@mapper
public interface UserMapper extends BaseMapper {
List<User> findAll();
}
package com.jt.springbootmybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@MapperScan("com.jt.mapper")
public class SpringbootMybatisApplication {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
//可以通过环境变量获取你的mapper路径,这样mapper扫描可以通过配置文件配置了
scannerConfigurer.setBasePackage("com.yourpackage.*.mapper");
return scannerConfigurer;
}
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class,args);
}
}
package com.jt.springbootmybatis;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMybatisApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void testFindAll() {
List<User> list = userMapper.selectList(null);
System.out.println(list);
}
}
启动测试类调用selectList方法报错,而调用findAll方法运行正常
解决方案:pojo的UserMapper中,通过@TableName与表名进行了关联,所以在继承BaseMapper接口时,要指定BaseMapper<User>的泛型
完美解决
原文出处:https://www.cnblogs.com/wq-57/p/11598292.html
来源:oschina
链接:https://my.oschina.net/u/4346199/blog/3251757