一文带你掌握mybatis的所有全局配置( 三 )


一个类型别名或完全限定类名 。
未设置
shrinkWhitespacesInSql
从SQL中删除多余的空格字符 。请注意 , 这也会影响SQL中的文字字符串 。(新增于 3.5.5)
true | false
false
defaultSqlProviderType
Specifies an sql provider class that holds provider method (Since 3.5.6). This class apply to the type(or value) attribute on sql provider annotation(e.g. @SelectProvider), when these attribute was omitted.
 
 
配置typeAliases类型别名类型别名可以为java类型设置别名 , 之后使用全类名时可以使用别名
<typeAliases><!-- typeAlias为某个类起别名 --><typeAlias type="com.zhanghe.study.mybatis.model.User" alias="User"/><!-- 为某个包下所有类批量起别名默认值为类名首字母小写--><package name="com.zhanghe.study.mybatis.model"/></typeAliases>也可以在类上使用@Alias注解来设置该类的别名
配置类型处理器typeHandlers数据库类型和java类型进行转换
实现 org.apache.ibatis.type.TypeHandler 接口 ,  或继承 org.apache.ibatis.type.BaseTypeHandler ,  并且可以将它映射到一个 JDBC 类型
@MappedJdbcTypes(JdbcType.VARCHAR)public class ExampleTypeHandler extends BaseTypeHandler<String> {@Overridepublic void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {ps.setString(i, parameter);}@Overridepublic String getNullableResult(ResultSet rs, String columnName) throws SQLException {return rs.getString(columnName);}@Overridepublic String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {return rs.getString(columnIndex);}@Overridepublic String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {return cs.getString(columnIndex);}}<typeHandlers><typeHandler handler="org.mybatis.example.ExampleTypeHandler"/></typeHandlers>配置插件pluginsmybatis可以使用插件来在映射语句执行过程中的某一点进行拦截调用 , 包括

  • Executor (update、query、flushStatements、commit、rollback、getTransaction、close、isClose)
  • ParameterHandler (getParameterObject、setParameters)
  • ResultSetHandler (handleResultSets、handleOutputParameters)
  • StatementHandler (prepare、parameterize、batch、update、query)
需要实现Interceptor接口 , 并指定想要拦截的方法签名来使用插件
Intercepts({@Signature(type= Executor.class,method = "update",args = {MappedStatement.class,Object.class})})public class ExamplePlugin implements Interceptor {private Properties properties = new Properties();public Object intercept(Invocation invocation) throws Throwable {// implement pre processing if needObject returnObject = invocation.proceed();// implement post processing if needreturn returnObject;}public void setProperties(Properties properties) {this.properties = properties;}}<plugins><plugin interceptor="org.mybatis.example.ExamplePlugin"><property name="someProperty" value=https://www.isolves.com/it/cxkf/kj/2021-03-12/"100"/>
注意:如果有多个插件拦截相同方法的时候 , 会按照配置的先后顺序来进行包装代理 , 在执行时会执行最外层的插件 , 也就是逆向执行
配置环境environmentsmybatis可以配置多个环境 , 可以连接多个数据库 , 但是每个SqlSessionFactory实例只可以选择一种环境
在构建sqlSessionFactory的时候可以指定选择创建哪个环境
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, environment);<!-- 环境 default表示默认使用环境--><environments default="development"><environment id="development"><!-- 事务管理器有两种类型1、JDBC该配置直接使用了JDBC的提交和回滚 , 依赖从数据源获得的连接来管理事务作用域2、MANAGED 使用容器来管理事务的整个生命周期(spring会使用自带的管理器 , 不需要配置mybatis的事务管理器)--><transactionManager type="JDBC"/><!-- 数据源有三种类型1、UNPOOLED每次请求时打开和关闭连接额外属性- defaultTransactionIsolationLevel默认的连接事务隔离级别- defaultNetworkTimeout等待数据库操作完成的默认网络超时时间2、POOLED利用连接池的概念将JDBC连接对象组织起来 , 避免了创建新的连接实例额外属性- poolMaximumactiveConnections 在任意时间可存在的活动连接数 , 默认10- poolMaximumIdleConnections任意时间可能存在的空闲连接数- poolMaximumCheckoutTime在被强制返回之前 , 池中连接被检出时间 , 默认20000毫秒- poolTimeToWait如果获取连接花费了很长的时间 , 连接池会打印状态日志并重新尝试获取一个连接 , 默认20000毫秒- poolMaximumLocalBadConnectionTolerance如果一个线程获取到一个坏的连接 , 数据源允许这个线程尝试重新获取一个新的连接 , 尝试次数不应该超过poolMaximumIdleConnections与poolMaximumLocalBadConnectionTolerance之和 , 默认为3- poolPingQuery发送到数据库的侦测查询 , 用来检验连接是否正常工作默认 NO PING QUERY SET- poolPingEnabled是否启用侦测查询默认false- poolPingConnectionsNotUsedFor配置poolPingQuery的频率(可以设置为和数据库连接超时时间一样 , 避免不必要的侦测)默认03、JNDI在外部配置数据源 , 然后一个JNDI上下文数据源引用额外属性- initial_context用来在InitialContext中寻找上下文- data_source引用数据源实例的上下文路径--><dataSource type="POOLED"><property name="driver" value=https://www.isolves.com/it/cxkf/kj/2021-03-12/"${jdbc.driver}"/>


推荐阅读