WYCPhoenix 2018-01-08 09:11 采纳率: 0%
浏览 5206
已结题

Mybatis查询Oracle数据库结果为null,但数据库中存在该条记录

最近学习mybatis时遇到问题,相关配置应该是正确的,用mysql查询也能返回结果,但是将数据源切换到Oracle时总是返回null,求教。mybatis查询日志如下:

Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Created connection 174573182.
Returned connection 174573182 to pool.
Opening JDBC Connection
Checked out connection 174573182 from pool.
Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@a67c67e]
==>  Preparing: SELECT ID id, LAST_NAME lastName, GENDER gender, EMAIL email FROM mybatis_employee WHERE ID = ? 
==> Parameters: 1(Integer)
<==      Total: 0
null
Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@a67c67e]
Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@a67c67e]
Returned connection 174573182 to pool.

但是,将mybatis发送的SQL复制到数据中直接查询也能查询到该记录。mybatis能返回null,可以说明Oracle数据库连接应该是没问题的,应该是结果的包装出了问题,相关代码描述如下文,求赐教。
Oracle中mybatis_employee表定义如下:

ID        INTEGER                                 
LAST\_NAME VARCHAR2(255) Y                         
GENDER    INTEGER       Y                         
EMAIL     VARCHAR2(255) Y 

mybatis对应的查询配置如下:

   <select id="getEmployeeById" resultType="cn.seu.edu.Mybatis.bean.Employee" databaseId="oracle">
        SELECT ID id, LAST_NAME lastName, GENDER gender, EMAIL email FROM  mybatis_employee WHERE ID = #{id}
    </select>

测试代码如下:

 public void testEmployeeMapper() throws IOException{
        // 创建SqlSessionFactory对象
        InputStream inputStream = Resources.getResourceAsStream("conf/mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 创建session
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Assert.assertNotNull(sqlSession);
        try {
            // 获取接口的实现类对象
            // Mybatis会为接口实现一个代理对象,由代理对象执行sql
            EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
            // 调用接口的方法
            Employee employee = employeeMapper.getEmployeeById(1);
            System.out.println(employee);
        } finally {
            sqlSession.close();
        }
    }

Employee类定义如下

    private Integer id;
    private String lastName;
    private Integer gender;
    private String email;

        以下省略setter和getter
  • 写回答

10条回答

  • zshzsh1994 2018-01-08 09:29
    关注

    你的Mapper中的语句只有返回类型,应该需要加一个参数类型。parameterType属性

    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题