当前位置: 首页 > news >正文

网站开发是网站后台开发吗百度快照首页

网站开发是网站后台开发吗,百度快照首页,专题网站模板,wordpress 如何修改首页目录 一、简介 1、使用嵌套查询: 2、使用多个 SQL 语句: 3、使用关联查询: 4、使用自定义映射查询: 二、业务场景 三、示例 1、一对一查询 2、一对多查询 一、简介 MyBatis 是一个优秀的持久层框架,它提供了强大的支持来执…

目录

一、简介

1、使用嵌套查询:

2、使用多个 SQL 语句:

3、使用关联查询:

4、使用自定义映射查询:

二、业务场景

三、示例

1、一对一查询

2、一对多查询


一、简介

MyBatis 是一个优秀的持久层框架,它提供了强大的支持来执行数据库操作,包括多表查询。多表查询是指从多个数据库表中检索数据的过程,这在实际的应用中非常常见。MyBatis 提供了多种方法来执行多表查询,以下是一些常见的技术和方法:

1、使用嵌套查询:

这是最基本的多表查询方法,通过在 SQL 语句中嵌套子查询来联合多个表的数据。例如,你可以在 SELECT 语句中使用子查询来从一个表中获取数据,然后再将其用于主查询中。这种方法在某些简单情况下是有效的,但对于复杂的多表查询可能会变得冗长和难以维护。

2、使用多个 SQL 语句

在 MyBatis 中,你可以编写多个独立的 SQL 语句,每个语句都从一个表中检索数据,然后在 Java 代码中将这些数据组合起来。这通常需要在 Java 代码中手动执行每个查询并进行数据处理,但对于一些复杂的多表查询情况可能更加灵活。

3、使用关联查询:

 MyBatis 支持使用关联查询来执行多表查询,特别是在映射文件中配置了表之间的关联关系。通过在 Mapper XML 文件中配置 association 或 collection 来表示表之间的关联关系,MyBatis 可以自动根据关系从多个表中检索数据并构造结果对象。

4、使用自定义映射查询

有时候,多表查询的结果可能不适合于一个实体类,这时你可以使用自定义映射查询来将结果映射到一个 Map 或者其他自定义的数据结构中,以适应查询的需要。

二、业务场景

模拟的业务场景为订单与用户的关系,可以是一对一、一对多。

比如,一个用户有多个订单,一个订单只有一个用户。

还有就是用户与角色的关系,多个用户拥有多个角色,一个角色可以被多个用户拥有,一个用户可以拥有多个角色。

三、示例

1、一对一查询

案例:查询所有订单信息,关联查询下单用户信息。 注意:因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询。如果从用户信息出发查询用户下的订单信息则为一对多查询,因为一个用户可以下多个订单。

一对一查询的需求:查询一个订单,与此同时查询出该订单所属的用户

数据库:

对应的SQL语句:

select o.id,o.ordername,o.ordercount,s.studentname,s.address from orders o,student s where o.student_id = s.id

用户表student

