mybatis learning framework - Profile

FIG configuration, the order can not be changed

(There are documents connected to the bottom, super-detailed, a little do not want to carry)

Generally speaking about the popular bar

1.Properties (property) 

You can connect to the database configuration information in internal label. Common references external profile information through the property

 2.typeAliases (type alias) 

Type an alias for Java type is set a short name. And only XML configuration related to the meaning of existence is only used to reduce redundancy fully qualified class name.

In two ways, one with a single alias, alias wrap 2 for the whole

In addition, mybatis establish the appropriate type aliases for many common Java types. They are not case-sensitive, detailed look at the document

3. Environment Configuration (environments)

MyBatis can configure multiple environments. This can help you map correspond to a variety of SQL databases.
For example, you want to develop, test, publish product configurations different environments. and many more.
However, it is worth noting that we: Although you can configure multiple environments, but you can only choose one of SqlsessionFactory instance.
For example: id attribute with the identified environment, and the default specified environments

To configure the environment inside

Transaction Manager (transactionManager): There are two types of transaction manager with MyBatis (ie type = "[JDBC | MANAGED]")

Data source (dataSource): There are three built-in data source types (ie type = "[UNPOOLED | POOLED | JNDI]"):

Specifically to see the document (bottom connection)

4.Mappers (mapper) 

Since MyBatis behavior has been configured by the above configuration elements, we're ready to define our mapped SQL statements.

But first we need to tell MyBatis where to find them.

Java automatically find in this area does not provide a good way, so the best way is to tell MyBatis where to find the map file.

You can use the resource class path relative to a reference, or a fully qualified resource locator (including  file: ///  of the URL), the class and package names, or the like.

Specifically to see the documents

These configuration will tell MyBatis where to go to find the mapping file, the rest of the details should be for each SQL mapping file, that is, then we have to discuss.

Individual configuration


        // localhost: 8080 / mybatisserver / demo1Servlet 
                protocol host port URI of the 

            URI of the: Uniform Resource Identifier Uniform Resource Identifier. It is in the application can locate a single resource. 
    -> 
    <the Properties Resource = " db.properties " > 
    ! <- <the Properties url = " File: /// D: \ the Java \ code \ the mybatis \ mybatis_para_res_config \ src \ main \ Resources / db.properties " > - -> 
        <-! 
      <Property name = " Driver " value = " com.mysql.jdbc.Driver " > </ Property> <Property name = " URL "JDBC: MySQL: // localhost: 3306 / Cong " > </ Property> <Property name = " username " value = " the root " > </ Property> <Property name = " password " value = " 123456 " > </ Property >
     -> </ properties> <-! typeAliases configuration using an alias, it is only in the configuration class pojo alias -> <typeAliases> <-! typeAlias .type to configure an alias attribute specifies the full entity class .alias qualified class name attribute specifies the alias, the alias and then when the designated case sensitive <typeAlias type = " com.cong.pojo.Account " alias="account"> </ typeAlias> -> <-! alias used to specify the configuration package, when specified, the entity class will alias register in the packet, and the class name is an alias, case insensitive -> < name = Package " com.cong.pojo " > </ Package> </ typeAliases> <-! If there are disposed a plurality of ambient environments, such as mysql, Oracle, etc. -> <environments default = " MySQL " > <! - - mysql the environment configuration -> <environment the above mentioned id = " mysql " > <-! configuration transaction management -> <transactionManager of the type = " JDBC " > </ transactionManager> <!- Configuration Data Source -> <type the dataSource = " the POOLED"> <property name="driver" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </dataSource> </ Environment> </ Environments> <-! location map configuration file -> <by mappers> <-! <Resource Mapper = " COM / Cong / Mapper / AccountMapper.xml " > </ Mapper> -> <-! <Mapper url = " File: /// D: \ the Java \ code \ the mybatis \ mybatis_para_res_config \ src \ main \ Resources \ COM \ Cong \ Mapper \ AccountMapper.xml " > </ Mapper> -> < ! - the following two AccountMapper.xml need AccountMapper.class the same directory structure -> <-! <Mapper class = " com.cong.mapper.AccountMapper " > </ Mapper> -> <! - - package tag is used to specify packet dao interface resides,When writing becomes unnecessary to specify a resource mapper and a class or -> <name = Package"com.cong.mapper"></package> </mappers> </configuration>

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/cong
jdbc.username=root
jdbc.password=123456

 

XML mapping file

Where the document pages: http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#Result_Maps

About AccountMapper.xml in the namespace namespace, documents say

In previous versions of MyBatis, the namespaces (Namespaces) role is not large, is optional. But now, with more and more important namespace, you must specify the namespace.

The role of namespaces has two, one is to use longer names to be fully qualified to isolate different statements, but also implements the interface to bind you see above. Even if you think being less than the interface binding, you should also follow the provisions here in case you change your mind someday. In the long run, as long as the namespace placed in an appropriate Java package naming in space, your code will become more tidy, but also help you more easily use MyBatis.

Name resolution: To reduce the amount of input, MyBatis configuration elements of all the names (including statements, result maps, cache, etc.) uses the following naming parsing rules.

  • The fully qualified name (such as "com.mypackage.MyMapper.selectAllThings) will be directly used to find and use.
  • Short name (such as "selectAllThings") if a globally unique can be used as a separate reference. If not the only, with the same name (such as "com.foo.selectAllThings" and "com.bar.selectAllThings") of two or more, it will have a "short name is not unique" error when using this case you must use the fully qualified name.

 

 

The real power lies in its MyBatis mapping statement, which is its magic lies. Because of its extremely powerful, XML mapper file becomes relatively simple. If you take it compare with JDBC code with the same function, you will immediately find save nearly 95% of the code. MyBatis was built to focus on SQL, to reduce the hassle for you as possible.

Note that there are major

1. The type of arguments passed

2. The output of the Type

3. Insert inserting data of data returned id

Some content on the blog post mentioned: https://www.cnblogs.com/ccoonngg/p/11300871.html

 

 Reference: https://blog.csdn.net/weixin_40254133/article/details/80615597

Documentation: http://www.mybatis.org/mybatis-3/zh/configuration.html#properties

Guess you like

Origin www.cnblogs.com/ccoonngg/p/11300906.html
Recommended