mybatis注解Select查询List,返回有对象个数,但是为null
2018-03-27 11:06
1651 查看
用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>
代码如下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>
相关文章推荐
- 使用MyBatis查询 返回类型为int,但是当查询结果为空NULL,报异常的解决方法
- 在使用mybatis时遇到查询结果返回为空的情况,但是查数据库能查到
- mybatis查询返回null解决方案
- CaptureDeviceManager.getDeviceList方法返回null对象问题探究
- mybatis查询得到list列表,列表长度不为零,但遍历列表获取对象属性值时,出现空指针异常
- mybatis 使用oracle char 字段查询返回结果总是null
- Mybatis Mapper.xml 需要查询返回List<String>
- Mybatis查不到数据查询返回Null问题
- Hibernate查询返回list全部为null的解决方案
- Hibernate查询之SQL查询,查询结果用new新对象的方式接受,hql查询,通过SQL查询的结果返回到一个实体中,查询不同表中内容,并将查到的不同表中的内容放到List中
- Mybatis查询到多条记录但是映射到list集合只有一条或者条数不对
- MyBatis查询单表返回List
- MyBatis:复合条件查询——对象封装成List类型
- mybatis查询如何返回List<Map>类型数据
- Mybatis selectOne方法执行sql有数据返回为null
- Mybatis-generator修改源代码实现自定义方法,返回List对象(一)
- Hibernate 查询多个字段返回的list如何转为自定义对象
- Mybatis查询复杂对象(对象包括对象和List<对象>)
- 注解写法mybatis 传List类型参数以及返回List类型参数
- MyBatis实现单表增删改查(CURD)--查询所有返回list