MyBatis- Profiles tab review

1.MyBatis Global Configuration

The following labels are in the parent tag Configuration

 

1. <Properties Resource = " jdbc.conf " > </ Properties> 
  introducing external file 

  . 1 .resource: introducing path from the class (the spring only need only add: CLASSPATH) 

  2 .url: introducing path from the disk or network path

 

 

2 . <-! Settings It is extremely important to adjust the settings in MyBatis , he will change the runtime behavior of MyBatis -> 
  <Settings>
  <Setting name = "mapUnderscoreToCamelCase" value = "to true" />
  </ Settings>

MyBatis can be provided in relevant official, here only part of the interception

 

 

 

 

 

3 <! - type aliases for common types (JavaBean) aliases -> 
    <typeAliases> 
        <- typeAlias:! JavaBean is a starting alias, the default is the class name (case insensitive), and you can specify your own -> 
        <typeAlias of the type = " com.indi.bean.Employee " alias = " Emp " /> 
        <- batch aliases;! name = " specifies the package name, the default alias is the name of the class " -> 
        <Package Penalty for name = " com.indi.bean " /> 
    </ typeAliases> 

If you think of an alias in a batch of time, marked the solution using the @Alias to class, but still recommend using the full name of the class
4. <-! Environment id specified environment, using the default Environments specified id, which environments, switching between multiple environments -> 
  <Environments default = " Development " >   <id = Environment " Development " >   </ Environment> </ Environments>
5 .Configuration The label also has the order from top to bottom, one place 
from top to bottom, followed by: the Properties, Setting, typeAliases, typeHandlers, objectFactory, objectWrapperFactory,
reflectorFactory, plugins, Environments, databaseidProvider, mappers
6 . <-! The mybatis to consider the transplant database, for example, can be applied to select a property databaseid specify the database name -> 
  <databaseIdProvider of the type = " DB_VENDOR " > 
      <- name:! Database vendor identification, value: to It represents taking a useful name -> 
      <Property name = " the MySQL " value = " MySQL " /> 
      <Property name = " the Oracle " value = " ORCL " /> 
  </ databaseIdProvider>
7 . <Mappers> 
      <-! Resource: looking at the class path sql mapping file
            class : direct reference to the interface to the full class name, but the xml placed under dao and interfaces with directory and file name, and the name of the interface consistent with 
          the need to xml file into the directory in the interface, and a class or built in the resource directory path as packet
url: reference network and local file
-> <Mapper resource = " MyMapper.xml " />
  <- if the file is! for a long time, one mapper registered too much trouble, direct scanning a package, this applies especially to the interface and file names in a
      directory that interface corresponds resource files in the package under the src folder under the mapping files in the same package - ->
   <Package name = "com.indi" /> </ by mappers>


class of the second usage, do not write the mapping file, the corresponding tag label directly on the interface, and then point the full class name of the class interface (examples below )

public interface MyMapper {

  @Insert ( "INSERT INTO t_employee values (ID # {}, {#} EmpName, Gender # {},#{email})")

  void insertEmp(Employee emp);
}

Small Summary: annotations and xml combined with simple annotations with xml complex, because of the complex generally do not change

 

 

2. Mapping Configuration

1 Get increment property