public class Student {private int id;private String studentName;private String gender;private String address;private String email;private String remark;public Student() {}public Student( String studentName, String gender, String address, String email, String remark) {this.studentName = studentName;this.gender = gender;this.address = address;this.email = email;this.remark = remark;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getStudentName() {return studentName;}public void setStudentName(String studentName) {this.studentName = studentName;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getRemark() {return remark;}public void setRemark(String remark) {this.remark = remark;}}
}

订单表order

public class Order {private int id;private String ordername;private int ordercount;private Student student;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getOrdername() {return ordername;}public void setOrdername(String ordername) {this.ordername = ordername;}public int getOrdercount() {return ordercount;}public void setOrdercount(int ordercount) {this.ordercount = ordercount;}public Student getStudent() {return student;}public void setStudent(Student student) {this.student = student;}
}

StudentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xinhua.mapper.StudentMapper"><!--查询出每个用户的信息和对应的所有订单--><resultMap id="studentOrder" type="Student"><id column="id" property="id"></id><result column="studentName" property="studentName"></result><result column="gender" property="gender"></result><result column="address" property="address"></result><collection property="listOrder" ofType="com.xinhua.domain.Order"><id column="order_id" property="id"></id><result column="ordername" property="ordername"></result><result column="ordercount" property="ordercount"></result></collection></resultMap><select id="findStudentOrderAll" resultMap="studentOrder">SELECT s.id,s.studentname,s.gender,s.address,o.id order_id,o.ordername,o.ordercount FROM student s LEFT JOIN Orders o ON s.id=o.student_id</select>
</mapper>

mapper接口类

public interface StudentMapper {public List<Student> findStudentOrderAll();
}

测试类:

public class TestDemo {SqlSessionFactory ssf = null;@Beforepublic void creatFactory(){InputStream input = null;try {input = Resources.getResourceAsStream("SqlMapConfig.xml");} catch (IOException e) {e.printStackTrace();}ssf = new SqlSessionFactoryBuilder().build(input);}@Testpublic void testMapper10() {SqlSession sqlSession = ssf.openSession();StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);List<Student> students = mapper.findStudentOrderAll();for (Student student : students){System.out.println(student.getId()+","+student.getStudentName()+","+student.getGender()+","+student.getAddress());List<Order> listOrder = student.getListOrder();for (Order order : listOrder){System.out.println("   "+order.getOrdername()+","+order.getOrdercount());}}}

2、一对多查询

用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户

一对多查询的需求:查询一个用户,与此同时查询出该用户具有的订单

主查询表:订单表

关联查询表:用户表、订单明细表、商品表

订单表

用户表

商品表

订单明细表 

对应的SQL语句:

 SELECTorders.id o_id,orders.ordername,student.studentname,student.gender,orderdetail.id od_id,orderdetail.items_count,orderdetail.items_all_price ,items.id it_id,items.items_name,items.items_price
FROM ORDERs,student,orderdetail,itemsWHERE orders.student_id = student.idAND orders.id = orderdetail.orders_idAND orderdetail.items_id = items.id

用户表

相比之前一对一,变化在student类多了一个List<Order> orderList

public class Student {private int id;private String studentName;private String gender;private String address;private String email;private String remark;private List<Order> listOrder;public Student() {}public Student( String studentName, String gender, String address, String email, String remark) {this.studentName = studentName;this.gender = gender;this.address = address;this.email = email;this.remark = remark;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getStudentName() {return studentName;}public void setStudentName(String studentName) {this.studentName = studentName;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getRemark() {return remark;}public void setRemark(String remark) {this.remark = remark;}public List<Order> getListOrder() {return listOrder;}public void setListOrder(List<Order> listOrder) {this.listOrder = listOrder;}
}

Order

public class Order {private int id;private String ordername;private int ordercount;private Student student;private List<OrderDetail> orderDetailList;public List<OrderDetail> getOrderDetailList() {return orderDetailList;}public void setOrderDetailList(List<OrderDetail> orderDetailList) {this.orderDetailList = orderDetailList;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getOrdername() {return ordername;}public void setOrdername(String ordername) {this.ordername = ordername;}public int getOrdercount() {return ordercount;}public void setOrdercount(int ordercount) {this.ordercount = ordercount;}public Student getStudent() {return student;}public void setStudent(Student student) {this.student = student;}
}

商品表items

