Spring-Boot and Mybatis integration

486 views
Skip to first unread message

Yoandy Terradas-Otero

unread,
Jun 3, 2014, 12:44:49 PM6/3/14
to mybati...@googlegroups.com

Hi,  

Im using Sprint-Boot which it is Spring 4 and Mybatis.  Annotation @MapperScan causes issues when combine with @Value annotated properties.  If I removed @MapperScan, properties are loaded but then Mapper interfaces annotated with @Repository are not added to the context for injection.  Moving Mybatis configuration to its own config class or xml yields the same behavior.  Any help would be much appreciated.  Thank you in advance for you time.  

Regards,

Yoandy

Iwao AVE!

unread,
Jun 5, 2014, 2:11:55 AM6/5/14
to mybatis-user
Hi Yoandy,

It seems to be the same issue as https://github.com/mybatis/spring/issues/30
Please see the comments there.

Regards,
Iwao
> --
> You received this message because you are subscribed to the Google Groups
> "mybatis-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mybatis-user...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Yoandy Terradas-Otero

unread,
Jun 5, 2014, 6:33:18 PM6/5/14
to mybati...@googlegroups.com
Thank you.  It looks like the Spring ticket has not being worked on for 3 months.  

Yoandy Terradas-Otero

unread,
Jul 8, 2014, 12:04:31 AM7/8/14
to mybati...@googlegroups.com
In case anyone is interested or comes across this post.  You can still use spring-boot and mybatis  with a little bit more of work.

 @Bean SqlSessionFactory sqlSessionFactory() {
    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    Environment environment = new Environment("dev", transactionFactory, dataSource());

    Configuration configuration = new Configuration(environment);

    // NOTE: you must register aliases before adding mappers.  
    configuration.getTypeAliasRegistry().registerAlias(MyClazz.class);

    // NOTE: you can add a package where your mappers reside or add one at a time.  
    configuration.addMapper(IMyClazzMapper.class);

    configuration.setMapUnderscoreToCamelCase(true);

    return new SqlSessionFactoryBuilder().build(configuration);
  }

  public interface IClazzMapper {
    MyClazz selectBySourceId( Integer sourceId );
  }

  <?xml version="1.0" encoding="UTF-8" ?>
  <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  <mapper namespace="com.mypackage.IMyClazzMapper">
   <select id="selectById" resultType="MyClazz">
    select id from my_table where id = #{id}
   </select>
 </mapper>

 public class MyClazzDAO {

  @Autowired SqlSessionFactory sessionFactory; 

  public MyClazz getById( final Integer Id ) {
    MyClazz myClazz = null;

    try (SqlSession session = sessionFactory.openSession()) {
      IMyClazzMapper myClazzMapper = session.getMapper(IMyClazzMapper.class);
      myClazz = myClazzMapper.selectById(id);
    }

    return myClazz;
  }
}

Hope this helps.  

Eduardo Macarron

unread,
Jul 8, 2014, 12:13:57 AM7/8/14
to mybati...@googlegroups.com
Hi Yoandry. Not directly related to spring-boot.

When using Spring you should use the Spring's MyBatis SqlSession called SqlSessionTemplate and you can inject Mappers directly to your beans (no need to add a DAO if it will just wrap calls to a mapper). 




--

yterradas.gmail

unread,
Jul 8, 2014, 12:25:13 AM7/8/14
to mybati...@googlegroups.com, Eduardo Macarron
Right, but I am not using mybatis-spring because of an issue with SqlSessionFactoryBean instantiation since Spring 4.0.2( https://jira.spring.io/browse/SPR-11582 ).  I guess I can still use the template so thank you for pointing that out.  

Regards,  
-- 
yoandy terradas
You received this message because you are subscribed to a topic in the Google Groups "mybatis-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mybatis-user/R94q39aUj9I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mybatis-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages