How to use Ibatis to handle complex object data relationships
IBatis is an Open-source object-relational mapper whose job is to map objects to SQL statements. Unlike other O/R Mapping frameworks, IBatis developers need to write and maintain SQL statements themselves, which brings a lot of flexibility to development, but also brings a great deal of complexity and effort. In a database, common object relationships are: 1 pairs of 1,1, many-to-many, single table mapping, multiple table mapping, single primary key, multiple primary key and object primary key, and so on. When using Ibatis to process data maps, it is necessary to write out different SQL statements and implementations of the SQL map, especially when complex relationships and complex primary keys are written in a large number of complex SQL. This article will detail how to use Ibatis to deal with the complex data object relationship in the database, according to the classification of the data relation, give the concrete Realization code template and explanation. The reader can learn how ibatis can handle complex relationships, and only the templates listed in this article need to be applied in the process of writing code, which can greatly simplify development and speed up development. The reader of this article needs to have some basic experience with Ibatis.
Ibatis Basic Introduction
The Open source project Ibatis, which originated in 2001, is a Java-based persistence layer framework. Unlike the Hibernate, TopLink and other persistence frameworks, Ibatis is a "semi-automatic" ORM implementation. Instead of providing a more complete encapsulation of the database structure, Ibatis provides a set of mapping mechanisms from POJO to database tables. This makes the development of Ibatis, the need to manually write SQL to mention the database and the mapping between the class object, which provides a lot of flexibility for development, while greatly increasing the workload of development. The main features of Ibatis are as follows:
1. Simplicity
Ibatis is a very simple and easy-to-use toolkit. This simplicity is not only reflected in the lightweight size of the development library, but IBATIS is also very simple for Java developers. Because it is very much like the way JDBC works without writing so many code, IBATIS is the JDBC code described in XML. IBATIS is also very easy to understand for database administrators and SQL programmers. The IBATIS configuration file is readable by almost everyone, as long as he has experience in SQL programming.
2. Efficiency and performance
IBATIS is configured and used in a simple way, and its performance is comparable to that of JDBC. By writing SQL queries directly, developers can directly control SQL statements to optimize execution efficiency, or even invoke stored procedures, providing developers with huge SQL optimization space.
3. Code Separation
In IBATIS, SQL statements are largely separate from the source code of the application, and SQL programmers can write it in the way that SQL did, without worrying about the SQL string connection. IBATIS provides sufficient freedom for anyone to develop, observe, and modify SQL statements executed in the database.
4. Portability
The IBATIS is portable. Because of its relatively simple design, it can be implemented on any platform in almost any language. IBATIS supports 3 of the most popular development platforms: Java, Ruby, and Microsoft. NET C #.
Common Object Mapping and implementation
Because in project development using Ibatis, you need to write the implementation SQL code based on the relationship between the table structure of the database and the Java class, and different relationships will correspond to different SQL implementations. These SQL code will be placed with other mapping information in the Ibatis SQL mapping file. The following is a detailed analysis of each of these mappings and an implementation example.
Single-Table object binding and multiple table objects binding
The simplest relationship in Ibatis is the binding of an object to a single table, which is also the simplest mapping relationship between a class and a database. In the absence of any foreign key relationships, ibatis the fields in the class directly to the columns of the table:
Listing 1. Implementation of a single object table binding
<typeAlias alias="Bank" type="com.example.domain.Bank"/>
<resultMap id="getBankResult" class="Bank">
<result property="id" column="B_ID"/>
<result property="name" column="B_NAME"/>
</resultMap>
<select id="getBankById" resultMap="getBankResult" parameterClass="string"/>
SELECT `BANKL`.`BANKID` AS `B_ID`, `BANKL`.`NAME` AS `B_NAME`
FROM `BANKL`
WHERE `BANKID` = #id#
</select/>