Java Data Persistence layer Framework Mybatis__java Data Persistence layer Framework

Source: Internet
Author: User

MyBatis's predecessor was IBatis. is a data persistence layer (ORM) framework.

The word IBATIS is derived from the combination of "Internet" and "Abatis", a Java-based persistence layer framework. The persistence layer framework provided by Ibatis includes SQL maps and Data Access Objects (DAO), along with a Jpetstore instance developed using this framework.

Overall, mybatis two things.

1. Establish a connection with the database according to the JDBC specification;

2. Realize the transformation between Java object and relational database by Annotaion/xml+java reflection technology.

MyBatis Technical document PPT sharing

------------------------------------------Split Line------------------------------------------

free download address in http://linux.linuxidc.com/

both the username and password are www.linuxidc.com

download Directory in /2014/8 months/3rd/java Data Persistence layer Framework MyBatis

Download method See http://www.linuxidc.com/Linux/2013-07/87684.htm

------------------------------------------Split Line------------------------------------------

Java Combat Applications: MyBatis to achieve a single table of additions and deletions to change http://www.linuxidc.com/Linux/2014-06/103456.htm

[Java] [MyBatis] Physical paging implementation http://www.linuxidc.com/Linux/2014-04/99889.htm

MyBatis Quick Start Tutorial http://www.linuxidc.com/Linux/2013-06/85762.htm

MyBatis's test http://www.linuxidc.com/Linux/2012-05/60863.htm for bulk data operations

Batch insert operation for list<object> object list in MyBatis http://www.linuxidc.com/Linux/2014-02/96916.htm

first, the principle of introduction

The application of MyBatis is centered around a sqlsessionfactory instance. Sqlsessionfactorybuilder creates sqlsessionfactory based on an XML mapping file.

Sqlsessionfactory by name can be thought of, can get a sqlsession through it. Sqlsession contains all the methods needed to execute SQL, and you can run the mapped SQL statement directly from the Sqlsession instance:

sqlsession session = Sqlsessionfactory.opensession ();
try {
Blog blog = session.selectone ("Org.mybatis.example.BlogMapper.selectBlog", 101);
finally {
Session.close ();
}

The above method is based on an older version of MyBatis, and in the latest version there is a clearer way to return a given SQL map by using a Java interface as an argument (e.g. Blogmapper.class).

sqlsession session = Sqlsessionfactory.opensession ();
try {
Blogmapper mapper = Session.getmapper (Blogmapper.class);
Blog blog = mapper.selectblog (101);
finally {
Session.close ();
}

When you see this, you might be curious about what the SQL statements that sqlsession and mapper classes actually execute. Let's take a look at the examples below.

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Mapper
Public "-//mybatis.org//dtd Mapper 3.0//en"
"Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace= "Org.mybatis.example.BlogMapper" >
<select id= "Selectblog" parametertype= "int" resulttype= "Blog" >
SELECT * from Blog where id = #{id}
</select>
</mapper>

This example is very simple and lightweight. You can define a number of SQL statements like this. This file defines a SQL statement called "Selectblog" in the Namespace "Org.mybatis.example.BlogMapper". This allows you to navigate to the SQL statement using an absolutely unique path, "Org.mybatis.example.BlogMapper.selectBlog".

As shown below:

Blog blog = (blog) session.selectone ("Org.mybatis.example.BlogMapper.selectBlog", 101);

Please note that this is an absolutely unique Java class invocation method how similar. This name maps directly to a namespace's mapping class, and a method that has a matching name, parameter, and return type to map a SELECT statement. This allows you to simply invoke the method that maps the interface, here is an example:

Blogmapper mapper = Session.getmapper (Blogmapper.class);
Blog blog = mapper.selectblog (101);

As you can see, the second method is simpler and does not need to return a value cast.

So far, we've learned mybatis how to map XML mapping files with Java classes to execute SQL statements, the meaning of the specific XML mapping file Please check the MyBatis official website, and this is not introduced.

For more information please continue to read the next page highlights : http://www.linuxidc.com/Linux/2014-08/105041p2.htm

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.