需求:查出所有的账户并且显示账户下的对应的用户信息
- 先准备实体类上代码:
Account实体类:
package com.test.domain;
/**
* @author:shi jiaojie
* @date:2019/6/26 20:15
* @description:
*/
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;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", uid=" + uid +
", money=" + money +
", user=" + user +
'}';
}
}
User的实体类:
package com.test.domain;
import java.util.Date;
import java.util.List;
/**
* @author:shi jiaojie
* @date:2019/6/26 20:15
* @description:
*/
public class User {
private Integer id ;
private String username ;
private String sex ;
private Date birthday ;
private String address;
private List<Account> accounts;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public List<Account> getAccounts() {
return accounts;
}
public void setAccounts(List<Account> accounts) {
this.accounts = accounts;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", sex='" + sex + '\'' +
", birthday=" + birthday +
", address='" + address + '\'' +
", accounts=" + accounts +
'}';
}
}
AccountDao:
package com.test.dao;
import com.test.domain.Account;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* @author:shi jiaojie
* @date:2019/6/26 20:22
* @description:
*/
public interface AccountDao {
//查询账户并且显示账户所对应的用户信息
@Select("select u.*,a.id as aid,a.uid,a.money from account2 a left join user2 u on a.uid=u.id order by a.money desc")
@Results(id = "AccountUser",value = {
@Result(id = true,property = "id",column = "id"),
@Result(property = "id",column = "aid"),
@Result(property = "uid",column = "uid"),
@Result(property = "money",column = "money"),
@Result(property = "user.id",column = "id"),
@Result(property = "user.username",column = "username"),
@Result(property = "user.sex",column = "sex"),
@Result(property = "user.birthday",column = "birthday"),
@Result(property = "user.address",column = "address")
})
public List<Account> findAccountUser();
}
以上步骤做完后就开始测试了上代码:
//查询所有的用户并且显示用户的所有账号信息
@Test
public void findAl2() {
List<Account> list = mapper.findAccountUser();
for (Account account : list) {
System.out.println(account);
}
}
运行结果如下图所示:
谢谢你的阅览,如有疑问欢迎在下方提问。