For example:
There are two entity classes User and Address
public class User {
private int id;
private String username; // 用户名
private List<Address> addresses;
// getter setter...
}
public class Address {
private int id;
private String detail; // 详细地址
private User user; //所属用户
// getter setter...
}
database:
create table t_user(
id int(10) primary key auto_increment,
username varchar(50)
);
create table t_address(
id int(10) primary key auto_increment,
detail varchar(255),
user_id int(10),
CONSTRAINT FOREIGN KEY (user_id) REFERENCES t_user(id)
);
mybatis mapping configuration:
<?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.mkh.shop.model.User">
<resultMap type="User" id="userMap" autoMapping="true">
<id property="id" column="u_id"/>
<collection property="address" ofType="Address">
<id property="id" column="a_id"/>
<result property="detail" column="detail"/>
</collection>
</resultMap>
<select id="find" resultType="User" parameterType="map">
select *,
ta.id as 'a_id',
tu.id as 'u_id'
from t_user tu
left join t_address ta on ta.user_id=tu.id
<where>
<if test="name != null">
(username like #{name})
</if>
</where>
<if test="sort != null">
order by ${sort}
<choose>
<when test="order != null">${order}</when>
<otherwise>asc</otherwise>
</choose>
</if>
limit #{pageOffset},#{pageSize}
</select>
<select id="find_count" resultType="int" parameterType="map">
select count(*)
from t_user tu
left join t_address ta on ta.user_id=tu.id
<where>
<if test="name != null">
(username like #{name})
</if>
</where>
</select>
</mapper>
The relationship between users and addresses is: one user has multiple addresses, one address can only belong to one user, one-to-many
Assume that the current demand is to query users in pages, display them in a table, and put each user All addresses are displayed
Then the problem comes
There is no problem with the paging data returned according to the above query, but the total number of paging records is wrong.
For example, the data found (database data, not page display) is as follows:
u_id | username | a_id | detail |
---|---|---|---|
1 | user1 | 1 | Haiding district, Beijing |
1 | user1 | 2 | Chaoyang District, Beijing |
2 | user2 | 3 | Tianjin City |
Because my requirement is to display users in pages, so a user is a piece of data displayed on the page, which looks like this. In theory, it is two pieces of data,
User id | username | address |
---|---|---|
1 | user1 | 1. Haidian District, Beijing 2. Chaoyang District, Beijing |
2 | user2 | 1. Tianjin City |
A total of 1 page, a total of 2 pieces of data, each page displays 10 pieces |
However, according to the find_count configuration of mybatis, it is found that there are 3 items. How to solve this problem?
When querying count(*), should all tables associated with left join be removed? Will this cause the returned data to be inaccurate
Additional explanation: I feel that everyone has misunderstood what I meant. In fact, my problem is mainly on SQL, not mybatis, because after the data I queried was mapped, there was no problem at all. There is just a problem with the total number of paging records, resulting in incorrect paging
刚才写了个例子测了一下
`![](http://img.php.cn/upload/image/000/000/001/597cae0663a5cc3c67cedd746c3fdad5-0.png)
两个实体类
public class User {
public class Address {
映射文件
测试结果![](http://img.php.cn/upload/image/000/000/001/f8701f7c2c3927fb9e501f9f84283232-1.png)
封装成的 List的size是没问题的
关键词group by 自己查一下具体操作
你希望想要两条 但是你的sql查出三条,所以显示错误,
应该拆分逻辑:
应该先查出你想要的用户
然后在
??是自己实现个 用userId查List
的方法这种情况不能这么分页,你需要对主表数据进行分页。
本来查询出100条数据,由于一对多会折叠去重很多数据,导致实际结果少于100条。
这种情况下可以采取嵌套查询方式解决,就是需要N+1次执行,可以懒加载。
或者看这里:https://my.oschina.net/flags/...
有关 MyBatis 内容可以访问:http://mybatis.tk
改:
用子查询就不会有问题了
楼主可以看一下
Mybatis-PageHelper
count sql 转换实现建议楼主直接使用 Mybatis-PageHelper 实现分页