Mybatis entry - additions and deletions to the basic way of investigation, CRUD mapper dynamic proxy mode, the type of converter

 

A basic way of additions and deletions to the investigation:
1.mybatis convention: parameterType input parameters and output parameters resulrType in the form of only one.
2. If the input / output parameters: a simple type (plus eight basic types String) may be used any of the placeholders, # {xxx};
                                If the object type, the property of the object must be, attribute name # {}.
3. Output parameter: if the return value is an object type (such as the Person), regardless of whether one or a plurality of return, are written in resultType org.lanqiao.entity.Person, i.e. resultType = "org.lanqiao.entity.Person".
4. Note: a. If using JDBC commit transaction schemes require manual lifting, i.e. session.commit ();.
                    b. All tags <select>. <update> and the like, must have sql statement, but optional parameters sql
                        There sql parameters: session.insert (statement, parameter values);
 
CRUD two .mapper dynamic proxy mode (MyBatis interface development):
Principles: convention over configuration
Hard-coded: abc.java
                                    Configuration conf=new Configuration();
                                    conf.setName("myProject");
Configuration: abc.xml
                                <name>myProject</name>
Conventions: The default value is myProject
 
Specific steps to achieve:
1. Basic environment: mybatis.jar / ojdbc.jar / conf.xml / mapper.xml
2. (difference) prescribed goals: Statement omitted, i.e., by convention, to be positioned directly sql statement.
    . A interfaces, interface methods must follow the following conventions:
          1) The method of the same name and the id value mapper.xml tag file;
          2) consistent with the type of input parameters parameterType mapper.xml method and file labels;
          Return value consistent resultType mapper.xml file type tag 3) of the process;
In addition to these conventions, to implement SQL label correspondence interface methods and Mapper.xml in, you also need the following point:
    1) namespace value is the full name of the class interface (Interface -mapper.xml one correspondence)
During the matching process agreed :()
1. Locate the file according to the interface name mapper.xml
2. Find (id = sql method name tag) sql tag mapper.xml file name of the method according to the interface
Habit: SQL mapping file (mapper.xml) and interfaces in the same package (note modify the path load mapper.xml of conf.xml file)
代码:PersonMappe personMapper=session.getMapper(PersonMapper.class);
     personMapper. Methods
 
optimization:
1. The configuration information may be placed in a separate file db.properties then introduced into the dynamic
db.properties;
    a = v
<properties resource="db.properties"/>
After introduction of the use of $ {key}
2.Mybaites global parameters
    <settings>
            <setting/>
    <settings/>
3. Alias ​​:( capitalization does not matter when you define an alias)
    a. providing a single alias
    b. Set up the bulk alias
In addition to custom alias outside, MyBatis also built some common aliases like.
 
Three type converter:
1.MyBatis comes with some common types of processors
    int——number
2. Custom Types processor MyBatis
    Java-- database (jdbc type)
Custom type converter (boolean-number) steps of:
. A converter created: the need to implement the interface TypeHandler
        To achieve the conversion has two options: (1) interface to implement the interface TypeHandler
                                                 (2) 继承 BaseTypeHandler
b. Configure conf.xml
 
Note: You must be capitalized when setting Integer.
 
resultMap can achieve two functions:
1. Type Conversion
2. Properties - mapping between fields
 

Guess you like

Origin www.cnblogs.com/ghlz/p/12210092.html