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

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 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错
  • ¥15 webapi 发布到iis后无法访问
  • ¥15 初学者如何快速上手学习stm32?
  • ¥15 如何自动更换布娃娃图片上的衣服
  • ¥15 心理学eprime编程
  • ¥15 arduino esp8266开发