In localhost execution of the project, I'm encountering an 'Invalid bound statement (not found)' error

52 Views Asked by At

The error message is as follows,

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.casey.mapper.SysRoleMapper.getUserRoleCode
    at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235) ~[mybatis-3.5.4.jar:3.5.4]
    at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.<init>(MybatisMapperMethod.java:50) ~[mybatis-plus-core-3.3.2.jar:3.3.2]
    at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.lambda$cachedMapperMethod$0(MybatisMapperProxy.java:101) ~[mybatis-plus-core-3.3.2.jar:3.3.2]
    at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660) ~[na:1.8.0_291]
    at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.cachedMapperMethod(MybatisMapperProxy.java:100) ~[mybatis-plus-core-3.3.2.jar:3.3.2]
    at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:95) ~[mybatis-plus-core-3.3.2.jar:3.3.2]
    at com.sun.proxy.$Proxy159.getUserRoleCode(Unknown Source) ~[na:na]
    at com.casey.service.impl.SysRoleServiceImpl.isSuperAdmin(SysRoleServiceImpl.java:29) ~[classes/:na]

AdminServiceApplication:

/**
 * admin-service 启动类
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@MapperScan(basePackages = { "com.casey.mapper" })
public class AdminServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminServiceApplication.class, args);
    }
}

An error occurred while running this line of code:

boolean superAdmin = sysRoleService.isSuperAdmin(userId);

SysRoleService:

public interface SysRoleService extends IService<SysRole>{

    /**
     * 判断一个用户是否为超级管理员
     * @param userId
     * @return
     */
    boolean isSuperAdmin(Long userId);
}

SysRoleServiceImpl

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService{

    @Autowired
    private SysRoleMapper sysRoleMapper;

    /**
     * 判断一个用户是否为超级管理员
     * @param userId
     * @return
     */
    @Override
    public boolean isSuperAdmin(Long userId) {
        // 当用户角色的 code 为 ROLE_ADMIN 时,该用户为超级管理员
        // 用户id -> 用户角色 -> 该角色是否为 ROLE_ADMIN
        String roleCode = sysRoleMapper.getUserRoleCode(userId);
        if(StringUtils.isEmpty(roleCode) && roleCode.equals("ROLE_ADMIN")){
            return true;
        }
        return false;
    }
}

SysRoleMapper:

public interface SysRoleMapper extends BaseMapper<SysRole> {
    /**
     * 获取用户 code 的实现
     * @param userId
     * @return
     */
    String getUserRoleCode(Long userId);
}
SysRoleMapper.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.casey.mapper.SysRoleMapper">
  <resultMap id="BaseResultMap" type="com.casey.domain.SysRole">
    <[email protected]>
    <!--@Table sys_role-->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="code" jdbcType="VARCHAR" property="code" />
    <result column="description" jdbcType="VARCHAR" property="description" />
    <result column="create_by" jdbcType="BIGINT" property="createBy" />
    <result column="modify_by" jdbcType="BIGINT" property="modifyBy" />
    <result column="status" jdbcType="TINYINT" property="status" />
    <result column="created" jdbcType="TIMESTAMP" property="created" />
    <result column="last_update_time" jdbcType="TIMESTAMP" property="lastUpdateTime" />
  </resultMap>
  <sql id="Base_Column_List">
    <[email protected]>
    id, `name`, code, description, create_by, modify_by, `status`, created, last_update_time
  </sql>

  <select id="getUserRoleCode" resultType="java.lang.String">
    SELECT
        r.`code`
    FROM
        sys_role AS r LEFT JOIN sys_user_role AS ur ON r.id = ur.role_id
    WHERE
        ur.user_id = #{userId};
  </select>
</mapper>

configuration file:

mybatis-plus:
    configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    mapper-locations: classpath:/mappers/*Mapper.xml

File structure is as follows: enter image description here

Even after attempting 'mvn clean' and clearing the 'target' folder, the error persists.

============= Bug has been fixed! =============================

The issue lies within the configuration class. The YAML file in Nacos gets overridden when I create a new MybatisSqlSessionFactoryBean.

@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
    //获取mybatis-plus全局配置
    GlobalConfig globalConfig = GlobalConfigUtils.defaults();
    //mybatis-plus全局配置设置元数据对象处理器为自己实现的那个
    globalConfig.setMetaObjectHandler(new AutoFillHandler());
    mybatisSqlSessionFactoryBean.setDataSource(dataSource);
    //mybatisSqlSessionFactoryBean关联设置全局配置
    mybatisSqlSessionFactoryBean.setGlobalConfig(globalConfig);
    return mybatisSqlSessionFactoryBean.getObject();
}

Setting the MapperLocations resolves the problem.

// 设置 Mapper 文件的位置
Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:mappers/*Mapper.xml");
mybatisSqlSessionFactoryBean.setMapperLocations(resources);
0

There are 0 best solutions below