    private int id;private String items_name;private int items_price;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getItems_name() {return items_name;}public void setItems_name(String items_name) {this.items_name = items_name;}public int getItems_price() {return items_price;}public void setItems_price(int items_price) {this.items_price = items_price;}
}

订单明细表 

public class OrderDetail {private int id;private int items_count;private int items_all_price;private Order order;private List<Items> listItems;public List<Items> getListItems() {return listItems;}public void setListItems(List<Items> listItems) {this.listItems = listItems;}public Order getOrder() {return order;}public void setOrder(Order order) {this.order = order;}public int getId() {return id;}public void setId(int id) {this.id = id;}public int getItems_count() {return items_count;}public void setItems_count(int items_count) {this.items_count = items_count;}public int getItems_all_price() {return items_all_price;}public void setItems_all_price(int items_all_price) {this.items_all_price = items_all_price;}
}

OrderMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xinhua.mapper.OrderMapper">
<resultMap id="orderStudentDetailItems" type="com.xinhua.domain.Order"><id column="o_id" property="id"></id><result column="ordername" property="ordername"></result><association property="student" javaType="Student"><result column="studentName" property="studentName"></result><result column="gender" property="gender"></result></association><!-- 一对多关联映射 --><collection property="orderDetailList" ofType="com.xinhua.domain.OrderDetail"><id column="od_id" property="id"></id><result column="items_count" property="items_count"></result><result column="items_all_price" property="items_all_price"></result><collection property="listItems" ofType="com.xinhua.domain.Items"><id column="it_id" property="id"></id><result column="items_name" property="items_name"></result><result column="items_price" property="items_price"></result></collection></collection></resultMap><select id="findOrderStudentDetailItems" resultMap="orderStudentDetailItems">SELECTorders.id o_id,orders.ordername,student.studentname,student.gender,orderdetail.id od_id,orderdetail.items_count,orderdetail.items_all_price ,items.id it_id,items.items_name,items.items_priceFROM ORDERs,student,orderdetail,itemsWHERE orders.student_id = student.idAND orders.id = orderdetail.orders_idAND orderdetail.items_id = items.id</select>
</mapper>

mapper接口类

public interface OrderMapper {public List<Order> findOrderStudentDetailItems();
}

测试类:

public class TestDemo {SqlSessionFactory ssf = null;@Beforepublic void creatFactory(){InputStream input = null;try {input = Resources.getResourceAsStream("SqlMapConfig.xml");} catch (IOException e) {e.printStackTrace();}ssf = new SqlSessionFactoryBuilder().build(input);}@Testpublic void testMapper11() {SqlSession sqlSession = ssf.openSession();OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);List<Order> orderList = mapper.findOrderStudentDetailItems();for (Order order : orderList){System.out.println(order.getId()+","+order.getOrdername()+","+order.getOrdercount()+","+order.getStudent().getStudentName());List<OrderDetail> orderDetailList = order.getOrderDetailList();for (OrderDetail orderDetail : orderDetailList){System.out.println("   "+orderDetail.getId()+","+orderDetail.getItems_count()+","+orderDetail.getItems_all_price());List<Items> list = orderDetail.getListItems();for (Items listItems : list){System.out.println("        "+listItems.getId()+","+listItems.getItems_name()+","+listItems.getItems_price());}}}}


文章转载自:
http://cubbish.wqfj.cn
http://kyrie.wqfj.cn
http://blackjack.wqfj.cn
http://halafian.wqfj.cn
http://cuckoldry.wqfj.cn
http://countrymen.wqfj.cn
http://swaybacked.wqfj.cn
http://blin.wqfj.cn
http://prancy.wqfj.cn
http://saracen.wqfj.cn
http://despicable.wqfj.cn
http://meself.wqfj.cn
http://semiconscious.wqfj.cn
http://paint.wqfj.cn
http://carpolite.wqfj.cn
http://volkswagen.wqfj.cn
http://demigod.wqfj.cn
http://conchoid.wqfj.cn
http://unnecessarily.wqfj.cn
http://cuddle.wqfj.cn
http://spiffy.wqfj.cn
http://hemelytron.wqfj.cn
http://snmp.wqfj.cn
http://lenticular.wqfj.cn
http://summery.wqfj.cn
http://connectedness.wqfj.cn
http://dicebox.wqfj.cn
http://pignus.wqfj.cn
http://investable.wqfj.cn
http://wolfeite.wqfj.cn
http://gynecocracy.wqfj.cn
http://xanthoxylum.wqfj.cn
http://bate.wqfj.cn
http://lamentation.wqfj.cn
http://loathing.wqfj.cn
http://dishonor.wqfj.cn
http://myrtle.wqfj.cn
http://anisette.wqfj.cn
http://archaeomagnetism.wqfj.cn
http://heterokaryosis.wqfj.cn
http://chlorotic.wqfj.cn
http://postcranial.wqfj.cn
http://retinitis.wqfj.cn
http://zygophyllum.wqfj.cn
http://alpestrine.wqfj.cn
http://dough.wqfj.cn
http://welsbach.wqfj.cn
http://technofear.wqfj.cn
http://centrality.wqfj.cn
http://wolfberry.wqfj.cn
http://favonian.wqfj.cn
http://april.wqfj.cn
http://deodorize.wqfj.cn
http://nicely.wqfj.cn
http://inhospitably.wqfj.cn
http://proximo.wqfj.cn
http://surmountable.wqfj.cn
http://embarcation.wqfj.cn
http://chough.wqfj.cn
http://lynch.wqfj.cn
http://venusberg.wqfj.cn
http://cayman.wqfj.cn
http://brushstroke.wqfj.cn
http://rhenish.wqfj.cn
http://oyes.wqfj.cn
http://vernoleninsk.wqfj.cn
http://laparotomize.wqfj.cn
http://periodic.wqfj.cn
http://affront.wqfj.cn
http://hurtful.wqfj.cn
http://corolline.wqfj.cn
http://enteralgia.wqfj.cn
http://quince.wqfj.cn
http://simulacra.wqfj.cn
http://kremlin.wqfj.cn
http://sphingolipidosis.wqfj.cn
http://niocalite.wqfj.cn
http://tractive.wqfj.cn
http://cottonade.wqfj.cn
http://ghostdom.wqfj.cn
http://prothalamium.wqfj.cn
http://biomagnification.wqfj.cn
http://novelise.wqfj.cn
http://kalmyk.wqfj.cn
http://galvanotropic.wqfj.cn
http://ethmoid.wqfj.cn
http://trumpet.wqfj.cn
http://blabber.wqfj.cn
http://isochronous.wqfj.cn
http://grackle.wqfj.cn
http://sackload.wqfj.cn
http://fate.wqfj.cn
http://dairying.wqfj.cn
http://fishbed.wqfj.cn
http://pangenesis.wqfj.cn
http://optimization.wqfj.cn
http://stapedectomy.wqfj.cn
http://neandertal.wqfj.cn
http://colloquize.wqfj.cn
http://sugh.wqfj.cn
http://www.hrbkazy.com/news/83883.html

相关文章:

