3

OR/マッピングに Mybatis (v3.0.5) を使用するプロジェクトがあります。典型的な (機能する) セットアップは次のとおりです。

  1. マップする POJO -Fruit
  2. 「MyBatis」マッパー XML ファイル - FruitMapper.xml- すべての SQL クエリが格納される場所
  3. すべて同じマッパー メソッドを定義するインターフェイス マッパー -FruitMapper.java
  4. インターフェイス マッパー参照を持つ DAO -FruitDao
  5. MyBatis 設定ファイル -mybatis-config.xml
  6. Spring config XML ですべてをリンクする -myapp-spring-config.xml

サンプル実装:

public class Fruit {
    private String type = "Orange"; // Orange by default!

    // Getters & setters, etc. This is just a VO/POJO
    // that corresponds to a [fruits] table in my DB.
}

public interface FruitMapper {
    public List<Fruit> getAllFruits();
}


public class FruitDao {
    private FruitMapper mapper;

    // Getters & setters

    public List<Fruit> getAllFruits() {
        return mapper.getAllFruits();
    }
}

FruitMapper.xml

<mapper namespace="net.me.myapp.FruitMapper">
    <select id="getAllFruits" resultSetType="FORWARD_ONLY">
        SELECT * FROM fruits
    </select>
</mapper>

mybatis-config.xml

<configuration>
    <!-- Nothing special here. -->
</configuration>

myapp-spring-config.xml: (これは私が取り除きたいものです)

<bean id="fruitDao" class="net.me.myapp.FruitDao">
    <property name="mapper" ref="fruitMapper" />
</bean>
<bean id="fruitMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
    <property name="mapperInterface" value="net.me.myapp.FruitMapper" />
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="myDatasource" /> 
    <property name="configLocation" value="classpath:mybatis-config.xml" />
    <property name="mapperLocations" value="classpath:*Mapper.xml" />
</bean>
<bean id="myDatasource" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true">
    <property name="jndiName">
        <value>java:/comp/env/jdbc/our-mysql-database</value>
    </property>
</bean>

これはうまくいきます。ただし、私はSpringの大ファンではなく、Spring構成ファイル内のすべてのBeanが行うことの独自の純粋なJavaバージョンをどのように実装するのか疑問に思っていました.

だから私は尋ねます:実行時にバインドされるように適切に実装するには、どの「グルーコード」/クラスを書く必要がありますか?FruitMapper.javaFruitMapper.xml私が書くときはいつでもそのように:

FruitMapperDao dao = new FruitMapperDao();
FruitMapperImpl mapper = new FruitMapperImpl(); // <== this is what I need to implement here
dao.setMapper(mapper);

List<Fruit> allFruits = dao.getAllFruits();

...fruitでは、データ ソース内のすべてのレコードのリストを取得する必要がありますか? 前もって感謝します!

アップデート

また、上記の設定を考えると、実行時のクラスパスにmybatis.jarとの両方が必要であることにも言及する必要があります。mybatis-spring.jarSpring を完全に取り除き、純粋な Java ソリューションを機能させるために Spring jar やクラスを必要としないようにしたいのです。

4

1 に答える 1

4

SqlSessionインスタンス (例: named session) を取得し、メソッドを呼び出す必要がありますsession.getMapper(FruitMapper.class)。マッパー インターフェイスを既に実装しているオブジェクトを取得し、そのメソッドを呼び出して DB からデータを取得します。

PS 次のように、Spring なしで SqlSession を取得できます。

InputStream inputStream = Resources.getResourceAsStream("/mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = factory.openSession();
于 2012-12-25T09:44:51.983 に答える