<! - let MyBatis automatically will increment id Employee object is assigned incoming to insert the id attribute
useGeneratedKeys = "true": native jdbc get increment primary keys method
keyProperty = "id": will just auto-incremented id to which the package properties ->
<INSERT ID = "insertEmp2" useGeneratedKeys = "to true" the keyProperty = "ID">
  INSERT INTO t_employee (EmpName, Gender, In Email) values (EmpName # {}, {Gender #}, {# In Email })
</ INSERT>

 

2. Obtain the growth of non-primary key from the above mentioned id 
<-! Public int - insertEmp3 (the Employee employeeid)> <INSERT the above mentioned id = " insertEmp3 " > <-! Query primary key the Order = " the BEFORE " : to run a sql statement before the carrier core found sql query id, the id of the found one attribute assigned to a JavaBean -> <= the selectKey Order " the BEFORE " the resultType = " Integer " the keyProperty = " id " > SELECT max (id) + . 1 from t_employee </ selectKey> </insert>

 

3 . The method of obtaining a plurality of interface problems values of parameters 
  1 a single parameter:
   basic types: Value: {#} just write
    incoming POJOs: property values # {name} POJO
  plurality of 2 parameters:
    public getEmpByIdAndEmpName the Employee (Integer id, String empName)
    values: # {} is invalid parameter names
    available: 0,1 (parameter index) or param1, param2 (paramN of several parameters), the actual identification parameters [0,1, param1, param2]
    reason: simply pass the plurality of parameters: mybati these parameters will be automatically packaged in a map in which:
      used when the package is first of several key parameters and the parameter index indicates
      Map <String, Object> map the HashMap new new = <> ();
      map.put ( ". 1", the value passed); map.put ( "2", the value passed)
      #} {Key is the value from the map
  3, @ Param : Specifies the key parameters, named parameters, we also recommend doing so in the future;
    we can tell mybatis, package parameter map when Freeze, we specify the use of key
    examples: the interface method:

    public the Employee getEmpByIdAndEmpName (@Param ( "the above mentioned id ") Integer id, @ Param ( " empName ")String empName)
  4. Incoming map, we own package map, a plurality of parameters to be used to encapsulate, values: # {key} 

extended: a plurality of parameters, automatic packaging Map
Method (@Param ( "ID") ID Integer, String empName, the Employee Employee);
Integer ID -> ID # {}
String empName -> {#} param2
the Employee Employee in the email -> # {param3.email}

 

4. The difference between # and $ are 
actually MyBatis, the two values ways:
# {attribute name}: a parameter pre-compiled, the location parameters are used? Alternatively, parameters are precompiled into the later, safety, no sql injection
$ {name} properties: not precompiled parameters, but direct access to the value, and then fight sql statement string unsafe
  $ usage scenarios, for example, I want to query the current table name uncertain, dynamic parameters from the get, then you can use the $, then fight to get direct string
sql statement, only the parameter position is to support pre-compiled

 

5 . On parameters 
# {key} may be provided when the value of some rules:
  ID # = {ID, the INT} = jdbcType
  the javaType, jdbcType, MODE, numericScale, The resultMap, typeHandler
  only jdbcType it may be desirable to be assigned
    a default do not specify jdbcType: mysq no problem, oracle no problem
    in case the incoming data is null, mysql insert null no problem, oracle do not know in the end is what kind of null

 

6 . Returns a collection 
<-! Public List <the Employee> getAllEmps () ->
<-! The resultType = "" If the return set, write elements inside the collection of class ->
<SELECT ID = " getAllEmps "resultType =" com.indi.bean.Employee "> </ the SELECT>

7 <-.! a query returns the Map ->
<-! public the Map <String, Object> getEmpByIdReturnMap (the above mentioned id Integer) ->
<SELECT ID = "getEmpByIdReturnMap" the resultType = "Map">
  SELECT * wHERE ID from t_employee ID = # {}
</ SELECT>

. 8 <- query a plurality of return map;.! case where a plurality of queries, a set of which is written element type ->
/ **
* @MapKey annotation used, so that the package id field as the map key, value record which is opaque object
* /
@MapKey ( "id")
public the map <Integer, the Employee> getAllEmpsReturnMap ( )

<-! public the Map <Integer,Employee> getAllEmpsReturnMap()->
<select id="getAllEmpsReturnMap" resultType="com.indi.bean.Employee">
  select * from t_employee
</select>

 

Guess you like

Origin www.cnblogs.com/ianhuafeng/p/12455029.html
Tab
Tab
Tab
Tab