mybatis注解Select查询List,返回有对象个数,但是为null

用mybatis注解Select查询List,返回有对象个数,但是为null,

代码如下

public interface SysRoleMapper {  
      
    @Select("select * from sys_role ")  
    List<SysRole> selectAll();  
  
}  

具体出现原因,查询出来的数据,与实体bean的字段不相匹配,导致null

实体bean的字段为驼峰形式,如,roleId, 而数据库为role_id,因此,匹配不上,导致映射失败

解决方式:

方式1、修改查询sql,修改代码为:

public interface SysRoleMapper {  
      
    @Select("select role_id as roleId from sys_role ")  
    List<SysRole> selectAll();  
  
}  

方式2、添加mybatis自动驼峰注解,配置如下:

spring配置如下:

 <!-- spring和MyBatis整合 -->  
    <bean id="sqlSessionFactoryBeanNew" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <!--打印sql-->  
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>  
        <!-- <property name="dataSource" ref="dynamicDataSource" /> -->  
        <property name="dataSource" ref="dataSource" />  
        <!-- 自动扫描mapping.xml文件 -->  
        <property name="mapperLocations" value="classpath:dcc/mapper/*.xml"></property>  
    </bean>  

mybatis-config.xml配置如下:

<configuration>  
    <settings>  
        <!-- 打印查询语句 -->  
        <setting name="logImpl" value="STDOUT_LOGGING" />  
        <!-- 自动驼峰 -->  
        <setting name="mapUnderscoreToCamelCase" value="true"/>  
    </settings>  
</configuration>  



猜你喜欢

转载自blog.csdn.net/weixin_41887312/article/details/80814653