Mybatis框架-12:resultMap(domain属性和数据库字段名不一致时如何封装数据)
2019-06-03 11:41
387 查看
第一步:使用resoultMap写好封装模板
column是数据库中的字段名,property是domain的字段名,这样写了之后就相当于将他们做了一个匹配,和hibernate的映射文件有点相似
[code]<resultMap id="Mycustomer" type="com.ctbu.domain.Customer"> <result column="cust_id" property="cust_ids"/> <result property="cust_names" column="cust_name"/> <result property="cust_phones" column="cust_phone"/> <result property="cust_professions" column="cust_profession"/> <result property="email" column="email"/> </resultMap>
第二步:执行的语句返回结果使用上面个自己写的那个模板
[code]<select id="getCustomer" resultMap="Mycustomer"> select * from customer where cust_id=#{id} </select>
第三步:执行测试,查看是否能够成功查询
[code] @Test public void test3(){ SqlSession session = MyUtils.openSession(); CustomerMapper mapper = session.getMapper(CustomerMapper.class); Customer customer = mapper.getCustomer(2); System.out.println(customer); session.close(); }
相关文章推荐
- mybaits(查询与别名、日志框架显示sql语句、对象属性和数据库表字段不匹配resultMap使用、mysql数据查询分页、执行sql和存储过程、动态SQL语句)
- mybatis配置文件中resultType和resultMap错写后将会封装成属性不一样数据,以及后台报错java.util.HashMap cannot be cast to com.entity
- 动手改造Ibatis,使其支持文件系统存储数据列 之 看我如何给ResultMap增加属性
- ## MyBatis持久层框架,实现数据访问,抽象方法多参数的处理,关于别名,MyBatis中的动态SQL,resultMap
- mybatis数据库字段名和实体类属性名不一致(四)
- Mybatis使用数据表&实体类属性不一致时候如何处理
- Mybatis总结(3)--之数据库字段名和实体类属性名不一致
- 好947 Mybatis 配置resultMap 带参数查询Map 注意selectOne数据库返回结果一条数据库 否则会报错
- MyBatis映射文件的resultMap如何做表关联
- Mybatis连结3表查询数据resultMap结果映射
- mybatis中类属性和数据表字段不一致的解决方法
- Mybatis 当实体属性与数据库字段不一致时的解决方案
- mybatis里面的resultMap中为什么只配置了jdbctype而不配置javatype呢?他如何对应 javatype的啊?
- 基于MyBatis的数据库切分框架,可实现数据的水平切分和垂直切分。 http://www.makersoft.org
- mybatis使用association的resultMap方式进行映射少数据问题
- 【Mybatis框架】输出映射-resultType与resultMap
- MyBatis项目查询数据库Could not find result map java.lang.String
- MyBatis_通过resultMap解决不一致的问题
- Mybatis框架的使用之四(resultMap的使用)
- Mybatis框架提供的resultMap返回值别名