Using MyBatis in Spring
Clarence Ho
Getting Started with MyBatis in Spring
In this section, we will cover how to set up MyBatis to work with Spring. First we will provide a brief introduction to MyBatis, and then we will demonstrate how to create a project in STS for working with MyBatis. Also, we will present the data model that will be used for implementing the samples in this chapter. Finally, we will discuss the Spring configuration required for working with MyBatis.
Introducing MyBatis
iBATIS, the old name of MyBatis, was started by Clinton Begin in 2001, with the project code donated to Apache Software Foundation (ASF) in 2004. Based on its SQL mapping focus, simple infrastructure, and easy-to-understand mapping definitions from SQL queries to the Java OO model, iBATIS gained popularity quickly and became one of the most used data access libraries in the Java developer community. After staying in ASF for six years, the iBATIS development team realized that data-accessing technologies in the open source world had changed dramatically. Consequently, the team decided to introduce numerous significant changes into the library. Starting with version 3, the project also changed its name to MyBatis, left ASF, and became an independent open source framework. The iBATIS project at ASF was also retired. At the time of this writing, the current version of MyBatis is 3.0.6. Before Spring 3.0, Spring had out-of-the-box support for iBATIS version 2. However, because of the difficulties in synchronizing the massive changes from iBATIS 2 to MyBatis 3 with the Spring framework, Springrsquo;s development team decided to drop the built-in support of MyBatis. To deal with this situation, the MyBatis team has started the MyBatis Spring Integration Project (called mybatis-spring). At the time of this writing, the current version of mybatis-spring is 1.0.2.
Configuring MyBatis SqlSessionFactory and MapperScannerConfigurer
The core concept of MyBatis surrounds the SqlSession interface (under the package org.apache.ibatis.session), which was obtained from the SqlSessionFactory interface (under the package org.apache.ibatis.session). Both the SqlSession and SqlSessionFactory interfaces belong to the mybatis-3.0.6 library. To configure the factory in Spring, we use the SqlSessionFactoryBean class (under the package org.mybatis.spring), which belongs to the mybatis-spring module (mybatisspring-1.0.2 library). Another important concept in MyBatis is the mapper interfaces, which are simple Java interface classes, that will be processed by MyBatis for mapping configuration between SQL queries and domain object properties. The mapping can be defined in either XML files (having the same name as the interface class) or annotations within the mapper interface. The mybatis-spring module provides another class, called MapperScannerConfigurer (under the package org.mybatis.spring.mapper), which supports a convenient way to instruct MyBatis to scan for mapper interface classes and register them as MapperFactoryBean (under the package org.mybatis.spring.mapper and belonging to the mybatisspring module), which enables injection of MyBatis mapper interfaces into other Spring beans. The mapper interfaces will be discussed in detail later, and you will see how it can help simplify the development of database operations without the need to interact with the SqlSession interface directly. Listing 11-4 shows the Spring application context configuration (app-context.xml).
Listing 11-4. Spring Configuration for MyBatis
Most of the beans should be familiar to you; letrsquo;s focus our attention on the beans sqlSessionFactory and org.mybatis.spring.mapper.MapperScannerConfigurer. The sqlSessionFactory is implemented by the mybatis-spring provided class org.mybatis.spring.SqlSessionFactoryBean. As you might expect, the factory requires the data source bean. Also, the property typeAliasesPackage defines the packages storing the domain objects that MyBatis should look for when perform mapping from SQL resultset into POJOs. Itrsquo;s similar to the mapped entity classes in Hibernate and JPA. Note that, in the configuration, the same instance of the dataSource bean is being injected into both Springrsquo;s transactionManager bean and MyBatisrsquo;s sqlSessionFactory bean. This is because the SqlSessionFactoryBean class is designed specifically to leverage the existing DataSourceTransactionManager in Spring. Once a Spring transaction manager is configured, you can configure transactions in Spring as you normally would. Both @Transactional annotations and AOP-style configurations are supported. The mybatis-spring module will transparently manage transactions once they are set up. You just need to ensure that the same instance of dataSource was injected into both the transactionManager and SqlSessionFactory beans. The class org.mybatis.spring.mapper.MapperScannerConfigurer is also provided by the mybatisspring module. The class accepts the package name, from which MyBatis will scan for mapper interfaces. Also, the MapperScannerConfigurer bean also needs an instance of SqlSessionFactory to be injected. If there is only one SqlSessionFactory bean in the ApplicationContext, it will be autowired into the MapperScannerConfigurer bean. However, if your application contains multiple SqlSessionFactory beans (for example, in a multiple data source application, you have sqlSessionFactoryA bean for data source A and another sqlSessionFactoryB bean for data source B), then you will need to explicitly pass the reference to the corresponding SqlSessionFactory bean into each MapperScannerConfigurer bean. Having the configuration in place, we can proceed to the next step, which is to define the SQL mapping in MyBatis between queries and the POJO model.
Considerations When Using MyBatis
As you can see from the sampl
剩余内容已隐藏,支付完成后下载完整资料
在Spring中使用MyBatis
Clarence Ho
在Spring中开始使用MyBatis
在本节中,我们将介绍如何设置MyBatis来与Spring一起工作。首先我们将简要介绍MyBatis,然后我们将演示如何在STS中创建一个与MyBatis一起工作的项目。此外,我们将介绍将在本章中用于实施样本的数据模型。最后,我们将讨论使用MyBatis所需的Spring配置。
MyBatis介绍
iBATIS是MyBatis的老名字,于2001年由Clinton Begin创建,并于2004年捐赠给Apache软件基金会(ASF)。基于它的SQL映射重点,简单的基础架构和易于理解的映射定义对Java OO模型的SQL查询,iBATIS迅速普及并成为Java开发人员社区中最常用的数据访问库之一。在ASF工作六年后,iBATIS开发团队意识到开源世界中的数据访问技术发生了巨大变化。因此,该团队决定在图书馆中引入许多重大变化。从版本3开始,该项目也将其名称更改为MyBatis,离开ASF,并成为独立的开源框架。ASF的iBATIS项目也退休了。在撰写本文时,MyBatis的当前版本是3.0.6。在Spring 3.0之前,Spring对iBATIS版本2提供了开箱即用的支持。然而,由于将IBATIS 2与MyBatis 3与Spring框架的巨大变化同步困难,Spring的开发团队决定放弃内置的开发工具,支持MyBatis。为了应对这种情况,MyBatis团队开始了MyBatis Spring集成项目(称为mybatis-spring)。在撰写本文时,当前版本的mybatis-spring是1.0.2。
配置MyBatis SqlSessionFactory和MapperScannerConfigurer
MyBatis的核心概念围绕着从SqlSessionFactory接口(包org.apache.ibatis.session下)获得的SqlSession接口(在org.apache.ibatis.session包下)。SqlSession和SqlSessionFactory接口都属于mybatis-3.0.6库。为了在Spring中配置工厂,我们使用属于mybatis-spring模块(mybatisspring-1.0.2库)的SqlSessionFactoryBean类(在org.mybatis.spring包下)。MyBatis中另一个重要的概念是映射器接口,它是简单的Java接口类,将由MyBatis处理,用于在SQL查询和域对象属性之间映射配置。映射可以在XML文件(与接口类名称相同)中定义,也可以在映射器接口中定义。mybatis-spring模块提供了另一个类,称为MapperScannerConfigurer(在org.mybatis.spring.mapper包下),它支持一种方便的方式来指示MyBatis扫描映射器接口类并将它们注册为MapperFactoryBean(在org.mybatis包下.spring.mapper并且属于mybatisspring模块),它可以将MyBatis映射器接口注入到其他Spring bean中。映射器接口将在后面详细讨论,您将看到它如何帮助简化数据库操作的开发,而无需直接与SqlSession接口进行交互。清单11-4显示了Spring应用程序上下文配置(app-context.xml)。
Listing 11-4. MyBatis的Spring配置
大部分的bean你应该都很熟悉;让我们把注意力集中在bean sqlSessionFactory和org.mybatis.spring.mapper.MapperScannerConfigurer上。sqlSessionFactory由mybatis-spring提供的类org.mybatis.spring.SqlSessionFactoryBean实现。正如您所预料的那样,工厂需要数据源bean。而且,属性类型AliasesPackage定义了存储MyBatis在执行从SQL结果集到POJO的映射时应该查找的域对象的包。它与Hibernate和JPA中映射的实体类相似。请注意,在配置中,同一个dataSource bean的实例被注入Spring的transactionManager bean和MyBatis的sqlSessionFactory bean。这是因为SqlSessionFactoryBean类是专门为了利用Spring中现有的DataSourceTransactionManager而设计的。一旦配置了Spring事务管理器,您就可以像平常一样配置Spring中的事务。支持@Transactional注释和AOP风格的配置。mybatis-spring模块一旦建立就会透明地管理交易。您只需确保将同一个dataSource实例注入到transactionManager和SqlSessionFactory bean中。org.mybatis.spring.mapper.MapperScannerConfigurer类也由mybatisspring模块提供。该类接受程序包名称,MyBatis将从该名称扫描映射程序接口。此外,MapperScannerConfigurer bean还需要注入一个SqlSessionFactory实例。如果ApplicationContext中只有一个SqlSessionFactory bean,它将被自动装入到MapperScannerConfigurer bean中。但是,如果您的应用程序包含多个SqlSessionFactory bean(例如,在多数据源应用程序中,您有用于数据源A的sqlSessionFactoryA bean和用于数据源B的另一个sqlSessionFactoryB bean),那么您将需要显式地将引用传递给相应的将SqlSessionFactory bean放入每个MapperScannerConfigurer bean中。完成配置后,我们可以继续下一步,即在查询和POJO模型之间的MyBatis中定义SQL映射。
使用MyBatis时的注意事项
从本章介绍的示例中可以看到,MyBatis是一个非常好的数据访问库,专注于SQL映射。在使用MyBatis开发应用程序时,大多数时候您将专注于结果地图的设计和映射以及各种选择,插入,更新和删除操作。在映射方面,MyBatis提供了一组丰富的标签,可以帮助您构建动态和复杂的查询并轻松映射到相应的域对象。正如您从样本中看到的,我们不需要直接与SqlSession界面进行交互。大多数时间,映射器接口将能够满足您的需求。但是,如果您愿意,您可以选择与SqlSession接口进行交互,并以编程方式构建查询。总之,如果您希望对提交给数据库的SQL语句有更多的控制权,并希望灵活而简单的方法来定义结果集到域对象的映射,MyBatis提供了比JDBC更优雅的解决方案。但是,使用MyBatis时,也需要考虑一些因素。例如,您需要实现记录锁定机制(例如,用于乐观锁定的VERSION列)并手动保留历史记录版本。另外,如果数据模型频繁更改,则需要查看所有将受更改影响的查询。就持续维护而言,使用MyBatis的努力将高于使用诸如Hibernate和JPA等ORM库。
在示例应用程序中使用MyBatis
在本节中,我们将讨论本章中与我们将开发的示例应用程序相关的主题。主题包括用于各种数据库操作的后端数据库和MyBatis实现。我们还将强调我们如何采用MyBatis中的功能来帮助我们简化数据访问逻辑。最后,我们将讨论如何跟踪基本审计信息以及如何实现实体版本控制功能,即使MyBatis不提供对这些功能的开箱即用支持。
数据库后端
对于数据库后端,将使用带有H2的JDBC嵌入式数据库。但是,脚本(数据库创建脚本,初始数据填充脚本)也将被设计为与MySQL兼容。所以,如果需要,应用程序可以使用MySQL作为后端数据库。对于Spring的ApplicationContext配置,我们将简单地使用标签声明一个嵌入式数据库。两个SQL脚本将被开发。一个用于创建数据库(称为schema.sql),另一个用于初始数据(例如,初始用户,类别和子类别,示例博客条目等)population(称为initial-data.sql)。
使用MyBatis实现持久层
如第3章所述,对于持久层,将提供两种不同的实现。一个将使用JPA,而另一个将使用MyBatis。对于MyBatis实现,我们将使用MyBatis 3.更具体地说,MyBatis映射器接口和XML风格的映射配置将被采用。所有的实体类将被放置在包com.apress.prospring3.springblog.domain下(实际上JPA和MyBatis将使用同一组域对象进行映射)。另外,所有MyBatis映射器接口(例如,支持Entry实体类的EntryMapper接口)都将放在com.apress.prospring3.springblog.persistence包下。此外,为了支持在Web应用程序前端浏览博客发布条目,还将实施分页支持。在数据库操作方面,查询是最复杂的部分,因为在前端用户可以选择通过发布日期,类别和子类别等来过滤发布条目。为了满足这个要求,我们将使用MyBatis的动态SQL功能。
审计和实体版本控制
在示例应用程序中,所有博客发布条目和评论都将启用审核功能。为了记录博客发布和评论的基本审计信息(由创建日期,最后修改日期,上次修改日期)以及保存每条记录的版本,我们将利用MyBatis的插件机制来拦截相应地更新操作并填充基本审计信息(由创建日期,最后修改日期,最后修改日期创建)。另一方面,preupdate记录的副本将被保存到历史记录表中。有关详细信息,请参阅第21章中的“MyBatis服务实现”部分。
剩余内容已隐藏,支付完成后下载完整资料
资料编号:[23370],资料为PDF文档或Word文档,PDF文档可免费转换为Word
课题毕业论文、外文翻译、任务书、文献综述、开题报告、程序设计、图纸设计等资料可联系客服协助查找。