1.org.apache.ibatis.executor.ExecutorException: No constructor found in:
private List<Role> roles;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
", accounts=" + accounts +
", roles=" + roles +
'}';
}
public User(Integer id, String username, Date birthday, String sex, String address, List<Account> accounts, List<Role> roles) {
this.id = id;
this.username = username;
this.birthday = birthday;
this.sex = sex;
this.address = address;
this.accounts = accounts;
this.roles = roles;
}
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
账户实体类也一样。
最近在进行多对多关系的建立过程中,发现了该问题,仔细斟酌,两个实体类中都要有对方类的申明和set/get函数,构造函数有全参构造的同时也要有无参构造,我少了无参构造,报错。
2.进行左查询的过程中发现只能查出一个用户的一个账户
在用户实体类中加了账户的list引用。
后来发现是在集合引用的id和表中id一致,所以在集合中查询到的后面的数据会覆盖前面的,因此在SQL语句中对账户的id起别名aid,就可以进行一对多查询了。
```bash`<resultMap id="listUserAndAccount" type="com.ly.entity.User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="birthday" column="birthday"/>
<result property="sex" column="sex"/>
<result property="address" column="address"/>
<!-- 配置user对象中accounts集合的映射 -->
<collection property="accounts" ofType="com.ly.entity.Account">
<id property="id" column="aid"/>
<result property="uid" column="uid"/>
<result property="money" column="money"/>
</collection>
</resultMap>
<!-- 里面的a.id重命名为aid,不然左查询只能查出来一条右边数据库的语句,因为两个表中id重复,会引起后面的数据覆盖前面的数据的现象。 -->
<select id="listUser" resultMap="listUserAndAccount">
SELECT u.*,a.id AS aid,a.uid,a.money FROM user u LEFT OUTER JOIN account a ON u.id = a.uid
</select>`
## 错误: org.apache.ibatis.builder.IncompleteElementException: Could not find result map com.ly.dao.UserDao.User
这里我在accountmapper.xml里面对账户的用户进行延迟查询:
```java
<resultMap id="AccountWithUserMap" type="Account">
<id property="id" column="aid"/>
<result property="uid" column="uid"/>
<result property="money" column="money"/>
<!--从 column 属性指定的列中检索数据,作为参数传递给目标 select 语句-->
<association property="user" javaType="User" column="uid" select="com.ly.dao.UserDao.getUserById"/>
</resultMap>
<select id="gelAllAccounts" resultMap="AccountWithUserMap">
<include refid="default"></include>
</select>
其中association里select调用另一个mapper.xml文件里面的select标签进行查询,column就是传入的值,
<select id="getUserById" parameterType="java.lang.Integer" resultMap="User">
SELECT * FROM user WHERE id = #{uid}
</select>
这里应该是resultType返回User对象属性,resultMap里面写的是我们在mapper文件中自定义的标签id,所以报错。