mybatis快速入门
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis快速入门相关的知识,希望对你有一定的参考价值。
今天写写user表和orders表的mybatis的高级映射,一对一映射和一对多映射
1.创建一个orders.java文件
1.1一对一映射,一条订单对应一个用户
package cn.my.mybatis.entity; import java.util.Date; public class Orders { private int id; private int user_id; private String number; private Date createtime; private String note; private User user;//对应一个用户 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; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String toString() { return "Orders [id=" + id + ", user_id=" + user_id + ", number=" + number + ", createtime=" + createtime + ", note=" + note + ", user=" + user + "]"; } }
1.2创建一个OrdersDaoMapper.java接口
新增一个查询方法
package cn.my.dao; import java.util.List; import cn.my.mybatis.entity.Orders; public interface OrdersDaoMapper { public List<Orders> findAll(); }
1.3.创建一个ordersMapper.xml映射文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.my.dao.OrdersDaoMapper"> <!-- 映射一对一 和前面的用户字段取别名类似--> <resultMap type="cn.my.mybatis.entity.Orders" id="oneOnone"> <id column="id" property="id"/> <result column="user_id" property="user_id"/> <result column="number" property="number"/> <result column="createtime" property="createtime"/> <result column="note" property="note"/> <!-- 重点在这里,映射一对一的 --> <association property="user" javaType="cn.my.mybatis.entity.User"> <id column="user_id" property="id"/> <result column="username" property="username"/> <result column="birthday" property="birthday"/> <result column="sex" property="sex"/> <result column="address" property="address"/> </association> </resultMap> <select id="findAll" resultMap="oneOnone"> SELECT a.*,b.username,b.birthday,b.sex,b.address FROM `orders` a,`user` b WHERE a.user_id=b.id </select> </mapper>
1.4.创建一个TestOrdersMapper.java文件
创建一个findAll的测试方法
package cn.my.test; import java.io.IOException; import java.io.InputStream; import java.util.List; 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 cn.my.dao.OrdersDaoMapper; import cn.my.mybatis.entity.Orders; public class TestOrdersMapper { private SqlSessionFactory factory; @Before public void setUp() throws IOException{ String resource = "SqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); factory = new SqlSessionFactoryBuilder().build(inputStream); } @Test public void testFindAll(){ SqlSession session = factory.openSession(); OrdersDaoMapper dao = session.getMapper(OrdersDaoMapper.class);//获取mybatis的动态代码实现接口对象 List<Orders> list = dao.findAll(); session.close(); for (Orders orders : list) { System.out.println(orders); } } }
以上都弄好了,最后记住一点很重要的,SqlMapConfig.xml需要引入<mapper resource="mapper/OrdersMapper.xml"/>
<mappers> <mapper resource="User.xml"/> <mapper resource="mapper/UserMapper.xml"/> <mapper resource="mapper/OrdersMapper.xml"/> </mappers>
运行结果:
Orders [id=3, user_id=1, number=1000010, createtime=Wed Feb 04 13:22:35 CST 2015, note=null, user=User [id=1, username=王五, sex=2, birthday=null, address=null]]
Orders [id=4, user_id=1, number=1000011, createtime=Tue Feb 03 13:22:41 CST 2015, note=null, user=User [id=1, username=王五, sex=2, birthday=null, address=null]]
Orders [id=5, user_id=10, number=1000012, createtime=Thu Feb 12 16:13:23 CST 2015, note=null, user=User [id=10, username=张三, sex=1, birthday=Thu Jul 10 00:00:00 CST 2014, address=北京市]]
2.一对多的映射,一个订单对应一个用户,一个订单对应多个详细
2.1创建一个Ordersdetail.java文件
package cn.my.mybatis.entity; public class Ordersdetail { private int id; private int items_id; private int items_num; private int orders_id; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getItems_id() { return items_id; } public void setItems_id(int items_id) { this.items_id = items_id; } public int getItems_num() { return items_num; } public void setItems_num(int items_num) { this.items_num = items_num; } public int getOrders_id() { return orders_id; } public void setOrders_id(int orders_id) { this.orders_id = orders_id; } @Override public String toString() { return "Ordersdetail [id=" + id + ", items_id=" + items_id + ", items_num=" + items_num + ", orders_id=" + orders_id + "]"; } }
2.2在ordersMapper.xml里面添加映射代码
<!-- extends="oneOnone"这里需要注意的是继承了上面的id名称为oneOnone的resultMap, 因为这里也包含了一对一映射,所以不需要在写了直接继承过来就好了 --> <resultMap type="cn.my.mybatis.entity.Orders" id="DetialAll" extends="oneOnone"> <!-- 这里就是映射list集合 --> <collection property="detailList" ofType="cn.my.mybatis.entity.Ordersdetail"> <id column="ordersdetail_id" property="id"/> <result column="items_id" property="items_id"/> <result column="items_num" property="items_num"/> <result column="orders_id" property="orders_id"/> </collection> </resultMap> <!-- 一对多的映射,一个订单对应一个用户,一个订单对应多个详细 --> <select id="findDetialAll" resultMap="DetialAll"> SELECT a.*,b.username,b.birthday,b.sex,b.address,c.id ordersdetail_id,c.items_id,items_num FROM `orders` a,`user` b,`orderdetail` c WHERE a.user_id=b.id AND a.id=c.orders_id </select>
2.3在OrdersDaoMapper.java添加接口方法
public List<Orders> findDetialAll();
2.4TestOrdersMapper.java中添加测试方法
@Test public void testfindDetialAll(){ SqlSession session = factory.openSession(); OrdersDaoMapper dao = session.getMapper(OrdersDaoMapper.class);//获取mybatis的动态代码实现接口对象 List<Orders> list = dao.findDetialAll(); session.close(); for (Orders orders : list) { System.out.println(orders); } }
测试结果:
Orders [
id=3, user_id=1, number=1000010, createtime=Wed Feb 04 13:22:35 CST 2015, note=null,
user=User [
id=1, username=王五, sex=2, birthday=null, address=null
],
detailList=[
Ordersdetail [id=1, items_id=1, items_num=1, orders_id=0],
Ordersdetail [id=2, items_id=2, items_num=3, orders_id=0]
]
]
Orders [id=4, user_id=1, number=1000011, createtime=Tue Feb 03 13:22:41 CST 2015, note=null, user=User [id=1, username=王五, sex=2, birthday=null, address=null], detailList=[Ordersdetail [id=3, items_id=3, items_num=4, orders_id=0], Ordersdetail [id=4, items_id=2, items_num=3, orders_id=0]]]
这个mybatis高级映射就写完了,有时间我会继续往下写的,如果需要加群一起研究的可以q我,939705214,也可以添加群号581591235
以上是关于mybatis快速入门的主要内容,如果未能解决你的问题,请参考以下文章
13.2 MyBatis Generator 快速入门(MyBatis Generator逆向代码生成工具) -《SSM深入解析与项目实战》