MyBatis the way to build and inject spring

Source: Internet
Author: User

MyBatis is actually a more focused framework for SQL statements, and his appearance is to make it easier for developers to manipulate databases.

Hibernate is more of a de-SQL than Hibernate, and although Hibernate can also be performed locally, Hibernate is more concerned with HQL writing and object mapping configuration,

As long as the configuration is done, Hibernate has encapsulated functions to help you execute the database, of course, Hibernate also relies on the production of SQL statements to implement the operational database, it is worth noting that

Hibernate can easily adapt to different types of databases, as long as the configuration changes can be made for different databases to produce the same SQL statements, hibernate difficulty may focus

Configuration issues for single-sided and several-to-several PO objects. So, to some extent, hibernate may have a higher threshold than mybatis.

Looking back, MyBatis relative to Hibernate "O/R", MyBatis is an ORM implementation of "SQL Mapping".

You only need to mybatis the object map file XML to write your own SQL statement, and also configure the parameters and return value type, then MyBatis can help you to save PO object to the database or

Map the data of the database to the object, then when using MyBatis, the main task is to write SQL, and then the details of some configuration rules.

Today we are building mybatis and, by the way, integrating into our spring project, the Spring project is a successful project built on Springmvc+hibernate's web application in my previous blog.

1mybatis of course, the response of the jar package, 3.2.7 is I download the jar package, of course, not the latest, the latest I looked for a long time, some because the jar download link stuck, some because the csdn downloaded points and C-currency ran out.

Is the jar package I downloaded.

Because our spring project is a Web project, it is good to throw it into Lib under WebContent, it can be done in the main function of application, or it can be run again in the Web server, it is very comfortable.

Then put in our configuration file under SRC, the configuration file is as follows:

The location of the configuration file is as follows:

You can see two parts, part about connecting to the database, and the other part about the PO configuration XML file.

Of course MyBatis also needs jdbc.driver, then the PO XML will be shown later.

What we need to do now is to set up the tables for the database, the creation of the Po class, and the problem of the PO corresponding XML configuration.

Let's look at the following:

So, now you can see that, with the definition of the parameter type, the definition of the value type, we call this SQL statement elsewhere

We can implement our database operations.

However, when performing this operation, we should first initialize the MyBatis configuration file to load the MyBatis.

This time we can do the following code:

Once we've initialized it, we can do what we want.

The new question is coming again, how do we incorporate this mybatis into our project?

Actually, there are two ways

1 because this is the spring project, we can add the Mybatisofspringutil class to the spring configuration file and then

Spring's context object Getbean method was obtained.

2 That is the way I use, from the above method can be seen sqlsessionfactory this variable is static, and implemented a singleton mode. Directly in the application you want to use

Place call Mybatisofspringutil.getsessionfactory () just fine. You do not need to inject this class into spring.

In fact, both methods are possible, one is to inject the spring project when initializing the MyBatis configuration file, one is the first call Mybatisofspringutil.getsessionfactory ()

To initialize the MyBatis configuration file.

When we initialize it, we can call it when we need it, and it means to add the MyBatis framework to our project.

My call is the way it is:

Of course, this code is the DAO layer code. With this code, the other layers can be called, from the service layer to the MVC controller layer.

Now let's see if we can run successfully.

Now I'm going to add a record to the database, and our SQL statement gets the object based on the primary key.

So the parameter is 1 when the database data can be found, other times can not query the database data.

Controller layer I determine whether the queried object is null to return a different page, respectively.

The controller layer code is:

The Coffe object is not NULL when the ID parameter is 1, and the other is null.

We'll see how the MyBatis works if we look at the page again.

We first visit: http://localhost:8080/springMvcOrm/anonymous.mvc?method=mybatisMethodTest&id=1

Then visit: http://localhost:8080/springMvcOrm/anonymous.mvc?method=mybatisMethodTest&id=6

OK, no problem. MyBatis the building was successful.

What do not understand can leave a message, I have the time will come back to reply, also welcome everyone to criticize correct.

See You Lala ...

MyBatis the way to build and inject spring

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.