700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 解决属性名和字段名不一致的问题(Mybatis)

解决属性名和字段名不一致的问题(Mybatis)

时间:2023-06-22 09:48:55

相关推荐

解决属性名和字段名不一致的问题(Mybatis)

解决属性名和字段名不一致的问题(Mybatis)

1、数据库中的字段

新建一个项目,拷贝之前,测试实体字段不一致的情况

User

package com.kk.pogo;public class User {private int id;private String name;private String password;}

问题:

User{id=2, name=‘wang’, password=‘null’}

解决方法:

核心配置文件

起别名

<select id="getUserById" resultType="User"parameterType="int">select id,name,pwd as password from mybatis.user where id = #{id}</select>

resultMap 结果集映射

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-////DTD Mapper 3.0//EN""/dtd/mybatis-3-mapper.dtd"><!--namespace绑定一个对应的mapper接口--><mapper namespace="com.hou.dao.UserMapper"><select id="getUserById" resultMap="UserMap" parameterType="int">select * from mybatis.user where id = #{id}</select><!--结果集映射--><resultMap id="UserMap" type="User"><!--colunm 数据库中的字段,property实体中的属性--><result column="id" property="id"></result><result column="name" property="name"></result><result column="pwd" property="password"></result></resultMap></mapper>

resultMap元素是 MyBatis 中最重要最强大的元素。ResultMap 的设计思想是,对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了。

<resultMap id="UserMap" type="User"><!--colunm 数据库中的字段,property实体中的属性--><!--<result column="id" property="id"></result>--><!--<result column="name" property="name"></result>--><result column="pwd" property="password"></result></resultMap>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。