企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 第一种一对一写法 ~~~ <select id="findAllAccount" resultType="com.like.domain.AccountUser"> select a.*,u.username,u.address from account a ,user u where a.uid = u.id; </select> ~~~ ## 第二种 ~~~ package com.like.domain; import java.io.Serializable; public class Account implements Serializable { private Integer id; private Integer uid; private Double money; private User user; //这里加上一的引用 @Override public String toString() { return "Account{" + "id=" + id + ", uid=" + uid + ", money=" + money + ", 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; } } ~~~ ~~~ <!--定义封装account和user的resultMap--> <resultMap id="accountUserMap" type="com.like.domain.Account"> <id property="id" column="aid"/> <result property="uid" column="uid"/> <result property="money" column="money"/> <!--一对一定义关联--> <association property="user" column="uid" javaType="com.like.domain.User"> <id property="id" column="id"/> <result property="username" column="username"/> <result property="address" column="address"/> <result property="birthday" column="birthday"/> <result property="sex" column="sex"/> </association> </resultMap> <select id="findAll" resultMap="accountUserMap"> select u.*,a.id as aid,a.uid,a.money from account a,user u where a.uid = u.id; </select> ~~~ ~~~ SqlSession session = build.openSession(); IAccountDao mapper = session.getMapper(IAccountDao.class); List<Account> accountList = mapper.findAll(); for (Account account : accountList) { System.out.println(account); } ~~~