项目结构
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);
}