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

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

【问题讨论】:

    标签: cdi mybatis


    【解决方案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/

    【讨论】:

      猜你喜欢
      • 2018-03-30
      • 2017-02-04
      • 2014-08-01
      • 1970-01-01
      • 2018-12-07
      • 2013-05-25
      • 2016-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多