MybatisPlus报错Invalidboundstatement(notfound)的解决⽅
案
今天使⽤MybatisPlus,测试时报错Invalid bound statement (not found)
使⽤⾃定义的mapper接⼝中的⽅法可以执⾏,⽽调⽤MybatisPlus中baseMapper中的⽅法会报错
因此可以排除是路径配置问题
查询⽹上各种解决⽅案依旧⽆果之后,从头到尾梳理了⼀下代码,到了错误
package com.jt.pojo;
import batisplus.annotation.IdType;
import batisplus.annotation.TableField;
import batisplus.annotation.TableId;
import batisplus.annotation.TableName;
import lombok.Data;
perimental.Accessors;
import org.springframework.stereotype.Component;
import java.io.Serializable;
//基于ORM思想,属性与表⼀致
@Data
@Accessors(chain=true)
invalids@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 apper.BaseMapper;
import com.jt.pojo.User;
import org.apache.ibatis.annotations.Mapper;
batis.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;
batis.spring.annotation.MapperScan;
batis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import t.annotation.Bean;
import t.annotation.ImportResource;
@SpringBootApplication
@MapperScan("com.jt.mapper")
public class SpringbootMybatisApplication {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
//可以通过环境变量获取你的mapper路径,这样mapper扫描可以通过配置⽂件配置了
scannerConfigurer.setBasePackage("urpackage.*.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.st.context.SpringBootTest;
import st.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>的泛型完美解决
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论