dimanche 8 mars 2015

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.


I have my mybatis-config.xml and the mappers. I have mappers in xml and interfaces with the same name of the mappers 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.



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. I know that my spring xml must be something like that. 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.



<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. What happen with MyBatisUtil class and the implementation of the interfaces?


Aucun commentaire:

Enregistrer un commentaire