java中两表联查的语句_SSM(二)MyBatis多表联查

这篇文章写了以下几个简单的例子,用来说明MyBatis多标联查基本语法

1.sql片段的用法

2.一对多查询

3.多条sql的一对多查询

4.多对一查询

5.多条sql一对多查询

6、多对多查询

这里沿着接口→小配置的路线写了,测试类就是遍历输出结果:

一、接口:

1 package cn.sohappy.acourses.course0921;2

3 import cn.sohappy.acourses.bean.BillManyToOne;4 import cn.sohappy.acourses.bean.UserOneToMany;5 import cn.sohappy.bean.Smbms_user;6

7 import java.util.List;8

9 public interfaceIUserDAO {10 //01.sql片段,查询所有user

11 ListfindAll();12 //02.oneToMany,传入user,返回包含账单信息的user

13 UserOneToMany getUserOneToManyBills(UserOneToMany user);14 //03.oneToMany,多条sql查询,传入user,返回包含账单信息的user

15 UserOneToMany getUserOneToManyBillsMultiSQL(UserOneToMany user);16 //04.manyToOne,传入bill,返回包含用户信息的bill

17 BillManyToOne getBillManyToOneUser(BillManyToOne bill);18 //05.manyToOne,多条sql查询,传入bill,返回包含用户信息的bill

19 BillManyToOne getBillManyToOneUserMultiSQL(BillManyToOne bill);20 }

二、小配置

先实现第一个方法

1、ListfindAll();查询所有user的编号,名字,密码

小配置的配置头

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

/p>

PUBLIC"-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

View Code

以下省略配置头

userCode,userName,userPassword

select fromsmbms_user

2、UserOneToMany getUserOneToManyBills(UserOneToMany user);

查询某个用户的账单信息,首先将账单List植入用户类中(第11行)

1 package cn.sohappy.acourses.bean;2

3 import java.util.List;4

5 public classUserOneToMany {6 privateLong id;7 privateString usercode;8 privateString username;9

10 //a user has lots of bills

11 private Listbills;12

13 //getter and setter

14 }

小配置代码:

resultMap中property是对象的属性名,column是数据表中的字段名。collection是UserOneToMany对象中植入的泛型集合属性List bills

语法是:...code...

1

2

3

4

5

6

7

8

9

10

11

12

13 select smbms_user.id as u_id,userName,smbms_bill.id as b_id,productName,billCode fromsmbms_user,smbms_bill14 where smbms_user.id=smbms_bill.createdBy and userCode=#{usercode}15

3、UserOneToMany getUserOneToManyBillsMultiSQL(UserOneToMany user);

该方法通过多条sql查询user和其账单

小配置代码:其中#{**}是占位符

1

2

3

4

5

6

7

8

9 select * from smbms_bill where createdBy=#{**}10

11

12 select * from smbms_user where userCode=#{usercode}13

4、BillManyToOne getBillManyToOneUser(BillManyToOne bill);

传入bill,返回包含用户信息的bill,这里需要在bill类中植入user属性及相应getter and setter:private UserOneToMany user;

小配置代码:这里使用的语法是:...code...

1

2

3

4

5

6

7

8

9

10

11

12 select smbms_user.id as u_id,userCode,userName,smbms_bill.id as b_id,billCode fromsmbms_user,smbms_bill13 where smbms_user.id=smbms_bill.createdBy and billCode=#{billcode}14

5.BillManyToOne getBillManyToOneUserMultiSQL(BillManyToOne bill);多条sql多对一查询

小配置代码:

1

2

3

4

5

6

7

8

9

10

11

12 select * from smbms_user where id=#{**}13

14

15

16 select id,billCode,createdBy from smbms_bill where billCode=#{billcode}17

最后写下多对多查询

其实多对多查询和一对多查询是一样的,只不过表中可能没有公共字段,要借助第三张表。

举个例子:根据老师id查询他所教授学生的id

下面建立三张表:

这是student表

8beba8a92f565e3f2b63480cb97f446a.png

这是老师表

cde9841b514e0830bbafc437dad20ff8.png

这是第三张表

f3e9415344d9270ecdb97a209cd59cda.png

步骤和一对多是一样的,先生成实体类,然后在老师中植入学生List

创建接口,写个方法:

1 package cn.sohappy.acourses.course0923;2

3 import cn.sohappy.acourses.bean.Teachert14;4

5 public interfaceITeacherDAO {6 Teachert14 findStudentsByTeacher(Teachert14 teacher);7 }

下面直接写小配置了:

1 <?xml version="1.0" encoding="UTF-8" ?>

2 /p>

4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

5

6

7

8

9

10

11

12

13

14

15 select studentt14.sid,sname,teachert14.tid,tname fromstudentt14,teachert14,teacher_studentt1416 where studentt14.sid=teacher_studentt14.sid and teachert14.tid=teacher_studentt14.tid17 and teachert14.tid=#{tid}18

19

最后附上测试类和MyBatis工具类:

测试类:

1 package cn.test;2

3 import cn.sohappy.acourses.bean.Studentt14;4 import cn.sohappy.acourses.bean.Teachert14;5 import cn.sohappy.acourses.course0923.ITeacherDAO;6 import cn.sohappy.util.MyBatisUtil;7 import org.apache.ibatis.session.SqlSession;8 import org.junit.Test;9

10 public classtest20170923 {11 //多对多,借助第三张表

12 @Test13 public voidfindStudentsByTeacher(){14 SqlSession session =MyBatisUtil.getSession();15 ITeacherDAO mapper = session.getMapper(ITeacherDAO.class);16 Teachert14 teachert14 = newTeachert14();17 teachert14.setTid(1L);18 Teachert14 teacher =mapper.findStudentsByTeacher(teachert14);19 for(Studentt14 item:teacher.getStudentt14s()) {20 System.out.println(item.getSname());21 }22 }23 }

MyBatis工具类:

1 package cn.sohappy.util;2

3 import org.apache.ibatis.io.Resources;4 import org.apache.ibatis.session.SqlSession;5 import org.apache.ibatis.session.SqlSessionFactory;6 import org.apache.ibatis.session.SqlSessionFactoryBuilder;7

8 import java.io.IOException;9 import java.io.InputStream;10

11 public classMyBatisUtil {12 private static InputStream is;13 private staticSqlSessionFactory sqlSessionFactory;14 static{15 try{16 is=Resources.getResourceAsStream("mybatis-config.xml");17 } catch(IOException e) {18 e.printStackTrace();19 }20 sqlSessionFactory= new SqlSessionFactoryBuilder().build(is);21 }22 privateMyBatisUtil(){}23 public staticSqlSession getSession(){24 returnsqlSessionFactory.openSession();25 }26 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值