MyBatis 3 + Spring 3:select返回空值

时间:2012-10-01 14:29:29

标签: spring mybatis

我根据MyBatis示例构建了一个项目(我还检查了JPetStore 6 application)。

问题是我的getTrains()方法总是返回null对象。 这是源文件和配置文件:

TrainMapper.java

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<Train> getTrains();
    void insertTrain(Train train);
}

TrainService.java

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<Train> getTrains() {
    return this.trainMapper.getTrains();
}

@Transactional
public void insertTrain(Train train) {
    this.trainMapper.insertTrain(train);
}

}

TrainMapper.xml

   <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

    <mapper namespace="org.example.mybatis.spring.persistence.TrainMapper">
      <cache />
      <select id="getTrain" resultType="Train" parameterType="int">
        SELECT
          train_type
        FROM trains
        WHERE trains.train_id = #{id}
      </select>
      <select id="getTrains" resultType="org.example.mybatis.spring.domain.Train" parameterType="int">
        SELECT
          train_type
        FROM trains
      </select>
      <insert id="insertTrain" parameterType="Train">
        INSERT INTO trains (train_type)
        VALUES (#{type})
      </insert>
    </mapper>

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">

  <context:property-placeholder location="classpath:mysql.jdbc.properties" />

  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
  </bean>

  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>

  <context:component-scan base-package="org.example.mybatis.spring" />

  <tx:annotation-driven />

  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="typeAliasesPackage" value="org.example.mybatis.spring.domain" />
    <!--property name="configLocation" value="classpath:mybatis-config.xml"/-->
    <!--property name="mapperLocations" value="classpath:org/example/mybatis/spring/persistence/*.xml" /-->
  </bean>

  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="org.example.mybatis.spring.persistence" />
  </bean>
</beans>

Train.java是一个简单的Java bean。

项目POM:

<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>mybatis-spring-example</artifactId>
  <name>MyBatis-Spring Example</name>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <description>Example of the MyBatis-Spring integration (within a Maven project)</description>

  <properties>
    <maven.compiler.plugin.version>2.5.1</maven.compiler.plugin.version>
    <log4j.version>1.2.17</log4j.version>
    <junit.version>4.10</junit.version>
    <mysql.connector.version>5.1.20</mysql.connector.version>
    <spring.version>3.1.2.RELEASE</spring.version>
    <mybatis.version>3.1.1</mybatis.version>
    <mybatis.spring.version>1.1.1</mybatis.spring.version>
    <hsqldb.version>2.2.8</hsqldb.version>
    <dbcp.version>1.4</dbcp.version>
    <cglib.version>2.2.2</cglib.version>
    <nexus.ip>x.y.w.z</nexus.ip>
    <nexus.port>xxxx</nexus.port>
    <nexus.public.group.url>http://${nexus.ip}:${nexus.port}/nexus/content/groups/public</nexus.public.group.url>
    <nexus.releases.url>http://${nexus.ip}:${nexus.port}/nexus/content/repositories/releases</nexus.releases.url>
    <nexus.snapshots.url>http://${nexus.ip}:${nexus.port}/nexus/content/repositories/snapshots</nexus.snapshots.url>
  </properties>

  <repositories>
    <repository>
      <id>nexus-internal-repo</id>
      <name>Sonatype Nexus Internal Repository</name>
      <url>${nexus.public.group.url}</url>
      <releases>
        <enabled>true</enabled>
        <updatePolicy>never</updatePolicy>
        <checksumPolicy>fail</checksumPolicy>
      </releases>
      <snapshots>
        <enabled>true</enabled>
        <updatePolicy>never</updatePolicy>
        <checksumPolicy>warn</checksumPolicy>
      </snapshots>
    </repository>
  </repositories>

  <dependencies>
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>${mybatis.version}</version>
</dependency>

<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>${mybatis.spring.version}</version>
</dependency>

<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>${log4j.version}</version>
</dependency>

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>${mysql.connector.version}</version>
</dependency>

<dependency>
  <groupId>commons-dbcp</groupId>
  <artifactId>commons-dbcp</artifactId>
  <version>${dbcp.version}</version>
</dependency>

<dependency>
  <groupId>cglib</groupId>
  <artifactId>cglib</artifactId>
  <version>${cglib.version}</version>
</dependency>

我将应用程序作为独立运行:

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<Train> 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);
        }
    }

