Three minutes to take you to understand mybatis association mapping (case analysis one-to-one, many-to-many)

------------------------------------------------ Originality is not easy , If you like it, please like it a little! ---------------------------------------------

Through the study of the previous articles, we have roughly understood the basic knowledge of mybatis, and can use basic SQL statements to operate the database. In relational databases, there are one-to-one, one-to-many, and many-to-many mapping relationships between tables. The mapping relationship is the core knowledge of mybatis. In the future work, we will often encounter the above several mapping relationships. Therefore, it is necessary for us to learn the mapping relationship well.

One, mybatis one-to-one mapping relationship

1. One-to-one mapping

  • What is a one-to-one mapping relationship? : From the perspective of the database is to introduce the primary key of another table as a foreign key in any table. Define an object of another class in the definition of this class.
  • In mybatis, we process through the association of the child elements of the resultMap element.
  • The association element has the following configuration attributes
Attribute name effect
property Specify the attribute of the entity class object that is mapped to, corresponding to the table field one-to-one
column Specify the corresponding field in the table
select Specify the sub-SQL statement that introduces the nested query. This attribute is used to associate the nested query in the mapping.
javaType Specify the type mapped to the attribute of the entity object
  • Two query methods are set in mybatis: nested query and nested result query
 <!--方式一:嵌套查询--!〉
        <association property="card" column="card_id" javaType="com.itheima.po.ldCard"
        select="com.itheima.mapper.ldCardMapper.findCodeByld" />

        <!--方式二:嵌套结果--!〉
        <association property="card" javaType="com.itheima.po.ldCard">
            <id property="id" column="card_id" />
            <result property="code" column="code" />
        </association>

2. [Case Analysis]

Case description: There is a one-to-one relationship between individuals and ID cards. Take the one-to-one relationship between personal information and personal ID cards as an example. Inquire about personal information and obtain corresponding ID card information through one-to-one mapping.
[Analysis]: According to the mybatis framework, we know that the mybatis relational mapping statements are written in Mapper.xml. In the mybatis relational mapping, we need to use the resuletMap element. The usage of this element is explained in detail in the resultMap mapping in the previous article It has been explained and can be consulted by yourself. StudentMapper.xml is written as follows

Mapping file code: StudentMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 3-5行是mybatis约束配置 -->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.mapper.StudentMapper">
	<!-- 嵌套查询 
	<select id="querystudent" parameterType="int" resultMap="IdS">
		select * from student where id=#{id}
	</select>
	<resultMap type="Student" id="IdS">
		<id property="id" column="id"/>
		<result property="s_name" column="s_name"/>
		<result property="s_age" column="s_age"/>
		<result property="s_sex" column="s_sex"/>
		<association property="card_id" column="card_id" javaType="Idcard"
			select="org.mybatis.mapper.IdcardMapper.queryinfo">
		</association>
	</resultMap>
	-->
	<!--  嵌套结果-->
	<select id="querystudent" resultMap="Ids"> 	<!-- select语句的返回值是resultMap类型-->
		<!-- sql查询语句, 当p.card_id = idcard.id时,获取student表中所有数据和idcard表中的code数据-->
		select p.*,idcard.code 
		from student p,idcard idcard
		where p.card_id = idcard.id
	</select>
		<!-- 一对一映射需要使用resultMap元素封装查询到的信息,id值需要与上文select语句中引用的resultMap="Ins"值相同--->
	<resultMap type="Student" id="Ids">
	<!--映射 Student类中属性与student表的字段一一对应-->
		<id property="id" column="id"/>
		<result property="s_name" column="s_name"/>
		<result property="s_age" column="s_age"/>
		<result property="s_sex" column="s_sex"/>
		<!--映射 Idcard类中属性与idcard表的字段一一对应,并将数据放入Student类的属性card_id中-->
		<association property="card_id" javaType="Idcard">
			<id property="id" column="card_id"/>
			<result property="code" column="code"/>
		</association>
	</resultMap>
</mapper>

Two, mybatis one-to-many mapping relationship

1. One-to-many mapping relationship

  • What is a one-to-many relationship: a one-to-many relationship is that one piece of data in table A corresponds to multiple pieces of data in table B, for example, the relationship between a user and an order, a user can have multiple order information.

  • In mybatis, we process through the collection of child elements of the resultMap element.

  • The collection element has the following configuration attributes

