1.解决属性名和字段名不一致问题
-
改变User类中的一个属性,使之与对应的字段名不一致
-
查出的结果有问题,不一致的字段为null
-
方法一:起别名,将sql语句改为
<select id="getUserById" parameterType="int" resultType="User"> <!--原来的select * from user where id=#{id}--> select id,username,password as pwd from user where id=#{id} </select>
-
方法二:resultMap,结果集映射
<!--结果集映射,resultMap标签的id和resultMap属性的值相等,引用--> <resultMap id="UserMap" type="User"> <!--column数据库表的字段名,property实体类的属性名--> <result column="id" property="id"/> <result column="username" property="username"/> <result column="password" property="pwd"/> </resultMap> <select id="getUserById" parameterType="int" resultMap="UserMap"> select * from user where id=#