Experience in the development of Ibatis (i) Ibatis and hibernate understanding in development

Source: Internet
Author: User

The bottom of the project is to use the Ibatis framework, these days are also used in this framework to write code, there are some simple understanding, the project brought over after the basic configuration has been configured, for example, some transactions, log control, in the development of the main use is to write SQL statements and familiar with Ibatis XML files provide a number of tags that are mostly used to handle inference, logic, and so that SQL can be dynamically assembled to become more flexible.

In the work of writing code Ibatis more than Hibernate, after you write the service layer will need some DAO layer, the DAO layer implementation, and then these in hibernate do not have to write their own, just write to the service layer can call hibernate provided object directly manipulate the data, Some methods of using Hibernate's own encapsulation to complete the database additions and deletions of the tea, hibernate is from the Java Entity class mapping to the database table, we call it to provide a method to complete the operation of the database, the SQL statement will be more call it the method of its own initiative to generate, Ibatis is to encapsulate the Java entity class to the SQL statement, run SQL to the data also need a process is also the Ibatis encapsulation implementation, compared to the developer writing code is not much less.

When to use which frame is better?

Ibatis is suitable for the need to add new functionality to a system, but the table structure can not be modified, because ibatis in the database operation details more flexible than hibernate, it is a framework for SQL statements and hibernate directly table-oriented structure, Suppose you want to change the table structure by a lot more places.

For high concurrency, very large data volume systems, the use of ibatis effect is better than hibernate, we can write their own optimized efficient SQL statements and stored procedures to solve the high data volume problem, from the current development of hibernate written by the Hsql statement is not easy to optimize, The more complex the relationship, the slower the efficiency will be, and of course nothing is immutable. Hibernate will also support native SQL statements, and some of its imperfections are intact.

In the maintenance of a system is also obviously easy to see, Ibatis occupies a clear advantage it writes SQL in the configuration file, maintenance if you need to change a query function, in the configuration file changes can be completed, assuming that Hibernate needs to modify the code and compile again.

The following is the Ibatis schematic diagram


The left side of the figure is the type of incoming parameters, the right is the return structure type, the middle is the configuration file information, it supports the basic data type (Int/string/boolean) and object, map and other types of data, it is more convenient to use.

Sqlmap-config.xml is equivalent to a hibernate.cfg.xml file, sqlmap.xml equivalent to a resource file, such as the following configuration information

Example of a query: a simple drop-down list box query

Select Onboarding Process

When the select tag is loaded, the option selected=true option is selected by default, and when the tag is selected, the Value property of option is assigned to the SELECT tag's values property, assuming that option's value is null , the <option> test </option> the two start and end tag values are assigned to select, and the implementation takes advantage of this assignment process, which is divided into 0, 1, and 23 values that represent three types of query criteria.

JSP pages such as the following

<span class= "STYLE1" > Please select Query method: <select name= "SelectType" value= "" ><option <%=selected==  " Default "? "Selected=true": "%> value=" 0 "> Press default Query </option><option  <%=selected==" name "? "Selected=true": "%> value=" 1 "> Query by Type </option><option  <%=selected==" CompanyName "? "Selected=true": "%> value=" 2 "> by company Inquiry </option></select> Please enter keyword:</span>


<!--Transaction Manager for a single JDBC DataSource DAO IoC--><bean id= "TransactionManager" class= "ORG.SPRINGF Ramework.jdbc.datasource.DataSourceTransactionManager "><property name=" datasource "><ref local=" DataSource "/></property></bean><!--Apache Database Connection Pool--><bean id=" DataSource " class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" ><property name= "Driverclassname" ><value>${db.driver}</value></property><property name= "url" ><value>${db.url} </value></property><property name= "username" ><value>${db.user}</value></ Property><property name= "Password" ><value>${db.password}</value></property></bean ><!--sqlmapclient for Daoibatisimpl--><bean id= "sqlmapclient" class= " Org.springframework.orm.ibatis.SqlMapClientFactoryBean "><property name=" Configlocation "><value> Classpath:sql-map-config.xml</value></property><property name= "DataSource" ><ref bean= "DataSource"/></property> <property name= "Lobhandler" ref= "Lobhandler"/> </bean> <bean id= "Lobhandler" class= "   Org.springframework.jdbc.support.lob.DefaultLobHandler "/>

Sqlmap.xml Code

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE sqlmap Public "-//ibatis.apache.org//dtd SQL Map 2.0//en" "Http://ibatis.apache.org/dtd/sql-map-2.dtd" > <!--note:auto-generated at 2006-7-6 16:05:43.--><sqlmap namespace= "ns" > <resultmap id= "orgInfoChildId "class=" com.zhjy.domain.Organization "> <result column=" id "property=" id "jdbctype=" VARCHAR "/> <result Column= "parent_id" property= "pid" jdbctype= "varchar"/> <result column= "name" property= "name" jdbctype= "varchar "/> <result column=" isparent "property=" isparent "jdbctype=" VARCHAR "/> <result column=" checked "proper Ty= "Checked" jdbctype= "varchar"/> <result column= "open" property= "open" jdbctype= "varchar"/> </resultmap ><!--start--><select id= "Ns.findcontacts_count" resultclass= "Java.lang.Integer" based on the left-hand tree query record statement parameterclass= "Java.util.Map" >select COUNT (ID) from Category,contactinfo,category_contactinfowhere category.id =category_contActinfo. category_id and Contactinfo.contactid=category_contactinfo. Contact_id<isequal prepend= "and" property= "SelectType" comparevalue= "1" ><isnotempty prepend= "" property= " Keyword ">name like '% $keyword $% ' </isnotempty></isequal><isequal prepend=" and "property=" SelectType "comparevalue=" 2 "><isnotempty prepend=" "property=" keyword ">companyname like '% $keyword $% ' </ Isnotempty></isequal><isequal prepend= "and" property= "SelectType" comparevalue= "0" ><isNotEmpty Prepend= "" property= "keyword" > (name like '% $keyword $% ' </isnotempty><isnotempty prepend= "or" property= " Keyword ">companyname like '% $keyword $% ') </isnotempty></isequal><isnotempty prepend=" and " property= "id" >id= #id:varchar#</isnotempty></select></sqlmap>

This is a dynamic loading query conditions, after assuming that the new query options corresponding to the modified configuration file can be, no need to modify the code inside the things, more flexible than I have.

Ibatis is still in use and familiar with ...







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.