gpt4 book ai didi

java - 使用MapperFactoryBean时如何获取当前 session ?

转载 作者:太空宇宙 更新时间:2023-11-04 12:28:49 25 4
gpt4 key购买 nike

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

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

问题:那么当出现上述情况时,如果希望获得当前的 Session,该怎么做呢?在 Hibernate 中,您可以执行 getSessionFactory().getCurrentSession() 来获取当前 session 。当使用 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);

}
}

最佳答案

您好,请按照文档使用 SqlSessionTemplate http://www.mybatis.org/spring/sqlsession.html

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

您还可以使用同一页面上记录的 SqlSessionDaoSupport(如果这更适合您的用例)。

关于java - 使用MapperFactoryBean时如何获取当前 session ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38081657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com