MyBatis输出映射为resultMap
来源:互联网 发布:055大驱数据 编辑:程序博客网 时间:2024/05/22 23:59
MyBatis输出映射为resultMap
背景:使用resultType进行输出映射,必须保证查询结果的列名与pojo中国的属性名一致,该列才可以映射成功。
如果查询的列名与pojo中那属性名不一致就需要使用resultMap.
需求:从数据库查询出来的结果集有别名,需要映射到pojo类。
mapper.xml中定义resultMap
<!-- 自定义resultMap输出类型 type:查询结果最终映射的类型,可以是别名 id: resultMap的唯一标识 --> <resultMap type="com.hl.myabtis.first.beas.User" id="userResultMap"> <!-- id:查询结果集中的唯一标识 column:对应列名 property:映射到pojo类中对应的属性名 --> <id column="t_id" property="id"/> <!-- result:除了唯一标识以外的字段 column:对应列名 property:映射到pojo类中对应的属性名 --> <result column="t_username" property="username"/> </resultMap>
使用resultMap
<!-- parameterType:输入参数类型 resultMap:输出参数类型 userResultMap:自定义resultMap --> <select id="findUserMapById" parameterType="int" resultMap="userResultMap"> select id t_id,username t_username from user where id=#{id} </select>
定义mapper接口
//查询用户信心以map形式输出public User findUserMapById(int id) throws Exception;
测试
@Test public void findUserMapById() throws Exception{ SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper usermapper = sqlSession.getMapper(UserMapper.class); User user = usermapper.findUserMapById(3); System.out.println(user); sqlSession.close(); }
阅读全文
0 0
- MyBatis输出映射为resultMap
- [mybatis]ResultMap输出结果映射
- Mybatis 输出映射-- resultType resultMap
- 【Mybatis框架】输出映射-resultType与resultMap
- 【Mybatis框架】输出映射-resultType与resultMap
- 【Mybatis框架】输出映射-resultType与resultMap
- 【Mybatis框架】输出映射-resultType与resultMap
- mybatis学习之路----输出映射之resultType resultMap
- MyBatis之输入(parameterType)与输出(resultType、resultMap)映射
- Mybatis中输出映射-resultType与resultMap的区别
- MyBatis结果集映射(ResultMap)
- Mybatis输入映射parameterType与输出映射ResultMap是Map类型的解决方案
- 【MyBatis学习07】输出类型resultType及输出参数映射resultMap
- MyBatis学习笔记(五)输入(parameterType)与输出(resultType、resultMap)映射
- 谷哥的小弟学后台(40)——MyBatis输出映射resultType以及resultMap
- Mybatis ResultMap 无法继承父resultMap的映射关系
- Mybatis 高级结果映射 ResultMap Association Collection
- Mybatis 高级结果映射 ResultMap Association Collection
- JavaWeb学习十四(JDBC入门)
- java调exe
- VS2015中的快捷键
- VUEX学习与实践
- RXjava/RxAndroid/lambdas 简单demo
- MyBatis输出映射为resultMap
- Python yield 使用浅析
- c#类的序列化
- container_of-简易理解
- 关于正则表达式,你应该会用到的一些知识
- vim 翻译插件
- 1,Android studio的新安装和更新后打开工程加载卡在新建界面分析
- 基础——Spark Job Scheduling
- CF