OR/マッピングに Mybatis (v3.0.5) を使用するプロジェクトがあります。典型的な (機能する) セットアップは次のとおりです。
- マップする POJO -
Fruit
- 「MyBatis」マッパー XML ファイル -
FruitMapper.xml
- すべての SQL クエリが格納される場所 - すべて同じマッパー メソッドを定義するインターフェイス マッパー -
FruitMapper.java
- インターフェイス マッパー参照を持つ DAO -
FruitDao
- MyBatis 設定ファイル -
mybatis-config.xml
- 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.java
FruitMapper.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.jar
Spring を完全に取り除き、純粋な Java ソリューションを機能させるために Spring jar やクラスを必要としないようにしたいのです。