Attribute name effect
property Specify the attribute of the entity class object that is mapped to, corresponding to the table field one-to-one
column Specify the corresponding field in the table
select Specify the sub-SQL statement that introduces the nested query. This attribute is used to associate the nested query in the mapping.
javaType Specify the type mapped to the attribute of the entity object
ofType The ofType attribute corresponds to the javaType attribute, which is used to specify the element type contained in the collection class attribute in the entity object.
  • It should be noted that in one-to-many mapping and many-to-many mapping, the ofType attribute is used in the collection element property to specify the entity object instead of the JavaType attribute.

2. [Case Analysis]

Case description: Taking users and orders as an example, if a user has three orders, how to obtain the order information corresponding to the user by querying the user.
[Analysis]: A user can have multiple order information. This constitutes a one-to-many mapping relationship. Through the one-to-many mapping relationship in mybatis, the requirement can be completed after writing the corresponding statement in the mapper.xml file.

Mapping file code: 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">
<!--a.一对多嵌套结果  -->
<mapper namespace="org.mybatis.mapper.UserMapper">
	<select id="queryorders" parameterType="int" resultMap="Ins">        <!-- select语句的返回值是resultMap类型-->
		<!-- sql查询语句, 当u.id = o.user_id, u.id="传入值"时,获取user 表中id="传入值"的数据以及orders 表中的订单数据-->
		select u.*,o.id as orders_id,o.number
		from user u,orders o
		where u.id = o.user_id
		and u.id=#{id}
	</select>
	<!-- 一对多映射需要使用resultMap元素封装查询到的信息,且id值需要与上文select语句中引用的resultMap="Ins"值相同-->
	<resultMap type="org.mybatis.beans.User" id="Ins">
		<!--映射 User类中属性与user表的字段一一对应-->
		<id property="id" column="id"/>
		<result property="username" column="username"/>
		<result property="address" column="address"/>
		  <!--  a.1对多关联映射: collection  
			ofType 表示属性集合中元素的类型, List<Orders>属性即 Orders 类-->
		<collection property="orderList" ofType="org.mybatis.beans.Orders">
		<!--映射 Orders类中属性与orders表的字段一一对应-->
		<id property="id" column="orders_id"/>
		<result property="number" column="number"/>
		</collection>
	</resultMap>
</mapper>

Three, mybatis many-to-many mapping relationship

1. Many-to-many mapping relationship

  • What is a many-to-many relationship: a one-to-many relationship is that one piece of data in Table A corresponds to multiple pieces of data in Table B. For example, the relationship between a user and an order, a user can have multiple order information.
  • In mybatis, consistent with one-to-many mapping, we process through the collection of child elements of the resultMap element. There are no more collection element attributes consistent with the above.
    • It should be noted that in one-to-many mapping and many-to-many mapping, the ofType attribute is used in the collection element property to specify the entity object instead of the JavaType attribute.

2. [Case Analysis]

Case description: Take orders and products as an example. If an order has three products, and one product has five different orders, how to obtain the corresponding product information by querying the order, and how to obtain the corresponding order information by querying the product.
[Analysis]: A product can have multiple order information. An order can have multiple product information. This constitutes a many-to-many mapping relationship. Generally speaking, in many-to-many mapping, it is processed by generating an intermediate table , and an intermediate table is used to store order information and product information. But the resultMap element in Mapper.xml is basically the same as one-to-many.

Mapping file code: 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="org.mybatis.mapper.OrdersMapper">
<!-- 多对多嵌套结果 	-->
	<select id="queryproduct" parameterType="int" resultMap="Ins">
		select o.*,p.id as pid,p.name,p.price
		from orders o,product p,ordersitem oi
		where oi.orders_id=o.id
		and oi.product_id=p.id
		and o.id=#{id}
	</select>
	<resultMap type="Orders" id="Ins">
		<id property="id" column="id"/>
		<result property="number" column="number"/>
		<collection property="productList" ofType="Product">
			<id property="id" column="pid"/>
			<result property="name" column="name"/>
			<result property="price" column="price"/>
		</collection>
	</resultMap>

</mapper>

-------------------------------------------------- -------------------------------------------------public There are more dry goods in the number. Welcome to follow!

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44989801/article/details/115266954