Mybatis关于使用注解问题,参数对应,多表查询

项目结构

在这里插入图片描述

1、pojo对象

User对象

package com.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 +
                ", accounts=" + accounts +
                '}';
    }
}

Account对象

package com.domain;

import java.io.Serializable;

public class Account implements Serializable {
    private Integer id;
    private Integer uid;
    private Double money;
//    多对一 多表查询
    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 +
                ", user=" + user +
                '}';
    }
}

2、查询接口

一个User对应多个Account,在UserDao中使用 many
UserDao

package com.dao;

import com.domain.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface UserDao {
    /*查询所有用户
    * 只有主键的值 id 要设置为 true
    * results的id设置为 userMap,可以方便别语句使用
    * 一个用户对应多个账户,在这里使用 many,many中的select中调用的方法在AccountDao中一定要有实现,反之在AccountDao中需要根据账户信息查询用户信息也是一样,具体看实现代码
    * @Result(property = "account",column = "id",many=@Many(select="com.dao.AccountDdao.findAccountByUid",fetchType = FetchType.LAZY))
    * property:对应User对象中的属性名
    * column:user对象的主键ID(因为是要根据user的id去查询account)
    * many:多表对应
    * fetchType:加载时机*/
    @Select("select * from user")
    @Results(id="userMap",value={
            @Result(id=true,column="id",property = "userId"),
            @Result(column = "username",property = "userName"),
            @Result(column = "address",property = "userAddress"),
            @Result(column = "sex",property = "userSex"),
            @Result(column = "birthday",property = "userBirthday"),
            @Result(property = "account",column = "id",many=@Many(select="com.dao.AccountDdao.findAccountByUid",fetchType = FetchType.LAZY))
    })
    List<User> findAll();

    /*根据id查询用户
    在这里可能会出现字段和数据库属性名对应不上,可以通过上面已经编写的 @Results(...)语句来调用 ,@ResultMap(value={"userMap"})
    * */
    @Select("select * from user where id=#{id}")
    @ResultMap(value={"userMap"})
    User findById(Integer userId);

    /*保存用户*/
    @Insert("insert into user(username,address,sex,birthday) values(#{username),#{address},#{sex},#{birthday}")
    void saveUser(User user);

    /*根据用户名模糊查询*/
//    方式一:name在传过来之前,就已经添加了 % 。值为 %xxx%
//    @Select("select * from user where name like #{name}")
//    方式二:
    @Select("select * from user where user like '%${name}%'")
    List<User> findUserByName(String username);
}

AccountDao
多个Account对应一个User,在AccountDao中使用 one

package com.dao;

import com.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 AccountDao {
    /*查询所有账户,并且获取每个账户所属的用户信息*/
    @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.dao.UserDao.findById",fetchType = FetchType.EAGER))
    })
    List<Account> findAll();

    /*根据用户id查询账户信息*/
    @Select("select * from account where uid=#{userId}")
    List<Account> findAccountByUid(Integer userId);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值