I have a problem about 'collection'. I hope get a list(size=0), or not null.

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

I have a problem about 'collection'. I hope get a list(size=0), or not null.

龚波
Model

public class User implements UserDetails {
private int id;
private String username;
private String password;
private String email;
private List<Purview> paths;

public int getId() { return id; }
public void setId(int id) { this.id = id; }
public List<Purview> getPaths() { return paths; }
public void setPaths(List<Purview> paths) { this.paths = paths; }
public String getUsername() { return username; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
}

public class Purview {
private int id;
private int user_id;
private String path;

public int getId() { return id; }
public void setId(int id) { this.id = id; }
public int getUser_id() { return user_id; }
public void setUser_id(int user_id) { this.user_id = user_id; }
public String getPath() { return path; }
public void setPath(String path) { this.path = path; }
}


UserMapper.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="com.qdtg.gitpurview.dao.UserDao">
<resultMap id="user_map" type="User">
<collection property="paths" ofType="Purview">
<id property="id" column="p_id"/>
<result property="user_id" column="user_id"/>
<result property="path" column="path"/>
</collection>
</resultMap>

<select id="selectUserByEmail" resultMap="user_map">
select
u.id as id,
u.email as email,
u.password as password,
u.username as username,
p.id as p_id,
p.user_id as user_id,
p.path as path
from purview.user as u
left outer join purview.purview as p on p.user_id = u.id
where u.email = #{email}
</select>
</mapper>

UserDao

public interface UserDao {
public User selectUserByEmail(String email);
}

Tables(USER)

USER.jpg


Tables(PURVIEW)

pruview.jpg


Run

   User user = userDao.selectUserByEmail("[hidden email]");

  The user is null, but I want to get the user({"email":"[hidden email]","id":1,"password":"admin","username":"admin","paths":[]}).






--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

purview.sql (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: I have a problem about 'collection'. I hope get a list(size=0), or not null.

Guy Rouillier-2
I ran your sample, using MariaDB 10.3.  The issue is in your UserMapper.xml file.  Update your resultMap to this:

 <resultMap id="user_map" type="org.sample.beans.User" autoMapping="true">

MyBatis User's Guide says this about autoMapping:

"If present, MyBatis will enable or disable the automapping for this ResultMap. This attribute overrides the global autoMapping Behavior. Default: unset."

The problem occurred because with autoMapping disabled, the User columns were not getting set.  So when List<Purview> was also null, the entire object was all null fields, so MyBatis discarded the result.

--
Guy Rouillier

------ Original Message ------
From: "龚波" <[hidden email]>
To: "mybatis-user" <[hidden email]>
Sent: 9/21/2018 5:13:06 AM
Subject: I have a problem about 'collection'. I hope get a list(size=0), or not null.

Model

public class User implements UserDetails {
private int id;
private String username;
private String password;
private String email;
private List<Purview> paths;

public int getId() { return id; }
public void setId(int id) { this.id = id; }
public List<Purview> getPaths() { return paths; }
public void setPaths(List<Purview> paths) { this.paths = paths; }
public String getUsername() { return username; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
}

public class Purview {
private int id;
private int user_id;
private String path;

public int getId() { return id; }
public void setId(int id) { this.id = id; }
public int getUser_id() { return user_id; }
public void setUser_id(int user_id) { this.user_id = user_id; }
public String getPath() { return path; }
public void setPath(String path) { this.path = path; }
}


UserMapper.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="com.qdtg.gitpurview.dao.UserDao">
<resultMap id="user_map" type="User">
<collection property="paths" ofType="Purview">
<id property="id" column="p_id"/>
<result property="user_id" column="user_id"/>
<result property="path" column="path"/>
</collection>
</resultMap>

<select id="selectUserByEmail" resultMap="user_map">
select
u.id as id,
u.email as email,
u.password as password,
u.username as username,
p.id as p_id,
p.user_id as user_id,
p.path as path
from purview.user as u
left outer join purview.purview as p on p.user_id = u.id
where u.email = #{email}
</select>
</mapper>

UserDao

public interface UserDao {
public User selectUserByEmail(String email);
}

Tables(USER)

USER.jpg


Tables(PURVIEW)

pruview.jpg


Run

   User user = userDao.selectUserByEmail("[hidden email]");

  The user is null, but I want to get the user({"email":"[hidden email]","id":1,"password":"admin","username":"admin","paths":[]}).






--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.