首先的 是 最原始的 pojo 类来 做简单映射
简单 pojo 映射:
以下都是对于原始基本的 pojo 类 进行包装扩展后来实现 其他列的映射
一对一:
一对多:
多对多:
关键还是在于 mapper.xml 文件的书写
上面配置文件都写完后,就可以进行 mapper 接口 mapper.java 的编写了
顺便贴上 相应的 pojo (User 里 包含一个 List<Orders> 然后 Orders 里又包含一个 List<OrderDetail> 最后 OrderDetail 里包含一个 Item ) 修改后的样子:
也就是 数据库的 主从表之间的外键关系,
User ------> Order (1:n)
Order -----> OrderDetail (1:n)
OrderDetail ----> Item (1:1)
下面开始 贴 多对多的 pojo 部分的 代码了 (我觉得 多对多都会了 其他的都不在话下,所以其他的没必要贴了):
package com.ghc.pojo;import java.util.Date;import java.util.List;public class User { private int id; private String userName; private Date birthday; private String sex; private String address; // 一个人有多个订单 private ListorderList; public List getOrderList() { return orderList; } public void setOrderList(List orderList) { this.orderList = orderList; } 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 com.ghc.pojo;import java.util.Date;import java.util.List;public class Orders { private int id; private int user_id; private String number; private Date createtime; private String note; public ListgetOrderDetailList() { return orderDetailList; } public void setOrderDetailList(List orderDetailList) { this.orderDetailList = orderDetailList; } // 一个订单可以有多个订单明细 private List orderDetailList; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getUser_id() { return user_id; } public void setUser_id(int user_id) { this.user_id = user_id; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } 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; }}
package com.ghc.pojo;public class OrderDetail { private int id; private int ordersId; private int itemsId; private int itemsNum; //而一个订单明细对应一条项目 private Items item; public Items getItem() { return item; } public void setItem(Items item) { this.item = item; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getOrdersId() { return ordersId; } public void setOrdersId(int ordersId) { this.ordersId = ordersId; } public int getItemsId() { return itemsId; } public void setItemsId(int itemsId) { this.itemsId = itemsId; } public int getItemsNum() { return itemsNum; } public void setItemsNum(int itemsNum) { this.itemsNum = itemsNum; }}
package com.ghc.pojo;import java.util.Date;public class Items { private int id; private String name; private float price; private String detail; private String pic; private Date createtime; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail; } public String getPic() { return pic; } public void setPic(String pic) { this.pic = pic; } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; }}
package com.ghc.dao;import com.ghc.pojo.User;import com.ghc.pojo.UserOrderWrapper;import com.ghc.pojo.UserOrderWrapperFirst;import java.util.List;public interface UserDao { User findUserById(int id) throws Exception; ListfindUserOrder() throws Exception; List findOneToMany() throws Exception; List findManyToMany() throws Exception;}
如果经常要打印输出,建议在 pojo 中 覆写 toString,不然就要跟下面这么苦逼的 拼 String 打印了
import com.ghc.dao.UserDao;import com.ghc.pojo.*;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.Before;import org.junit.Test;import java.io.IOException;import java.util.List;public class UserDaoTest { private SqlSessionFactory sqlSessionFactory; @Before public void injectSqlSessionFactory() throws IOException { sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("config/SqlMappingConfig.xml")); } @Test public void testFindUserById() throws Exception { UserDao userDao = sqlSessionFactory.openSession().getMapper(UserDao.class); User user = userDao.findUserById(1); System.out.println(user.getUserName()); ListuserOrderWrapperFirstList = userDao.findUserOrder(); for (UserOrderWrapperFirst userOrder : userOrderWrapperFirstList) { String userName = userOrder.getUser().getUserName(); int orderId = userOrder.getId(); System.out.println(userName + " <-----> " + String.valueOf(orderId)); } } @Test public void testFindOneToMany() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); UserDao userDao = sqlSession.getMapper(UserDao.class); List userOrderWrappers = userDao.findOneToMany(); for (int i = 0; i < userOrderWrappers.size(); i++) { UserOrderWrapper uow = userOrderWrappers.get(i); List orderDetails = uow.getOrderDetails(); User user = uow.getUser(); for (int j = 0; j < orderDetails.size(); j++) { System.out.print(uow.getId() + "--" + uow.getUser_id() + "--" + uow.getNumber() + "--" + uow.getCreatetime() + "--" + uow.getNote() + "--"); System.out.print(user.getUserName() + "--" + user.getSex() + "--" + user.getAddress() + "--"); OrderDetail odt = orderDetails.get(j); System.out.println(odt.getId() + "--" + odt.getItemsId() + "--" + odt.getItemsNum() + "--" + odt.getOrdersId()); } } } @Test public void testFindManyToMany() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); UserDao userDao = sqlSession.getMapper(UserDao.class); List userList = userDao.findManyToMany(); for (User user : userList) { List orderList = user.getOrderList(); for (Orders order : orderList) { List orderDetailList = order.getOrderDetailList(); for (OrderDetail orderDetail : orderDetailList) { Items item = orderDetail.getItem(); System.out.println(order.getId() + "--" + order.getUser_id() + "--" + order.getNumber() + "--" + order.getCreatetime() + "--" + order.getNote() +"--" + user.getUserName() + "--" + user.getSex() + "--" + user.getAddress() + "--" + orderDetail.getId() + "--" + orderDetail.getItemsId() + "--" + orderDetail.getItemsNum() + "--" + orderDetail.getOrdersId() + "--" + item.getId() + "--" + item.getName() + "--" + item.getName() + "--" + item.getPrice() + "--" + item.getDetail() + "--" + item.getPic() + "--" + item.getCreatetime()); } } } }}
测试结果 与数据库基本一致 :
至此, mybatis 的 所谓高级部分全部完成(动态 sql 部分另开一篇去说好了),实际项目开发的时候,单表是采用 generator 逆向工程生成 pojo,mapper.xml , mapper.java 的
但是也需要配置数据库连接,表信息等等。。。到一个 generatorconfig.xml 这样类似的 一个 配置文件里,然后调用生成工具的代码进行生成。
但是懂了原理,任他怎么来都容易理解了。