回滚 ORMapper 查询
我们使用 MyBatis (3.0.5) 作为我们的或映射工具(对此我没有任何意见!)。
我创建了一个 Response
对象,通过 MyBatis,映射到我们的 [responses]
数据库表(我们使用 PostgreSQL)。
实际上,我们使用的 ormapping 结构如下:
- ResponseMapper.xml - 这是定义 PSQL 查询并将其映射到 ResponseMapper.java** 类及其方法的 XML 文件
- ReponseMapper .java - MyBatis 用于执行 XML 文件(上面)中定义的查询的接口
- ResponseDAO.java - 用于 DI 目的的接口(我们使用 Spring)
- ResponseDAOImpl.java - 实际上调用
ResponseMapper
方法的ResponseDAO
的具体实现;我们使用 Spring 将此类的实例注入到我们的应用程序中,
因此,要将新的 [responses]
记录插入到 PostgreSQL 中,代码在调用组件中看起来像这样:
@Autowired
private ResponseDAO response;
public void doStuff()
{
int action = getAction();
response.setAction(action);
response.insert();
}
这个设置对我们来说效果很好。不过,我现在正在尝试为 ResponseDAOImpl 类编写一组 JUnit 测试,并且我想确保它正确执行对 PostgreSQL 数据库的查询。
据我所知,没有办法“模拟”整个数据库。所以我唯一的选择(似乎)是让测试方法执行查询,检查是否成功,然后无论如何将其回滚。
MyBatis 似乎不支持这种回滚功能。我发现 this 在 Old Nabble 上的 mybatis-user
邮件列表中发布,但发帖者使用的是 Guice,他/她的问题似乎更多是关于回滚通过 Guice 进行交易。
如果 MyBatis 不支持事务/回滚(支持吗?!?!),那么我唯一的解决办法就是 PostgreSQL-JDBC 驱动程序支持这些。我想我可以尝试配置我的测试方法,以便它们运行 ResponseDAO.insert() 方法,然后手动尝试直接通过驱动程序回滚事务(无 MyBatis)。
SO 有这方面的经验吗? 代码示例?尖端?最佳实践?提前致谢!
We're using MyBatis (3.0.5) as our or-mapping tool (and I don't have any say-so over that!).
I've created a Response
object that, through MyBatis, mapped to our [responses]
database table (we use PostgreSQL).
Actually, the ormapping structure we use is as follows:
- ResponseMapper.xml - this is the XML file where the PSQL queries are defined and mapped to the ResponseMapper.java** class and its methods
- ReponseMapper.java - An interface used by MyBatis for executing the queries defined in the XML file (above)
- ResponseDAO.java - An interface used for DI purposes (we use Spring)
- ResponseDAOImpl.java - A concrete implementation of
ResponseDAO
that actually callsResponseMapper
methods; we use Spring to inject instances of this class into our application
So, to INSERT a new [responses]
record into PostgreSQL, the code looks like this from the invoking component:
@Autowired
private ResponseDAO response;
public void doStuff()
{
int action = getAction();
response.setAction(action);
response.insert();
}
This set up works beautifully for us. However I am now trying to write a set of JUnit tests for the ResponseDAOImpl
class, and I want to make sure that it is correctly executing queries to our PostgreSQL database.
As far as I can tell, there is no way to "mock" an entire database. So my only option (seemingly) is to have the test method(s) execute a query, check for success, and then roll it back regardless.
MyBatis doesn't seem to support this kind of rollback feature. I found this post off the mybatis-user
mailing list on Old Nabble, but the poster was using Guice and his/her question seemed to be more about rolling back transactions through Guice.
If MyBatis doesn't support transactions/rollbacks (does it?!?!), then it seems like my only repireve would be if the PostgreSQL-JDBC driver supports these. I guess I could then try to configure my test methods so that they run the ResponseDAO.insert()
method, and then manually try to rollback the transaction directly through the driver (sans MyBatis).
Does SO have any experience with this? Code samples? Tips? Best practices? Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MyBatis 允许在使用“SqlSession”时进行回滚,问题是您使用 spring 依赖注入部分,它会在方法完成时自动提交您的事务。
您有几个选项,其中
注入依赖项的模拟。有一些很棒的库可以帮助解决这个问题。例如 Mockito、这是一个关于 Spring Mockito 内容的好问题。这将测试您的 java 中的业务逻辑,但不会测试您的实际查询。
提交您的查询,并在测试运行后删除您的数据。这是我们采用的方法,因为它也测试我们的数据库。显然,您需要数据库的测试实例,而有些人没有。
您可以尝试为在 MyBatis Spring Integration 中执行自动提交的类提供自己的测试绑定,并覆盖那里的行为,以便在测试环境中该行为是回滚查询而不是提交。 Guice 集成中使用了类似的方法,描述如下:此处。
MyBatis allows rollbacks when working with an "SqlSession", the thing is your using the spring dependency injection piece, which automatically commits your transaction when the method completes.
You have a few options, among them
Inject a Mock of your dependencies. There is some rocking libraries to help with this. Like Mockito, here is a good question on Spring Mockito stuff. This will test your business logic in your java, but not your actual queries.
Commit your queries, and delete your data after the test runs. This is the approach we took, because it tests our database as well. You would obviously need a test instance of your database, which some people don't have.
You could try to provide your own test bindings for the classes that do the automatic commit in the MyBatis Spring Integration and override there behavior so that in the test environment the behavior is to rollback the query instead of committing. A similar approach was use in the Guice integration, and it is described here.
不确定这是否是您所需要的,但是 org.apache.ibatis.session.SqlSession 类具有 rollback() 方法,可用于 回滚。
另一种方法是使用同一类中的 getConnection() 方法,该方法将返回 javax.sql.Connection javax.sql.Connection 类,它也有 commit() 和 rollback() 方法。
希望有帮助。
雷米斯B
Not sure this is what you need, but org.apache.ibatis.session.SqlSession class has rollback() method which can be used to rollback.
Another approach is to use getConnection() method from the same class which will return javax.sql.Connection javax.sql.Connection class which also has commit() and rollback() methods.
Hope it helps.
Remis B