赞
踩
我们在学习Hibernate的时候,如果表涉及到两张的话,那么我们是在映射文件中使用..
等标签将其的映射属性关联起来的…那么在我们Mybatis中又怎么做呢???
先来回顾一下我们SQL99的语法:
一)内连接(等值连接):查询客户姓名,订单编号,订单价格 --------------------------------------------------- select c.name,o.isbn,o.price from customers c inner join orders o where c.id = o.customers_id; --------------------------------------------------- select c.name,o.isbn,o.price from customers c join orders o where c.id = o.customers_id; --------------------------------------------------- select c.name,o.isbn,o.price from customers c,orders o where c.id = o.customers_id; --------------------------------------------------- select c.name,o.isbn,o.price from customers c join orders o on c.id = o.customers_id; --------------------------------------------------- 注意:内连接(等值连接)只能查询出多张表中,连接字段相同的记录二)外连接:按客户分组,查询每个客户的姓名和订单数 --------------------------------------------------- 左外连接: select c.name,count(o.isbn) from customers c left outer join orders o on c.id = o.customers_id group by c.name; --------------------------------------------------- 右外连接: select c.name,count(o.isbn) from orders o right outer join customers c on c.id = o.customers_id group by c.name; --------------------------------------------------- 注意:外连接既能查询出多张表中,连接字段相同的记录;又能根据一方,将另一方不符合相同记录强行查询出来三)自连接:求出AA的老板是EE --------------------------------------------------- 内自连接: select users.ename,boss.ename from emps users inner join emps boss on users.mgr = boss.empno; --------------------------------------------------- 外自连接: select users.ename,boss.ename from emps users left outer join emps boss on users.mgr = boss.empno; --------------------------------------------------- 注意:自连接是将一张表,通过别名的方式,看作多张表后,再进行连接。 这时的连接即可以采用内连接,又可以采用外连接
由于我们Mybatis中并没有像Hibernate这样全自动化的,因此我们是没有..
等标签的,我们还是使用手写SQL语句来使我们的关联属性连接起来…
需求:
学生和身份证
--mysqlcreate table cards( cid int(5) primary key, cnum varchar(10));create table students( sid int(5) primary key, sname varchar(10), scid int(5), constraint scid_fk foreign key(scid) references cards(cid));insert into cards(cid,cnum) values(1,'111');insert into students(sid,sname,scid) values(1,'哈哈',1);select * from cards;select * from students;
- /** * 身份证(单方) * @author AdminTC */public class Card {
- private Integer id; private String num; public Card(){} public Integer getId() {
- return id; } public void setId(Integer id) {
- this.id = id; } public String getNum() {
- return num; } public void setNum(String num) {
- this.num = num; }}
- /** * 学生(单方) * @author AdminTC */public class Student {
- private Integer id; private String name; private Card card;//关联属性 public Student(){} public Integer getId() {
- retu
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。