【问题标题】:How to get current session when using MapperFactoryBean?使用 MapperFactoryBean 时如何获取当前会话?
【发布时间】:2016-07-02 04:06:13
【问题描述】:

免责声明:我对 MyBatis 和 Spring 还是非常新手,所以如果下面缺少一些细节,请原谅我。

我有一些代码在 MyBatis 中使用 MapperFactoryBean 来自动管理数据库事务,而无需在我的 Java 代码中创建 DAO 类。这也意味着当使用这种方法时,用户不需要编写任何特定于会话的代码,因为 MyBatis 直接处理会话以与数据库本身进行通信。

问题: 那么在上述情况下,如果想获得当前的 Session,你会怎么做呢?在 Hibernate 中,您可以通过 getSessionFactory().getCurrentSession() 获取当前会话。当使用 MyBatis 的MapperFactoryBean 方法与数据库通信时,MyBatis 的等效命令是什么?

这是我当前代码的 sn-ps:

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

    }
}

【问题讨论】:

    标签: java spring session mybatis ibatis


    【解决方案1】:

    您好,请使用 SqlSessionTemplate 记录的 http://www.mybatis.org/spring/sqlsession.html

    它将确保您获得与您正在使用的映射器相同的 sqlsession。

    如果更适合您的用例,您也可以使用同一页面上记录的 SqlSessionDaoSupport

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多