Environment
- Default environment ID (e.g. :default=”development”)
- The environment ID defined by each environment element (e.g. ID =”development”)
- Transaction manager configuration (e.g. Type =”JDBC”)
- Data source configuration (e.g. Type =”POOLED”)
- Each database has one SqlSessionFactory instance, and each SqlSessionFactory instance can only be Environment
Common configurations <! -- Set a default connection environment --> <environments default="development">
<environment id="development"> <! Mybatis uses JDBC transaction management --> <transactionManagertype="JDBC"/ > <! -- Mybatis uses connection pooling to get connections --> <dataSourcetype="POOLED"> <! Configure the four necessary properties to interact with the database --> <property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
public final class Environment {
private final String id;
private final TransactionFactory transactionFactory;
private final DataSource dataSource;
}
Copy the code
TransactionManager
- JDBC – This configuration directly uses JDBC’s commit and rollback Settings, which rely on connections from the data source to manage transaction scopes.
- MANAGED – This configuration does little. It never commits or rolls back a connection, but lets the container manage the entire life cycle of the transaction
- The Spring module uses its own manager to override the previous configuration.
<transactionManager type="MANAGED">
<property name="closeConnection" value="false"/>
</transactionManager>
public interface TransactionFactory {
void setProperties(Properties props);
Transaction newTransaction(Connection conn);
Transaction newTransaction(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit);
}
public interface Transaction {
Connection getConnection() throws SQLException;
void commit() throws SQLException;
void rollback() throws SQLException;
void close() throws SQLException;
Integer getTimeout() throws SQLException;
}
Copy the code
DataSource
- UNPOOLED – The implementation of this data source simply opens and closes the connection each time it is requested
- POOLED – This data source implementation leverages the concept of “pooling” to organize JDBC connection objects, avoiding the initialization and authentication time required to create new connection instances. This is a popular process that allows concurrent Web applications to respond quickly to requests.
- JNDI – This data source is implemented for use in a container such as an EJB or application server, which can centrally or externally configure the data source and then place a reference to the JNDI context
Pooled configuration
Driver - this is the fully qualified name URL of the Java class for the JDBC driver - this is the JDBC URL address of the database. Username - username for logging in to the database. Password - Password for logging in to the database. DefaultTransactionIsolationLevel - the default connection transaction isolation level. PoolMaximumActiveConnections - activity can exist at any time (i.e., are using) the number of connections, the default value: 10 poolMaximumIdleConnections - possible number of idle connections at any time. PoolMaximumCheckoutTime - The time at which a connection in the pool is checked out before being forced back. Default: 20000 ms (20 seconds) poolTimeToWait - This is a low-level setting that if it takes a long time to get a connection, the pool prints a status log and tries to get a connection again (to avoid silent failure in case of misconfiguration). Default: 20000 milliseconds (20 seconds). PoolMaximumLocalBadConnectionTolerance - this is a bad connection on tolerance of the underlying set, acting on each attempts to get connection from buffer pool of threads. If the thread gets a bad connection, the data source allows the thread to try to get a new connection, But the number of the retry should not exceed poolMaximumIdleConnections with poolMaximumLocalBadConnectionTolerance combined. Default: 3 (added in 3.4.5) poolPingQuery - A detection query sent to the database to verify that the connection is working properly and ready to accept the request. The default is "NO PING QUERY SET", which causes most database drivers to fail with an appropriate error message. PoolPingEnabled - Whether to enable detection query. If enabled, you need to set the poolPingQuery property to an executable SQL statement (preferably a very fast SQL statement). The default value is:false. The frequency of poolPingQuery poolPingConnectionsNotUsedFor - configuration. Can be set to the same as the database connection timeout to avoid unnecessary detection, default: 0 (that is, all connections are detected at any time - of course only if poolPingEnabled is set totrueIs applicable).Copy the code
Plugins
MyBatis allows you to intercept calls at certain points during the execution of mapped statements. By default, MyBatis allows you to intercept method calls using plug-ins:
- Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
- ParameterHandler (getParameterObject, setParameters)
- ResultSetHandler (handleResultSets, handleOutputParameters)
- StatementHandler (prepare, parameterize, batch, update, query)
Custom plug-in
Implement the Interceptor interface and specify the method signature you want to intercept
// exampleplugin.java // the plug-in will intercept all "Update" method calls in the Executor instance, where Executor is the internal object responsible for executing low-level mapping statements. @Intercepts({@Signature(type= Executor.class,
method = "update". args = {MappedStatement.class,Object.class})}) public class ExamplePlugin implements Interceptor { public Object intercept(Invocation invocation) throws Throwable {return invocation.proceed();
}
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
public void setProperties(Properties properties) { } } <! -- mybatis-config.xml --> <plugins> <plugin interceptor="org.mybatis.example.ExamplePlugin">
<property name="someProperty" value="100"/>
</plugin>
</plugins>
Copy the code