Quick learn - MyBatis global profile

Chapter 3 MyBatis global profile

3.1 introduction to mybatis global profile

  1. The MyBatis configuration contains settings and properties that have a drastic effect on how MyBatis lives.

  2. The file structure is as follows:

Configuration configuration 
properties property
 Settings settings
 typeAliases type naming
 typeHandlers type processor
 objectFactory object factory
 plugins plug in
 environments environment 
Environment environment variable 
transactionManager transaction manager
 dataSource data source
 databaseIdProvider database vendor ID
 mappers mapper

3.2 properties

  1. It can be configured externally and dynamically, either in a typical Java property file or through the child elements of the properties element
     <property name="driver" value="com.mysql.jdbc.Driver" />
     <property name="url" 
             value="jdbc:mysql://localhost:3306/test_mybatis" />
     <property name="username" value="root" />
     <property name="password" value="1234" />
  1. However, the function of properties is not just like this. You can create a resource file named jdbc.properties, which places the data of four connection strings in the resource file by key value pairs (key=value), without any symbols, one line at a time
		properties: import external property file
			resource: import the property file from the classpath 
			url: import the attribute file under the network path or disk path
<properties resource="db.properties" ></properties>
  1. Set it dynamically in the dataSource element of the environment element
<environments default="oracle">
		<environment id="mysql">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${jdbc.driver}" />
				<property name="url" value="${jdbc.url}" />
				<property name="username" 
                         value="${jdbc.username}" />
				<property name="password" 
                         value="${jdbc.password}" />

3.3 settings settings

<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="useGeneratedKeys" value="false"/>
<setting name="autoMappingBehavior" value="PARTIAL"/>
<setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>
<setting name="defaultExecutorType" value="SIMPLE"/>
<setting name="defaultStatementTimeout" value="25"/>
<setting name="defaultFetchSize" value="100"/>
<setting name="safeRowBoundsEnabled" value="false"/>
<setting name="mapUnderscoreToCamelCase" value="false"/>
<setting name="localCacheScope" value="SESSION"/>
<setting name="jdbcTypeForNull" value="OTHER"/>
<setting name="lazyLoadTriggerMethods"

3.4 type aliases processing

  1. Type alias is to set a short name for Java type, which is convenient for us to reference a class.
	 	<typeAlias type="com.atguigu.mybatis.beans.Employee"
  1. In many cases, you can set aliases in batches. For each class in this package, you can create a default alias, that is, the simple class name is lowercase
	 	<package name="com.atguigu.mybatis.beans"/>
  1. The alias MyBatis has taken

3.5 environment configuration

  1. MyBatis can be configured in many environments, such as development, test and production environments
  2. Each environment is configured with an environment label and a unique identifier
  3. You can quickly switch environments by specifying an environment identifier through the default attribute in the environments tab
  4. Environment - specify specific environment
    id: Specifies the unique id of the current environment
    Both transactionManager,, and dataSource must have
<environments default="oracle">
		<environment id="mysql">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${jdbc.driver}" />
				<property name="url" value="${jdbc.url}" />
				<property name="username" value="${jdbc.username}" />
				<property name="password" value="${jdbc.password}" />
		 <environment id="oracle">
			<transactionManager type="JDBC"/>	
			<dataSource type="POOLED">
				<property name="driver" value="${orcl.driver}" />
				<property name="url" value="${orcl.url}" />
				<property name="username" value="${orcl.username}" />
				<property name="password" value="${orcl.password}" />
  1. transactionManager
type: JDBC | MANAGED | custom
	JDBC: uses JDBC's commit and rollback settings, and relies on connections from data sources to manage transaction scope. JdbcTransactionFactory
	MANAGED: do not commit or roll back a connection and let the container manage the whole life cycle of the transaction (such as the context of JEE application server). ManagedTransactionFactory
	Custom: implement TransactionFactory interface, type = full class name / alias
  1. dataSource
type: uncooled | pooled | JNDI | custom
	UNPOOLED: do not use connection pool, UnpooledDataSourceFactory
	POOLED: use connection pool, PooledDataSourceFactory
	JNDI: find the specified data source in a container like EJB or application server
	Customization: implement the DataSourceFactory interface, and define the acquisition method of data source.

7) In actual development, we use Spring to manage data sources and configure transaction control to override the above configuration

3.6 mappers mapper

  1. Used to tell mybatis which Mapper mapping files to import when initializing mybatis
  2. mapper registers SQL mapping files one by one
resource: introduce files under the classpath 
url: import the file under the network path or disk path
 class: import Mapper interface
		There are SQL mapping files. Mapper interface and SQL mapping file should have the same name and location 
			There is no SQL mapping file. Use annotations to write SQL statements on interface methods
		<mapper resource="EmployeeMapper.xml" />
		<mapper class="com.atguigu.mybatis.dao.EmployeeMapper"/>
		<package name="com.atguigu.mybatis.dao"/>
  1. Using batch registration requires that the SQL mapping file name must be the same as the interface name and in the same directory
		<package name="com.atguigu.mybatis.dao"/>
Published 1306 original articles, won praise 10.29 million visitors+
His message board follow

Tags: JDBC Mybatis MySQL SQL

Posted on Mon, 10 Feb 2020 06:29:45 -0500 by Paghilom