1

我正在编写一个简单的 CRUD 应用程序(我在 oracle 数据库上使用 JSF 2.0 和 mybatis3),到目前为止,在显示我查询的数据(在 a 中)时遇到错误。我知道这真的很混乱,但我想对我做错的事情提出一些建议。谢谢 !

编辑 1:我还制作了一个普通的 java 项目并测试了这些函数,看看它们是否返回任何东西并且它工作正常......

编辑 2:在此处登录, http ://pastebin.com/gYyqBX7W没有错误

这是 UserMapper.xml 的一部分:

<resultMap id="result" type="User">
    <result property="username" column="USER_ID" />
    <result property="userDescription" column="USER_DESC" />
    <result property="password" column="USER_PWD" />
</resultMap>

<select id="getAllUsers" resultMap="result"> 
SELECT USER_ID as username, USER_DESC as userDescription,
    USER_PWD as password FROM SSLS_GUI.USERS
</select>

UserMapper.java 文件:

package com.mybatis.mappers;
import java.util.List;
import com.optsol.beans.User;

public interface UserMapper {

public List<User> getAllUsers();

}

UserService.java 中让我感兴趣的部分:

@ManagedBean(name = "userServices")
public class UserService {
public List<User> getAllUsers() {
    SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory()
            .openSession();
    try {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        return userMapper.getAllUsers();
    } finally {
        sqlSession.close();
    }
}

我从中获取工厂实例的 MyBatisUtil.java 文件:

package com.mybatis.service;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtil {
private static SqlSessionFactory factory;

private MyBatisUtil() {
}

    static {
    String resource = "com/mybatis/config/mybatis-config.xml";
    Reader reader = null;
    try {

        reader = Resources.getResourceAsReader(resource);
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage());
    }
    factory = new SqlSessionFactoryBuilder().build(reader);
}

    public static SqlSessionFactory getSqlSessionFactory() {
    return factory;
}
}

User.java(没有 getter 和 setter):

package com.optsol.beans;

import javax.faces.bean.ManagedBean;
@ManagedBean(name = "user")
public class User {

    private String username;
    private String userDescription;
    private String password;
}

我尝试显示来自数据库的数据的 index.xhtml:

<h:dataTable value="#{userService.getAllUsers}" var="u">
    <h:column>
        <f:facet name="header">
                USERNAME
            </f:facet>
                #{u.username}
        </h:column>

    <h:column>
        <f:facet name="header">
                USER DESCRIPTION
            </f:facet>
                #{u.userDescription}
        </h:column>
                <h:column>
        <f:facet name="header">
                PASSWORD(HASHED)
            </f:facet>
                #{u.password}
        </h:column>
</h:dataTable>
4

1 回答 1

0

属性的名称与列的名称不匹配。

<resultMap id="result" type="User">
    <result property="username" column="USER_ID" />
    <result property="userDescription" column="USER_DESC" />
    <result property="password" column="USER_PWD" />
</resultMap>

删除 SQL 中的别名。

<select id="getAllUsers" resultMap="result"> 
    SELECT USER_ID, 
           USER_DESC,
           USER_PWD
      FROM SSLS_GUI.USERS
</select>
于 2013-08-13T14:46:58.853 回答