首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Mybatis Spring集成

Mybatis Spring集成
EN

Stack Overflow用户
提问于 2015-03-09 08:44:35
回答 1查看 902关注 0票数 0

我有一个使用没有spring的Mybatis的web应用程序(JSF和Managedbeans),我想在这个web应用程序中使用Spring,并将mybatis与Spring集成。

我有mybatis-config.xml和映射器。我有xml格式的映射器和与xml中的映射器同名的接口。

那么实现这些接口的类就是这样的。

代码语言:javascript
运行
复制
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代码是这样的。

代码语言:javascript
运行
复制
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;
}}

我一直在读一些关于Mybatis spring集成的东西,但我对所有的东西都不太了解。我知道我的spring xml一定是这样的。我没有放入mappersLocation属性,因为我已经在mybatis-config.xml上定义了我的映射器,所以这不是必须的。

代码语言:javascript
运行
复制
<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> 

MyBatisUtil类和接口的实现发生了什么?

EN

回答 1

Stack Overflow用户

发布于 2015-05-15 01:35:48

当您使用mybatis-spring时,您将从mybatis中自动获得mapper接口的实现。因此,您可以在任何spring bean中安全地使用带有@Autowired的接口。就这样。哪里有弹簧,哪里就有DI。是的,使用mybatis-spring你根本不需要MybatisUtil,因为所有的任务都是在spring xml中管理的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28933674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档