数据库中的插入工作正常,而我无法从“train”表中检索数据。 这是日志输出:

  

2012-10-01 15:25:03,400 DEBUG [org.mybatis.spring.SqlSessionUtils] -    2012-10-01 15:25:03,413调试   [org.mybatis.spring.SqlSessionUtils] -   2012-10-01 15:25:03,463调试   [org.apache.ibatis.cache.decorators.LoggingCache] - 2012-10-01   15:25:03,473调试   [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调试   [org.example.mybatis.spring.persistence.TrainMapper.getTrains] - &lt; ==&gt;   准备:SELECT train_type FROM train&gt; 2012-10-01 15:25:03,578   DEBUG [org.example.mybatis.spring.persistence.TrainMapper.getTrains] -   &LT; ==&GT;参数:&gt; 2012-10-01 15:25:03,660调试   [org.mybatis.spring.SqlSessionUtils] -   2012-10-01 15:25:03,661调试   [org.springframework.jdbc.datasource.DataSourceUtils] - [null,null,null,null,null]   2012-10-01 15:25:03,664调试   [org.springframework.beans.factory.support.DefaultListableBeanFactory]    - 2012-10-01 15:25:03,668调试   [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调试   [org.springframework.jdbc.datasource.DataSourceTransactionManager] -      2012-10-01 15:25:03,674 DEBUG [org.mybatis.spring.SqlSessionUtils] -    2012-10-01 15:25:03,674调试   [org.mybatis.spring.SqlSessionUtils] -   2012-10-01 15:25:03,679调试   [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调试   [org.example.mybatis.spring.persistence.TrainMapper.insertTrain] -   &LT; ==&GT;准备:插入列车(train_type)VALUES(?)&gt;   2012-10-01 15:25:03,680调试   [org.example.mybatis.spring.persistence.TrainMapper.insertTrain] -   &LT; ==&GT;参数:Interregionale(String)&gt; 2012-10-01 15:25:03,681调试   [org.mybatis.spring.SqlSessionUtils] -   2012-10-01 15:25:03,681调试   [org.springframework.jdbc.datasource.DataSourceTransactionManager] -    2012-10-01 15:25:03,683调试   [org.springframework.jdbc.datasource.DataSourceTransactionManager] -    2012-10-01 15:25:03,727调试   [org.mybatis.spring.SqlSessionUtils] -   2012-10-01 15:25:03,727 DEBUG [org.mybatis.spring.SqlSessionUtils] -      2012-10-01 15:25:03,728调试   [org.springframework.jdbc.datasource.DataSourceTransactionManager] -      2012-10-01 15:25:03,728调试   [org.springframework.jdbc.datasource.DataSourceUtils] -

请注意,我使用JDBC运行相同的示例,它运行正常。

图书馆和环境: 春季3.1.2 MyBatis 3.1.1 MyBatis-Spring 1.1.1 MySQL 5.5

提前致谢。

1 个答案:

答案 0 :(得分:0)

你还没有发布你的“Train”java bean。但是在Insert语句中你使用#{type},我是guest,你的bean属性名是“id”和“type”。但是在数据库表中,字段名称是“id”和“train_type”。 Mybatis不知道如何将表字段映射到java bean属性。在Mapper.xml中,您尚未添加结果映射。有两种方法:

  1. 在mapper.xml中添加resultMap元素,告诉mybiatis如何将数据库表字段名称映射到java bean属性名称。
  2. select语句中的
  3. 像这样使用“as”

    选择   train_type为类型 来自火车