配置文件编写总结(二)一对一映射

1.首先给出几张必要的表和对应的.java文件
1.1t_user表和user.java文件
在这里插入图片描述

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable{
    private Integer uid; //用户id
    private String username;// 用户姓名
    private String sex;// 性别
    private Date birthday;// 生日
    private String address;// 地址
    private List<Account> accountList; //用户和账号的一对多关系

    private List<Role> roleList; //用户和角色的一对多关系
}

1.2t_role表和Role.java文件
在这里插入图片描述

@Data
public class Role implements Serializable{
    private Integer rid;
    private String rName;
    private String rDesc;
    private List<User> userList;// 角色和用户的一对多关系

}

1.3t_account表和Account.java
在这里插入图片描述

@Data
public class Account implements Serializable{
    private Integer aid;
    private Double money;
    private Integer uid;
    private User user;
}

1.4 t_user和t_role的中间表user_role
在这里插入图片描述
2.根据账户表的aid查询账户信息并且根据账户信息查询拥有该账户的用户信息。
一个账户只能有一个用户因此是一对一映射关系)需要连接t_account和t-user,连接条件是外键指向主键,t_account表中的uid指向t_user中的uid。
在这里插入图片描述
以uid=1为例进行查询
在这里插入图片描述
查询出来的信息怎么封装?
Account信息封装到Account ,user信息封装到User。但是现在查询出来的既有account又有user.不能让account继承user,因为本身他们二个没有继承关系。重点:在 Account 类中加入 User类的对象作为 Account 类的一个属性。因为是用账户信息来查的用户,一个账户有一个用户,但是一个用户可以多个账户。
以哪张表为主体进行查询,查询到的数据就封装到哪张表对应的pojo中。如果是一对一的关系,那么就在pojo中添加另外一个pojo类型的属性。
以上图查询为例是以account为主体进行查询,那么就在account表中添加user属性,把user的属性当做一个整体看做account的一个属性。

3.在AccountDao创建查询方法: findAccountUserByAid()

public interface AccountDao {
    /**
     * 根据aid查询账户信息,并且连接t_user表查询该账户对应的用户信息
     * @param aid
     * @return
     */
    Account findAccountUserByAid(int aid);
}

4.AccountDao.xml编写


<mapper namespace="com.***.***.AccountDao">//这里是AccountDao的全限定名
    <!--resultType是默认映射-->
    <resultMap id="AccountUserMap" type="Account">
    <id column="aid" property="aid"/>
   <result column="money" property="money"/>
    <result column="uid" property="uid"/>
    <!--使用association标签进行一对一映射
    property 表示要进行一对一映射的属性名
    javaType要进行一对一映射的属性类型
    -->
    <association property="user" javaType="User">
        <result column="uid" property="uid"/>
        <result column="username" property="username"/>
        <result column="birthday" property="birthday"/>
        <result column="sex" property="sex"/>
        <result column="address" property="address"/>

    </association>
 </resultMap>
    <select id="findAccountUserByAid" parameterType="int" resultMap="AccountUserMap">
 select * from t_account a,t_user u where a.uid=u.uid and a.aid=#{aid}
    </select>
</mapper>

5.Test文件编写

@Test
    public void testFindAccountUserByAid() throws IOException {
        //1. 创建SqlSession
        SqlSession sqlSession = SqlSessionFactoryUtil.openSession();
        AccountDao accountDao = sqlSession.getMapper(AccountDao.class);

        //2. 调用方法
        Account account = accountDao.findAccountUserByAid(1);

        //System.out.println(account.getUser().getUsername());
        System.out.println(account);
        SqlSessionFactoryUtil.closeAndCommit(sqlSession);
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值