本文主要是介绍Mybatis 数据表一对一查询mapper文件配置(association ),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
银行账户与用户为一对一的关系,即一个银行账户只对应于一个用户
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sealong.dao.IAccountDao"><!--主表:account--><resultMap id="AccountUserMap" type="com.sealong.domain.Account"><id column="aid" property="id"></id><result property="uid" column="uid"></result><result property="money" column="money"></result><!--从表:Userassociation 用于数据表一对一的标签property为对象Account中的user属性名column为主表与从表相关联的字段属性:即account表中的uid与user表中的id字段相对应javaType为对象Account中的user属性所对应的对象--><association property="user" column="uid" javaType="com.sealong.domain.User"><id column="id" property="id"></id><result column="username" property="username"></result><result column="birthday" property="birthday"></result><result column="sex" property="sex"></result><result column="address" property="address"></result></association></resultMap><select id="findAll" resultMap="AccountUserMap">select a.id as aid,a.uid ,a.money,u.*from account aleft join user uon a.uid = u.id order by a.uid</select>
</mapper>
package com.sealong.domain;public class Account {private Integer id;private Integer uid;private Double money;private User user;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public Integer getUid() {return uid;}public void setUid(Integer uid) {this.uid = uid;}public Double getMoney() {return money;}public void setMoney(Double money) {this.money = money;}public User getUser() {return user;}public void setUser(User user) {this.user = user;}@Overridepublic String toString() {final StringBuffer sb = new StringBuffer("Account{");sb.append("id=").append(id);sb.append(", uid=").append(uid);sb.append(", money=").append(money);sb.append(", user=").append(user);sb.append('}');return sb.toString();} }
这篇关于Mybatis 数据表一对一查询mapper文件配置(association )的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!