mybatis中resultType与resultMap的区别

resultType与resultMap的区别

1)使用resultType时,实体类中的变量名必须与SQL语句中的字段名相同

resultType="类路径"

如若在mybatis-config.xml文件中,存在这样的配置:

开启驼峰命名转换:Table{create_time} -> pojo{createTime}

<setting name="mapUnderscoreToCamelCase" value="true"/>

因此只要遵循这样的规范,即数据库中使用下划线这样的形式,实体类中使用驼峰命名,java类中的变量名可以与SQL语句中的字段名不同。


2)使用resultMap时,resultMap="id名"

在mybatis的mapper配置文件中,直接绑定了实体类的变量名与数据库表中的字段名。例如:

<mapper>
<resultMap type="实体类所在位置" id="">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="username" jdbcType="VARCHAR" property="username"/>
        <result column="password" jdbcType="VARCHAR" property="password.encrypted"/>
        <result column="administrator" jdbcType="BOOLEAN" property="administrator"/>
</resultMap>
</mapper>



猜你喜欢

转载自blog.csdn.net/weixin_42228338/article/details/80767008