- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我第二次尝试使用 MyBatis 创建集成测试。我已经尝试了很多方法,但似乎没有解决这个问题的方法。希望大家能够帮助我。
In my previous question我尝试编写一个集成测试来检查其余 API 的输出。场景如下:rest API 调用注入(inject)的 EJB,该 EJB 使用 MyBatis 执行一些 SQL:rest api > ejb > mybatis。不幸的是,我既无法注入(inject),也无法模拟 MyBatis 映射器接口(interface),因此我的测试不起作用:(
现在我创建了另一个测试场景,但最终遇到了同样的情况。现在我的场景非常简单:我有一个带有注入(inject)的 MyBatis 映射器的 EJB。我想在带有 Arquillian 的嵌入式 Glassfish/Payara 服务器中测试它。
这是我的异常(exception):
org.glassfish.deployment.common.DeploymentException: CDI deployment failure:WELD-001408: Unsatisfied dependencies for type AccountDao with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject private a.b.c.AppleBean.accountDao
at a.b.c.AppleBean.accountDao(AppleBean.java:0)
EJB:
@Stateless
public class AppleBean {
@Inject
private AccountDao accountDao;
public String say() {
return "Apple";
}
}
帐户映射器 (DAO):
@Mapper
public interface AccountDao {
@Select("SELECT * FROM account WHERE id = #{id}")
@Results({
@Result(property = "email", column = "email", javaType = String.class),
@Result(property = "firstName", column = "first_name", javaType = String.class),
@Result(property = "lastName", column = "last_name", javaType = String.class),
})
Account findById(@Param("id") Long id);
}
我的测试类:
@RunWith(Arquillian.class)
public class AppleBeanTest {
@EJB
private AppleBean bean;
@Deployment
public static WebArchive createDeployment() {
return ShrinkWrap
.createFromZipFile(WebArchive.class, new File("target/war-demo-test-1.0.war"))
.addPackages(true, "a.b");
}
@Test
public void say() throws Exception {
assertNotNull(bean);
System.out.println(bean.say());
}
}
如果我注释 AppleBeanTest
中的两行以删除对 MyBatis 映射器的引用,那么我的测试工作正常。
I uploaded the source code to github as well.
<小时/>解决方案
我的测试中缺少以下类(class)。 @blackwizard 谢谢你让我找到了正确的方向。
SessionFactoryProducer.java
@ApplicationScoped
public class SessionFactoryProducer {
@ApplicationScoped
@Produces
@SessionFactoryProvider
public SqlSessionFactory produce() throws Exception {
SqlSessionFactory sessionFactory;
try (Reader reader = Resources.getResourceAsReader("mybatis.xml")) {
sessionFactory = new SqlSessionFactoryBuilder().build(reader);
}
// create sample table
//createTable(sessionFactory);
return sessionFactory;
}
private void createTable(final SqlSessionFactory manager) throws Exception {
try (SqlSession session = manager.openSession()) {
LOGGER.info("-> Initializing database...");
Connection conn = session.getConnection();
Reader reader = Resources.getResourceAsReader("create-table-postgresql.sql");
ScriptRunner runner = new ScriptRunner(conn);
runner.runScript(reader);
reader.close();
LOGGER.info("=> Database has been initialized properly.");
} catch (Exception ex) {
LOGGER.error("Error executing SQL Script...", ex);
}
}
}
git 项目已更新。
最佳答案
此时此地,我没有资源来克隆和运行您的项目来确认我要说的话。但如果有必要的话,我会在周一,同时,以下可能是一个轨道:
我认为它不起作用,因为缺少一些非常重要的东西:SqlSessionFactory
。
mybatis-cdi doc第一段中指出:
The SqlSessionFactory is the source of any MyBatis bean so first you need to create one (at least) and let the container know about it existence.
确实,如果没有Session,就没有理由获取Mapper实例。如果@Mapper注释足够了,它只能提供一个空壳,因为不链接到任何底层数据源。那么如果没有Mapper,就无法注入(inject)到EJB中,这就是Weld提示的地方。
部署成功后,是用@Inject private AccountDao accountDao
吗?我不明白为什么 Weld 允许不注入(inject)任何东西。但如果确实如此,请检查 accountDao
值(调试断点或日志)。
关于java - 如何使用 Arquillian 模拟 MyBatis 映射器接口(interface)(第 2 部分)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42728129/
1.概述 转载:MyBatis 二级缓存全详解 上一篇文章中我们介绍到了 MyBatis 一级缓存其实就是 SqlSession 级别的缓存,什么是 SqlSession 级别的缓存呢?一级缓存的本质
1.概述 转载:核心配置综述之StatementHandler 2.MyBatis 四大组件之StatementHandler StatementHandler 是四大组件中最重要的一个对象,负责操作
1.概述 转载:MyBatis 启动流程 MyBatis 是第一个支持自定义 SQL、存储过程和高级映射的类持久框架。MyBatis 消除了大部分 JDBC 的样板代码、手动设置参数以及检索结果。My
1.概述 转载:MyBatis 基础搭建及架构概述 2.MyBatis 是什么? MyBatis是第一个支持自定义SQL、存储过程和高级映射的类持久框架。MyBatis消除了大部分JDBC的样板代码、
1.概述 转载:核心配置综述之 ParameterHandler MyBatis 四大核心组件我们已经了解到了两种,一个是 Executor ,它是MyBatis 解析SQL请求首先会经过的第一道关卡
1.概述 转载:核心配置综述之 ResultSetHandler 我们之前介绍过了MyBatis 四大核心配置之 Executor、StatementHandler、 ParameterHandler
如果我使用mybatis,我可以很容易地得到更新的行数,就像 update table set desc = 'xxx' where name = ? 但是,如果我想获取更新的行数,而不是计数,我该如
如何在MyBatis 3中使用小于等于 SELECT * FROM( SELECT * FROM TABLE1 WHERE COL1 =#{COL1,jdbc
我将 mybatis3.0.6 与 java 一起使用 哪个性能更好? [select id="getData" parameterType="Integer" resultType="Integer
我无法在 mybatis 中使用动态排序类型创建 SQL,如下例 select user_profile.user_profile_id, user_profile.first_name
这是一个流行的例子。 insert into ACCOUNT ( ACC_ID, ACC_FIRST_NAME, ACC_LAST_NAME, ACC_EMAIL )values (
我下载了MyBatis,文件夹中有一个mybatis-3.0.4-javadoc.jar,我解压并打开它,但它几乎是空的。 哪里可以找到MyBatis的API文档? 最佳答案 http://repo1
我正在尝试为 ArrayList 编写类型处理程序,但这给了我错误,任何人都可以帮助我。 我想将 ArrayList 作为 VARCHAR 存储在数据库中并将其检索为 ArrayList。 packa
目录 依赖 配置 CodeGenerator mybatis-plus-generator + clickhouse 自动生成代码 依赖
目录 三者实现对比 使用fluent mybatis 来实现上面的功能 换成mybatis原生实现效果 换成mybatis plus
例如,我有查询从员工中选择 ID、姓名、年龄、地址,而不是拥有员工对象列表。我希望有一个 map 列表,如 list{ map{ ("id", 123), ("name","jac
我在使 MyBatis (3.4.6) 工作时遇到一些麻烦。 我已将 mybatis-config.xml 文件放置在项目的 src/main/resources 文件夹中,但是当我运行单元测试时,出
我现在使用 Mybatis 和 spring-boot。我没有添加mybatis-config.xml。我根据说明通过 application.properties 为数据源和 mybatis 进行所
这是我的第一篇文章,用我糟糕的英语...... 我使用的是MyBatis3.0 在查询中,我使用 SqlBuilder 的方法如下: public class DataStatisticSqlBuil
主题:MyBatis:Boolean Paraeter:MyBatis 正在使用 Getter 内容: 大家好, 我一直在寻找解决我近乎简单的 MyBatis 问题的方法: 给定代码(仅必要部分):
我是一名优秀的程序员,十分优秀!