MyBatis Global configuration file Sqlmapconfig.xml, the configuration is as follows:
Properties (Attributes)
Settings (Global configuration parameters)
Typealiases (Type alias)
Typehandlers (Type processor)
Objectfactory (Object Factory)
Plugins (plug-in)
Environments (Environment collection Property object)
Environment (Environment sub-Property object)
TransactionManager (transaction Management)
DataSource (data source)
Mappers (Mapper)
5.1 Properties Property
Demand:
To configure the database connection parameters separately in Db.properties, you only need to load the Db.properties property values in Sqlmapconfig.xml.
You do not need to hard-code database connection parameters in Sqlmapconfig.xml.
The database connection parameters are only configured in Db.properties, because it is convenient to manage the parameters uniformly, and other XML can refer to the db.properties.
Load the properties file in Sqlmapconfig.xml:
Properties Features:
Note: MyBatis will load the properties in the following order:
- Attributes defined in the properties element are first read.
- It then reads the properties of the resource or URL loaded in the property element, overwriting properties that have been read with the same name.
- Finally read the property passed by the ParameterType, which overrides the Read property with the same name.
Suggestions:
Do not add any attribute values in the properties element, only the attribute values are defined in the property file.
Defining attribute names in the properties file is specific, such as: XXXXX.XXXXX.XXXX
5.2 Settings Global parameter configuration
<!--global configuration parameters, when needed and set-
<!--<settings>
</settings>
The MyBatis framework can adjust some of the running parameters at run time.
For example: Turn on level Two cache, turn on lazy loading.
Global parameters will affect the running behavior of MyBatis.
5.3 typealiases (alias) key 5.3.1 Requirements
In Mapper.xml, defining a lot of statement,statement requires parametertype specifying the type of input parameter, the type of mapping that needs to be resulttype to specify the output result.
If you enter a type full path when specifying a type, it is inconvenient to develop, you can define some aliases for the type specified by ParameterType or Resulttype, and the alias definition in mapper.xml to facilitate development.
5.3.2 MyBatis Default Support aliases
Alias |
Types of mappings |
_byte |
Byte |
_long |
Long |
_short |
Short |
_int |
Int |
_integer |
Int |
_double |
Double |
_float |
Float |
_boolean |
Boolean |
String |
String |
Byte |
Byte |
Long |
Long |
Short |
Short |
Int |
Integer |
Integer |
Integer |
Double |
Double |
Float |
Float |
Boolean |
Boolean |
Date |
Date |
Decimal |
BigDecimal |
BigDecimal |
BigDecimal |
5.3.3 Custom alias 5.3.3.1 single alias definition
In Sqlmapconfig.xml:
<!--- <typealiases> <!-- define type for a single alias : path to type alias : alias-- < Type= "Cn.itcast.mybatis.po.User" alias= "User"/> </typealiases>
Reference aliases in Usermapper.xml:
<id= "Finduserbyusername" parametertype= "Java.lang.String" Resulttype = "User" > SELECT * from USER WHERE username like '%${value}% ' </SELECT>
5.3.3.2 Bulk alias definition
<!--- <typealiases> <!-- The bulk alias definition Specifies the package name, MyBatis automatically scans the PO class in the package, automatically defines the alias, and the alias is the class name (either uppercase or lowercase)- < Name = "Cn.itcast.mybatis.po"/></ Typealiases>
5.4 Typehandlers (Type processor)
The conversion of JDBC type and Java type is done through typehandlers in MyBatis.
Typically, MyBatis provides a type processor that meets daily needs and does not require customization.
MyBatis supports type processors:
Type processor |
Java Type |
JDBC Type |
Booleantypehandler |
Boolean,boolean |
Any of the compatible Boolean values |
Bytetypehandler |
Byte,byte |
Any compatible number or byte type |
Shorttypehandler |
Short,short |
Any compatible number or short integer |
Integertypehandler |
Integer,int |
Any compatible number and integral type |
Longtypehandler |
Long,long |
Any compatible number or long integer |
Floattypehandler |
Float,float |
Any compatible digital or single-precision floating-point type |
Doubletypehandler |
Double,double |
Any compatible digital or double-precision floating-point type |
Bigdecimaltypehandler |
BigDecimal |
Any compatible number or decimal decimal type |
Stringtypehandler |
String |
char and varchar types |
Clobtypehandler |
String |
CLOB and LongVarChar types |
Nstringtypehandler |
String |
nvarchar and nchar types |
Nclobtypehandler |
String |
NCLOB type |
Bytearraytypehandler |
Byte[] |
Any compatible byte stream type |
Blobtypehandler |
Byte[] |
Blob and LongVarBinary types |
Datetypehandler |
Date (Java.util) |
Timestamp type |
Dateonlytypehandler |
Date (Java.util) |
Date type |
Timeonlytypehandler |
Date (Java.util) |
Time Type |
Sqltimestamptypehandler |
Timestamp (java.sql) |
Timestamp type |
Sqldatetypehandler |
Date (java.sql) |
Date type |
Sqltimetypehandler |
Time (java.sql) |
Time Type |
Objecttypehandler |
Any |
Other or unspecified type |
Enumtypehandler |
Enumeration type |
varchar-any compatible string type as a code store (not an index). |
5.5 mappers (map configuration) 5.5.1 load a single mapping file via resource
< mappers > < resource= "Usermapper.xml"/> </mappers >
5.5.2 loading a single mapper via the Mapper interface
< mappers > <!-- loading a single mapping file via the Mapper interface follows some specifications: The Mapper interface class name and the Mapper.xml mapping file name need to be consistent, and the upper specification in one directory is the premise that the Mapper proxy method is used -- <class= " Cn.itcast.mybatis.mapper.UserMapper "/> </mappers>
According to the specification above, Mapper.java and Mapper.xml are placed in a directory with the same name.
5.5.3 Bulk Load mapper (recommended)
< mappers > <!-- Bulk Load Mapper Specifies the package name of the Mapper interface, MyBatis automatically scans all mapper interfaces underneath the package for loading Follow some specifications: You need to keep the Mapper interface class name and the Mapper.xml mapping file name consistent, and the upper specification in one directory is the premise that you are using the Mapper proxy method-- <name= "Cn.itcast.mybatis.mapper"/> </ mappers >
MyBatis Series -05-sqlmapconfig.xml Detailed