2016-06-28 19 views
0

免責事項:私はまだ非常に MyBatisに新しい&春ですので、下記のいくつかの詳細が欠落している場合は、私を許してください。MapperFactoryBeanを使用しているときに現在のセッションを取得する方法は?

私は、JavaコードでDAOクラスを作成することなくデータベーストランザクションを自動的に管理するMyBatisでMapperFactoryBeanを使用するコードをいくつか持っています。これは、MyBatisがデータベース自体と通信するためのセッションを直接処理するため、このアプローチを使用する場合、セッション固有のコードを書く必要がないことも意味します。

質問:質問:上記の場合、現在のセッションを取得する場合は、どうすればよいですか? Hibernateでは、現在のセッションを取得するためにgetSessionFactory().getCurrentSession()を実行できます。 MyBatisのMapperFactoryBeanアプローチを使用してデータベースと通信する場合、MyBatisの同等のコマンドは何ですか?

beans.xmlの:

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
    <property name="basePackage" value="com.countries.dataaccess" /> 
</bean> 

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" />  
</bean> 

<bean id="countriesMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
<property name="mapperInterface" value="com.countries.dataaccess.CountryMapper" /> 
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />   
</bean> 

<bean id="countriesFacade" class="com.countries.facade.CountriesFacade" /> 

com.countries.dataaccess.CountryMapper:

public interface CountryMapper { 

    public List<CountryOutlineDTO> getAllCountries(List<String> merchantType); 

    public CountryDetailsDTO getCountryInfo(String countryCode); 

    //etc .... 
} 

com.countries

は、ここに私の現在のコードのスニペットです.delegate.CountriesServiceDelegate:

public interface CountriesServiceDelegate { 

    public CountryDetails getCountryDetails(String countryCode, String locale) throws Exception; 

    public List<CountryDetails> getAllCountryDetails(List<String> merchantTypeList, boolean verbose) throws Exception; 

    public Health getDBHealth(); 
} 

com.countries.delegate.impl.CountriesServiceDelegateImpl:

public class CountriesServiceDelegateImpl implements CountriesServiceDelegate { 

    @Autowired 
    private CountriesFacade countriesFacade; 

    @Override 
    public CountryDetails getCountryDetails(String countryCode, String locale) { 

    //Implementation logic here ... 

    } 

    @Override 
    public List<CountryDetails> getAllCountryDetails(List<String> merchantTypeList, boolean verbose) { 

    //Implementation logic here ... 

    } 
} 

com.countries.facade:

public class CountriesFacade { 

    @Autowired 
    @Qualifier("countriesMapper") 
    private CountryMapper countriesMapper; 

    public CountryDetails getCountryInfo(String countryCode, String locale) { 

    //Implementation logic here such as xyz = countriesMapper.getCountryInfo(countryCode); 

    } 

    public List<CountryDetails> getAllCountries(List<String> merchantType, boolean verbose) { 

    //Implementation logic here such as xyz = countriesMapper.getCountryInfo(countryCode); 

    } 
} 

答えて

0

こんにちは文書http://www.mybatis.org/spring/sqlsession.html

として SqlSessionTemplateを使用

これは、使用しているマッパーと同じsqlsessionを確実に取得します。

あなたのユースケースに適している場合は、同じページに記載されているようにSqlSessionDaoSupportを使用することもできます。

関連する問題