数据库查询结果、Ibatis的ResultMap与JavaBean的对应关系
来源:互联网 发布:windows系统 编辑:程序博客网 时间:2024/06/03 16:01
对数据库的Select查询结果字段多少、名称、类型先与Ibatis的ResultMap对应是否一致,然后Ibatis的ResultMap再与JavaBean对应是否一致。Ibatis的ResultMap是核心。1、Select查询结果的字段个数多于Ibatis的ResultMap中定义的字段,不会出错
2、Select查询结果的字段个数少于Ibatis的ResultMap中定义的字段,会出错。
测试:ResultMap文件名为SqlMapStudent.xml,查询结果ResultMap的ID为student.StudentRecordResult。JavaBean为com.cea.callcenter.bean.Student。Select查询结果里没有字段stuEmail,StudentRecordResult里定义了字段stuEmail
结果:运行产生异常,异常信息提示在SqlMapStudent.xml的payment.PayRecordResult里有非法字段stuEmail
org.springframework.jdbc.BadSqlGrammarException: SqlMapClient operation; bad SQL grammar []; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException: --- The error occurred in daoConfig/SqlMapStudent.xml. --- The error occurred while applying a result map. --- Check the payment.PayRecordResult. --- Check the result mapping for the 'stuEmail' property. --- Cause: java.sql.SQLException: Invalid column name
3、Ibatis的ResultMap中定义的字段多于JavaBean中定义的字段,会出错。
测试:ResultMap文件名为SqlMapStudent.xml,查询结果ResultMap的ID为student.StudentRecordResult。JavaBean为com.cea.callcenter.bean.Student。StudentRecordResult里定义了字段stuEmail,com.cea.callcenter.bean.Student没有字段stuEmail
结果:运行产生异常,异常信息提示在类Student里没有可写的属性stuEmail
org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0]; --- The error occurred in daoConfig/SqlMapStudent.xml. --- The error occurred while applying a result map. --- Check the student.StudentRecordResult. --- The error happened while setting a property on the result object. --- Cause: com.ibatis.common.beans.ProbeException: There is no WRITEABLE property named 'stuEmail' in class 'com.cea.callcenter.bean.Student'; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
3、Ibatis的ResultMap中定义的字段少于JavaBean中定义的字段,不会出错。JavaBean中多的那个字段结果为默认值null
所以对于字段个数,按以下原则就不会出错
Select查询结果的字段个数>=Ibatis的ResultMap中定义的字段个数<=JavaBean中定义的字段个数
4、如果把resultMap换成resultClass,select查询结果的字段个数多于或少于resultClass都不会出现异常。
<select id="getAllInsuranceRecordByBean" resultMap="student.StudentRecordResult" parameterClass="com.cea.callcenter.bean.Student">select stuName from tbl_student</select>
换为如下
<select id="getAllInsuranceRecordByBean" resultClass="com.cea.callcenter.bean.Student" parameterClass="com.cea.callcenter.bean.Student">select stuName from tbl_student</select>
1、Select查询结果的字段个数多于resultClass中定义的字段,resultClass中没有这个字段,也不报错
2、Select查询结果的字段个数少于resultClass中定义的字段,resultClass中多的这个字段值为null
看来用resultClass比resultMap更方便,还不出错
0 0
- 数据库查询结果、Ibatis的ResultMap与JavaBean的对应关系
- ibatis框架的结果集中column的对应关系
- ibatis的resultClass与resultMap
- ibatis的resultClass与resultMap
- IBatis映射的结果不能与类对应
- [Java][ibatis]通过resultMap与resultClass取得Map结果集的差异
- ibatis的resultMap
- 查询域名与对应ip的关系
- ibatis的级联查询、resultmap和resultclass区别
- 数据库会话与操作系统进程号对应关系的查询SQL
- myBatis ibatis 查询多行结果集 resultMap 配置
- ibatis resultMap使用的问题
- SQL语句生成表结构对应的javabean及mybatis的resultMap
- 通过iBatis的queryForMap方法比较resultClass与resultMap
- 通过iBatis的queryForMap方法比较resultClass与resultMap
- 通过iBatis的queryForMap方法比较resultClass与resultMap
- 通过iBatis的queryForMap方法比较resultClass与resultMap
- 通过iBatis的queryForMap方法比较resultClass与resultMap
- Android手机屏幕坐标定义
- 中国剩余定理(孙子定理)
- MM模块有关采购申请的认知
- ViewPager+Fragment+TabPageIndicator
- 吹捧一下Java策略模式,策略在手,万事无忧
- 数据库查询结果、Ibatis的ResultMap与JavaBean的对应关系
- leetcode.336. Palindrome Pairs
- unity中如何解决两个物体的绑定
- 图片的异步加载框架比较
- React Native iOS环境搭建
- linux 下安装mysql
- 校验日期时间的JS函数
- Redis的安装与使用(单节点)
- iOS开发之证书类别