一、问题引入:
查询用户信息时,将用户的所有账户也查询出来,使用注解方式实现
(一个账户具有多个用户信息,所以形成了用户和账户之间的一对多关系)
account表
user表:
二、 User实体类:
User.java:(加入 List< Account >)
package com.itheima.domain;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
public class User implements Serializable {
private Integer id;
private String username;
private String address;
private String sex;
private Date birthday;
// 一对多关系映射:一个用户对应多个账户
private List<Account> accounts;
public List<Account> getAccounts() {
return accounts;
}
public void setAccounts(List<Account> accounts) {
this.accounts = 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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", address='" + address + '\'' +
", sex='" + sex + '\'' +
", birthday=" + birthday +
'}';
}
}
Account.java
package com.itheima.domain;
import java.io.Serializable;
public class Account implements Serializable {
private Integer id;
private Integer uid;
private Double money;
//多对一(Mybatis称之为1对1)的映射:一个账户只能属于一个用户
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.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;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", uid=" + uid +
", money=" + money +
'}';
}
}
三、持久层接口:
IUserDao.java(用户的持久层接口):
里面的FetchType.LAZY(这个意思是延迟加载)
package com.itheima.dao;
import com.itheima.domain.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import java.util.List;
/*
* 在mybatis针对CRUD一共有四个注解
* @Select @Insert @Update @Delete
* */
@CacheNamespace(blocking = true)
public interface IUserDao {
/*当属性名称和数据库中字段名不一致时,用@results和@result注解*/
//查询所有用户
@Select("select * from user")
@Results(id = "userMap",value ={
@Result(id = true,column = "id",property = "id"),
@Result(column = "username",property = "username"),
@Result(column = "address",property = "address"),
@Result(column = "sex",property = "sex"),
@Result(column = "birthday",property = "birthday"),
@Result(property = "accounts",column = "id",
many = @Many(select = "com.itheima.dao.IAccountDao.findAccountByUid",
fetchType = FetchType.LAZY))
})
List<User> findAll();
/*
* 根据id查询用户
* */
@Select("select * from user where id=#{id}")
User findById(Integer userId);
//根据用户名称模糊查询
@Select("select * from user where username like #{username}")
List<User> findUserByName(String username);
}
IAccountDao.java(账户的持久层接口)
package com.itheima.dao;
import com.itheima.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;
public interface IAccountDao {
/*
* 查询所有账户,并且获取每个账户所属的用户信息
* */
@Select("select * from account")
@Results(id = "accountMap",value={
@Result(id =true,column = "id",property = "id"),
@Result(column = "uid",property = "uid"),
@Result(column = "money",property = "money"),
@Result(property = "user",column = "uid",one = @One(select="com.itheima.dao.IUserDao.findById",fetchType= FetchType.EAGER))
})
List<Account> findAll();
//根据用户id查询账户信息
@Select("select * from account where uid=#{userId}")
List<Account> findAccountByUid(Integer userId);
}
四、测试类
package com.itheima.test;
import com.itheima.dao.IUserDao;
import com.itheima.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
public class AnnotationCRUDTest {
private SqlSessionFactory factory;
private SqlSession session;
private IUserDao userDao;
private InputStream in;
@Before
public void init() throws Exception{
in= Resources.getResourceAsStream("SqlMapConfig.xml");
factory=new SqlSessionFactoryBuilder().build(in);
session=factory.openSession();
userDao=session.getMapper(IUserDao.class);
}
@After
public void destroy() throws Exception{
session.commit();
session .close();
in.close();
}
@Test
public void testFindAll(){
List<User> users=userDao.findAll();
for(User user:users){
System.out.println("-----每个用户的信息-----");
System.out.println(user);
System.out.println(user.getAccounts());
}
}
@Test
public void testFindOne(){
User user=userDao.findById(56);
System.out.println(user);
}
@Test
public void testFindByName(){
List<User> users=userDao.findUserByName("%mybatis%");
for(User user:users){
System.out.println(user);
}
}
}