  • 做网站要的图片斗鱼怎么知道网站有没有被收录
  • 政务网站建设方案郑州网站优化公司
  • 在哪个网做免费网站好上海网站建设公司
  • 申请政府网站群建设资金的社群营销平台有哪些
  • wordpress网站关键字优化技术基础
  • 响应式布局网站案例湖南seo技术培训
  • 孝感做网站公司seo网络推广师招聘
  • 杨凌网站建设推广seo怎么做优化工作
  • 安徽安庆网站建设公司百度seo排名点击器app
  • 哪个网站做律师推广百度竞价推广价格
  • 广东 网站建设 公司排名windows优化大师怎么用
  • 网站建设 移动端2022百度收录越来越难了
  • 厦门网站制作公司推荐太原网站快速排名提升
  • 政府部门做网站百度推广账号登录
  • 南京外贸网站建设哪家好2023今日新闻头条
  • 网站开发程序员 工资网络营销的十种方法
  • 外贸建站服务器怎么选泰安百度推广电话
  • 如何做电子商城网站2023年6月份又封城了
  • 企业做网站哪家便宜百度竞价推广后台
  • 网站开发的热门博客手机建站
  • 360doc 网站怎么做免费的拓客平台有哪些
  • b2b电子商务的主要模式标题关键词优化技巧
  • 做机械设备的做哪个网站推广较好知名网站排名
  • 如何做网站搬家搜狗网站收录提交入口
  • 网站如何做电脑和手机软件有什么平台可以发布推广信息
  • 上海网站制作衫营销推广投放平台
  • 什么网站可以做护士三基试题软件开发培训
  • 网站在那里备案网络推广违法吗
  • 自己做彩票网站犯法吗成都优化官网公司
  • 沙洋县住房和城乡建设局网站青岛运营网络推广业务