javaweb advanced study mybatis

--- --- restore content begins

In the native browser - server architecture, using mvc hierarchical thinking, our whole program is divided into the control layer, the business layer, persistence layer three

Request control layer (the servlet) is responsible for receiving a request, transmitted from the user's browser processes (calls business layer) and the response back to the browser,

Service layer (service) is responsible for handling business, call dao layer, and the results returned to the control level

Persistence Layer (dao) is responsible for writing sql statement, the database connection (JDBC), execute and get results in the database and return to the business layer

 

This is very troublesome, native code is not easy to modify, and jdbc this set of processes is dead, how can they be a great waste time on these repetitive meaningless work?

Just think, when writing function, met a lot of duplicate code, what is the first thing should be? Among other things, at least for the main process clear, things should be packaged into a repeat method, when used directly call.

On the server side, it was also made such operation is no longer just packaged as a method. Instead dubbed xml file

Have to mention here xml, when the line on the program, modify the source code is a hassle sometimes need to shut down and restart the server project, the redeployment of the source code. The price is still very large. And with xml,

We can write the properties and methods in xml (profile), without shutting down the server can also be modified to achieve the purpose of the function.

Here, mybatis.xml we are about to use is such a configuration file.

 

Use mybatis embodiment, in addition to the native code dao layer, you need only create a packet mapper, and the corresponding .xml write interface (such as UserMapper and UserMapper.xml) within the package, the interface to write abstract methods UserMapper

UserMapper.xml to write configuration, id write the name of the method to be executed, CRUD has a corresponding label.

It was originally a call to the business layer dao layer, there is no layer dao how to do?

Then we introduced SqlSession object, creating SqlSession objects in the business layer, create a dynamic proxy, use the proxy method to call the additions and deletions to change search returns results and returns results to the control layer, response.

If you need to sql parameters, then how to deal with it? (Basic types here refers to non-human writing class)

A single parameter, the base type, only needed for the parameters corresponding to # sql statement corresponding to a tag {0}, the first parameter represents

2 single parameter reference type, write-only # {} key or attribute names corresponding to the position indicated in

Three pairs of parameters, basic types, # {0}, {# 1}

Four pairs of parameter reference type, # {param1. Keys} # {param2.} Key name

5 pairs of a reference basic parameters, # {param1} # {param2.} Key name

Such a dynamically generated by mybatis dao layer on the production well

 

Guess you like

Origin www.cnblogs.com/waibangma/p/11356115.html