mybatis resultmap映射结果集(xml映射配置一)

发布时间:2024-11-12 13:54

最新推荐文章于 2024-09-24 08:58:10 发布

dengjili 于 2018-10-25 21:11:23 发布

自动映射例子

当数据库名称与pojo一致时候,可以直接自动映射

<select id="selectUsers" resultType="com.someapp.model.User"> select id, username, hashedPassword from some_table where id = #{id} </select> 12345

当数据库名称与pojo不一致时候,采用类型别名,比如:

<select id="selectUsers" resultType="User"> select user_id as "id", user_name as "userName", hashed_password as "hashedPassword" from some_table where id = #{id} </select> 12345678 ResultMap设计

使用外部的 resultMap 会怎样,解决列名不匹配的另外一种方式。

数据库column -> java pojo property

<resultMap id="userResultMap" type="User"> <id property="id" column="user_id" /> <result property="username" column="user_name"/> <result property="password" column="hashed_password"/> </resultMap> 12345

其中id表示主键,对应xml文件可配置如下

<select id="selectUsers" resultMap="userResultMap"> select user_id, user_name, hashed_password from some_table where id = #{id} </select> 12345 ResultMap高级结果映射

pojo对象

public class Employee { private Integer id; private String realName; private SexEnum sex; private Date birthday; private String mobile; private String email; private String position; private String note; private WorkCard workCard; private List<EmployeeTask> employeeTasks; }

123456789101112131415161718192021

xml配置文件

<resultMap id="BaseResultMap" type="priv.dengjl.ns.bean.Employee"><result column="id" jdbcType="INTEGER" property="id" /><result column="real_name" jdbcType="VARCHAR" property="realName" /><result column="sex" typeHandler="priv.dengjl.ns.handler.SexEnumHandler"property="sex" /><result column="birthday" jdbcType="DATE" property="birthday" /><result column="mobile" jdbcType="VARCHAR" property="mobile" /><result column="email" jdbcType="VARCHAR" property="email" /><result column="position" jdbcType="VARCHAR" property="position" /><result column="note" jdbcType="VARCHAR" property="note" /><association column="id" property="workCard"select="priv.dengjl.ns.mapper.WorkCardMapper.getWorkCardByEmpId" /><collection column="id" property="employeeTasks"select="priv.dengjl.ns.mapper.EmployeeTaskMapper.getEmployeeTaskListByEmpId" /><discriminator column="sex" javaType="int"><case value="1" resultMap="MaleEmployeeMap"/><case value="0" resultMap="FemaleEmployeeMap"/></discriminator></resultMap>

12345678910111213141516171819 一对一 workCard对象

<association column="id" property="workCard"select="priv.dengjl.ns.mapper.WorkCardMapper.getWorkCardByEmpId" /> 12 一对多 workCard对象

<association column="id" property="workCard"select="priv.dengjl.ns.mapper.WorkCardMapper.getWorkCardByEmpId" /> 12 鉴别器

<discriminator column="sex" javaType="int"><case value="1" resultMap="MaleEmployeeMap"/><case value="0" resultMap="FemaleEmployeeMap"/></discriminator> 1234

其中select对应的值为单个文件的xml配置
1

网址:mybatis resultmap映射结果集(xml映射配置一) https://www.yuejiaxmz.com/news/view/56480

相关内容

【原创】springboot+mysql宠物领养和论坛系统设计与实现
萧山滨映时代府预约电话-滨映时代府-滨映时代府楼盘详情/房价
嘉定映翠府官网房价-上海映翠府楼盘优惠-嘉定映翠府房价折扣详情
【装修案例】射灯、轨道灯怎么布置和使用?
【手工折纸枪】折纸枪可发射子弹教程 一把可以发射的纸枪
反映生活智慧的谚语
太原全面实行死刑注射方式 新建注射刑场
7招避免生活辐射
后背为五脏六腑的反射区!谨记:保养后背=保命
居家 DIY光栅衍射系列实验

随便看看