简体   繁体   English

Mybatis Spring 集成

[英]Mybatis Spring integration

I have a web application (JSF and Managedbeans) using Mybatis without Spring and I want to use Spring in this web application and integrate mybatis with spring.我有一个使用 Mybatis 而没有 Spring 的 Web 应用程序(JSF 和 Managedbeans),我想在这个 Web 应用程序中使用 Spring 并将 mybatis 与 spring 集成。

I have my mybatis-config.xml and the mappers.我有 mybatis-config.xml 和映射器。 I have mappers in xml and interfaces with the same name of the mappers un xml.我有 xml 中的映射器和与映射器 un xml 同名的接口。

Then the classes that implements the interfaces are something like that.那么实现接口的类就是这样的。

public class LocalidadPersistence implements LocalidadMapper {

@Override
public List<Localidad> getLocalidades() throws SQLException {
    List<Localidad> listaLocalidades = null;
    SqlSession session = new MyBatisUtil().getSession();
    if (session != null) {
        try {
            listaLocalidades = session.selectList("com.mybatis.mappers.localidad.getLocalidades");
        } catch (Exception ex) {
            throw new SQLException("Error obteniendo listado Localidades");
        } finally {
            session.close();
        }
    } else {
        throw new SqlSessionException("Sesion no encontrada");
    }
    return listaLocalidades;
}}

MyBatisUtil code is this. MyBatisUtil 代码是这样的。

public class MyBatisUtil {
private String resource = "com/mybatis/mybatis-config.xml";
private SqlSession session = null;

public SqlSession getSession(){
    try{
        Reader reader = Resources.getResourceAsReader(resource);

        SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
        session = sqlMapper.openSession();
    }catch (IOException ex){
        ex.printStackTrace();
    }
    return session;
}}

I have been reading something about Mybatis spring integration but I don't understand all very well.我一直在阅读有关 Mybatis spring 集成的一些内容,但我不是很了解。 I know that my spring xml must be something like that.我知道我的 spring xml 必须是这样的。 I have not put the mappersLocation property because I have read that this is not neccesary since i have defined my mappers on mybatis-config.xml.我没有放置 mappersLocation 属性,因为我已经读到这不是必需的,因为我已经在 mybatis-config.xml 上定义了我的映射器。

<context:annotation-config /> 

<context:component-scan base-package="com" />

<tx:annotation-driven />  

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
    <property name="url" value="jdbc:mysql://localhost:3306/DB" />  
    <property name="username" value="root" />  
    <property name="password" value="" />  
</bean> 

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:/com/mybatis/mybatis-config.xml" />
    <property name="transactionFactory" ref="springManagedTransactionFactory" />
</bean>


<bean id="springManagedTransactionFactory" class="org.mybatis.spring.transaction.SpringManagedTransactionFactory">  
</bean> 

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

<bean id="registroClimaMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
    <property name="mapperInterface" value="com.mybatis.dao.RegistroClimaMapper" />  
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
</bean> 

Also I have no idea about how to use this inside my application after it has been configured with spring.另外,我不知道在我的应用程序中配置了 spring 之后如何在我的应用程序中使用它。 What happen with MyBatisUtil class and the implementation of the interfaces? MyBatisUtil 类和接口的实现会发生什么?

When you use mybatis-spring, you will get an implementation of mapper interface automatically from the mybatis.当你使用mybatis-spring时,你会自动从mybatis中得到一个mapper接口的实现。 So, you can safely use the interface with @Autowired inside any of your spring beans.因此,您可以在任何 spring bean 中安全地使用带有 @Autowired 的接口。 That's it.就是这样。 Where there is a spring, there a DI.哪里有弹簧,哪里就有 DI。 And yes, with mybatis-spring you don't need MybatisUtil at all as all the task are managed in your spring xml .是的,使用 mybatis-spring 你根本不需要 MybatisUtil 因为所有任务都在你的 spring xml 中管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM