项目代码
http://pan.baidu.com/s/1c01BLvi
主要文件
1.Mapper.xml
2.Mapper.java
3.pojo类
1.Mapper.xml
注意事项:
1.若有大量的重复代码,resultMap可以使用继承的方式,示例
collection: 将关联查询到的多条记录映射到集合对象中,与 ofType 配合使用
3.id 中的 columun
column="xxx" xxx对应数据库查询出来的字段
注意id 中的column需要各不相同, 可以采用取别名的方式
示例
package cn.itcast.mybatis.mapper;
import java.util.List;
import cn.itcast.mybatis.po.Orders;
import cn.itcast.mybatis.po.OrdersCustom;
public interface UserCustomMapper {
// 根据订单查询用户信息
// 主表:Orders
// 从表:User
List findOrderUser() throws Exception;
// 利用ResultMap进行关联映射
List findOrderUserResultMap() throws Exception;
// 利用ResultMap进行关联映射
List findOrdersAndOrderDetailMap() throws Exception;
}
package cn.itcast.mybatis.po;
import java.util.Date;
import java.util.List;
public class Orders {
private Integer id;
private Integer userId;
private String number;
private Date createtime;
private String note;
//用户信息
private User user;
//订单明细
private List orderdetails;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number == null ? null : number.trim();
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note == null ? null : note.trim();
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List getOrderdetails() {
return orderdetails;
}
public void setOrderdetails(List orderdetails) {
this.orderdetails = orderdetails;
}
@Override
public String toString() {
return "Orders [id=" + id + ", userId=" + userId + ", number=" + number
+ ", createtime=" + createtime + ", note=" + note + ", user="
+ user + ", orderdetails=" + orderdetails + "]";
}
}
package cn.itcast.mybatis.po;
import java.util.Date;
//hibernate字段名和属性名相对应
public class User {
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", birthday="
+ birthday + ", sex=" + sex + ", address=" + address + "]";
}
private int id;
private String username;
private Date birthday;
private String sex;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package cn.itcast.mybatis.po;
public class Orderdetail {
private Integer id;
private Integer ordersId;
private Integer itemsId;
private Integer itemsNum;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getOrdersId() {
return ordersId;
}
public void setOrdersId(Integer ordersId) {
this.ordersId = ordersId;
}
public Integer getItemsId() {
return itemsId;
}
public void setItemsId(Integer itemsId) {
this.itemsId = itemsId;
}
public Integer getItemsNum() {
return itemsNum;
}
public void setItemsNum(Integer itemsNum) {
this.itemsNum = itemsNum;
}
@Override
public String toString() {
return "Orderdetail [id=" + id + ", ordersId=" + ordersId
+ ", itemsId=" + itemsId + ", itemsNum=" + itemsNum + "]";
}
}