如何在 CDI 中使用 MyBatis

Posted

技术标签:

【中文标题】如何在 CDI 中使用 MyBatis【英文标题】:How to use MyBatis with CDI 【发布时间】:2014-02-15 15:28:55 【问题描述】:

我正在编写一个 Web 应用程序,我正在使用 MyBatis 框架进行持久性。我想使用 CDI 轻松地注入映射器并以声明方式管理事务。

【问题讨论】:

【参考方案1】:

MyBatis 有官方的 CDI 支持:

映射器声明:

@Mapper
public interface UserMapper 
  @Select("SELECT * FROM users WHERE id = #userId")
  User getUser(@Param("userId") String userId);
 

映射器用法:

public class MyService 

    @Inject UserMapper userMapper;

    public User doSomeStuff(String userId) 
       return this.userMapper.getUser(userId);
    

文档中的更多信息:http://mybatis.org/cdi/

【讨论】:

以上是关于如何在 CDI 中使用 MyBatis的主要内容,如果未能解决你的问题,请参考以下文章