我是mybatis的新手,面临在结果集中获取集合的问题。结果集定义如下:
<resultMap type="User" id="userMap">
<id column="id" property="id" />
<result property="userName" column="user_name" />
<result property="enabled" column="enabled" />
<result property="firstName" column="first_name" />
<result property="lastName" column="last_name" />
<result property="email" column="email" />
<result property="password" column="user_password" />
<result property="passwordSalt" column="user_password_salt" />
<result property="lastLogin" column="last_login_time" />
<result property="creationTime" column="creation_time" />
<result property="lastEdit" column="last_edit_time" />
<result property="validTill" column="valid_till" />
<collection property="roles" javaType="ArrayList" ofType="Role" column="id">
<id property="id" column="role_id" />
<result property="name" column="role_name" />
<result property="description" column="role_description" />
</collection>
</resultMap>
Java代码看起来像这样
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
throws AuthenticationException
{
UsernamePasswordToken upToken = (UsernamePasswordToken)token;
User user = null;
user = new UserService().getWithRole(upToken.getUsername());
if (user == null) {
throw new UnknownAccountException("Login name [" + upToken.getUsername() + "] not found!");
}
if (!user.getEnabled().booleanValue()) {
throw new DisabledAccountException("Login name [" + upToken.getUsername() + "] is disabled!");
}
if (user.getRoles().size() == 0) {
throw new AuthenticationException("There is no role assigned for you");
}
if ((user.getValidTill() != null) && (user.getValidTill().getTime() < new Date().getTime())) {
throw new ExpiredCredentialsException("Your account has expired!");
}
_logger.debug("Found user with username [" + upToken.getUsername() + "]");
return new SimpleAuthenticationInfo(user, user.getPassword(), new SimpleByteSource(user.getPasswordSalt()), getName());
}
在这里,登录时我遇到了异常There is no role assigned to you
关于代码是正确的,它工作得很好但是我不能做任何改变,因为我只使用了我正在使用JD进行反编译的WAR。函数getWithRole()
触发以下查询
<select id="getWithRole" resultMap="userMap" parameterType="User">
SELECT * FROM view_boss_user_role
<trim prefix="WHERE" prefixOverrides="AND | OR ">
<if test="id != null">id = #{id}</if>
<if test="userName != null">AND user_name = #{userName}</if>
<if test="firstName != null">AND first_name = #{firstName}</if>
<if test="lastName != null">AND last_name = #{lastName}</if>
<if test="email != null">AND email = #{email}</if>
</trim>
</select>
并且视图的结构是这样的
CREATE OR REPLACE VIEW public.view_boss_user_role(
id,
user_name,
enabled,
first_name,
last_name,
email,
user_password,
user_password_salt,
last_login_time,
creation_time,
last_edit_time,
valid_till,
role_id,
role_name,
role_description)
AS
SELECT boss_user.id,
boss_user.user_name,
boss_user.enabled,
boss_user.first_name,
boss_user.last_name,
boss_user.email,
boss_user.user_password,
boss_user.user_password_salt,
boss_user.last_login_time,
boss_user.creation_time,
boss_user.last_edit_time,
boss_user.valid_till,
boss_role.id AS role_id,
boss_role.name AS role_name,
boss_role.description AS role_description
FROM boss_user,
boss_role,
boss_role_user_map
WHERE boss_role_user_map.user_id = boss_user.id AND
boss_role.id = boss_role_user_map.role_id;
示例输出如下所示
"id" "user_name" "enabled" "first_name" "last_name" "email" "user_password" "user_password_salt" "last_login_time" "creation_time" "last_edit_time" "valid_till" "role_id" "role_name" "role_description"
"1" "adminuser" "True" "Admin" "User" "adminuser@domain.com" "SEli9hsMTTxGgjrlCz48+5w1CaMnUe/8xJJ0enOZ2ag=" "mBPa0wNPjvnGK3gzDMOHOg==" "28-05-2017" "28-05-2017" "28-05-2017" "31-05-2018" "1" "ADMIN" "Administrator user"
我坚持这个错误,我很确定这是对mybatis的理解限制了我。请赐教我