需求:查询所有的账户并且显示账户所对应的用户的信息
- 准备实体类:
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 +
'}';
}
}
2.然后准备AccountDao,废话少说直接上代码:
package com.test.dao;
import com.test.domain.Account;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;
import java.util.List;
/**
* @author:shi jiaojie
* @date:2019/6/26 20:22
* @description:
*/
public interface AccountDao {
//查询账户并且显示账户所对应的用户信息
@Select("select * from account2 order by money desc")
@Results(id = "AccountUser2",value = {
@Result(id = true,property = "id",column = "id"),
@Result(property = "id",column = "id"),
@Result(property = "uid",column = "uid"),
@Result(property = "money",column = "money"),
@Result(property = "user",column = "uid",one = @One(select = "com.test.dao.UserDao.findByUid",fetchType = FetchType.LAZY))
})
public List<Account> findAccountUser2();
}
3.准备UserDao的代码:
package com.test.dao;
import com.test.domain.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
/**
* @author:shi jiaojie
* @date:2019/6/26 20:22
* @description:
*/
public interface UserDao {
//通过uid查询用户信息
@Select("select * from user2 where id=#{uid}")
public User findByUid(int uid);
}
4.进行单元测试:
//查询所有的账户并且显示用户的信息222
@Test
public void findAccountUser2() {
List<Account> list = mapper.findAccountUser2();
for (Account account : list) {
System.out.println(account);
}
}
5.测试显示的结果如下:
如有问题欢迎下方提问交流,喜欢就点个赞哦。