sagwsagw
WYCPhoenix
2018-01-08 09:11

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

5
  • oracle
  • mybatis
  • 数据库

最近学习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
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

9条回答