Reprint Please specify source: http://www.cnblogs.com/Joanna-Yan/p/6874672.html
It 's written in the front . Spring+springmvc+mybatis Deep Learning and Building (ii)--mybatis original DAO development and Mapper agent development
Global configuration file for MyBatis Sqlmapconfig.xml, configure the contents and order as follows:
Properties (Attributes)
Setting (Global configuration parameters)
Typealiases (class name alias)
Typehandlers (class name processor)
Objectfactory (Object Factory)
Plugins (plug-in)
Environments (Environment collection Property object)
Environment (Environment sub-Property object)
Transationmanager (transaction Management)
DataSource (data source)
Mappers (Mapper)
1.properties (properties)
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.
Define the Db.properties file under Classpath:
Jdbc.driver=com.mysql.jdbc.driverjdbc.url=jdbc\:mysql\://localhost\:3306/mybatisdemojdbc.username= rootjdbc.password=
Load the properties file in Sqlmapconfig.xml:
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE configurationpublic "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" > <Configuration> <!--Load Database file Db.properties - <PropertiesResource= "Db.properties"> <!--properties can be configured with some property names and property values, where the precedence load - <!--<property name= "Driver" value= ""/> - </Properties> <!--after integration with spring, the environments configuration will be abolished - <Environmentsdefault= "Development"> <EnvironmentID= "Development"> <!--with JDBC transaction management, transaction control is managed by MyBatis - <TransactionManagertype= "JDBC"/> <!--database connection pool, managed by MyBatis - <DataSourcetype= "Pooled"> < Propertyname= "Driver"value= "${jdbc.driver}"/> < Propertyname= "url"value= "${jdbc.url}"/> < Propertyname= "username"value= "${jdbc.username}"/> < Propertyname= "Password"value= "${jdbc.password}"/> </DataSource> </Environment> </Environments> </Configuration>
Properties Features:
Note: MyBatis will load the properties in the following order:
(1) attributes defined in the properties element are first read.
(2) The properties that are loaded with the Resourse or URL in the property element are then read, overwriting properties that have been read with the same name.
(3) Finally read the property passed by the ParameterType, which overrides the Read property with the same name.
So there may be a problem here: if there is a statement in Usermapper.xml named name,
There is also a parameter named name in Db.properties.
The select in the final usermapper.xml will read to Name=root instead of the value passed in by the user.
Suggestions:
Do not add any attribute values in the properties element, only the attribute values are defined in the property file.
It is necessary to define the attribute name in the properties file, such as: Xxx.xxx.xxx,jdbc.url,jbdc.username.
2.settings (Global parameter configuration)
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.
3.typeAliases (alias)Focus3.1 Requirements
In Mapper.xml, a lot of statement are defined, and statement needs to parametertype specify the type of input parameter, which requires Resulttype to specify the type of mapping for the output result.
If the type full path is entered when the type is specified, it is inconvenient to develop. You can define some aliases for the type specified by ParameterType or resulttype, which are defined by aliases in Mapper.xml for easy development.
3.2mybatis Default Support aliases
3.3 Custom alias 3.3.1 single alias definition
<!--- <typealiases> < type= "Joanna.yan.mybatis.entity.User" alias= "User"/> </typealiases>
Reference aliases:
<id= "Finduserbyid" parametertype= "int" resulttype = "User" > SELECT * from user where id=#{id}</Select>
3.3.2 Batch definition aliases (common)
<!-- definition of bulk aliases: - <typealiases> < Name = "joanna.yan.mybatis.entity"/><!-- -</ typealiases >
4.typeHandlers (Type processor)
Conversion of JBDC 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.
5.mappers (Map Configuration) 5.1 loading a single file via resource
<!--- <mappers> <!-- Load one mapping file at a time with the resource method -- <resource= " Mapper/usermapper.xml "/></mappers>
5.2 Loading a single mapper via the Mapper interface
<!--- <mappers> <!-- loading a single mapping profile via the Mapper interface follows certain specifications: The Mapper interface class name and the Mapper.xml mapping file name need to be consistent and in a directory; The above specification premise is: The use is Mapper agent method; - < class= "Joanna.yan.mybatis.mapper.UserMapper"/> </ mappers>
According to the specification above, Mapper.java and Mapper.xml are placed in a directory with the same name.
5.3 Bulk Load mapper (recommended)
< mappers > <!-- Bulk Load Map configuration file, MyBatis Automatic Scan package under the Mapper interface to load; following certain specifications: The Mapper interface class name and Mapper.xml mapping file name need to be consistent and in a directory; The above specification premise is: The use is Mapper agent method; - < name= "Joanna.yan.mybatis.mapper"/> </ Mappers>
If this article is helpful to you, please give me a reward!
Spring+springmvc+mybatis Deep Learning and Building (iii)--mybatis global profile parsing