MyBatis の例に基づいてプロジェクトを作成しました ( JPetStore 6 アプリケーションも調べました)。
問題は、私の 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);
}
}
データベースへの挿入は正常に機能しますが、「trains」テーブルからデータを取得することはできません。ログ出力は次のとおりです。
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] - <==> 準備: SELECT train_type FROM trains > 2012-10-01 15:25:03,578 DEBUG [org.example.mybatis.spring.persistence. TrainMapper.getTrains] - <==> パラメータ: > 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] - <==> 準備中: INSERT INTO 列車 (train_type)値 (?) > 2012-10-01 15:25:03,680 DEBUG [org.example.mybatis.spring.persistence.TrainMapper.insertTrain] - <==> パラメータ: Interregionale(文字列)> 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] -
JDBC を使用して同じ例を実行したところ、問題なく動作したことに注意してください。
ライブラリと環境: Spring 3.1.2 MyBatis 3.1.1 MyBatis-Spring 1.1.1 MySQL 5.5
前もって感謝します。