title: MyBatis 3 + Spring 3: select returns null values
body: <p>I builded a project based on the MyBatis examples (I also examined the <a href="http://www.mybatis.org/spring/sample.html" rel="nofollow">JPetStore 6 application</a>).</p>
  
  <p>The problem is that my getTrains() method always returns null objects.
  Here is the source files and the configuration ones:</p>
  
  <p>TrainMapper.java</p>
  
  <pre><code>package org.example.mybatis.spring.persistence;
  
  import java.util.List;
  import org.example.mybatis.spring.domain.Train;
  
  public interface TrainMapper {
  
      Train getTrain(int id);
      List&lt;Train&gt; getTrains();
      void insertTrain(Train train);
  }
  </code></pre>
  
  <p>TrainService.java</p>
  
  <pre><code>package org.example.mybatis.spring.service;
  
  import java.util.List;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  import org.example.mybatis.spring.domain.Train;
  import org.example.mybatis.spring.persistence.TrainMapper;
  
  @Service("trainService")
  
  public class TrainService {
  
  @Autowired
  private TrainMapper trainMapper;
  
  public void setTrainMapper(TrainMapper trainMapper) {
      this.trainMapper = trainMapper;
  }
  
  public Train getTrain(int id) {
      return this.trainMapper.getTrain(id);
  }
  
  public List&lt;Train&gt; getTrains() {
      return this.trainMapper.getTrains();
  }
  
  @Transactional
  public void insertTrain(Train train) {
      this.trainMapper.insertTrain(train);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>TrainMapper.xml</p>
  
  <pre><code>   &lt;?xml version="1.0" encoding="UTF-8" ?&gt;
      &lt;!DOCTYPE mapper
          PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
          "http://mybatis.org/dtd/mybatis-3-mapper.dtd"&gt;
  
      &lt;mapper namespace="org.example.mybatis.spring.persistence.TrainMapper"&gt;
        &lt;cache /&gt;
        &lt;select id="getTrain" resultType="Train" parameterType="int"&gt;
          SELECT
            train_type
          FROM trains
          WHERE trains.train_id = #{id}
        &lt;/select&gt;
        &lt;select id="getTrains" resultType="org.example.mybatis.spring.domain.Train" parameterType="int"&gt;
          SELECT
            train_type
          FROM trains
        &lt;/select&gt;
        &lt;insert id="insertTrain" parameterType="Train"&gt;
          INSERT INTO trains (train_type)
          VALUES (#{type})
        &lt;/insert&gt;
      &lt;/mapper&gt;
  </code></pre>
  
  <p>applicationContext.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:jdbc="http://www.springframework.org/schema/jdbc"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
           http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"&gt;
  
    &lt;context:property-placeholder location="classpath:mysql.jdbc.properties" /&gt;
  
    &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
      &lt;property name="driverClassName" value="${jdbc.driverClassName}" /&gt;
      &lt;property name="url" value="${jdbc.url}" /&gt;
      &lt;property name="username" value="${jdbc.username}" /&gt;
      &lt;property name="password" value="${jdbc.password}" /&gt;
    &lt;/bean&gt;
  
    &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
    &lt;/bean&gt;
  
    &lt;context:component-scan base-package="org.example.mybatis.spring" /&gt;
  
    &lt;tx:annotation-driven /&gt;
  
    &lt;bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="typeAliasesPackage" value="org.example.mybatis.spring.domain" /&gt;
      &lt;!--property name="configLocation" value="classpath:mybatis-config.xml"/--&gt;
      &lt;!--property name="mapperLocations" value="classpath:org/example/mybatis/spring/persistence/*.xml" /--&gt;
    &lt;/bean&gt;
  
    &lt;bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"&gt;
      &lt;property name="basePackage" value="org.example.mybatis.spring.persistence" /&gt;
    &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>Train.java is a simple Java bean.</p>
  
  <p>The project POM:</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;org.example&lt;/groupId&gt;
    &lt;artifactId&gt;mybatis-spring-example&lt;/artifactId&gt;
    &lt;name&gt;MyBatis-Spring Example&lt;/name&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;
    &lt;description&gt;Example of the MyBatis-Spring integration (within a Maven project)&lt;/description&gt;
  
    &lt;properties&gt;
      &lt;maven.compiler.plugin.version&gt;2.5.1&lt;/maven.compiler.plugin.version&gt;
      &lt;log4j.version&gt;1.2.17&lt;/log4j.version&gt;
      &lt;junit.version&gt;4.10&lt;/junit.version&gt;
      &lt;mysql.connector.version&gt;5.1.20&lt;/mysql.connector.version&gt;
      &lt;spring.version&gt;3.1.2.RELEASE&lt;/spring.version&gt;
      &lt;mybatis.version&gt;3.1.1&lt;/mybatis.version&gt;
      &lt;mybatis.spring.version&gt;1.1.1&lt;/mybatis.spring.version&gt;
      &lt;hsqldb.version&gt;2.2.8&lt;/hsqldb.version&gt;
      &lt;dbcp.version&gt;1.4&lt;/dbcp.version&gt;
      &lt;cglib.version&gt;2.2.2&lt;/cglib.version&gt;
      &lt;nexus.ip&gt;x.y.w.z&lt;/nexus.ip&gt;
      &lt;nexus.port&gt;xxxx&lt;/nexus.port&gt;
      &lt;nexus.public.group.url&gt;http://${nexus.ip}:${nexus.port}/nexus/content/groups/public&lt;/nexus.public.group.url&gt;
      &lt;nexus.releases.url&gt;http://${nexus.ip}:${nexus.port}/nexus/content/repositories/releases&lt;/nexus.releases.url&gt;
      &lt;nexus.snapshots.url&gt;http://${nexus.ip}:${nexus.port}/nexus/content/repositories/snapshots&lt;/nexus.snapshots.url&gt;
    &lt;/properties&gt;
  
    &lt;repositories&gt;
      &lt;repository&gt;
        &lt;id&gt;nexus-internal-repo&lt;/id&gt;
        &lt;name&gt;Sonatype Nexus Internal Repository&lt;/name&gt;
        &lt;url&gt;${nexus.public.group.url}&lt;/url&gt;
        &lt;releases&gt;
          &lt;enabled&gt;true&lt;/enabled&gt;
          &lt;updatePolicy&gt;never&lt;/updatePolicy&gt;
          &lt;checksumPolicy&gt;fail&lt;/checksumPolicy&gt;
        &lt;/releases&gt;
        &lt;snapshots&gt;
          &lt;enabled&gt;true&lt;/enabled&gt;
          &lt;updatePolicy&gt;never&lt;/updatePolicy&gt;
          &lt;checksumPolicy&gt;warn&lt;/checksumPolicy&gt;
        &lt;/snapshots&gt;
      &lt;/repository&gt;
    &lt;/repositories&gt;
  
    &lt;dependencies&gt;
  &lt;dependency&gt;
    &lt;groupId&gt;org.mybatis&lt;/groupId&gt;
    &lt;artifactId&gt;mybatis&lt;/artifactId&gt;
    &lt;version&gt;${mybatis.version}&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
    &lt;groupId&gt;org.mybatis&lt;/groupId&gt;
    &lt;artifactId&gt;mybatis-spring&lt;/artifactId&gt;
    &lt;version&gt;${mybatis.spring.version}&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
    &lt;groupId&gt;log4j&lt;/groupId&gt;
    &lt;artifactId&gt;log4j&lt;/artifactId&gt;
    &lt;version&gt;${log4j.version}&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
    &lt;groupId&gt;mysql&lt;/groupId&gt;
    &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
    &lt;version&gt;${mysql.connector.version}&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
    &lt;groupId&gt;commons-dbcp&lt;/groupId&gt;
    &lt;artifactId&gt;commons-dbcp&lt;/artifactId&gt;
    &lt;version&gt;${dbcp.version}&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
    &lt;groupId&gt;cglib&lt;/groupId&gt;
    &lt;artifactId&gt;cglib&lt;/artifactId&gt;
    &lt;version&gt;${cglib.version}&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>I run the application as standalone:</p>
  
  <pre><code>package org.example.mybatis.spring;
  
  import java.util.List;
  import org.example.mybatis.spring.domain.Train;
  import org.example.mybatis.spring.service.TrainService;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  
      public class SelectTrainExample {
  
          public static void main(String[] args) {
              ApplicationContext context = new ClassPathXmlApplicationContext(
                      "applicationContext.xml");
              TrainService trainBean = (TrainService) context.getBean("trainService");
  
              List&lt;Train&gt; trains = trainBean.getTrains();
              System.out.println(trains.toString());
  
              Train train = trainBean.getTrain(1);
              System.out.println(train.toString());
  
              Train t = new Train();
              t.setType("Interregionale");
              trainBean.insertTrain(t);
          }
      }
  </code></pre>
  
  <p>The insert in the database work fine, while I'm not be able to retrieve data from the "trains" table.
  Here is the log output:</p>
  
  <blockquote>
    <p>2012-10-01 15:25:03,400 DEBUG [org.mybatis.spring.SqlSessionUtils] -
     2012-10-01 15:25:03,413 DEBUG
    [org.mybatis.spring.SqlSessionUtils] - 
    2012-10-01 15:25:03,463 DEBUG
    [org.apache.ibatis.cache.decorators.LoggingCache] -  2012-10-01
    15:25:03,473 DEBUG
    [org.springframework.jdbc.datasource.DataSourceUtils] -  2012-10-01 15:25:03,474 DEBUG
    [org.mybatis.spring.transaction.SpringManagedTransaction] -  2012-10-01 15:25:03,479 DEBUG
    [org.example.mybatis.spring.persistence.TrainMapper.getTrains] -  2012-10-01
    15:25:03,497 DEBUG
    [org.example.mybatis.spring.persistence.TrainMapper.getTrains] - &lt;==> 
    Preparing: SELECT train_type FROM trains > 2012-10-01 15:25:03,578
    DEBUG [org.example.mybatis.spring.persistence.TrainMapper.getTrains] -
    &lt;==> Parameters: > 2012-10-01 15:25:03,660 DEBUG
    [org.mybatis.spring.SqlSessionUtils] - 
    2012-10-01 15:25:03,661 DEBUG
    [org.springframework.jdbc.datasource.DataSourceUtils] -  [null, null, null, null, null]
    2012-10-01 15:25:03,664 DEBUG
    [org.springframework.beans.factory.support.DefaultListableBeanFactory]
    -  2012-10-01 15:25:03,668 DEBUG
    [org.springframework.jdbc.datasource.DataSourceTransactionManager] -
     2012-10-01 15:25:03,669
    DEBUG
    [org.springframework.jdbc.datasource.DataSourceTransactionManager] -
    
    2012-10-01 15:25:03,673 DEBUG
    [org.springframework.jdbc.datasource.DataSourceTransactionManager] -
    
    2012-10-01 15:25:03,674 DEBUG [org.mybatis.spring.SqlSessionUtils] -
     2012-10-01 15:25:03,674 DEBUG
    [org.mybatis.spring.SqlSessionUtils] - 
    2012-10-01 15:25:03,679 DEBUG
    [org.mybatis.spring.transaction.SpringManagedTransaction] -  2012-10-01 15:25:03,679 DEBUG
    [org.example.mybatis.spring.persistence.TrainMapper.insertTrain] -
     2012-10-01
    15:25:03,679 DEBUG
    [org.example.mybatis.spring.persistence.TrainMapper.insertTrain] -
    &lt;==>  Preparing: INSERT INTO trains (train_type) VALUES (?) >
    2012-10-01 15:25:03,680 DEBUG
    [org.example.mybatis.spring.persistence.TrainMapper.insertTrain] -
    &lt;==> Parameters: Interregionale(String)> 2012-10-01 15:25:03,681 DEBUG
    [org.mybatis.spring.SqlSessionUtils] - 
    2012-10-01 15:25:03,681 DEBUG
    [org.springframework.jdbc.datasource.DataSourceTransactionManager] -
     2012-10-01 15:25:03,683 DEBUG
    [org.springframework.jdbc.datasource.DataSourceTransactionManager] -
     2012-10-01 15:25:03,727 DEBUG
    [org.mybatis.spring.SqlSessionUtils] - 
    2012-10-01 15:25:03,727 DEBUG [org.mybatis.spring.SqlSessionUtils] -
    
    2012-10-01 15:25:03,728 DEBUG
    [org.springframework.jdbc.datasource.DataSourceTransactionManager] -
    
    2012-10-01 15:25:03,728 DEBUG
    [org.springframework.jdbc.datasource.DataSourceUtils] - </p>
  </blockquote>
  
  <p>Note that I ran the same example using JDBC, and it worked fine.</p>
  
  <p>Libraries and environment:
  Spring 3.1.2
  MyBatis 3.1.1
  MyBatis-Spring 1.1.1
  MySQL 5.5</p>
  
  <p>Thanks in advance.</p>