MyBatis learning-SqlMapConfig. xml configuration file, sqlmapconfig. xml configuration
SqlMapConfig. xml is the global configuration file of MyBatis. As shown in the previous article. xml mainly configures data sources, transactions, and ing files. you can also configure a lot of information in xml, such:
1. properties)
2. settings (global configuration parameters)
3. typeAliases (type alias)
4. typeHandlers (type processor)
5. objectFactory (Object factory)
6. plugins)
7. environments (Environment set attribute object)
7.1. environment (environment sub-attribute object)
7.2. transactionManager (Transaction Management)
7.3 dataSource (Data Source)
8. mappers ()
The database connection parameters are separately configured in the db. properties, only need to be in SqlMapConfig. load db in xml. properties attribute value in SqlMapConfig. in xml, you do not need to hard encode the database connection parameters. You can configure the database connection parameters only in db. properties, cause: it is convenient to manage the parameters in a unified manner. Other xml can reference this db. properties. In this way, you only need to modify the db. properties configuration file if multiple data sources are modified.
In classpath, the db. properties file is defined as follows:
1 jdbc.driver=com.mysql.jdbc.Driver2 jdbc.url=jdbc:mysql://localhost:3306/shop3 jdbc.username=root4 jdbc.password=
SqlMapConfig. xml reference:
1 <properties resource="db.properties"/> 2 <environments default="development"> 3 <environment id="development"> 4 <transactionManager type="JDBC"/> 5 <dataSource type="POOLED"> 6 <property name="driver" value="${jdbc.driver}"/> 7 <property name="url" value="${jdbc.url}"/> 8 <property name="username" value="${jdbc.username}"/> 9 <property name="password" value="${jdbc.password}"/>10 </dataSource>11 </environment>12 </environments>
Properties features:
MyBatis loads attributes in the following order:
1. attributes defined in the properties element are read first.
2. The properties loaded by resource or url in the properties element will be read, and the read attributes with the same name will be overwritten.
3. Finally, read the attribute passed by parameterType, which overwrites the read attribute of the same name.
Therefore, attributes passed through parameterType have the highest priority, followed by Attributes loaded by resources or URLs, and attributes defined in the properties element are the lowest priority.
Generally, do not add any attribute values to the properties element. Only the attribute values are defined in the properties file. Property names defined in the properties file must have some special characteristics, such as: XXXXX. XXXXX. XXXX.
The mybatis framework can adjust some running parameters during runtime. For example, enable Level 2 Cache and enable delayed loading. Global parameters will affect the running behavior of mybatis. For more information, see MyBatis documentation.
In mapper. xml, many statement are defined. statement requires parameterType to specify the input parameter type, and resultType to specify the ing type of the output result. If the full path of the input type is not convenient for development when the type is specified, you can define aliases for the type specified by parameterType or resultType, and define aliases in mapper. xml to facilitate development.
1. Default aliases supported by MyBatis:
|
Alias |
Ing type |
|
_ 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 |
2. Custom alias:
1 <typeAliases> 2 <! -- Single alias --> 3 <typeAlias type = "com. luchao. mybatis. first. po. User" alias = "user"/> 4 </typeAliases>
3. Batch aliases:
1 <typeAliases> 2 <! -- Batch alias definition: scans the classes in the entire package. aliases are class names (uppercase or lowercase letters are acceptable) --> 3 <package name = "com. luchao. mybatis. first. po "/> 4 </typeAliases>
- TypeHandlers (type processor)
Type processor is used for ing java and jdbc types, as follows:
1 <select id="findUserById" parameterType="int" resultType="user">2 select * from user where id = #{id}3 </select>
The Type processor provided by mybatis basically meets daily requirements and does not need to be defined separately.
Mybatis supports type processors:
|
Type Processor |
Java type |
JDBC Type |
|
BooleanTypeHandler |
Boolean, boolean |
Any compatible Boolean Value |
|
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 integer type |
|
LongTypeHandler |
Long, long |
Any compatible number or long integer |
|
FloatTypeHandler |
Float, float |
Any compatible numeric or single precision floating point type |
|
DoubleTypeHandler |
Double, double |
Any compatible numeric or Double Precision Floating Point Type |
|
BigDecimalTypeHandler |
BigDecimal |
Any compatible numeric or 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 |
Arbitrary |
Other or unspecified types |
|
EnumTypeHandler |
Enumeration type |
VARCHAR-any compatible string type that is stored as code (rather than indexed ). |
Mapper configuration methods:
1. <mapper resource = ""/>
Use resources relative to the class path, such as: <mapper resource = "sqlmap/User. xml"/>
2. <mapper url = ""/>
Use a fully qualified path, for example, <mapper url = "file: // D: \ workspace_spingmvc \ mybatis_01 \ config \ sqlmap \ User. xml"/>
3. <mapper class = ""/>
Define a single alias using the mapper Interface class path, for example, <mapper class = "com. luchao. mybatis. first. mapper. UserMapper"/>
Note: This method requires that the mapper Interface Name and mapper ing file name be the same and put in the same directory.
Loading a single ing file through the mapper interface follows some rules: the mapper Interface Class Name and mapper. xml ing file name must be consistent and in a directory
The premise of the preceding rule is that the mapper proxy method is used.
4. <package name = ""/>
Register All ER er Interfaces under the specified package, for example, <package name = "cn. itcast. mybatis. mapper"/>
Note: This method requires that the mapper Interface Name and mapper ing file name be the same and put in the same directory.
Load mapper in batches and specify the package name of the mapper interface. mybatis automatically scans all the mapper Interfaces under the package for loading. The ER interface class name and mapper must be specified. the xml ing file names must be consistent in a directory.
The premise of the preceding rule is that the mapper proxy method is used.