Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用MapperFactoryBean时如何获取当前会话?_Java_Spring_Session_Mybatis_Ibatis - Fatal编程技术网

Java 使用MapperFactoryBean时如何获取当前会话?

Java 使用MapperFactoryBean时如何获取当前会话?,java,spring,session,mybatis,ibatis,Java,Spring,Session,Mybatis,Ibatis,免责声明:我还是MyBatis&Spring的新手,所以如果下面缺少一些细节,请原谅我 我有一些代码在MyBatis中使用MapperFactoryBean自动管理数据库事务,而无需在Java代码中创建DAO类。这也意味着当使用这种方法时,用户不需要编写任何特定于会话的代码,因为MyBatis直接处理与数据库本身通信的会话 问题:如果出现上述情况,如果希望获得当前会话,您如何做到??在Hibernate中,您可以执行getSessionFactory().getCurrentSession()来

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

我有一些代码在MyBatis中使用
MapperFactoryBean
自动管理数据库事务,而无需在Java代码中创建DAO类。这也意味着当使用这种方法时,用户不需要编写任何特定于会话的代码,因为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" />
public interface CountryMapper {

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

    public CountryDetailsDTO getCountryInfo(String countryCode);

  //etc ....
}
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();
}
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 ...

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

    }
}

com.countries.dataaccess.CountryMapper:

<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" />
public interface CountryMapper {

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

    public CountryDetailsDTO getCountryInfo(String countryCode);

  //etc ....
}
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();
}
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 ...

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

    }
}
公共接口CountryMapper{
公共列表getAllCountries(列表商品类型);
公共CountryDetails用于获取CountryInfo(字符串countryCode);
//等等。。。。
}
com.countries.delegate.countries服务代表:

<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" />
public interface CountryMapper {

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

    public CountryDetailsDTO getCountryInfo(String countryCode);

  //etc ....
}
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();
}
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 ...

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

    }
}
公共接口国家/地区服务委托{
public CountryDetails getCountryDetails(字符串countryCode,字符串locale)抛出异常;
公共列表getAllCountryDetails(List merchantTypeList,布尔详细信息)引发异常;
公共卫生getDBHealth();
}
com.countries.delegate.impl.countries服务delegateimpl:

<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" />
public interface CountryMapper {

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

    public CountryDetailsDTO getCountryInfo(String countryCode);

  //etc ....
}
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();
}
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 ...

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

    }
}
公共类CountriesServiceDelegateImpl实现CountriesServiceDelegate{
@自动连线
私人国家基金会国家基金会;
@凌驾
public CountryDetails getCountryDetails(字符串countryCode,字符串locale){
//这里的实现逻辑。。。
}
@凌驾
公共列表getAllCountryDetails(列表merchantTypeList,布尔详细信息){
//这里的实现逻辑。。。
}
}
com.countries.facade:

<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" />
public interface CountryMapper {

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

    public CountryDetailsDTO getCountryInfo(String countryCode);

  //etc ....
}
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();
}
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 ...

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

    }
}
公共类国家基金会{
@自动连线
@限定符(“countriesMapper”)
私人国家地图绘制者国家地图绘制者;
public CountryDetails getCountryInfo(字符串countryCode,字符串locale){
//这里的实现逻辑,比如xyz=countriesMapper.getCountryInfo(countryCode);
}
公共列表getAllCountries(列表商品类型,布尔详细){
//这里的实现逻辑,比如xyz=countriesMapper.getCountryInfo(countryCode);
}
}

高使用
SqlSessionTemplate
如文档所示

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


您还可以使用同一页面上记录的
SqlSessionDaoSupport
,前提是它更适合您的用例。

Hi-use
SqlSessionTemplate

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

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