2

【笔记】MyBatis将Mysql字段名和Java属性名映射

 1 year ago
source link: https://feiju12138.github.io/2022/09/25/MyBatis%E5%B0%86Mysql%E5%AD%97%E6%AE%B5%E5%90%8D%E5%92%8CJava%E5%B1%9E%E6%80%A7%E5%90%8D%E6%98%A0%E5%B0%84/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

【笔记】MyBatis将Mysql字段名和Java属性名映射

MyBatis将Mysql字段名和Java属性名映射

在sql映射xml中配置映射

  • 在sql映射xml中配置文件中,利用<resultMap></resultMap>标签配置Mysql字段名和Java属性名的映射
  • 在其他需要返回值的标签属性中,不使用ResultType,改用ResultMap,值为<resultMap></resultMap>的id属性

src/main/resources/UserMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.Mapper.UserMapper">

<resultMap id="UserResultMap" type="com.pojo.User">
<result column="user_name" property="userName" />
</resultMap>

<select id="selectAll" resultMap="UserResultMap">
select * from user
</select>

</mapper>

哔哩哔哩——黑马程序员


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK