MyBatis: related query

One-to-one query

Method one, define the packaging class pojo as the output type

After the OrdersCustom class inherits the Orders class, the OrdersCustom class includes all the fields of the Orders class. You only need to define the User class fields.

public class OrdersCustom extends Orders {

	private String username; //用户名称
public class Orders {

	private Integer oid;
	private String name;
	private Integer userId;
<select id="findOrdersList" resultType="org.haiwen.pojo.OrdersCustom">
    SELECT
    	`user`.username,
    	orders.*
    FROM
    	`user`,
    	orders
    WHERE
    	`user`.uid = orders.user_id
</select>
public List<OrdersCustom> findOrdersList() throws Exception;
@Test
public void testFindOrdersList() throws Exception {
	SqlSession sqlSession = sqlSessionFactory.openSession();
	UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
	List<OrdersCustom> list = userMapper.findOrdersList();
	System.out.println(list);
	sqlSession.close();
}

Method two, use association for association query

public class Orders {

	private Integer oid;
	private String name;
	private Integer userId;
	private User user;
<select id="findOrdersListResultMap" resultMap="userordermap">
    SELECT
    	`user`.username,
    	orders.*
    FROM
    	`user`,
    	orders
    WHERE
    	`user`.uid = orders.user_id
</select>

<resultMap type="org.haiwen.pojo.Orders" id="userordermap">
	<!-- 这里的id,是mybatis在进行一对一查询时将order字段映射为order对象时要使用,必须写 -->
	<id property="oid" column="oid" />
	<result property="name" column="name" />
	<result property="userId" column="user_id" />
	<association property="user" javaType="org.haiwen.entity.User">
		<id property="uid" column="uid" />
		<result property="username" column="username" />
	</association>
</resultMap>

association: indicates a single record for association query

property: indicates that the result of the associated query is stored in the user property of org.haiwen.pojo.Orders

javaType: indicates the result type of the associated query

<id property = "id" column = "id" />: the id column of the query result corresponds to the id attribute of the associated object, where id is the unique identifier of the associated query object.

<result property = "username" column = "username" />: The username column of the query result corresponds to the username attribute of the associated object.

public List<Orders> findOrdersListResultMap() throws Exception;
@Test
public void testFindOrdersListResultMap() throws Exception {
	SqlSession sqlSession = sqlSessionFactory.openSession();
	UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
	List<Orders> list = userMapper.findOrdersListResultMap();
	System.out.println(list);
	sqlSession.close();
}

One-to-many query

Method one, use collection to perform association query

public class Orders {

	private Integer oid;
	private String name;
	private Integer userId;
	private User user;
	private List<OrdersDetail> ordersDetail;
public class OrdersDetail {

	private Integer odid;
	private Integer num;
	private Integer ordersId;
<select id="findOrdersDetailList" resultMap="userordersdetailmap">
	SELECT
		*
	FROM
		`user`,
		orders,
		ordersdetail
	WHERE
		`user`.uid = orders.user_id
	AND orders.oid = ordersdetail.orders_id
</select>

<resultMap type="org.haiwen.pojo.Orders" id="userordersdetailmap">
	<id property="oid" column="oid" />
	<result property="name" column="name" />
	<result property="userId" column="user_id" />
	<association property="user" javaType="org.haiwen.entity.User">
		<id property="uid" column="uid" />
		<result property="username" column="username" />
	</association>
	<collection property="ordersDetail" ofType="org.haiwen.pojo.OrdersDetail">
		<id property="odid" column="odid" />
		<result property="num" column="num" />
		<result property="ordersId" column="orders_id" />
	</collection>
</resultMap>

collection: indicates the related query result set

property = "ordersDetail": The result set of the associated query is stored in the ordersDetail property of org.haiwen.pojo.Orders

ofType = "org.haiwen.pojo.OrdersDetail": Specifies the object type in the result set of the associated query, that is, the object type in the List.

public List<Orders> findOrdersDetailList() throws Exception;
@Test
public void testFindOrdersDetailList() throws Exception {
	SqlSession session = sqlSessionFactory.openSession();
	UserMapper userMapper = session.getMapper(UserMapper.class);
	List<Orders> list = userMapper.findOrdersDetailList();
	for (Orders orders : list) {
		System.out.println(list);
	}
	session.close();
}

Method two, use extends to inherit order information userordermap

The resultMap defined above is the same as the resultMap for one-to-one query of order information except for the collection tag. Here, inheritance can be used to avoid filling in duplicate content, as follows:

<resultMap type="org.haiwen.pojo.Orders" id="userordersdetailmap" extends="userordermap">
	<collection property="ordersDetail" ofType="org.haiwen.pojo.OrdersDetail">
		<id property="odid" column="odid" />
		<result property="num" column="num" />
		<result property="ordersId" column="orders_id" />
	</collection>
</resultMap>

Many-to-many query

public class User {

	private Integer uid;
	private String username;
	private List<Orders> orders;
public class Orders {

	private Integer oid;
	private String name;
	private List<OrdersDetail> ordersDetail;
public class OrdersDetail {

	private Integer odid;
	private Integer num;
	private Items items;
public class Items {

	private Integer iid;
	private String itemsName;
	private String itemsNum;
	private Integer ordersdetailId;

The information that needs to be correlated with the query map is: order, order details, product information

Orders: One user corresponds to multiple orders, and the collection is mapped to the order list attribute of the user object

Order details: An order corresponds to multiple details, and is mapped to the detail attribute in the order object using collection

Commodity information: an order detail corresponds to a commodity, and association is mapped to the commodity attribute of the order detail object

<select id="findAllItems" resultMap="userOrderListResultMap">
	SELECT
		`user`.username,
		orders.`name`,
		ordersdetail.num,
		items.items_name,
		items.items_num
	FROM
		`user`,
		orders,
		ordersdetail,
		items
	WHERE
		`user`.uid = orders.user_id
	AND orders.oid = ordersdetail.orders_id
	AND ordersdetail.odid = items.ordersdetail_id
</select>

<resultMap type="org.haiwen.entity.User" id="userOrderListResultMap">
<id property="uid" column="uid" />
<result property="username" column="username" />
<collection property="orders" ofType="org.haiwen.pojo.Orders">
	<id property="oid" column="oid" />
	<result property="name" column="name" />
	<collection property="ordersDetail" ofType="org.haiwen.pojo.OrdersDetail">
		<id property="odid" column="odid" />
		<result property="num" column="num" />
		<association property="items" javaType="org.haiwen.pojo.Items">
			<id property="iid" column="iid" />
			<result property="itemsName" column="items_name" />
			<result property="itemsNum" column="items_num" />
		</association>
	</collection>
</collection>
</resultMap>
public List<User> findAllItems() throws Exception;
@Test
public void testFindAllItems() throws Exception {
	SqlSession session = sqlSessionFactory.openSession();
	UserMapper userMapper = session.getMapper(UserMapper.class);
	List<User> list = userMapper.findAllItems();
	for (User user : list) {
		System.out.println(list);
	}
	session.close();
}

summary

resultType:

Function: Map query results to pojo according to the consistency of sql column name pojo attribute name.

Occasions: Common display of some detailed records, such as user purchase product details, when all related query information is displayed on the page, you can directly use the resultType to map each record to pojo, and traverse the list on the front page (list is pojo ).

resultMap:

When there are special mapping requirements for the results, use association and collection to complete one-to-one and one-to-many advanced mapping.

association:

Role: Map related query information to a pojo object.

Occasion: For the convenience of querying related information, you can use association to map related order information to the pojo attribute of the user object, for example: query order and related user information.

The result type cannot be used to map the query result to the pojo attribute of the pojo object. According to the need to traverse the result set query, choose whether to use resultType or resultMap.

collection:

Function: Map related query information to a list collection.

Occasion: In order to facilitate the query and traversal of related information, you can use collection to map related information to the list collection, for example: query the user permission range module and the menu under the module, you can use collection to map the module to the module list, and the menu list to the module In the menu list property of the object, the purpose of this operation is also to facilitate the traversal query of the query result set.

If you use resultType, you cannot map the query results to the list collection.

Published 202 original articles · praised 37 · 30,000+ views

Guess you like

Origin blog.csdn.net/lovecuidong/article/details/102496058