Mybatis的配置文件参数详解

偏执的太偏执、 2023-06-04 10:56 215阅读 0赞

1.Myatis配置文件主要是mybatis-config.xml

1672408-20190826112543777-591344260.png

我们来看一下这里的详细的配置和需要注意的地方:

ContractedBlock.gif ExpandedBlockStart.gif

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
  3. <configuration>
  4. <!-- 参数设置 -->
  5. <settings>
  6. <!-- 这个配置使全局的映射器启用或禁用缓存 -->
  7. <setting name="cacheEnabled" value="true" />
  8. <!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载 -->
  9. <setting name="lazyLoadingEnabled" value="true" />
  10. <!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载 -->
  11. <setting name="aggressiveLazyLoading" value="true" />
  12. <!-- 允许或不允许多种结果集从一个单独的语句中返回(需要适合的驱动) -->
  13. <setting name="multipleResultSetsEnabled" value="true" />
  14. <!-- 使用列标签代替列名。不同的驱动在这方便表现不同。参考驱动文档或充分测试两种方法来决定所使用的驱动 -->
  15. <setting name="useColumnLabel" value="true" />
  16. <!-- 允许JDBC支持生成的键。需要适合的驱动。如果设置为true则这个设置强制生成的键被使用,尽管一些驱动拒绝兼容但仍然有效(比如Derby) -->
  17. <setting name="useGeneratedKeys" value="true" />
  18. <!-- 指定MyBatis如何自动映射列到字段/属性。PARTIAL只会自动映射简单,没有嵌套的结果。FULL会自动映射任意复杂的结果(嵌套的或其他情况) -->
  19. <setting name="autoMappingBehavior" value="PARTIAL" />
  20. <!--当检测出未知列(或未知属性)时,如何处理,默认情况下没有任何提示,这在测试的时候很不方便,不容易找到错误。 NONE : 不做任何处理
  21. (默认值) WARNING : 警告日志形式的详细信息 FAILING : 映射失败,抛出异常和详细信息 -->
  22. <setting name="autoMappingUnknownColumnBehavior" value="WARNING" />
  23. <!-- 配置默认的执行器。SIMPLE执行器没有什么特别之处。REUSE执行器重用预处理语句。BATCH执行器重用语句和批量更新 -->
  24. <setting name="defaultExecutorType" value="SIMPLE" />
  25. <!-- 设置超时时间,它决定驱动等待一个数据库响应的时间 -->
  26. <setting name="defaultStatementTimeout" value="25000" />
  27. <!--设置查询返回值数量,可以被查询数值覆盖 -->
  28. <setting name="defaultFetchSize" value="100" />
  29. <!-- 允许在嵌套语句中使用分页 -->
  30. <setting name="safeRowBoundsEnabled" value="false" />
  31. <!--是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN 到经典 Java 属性名 aColumn
  32. 的类似映射。 -->
  33. <setting name="mapUnderscoreToCamelCase" value="false" />
  34. <!--MyBatis 利用本地缓存机制(Local Cache)防止循环引用(circular references)和加速重复嵌套查询。
  35. 默认值为 SESSION,这种情况下会缓存一个会话中执行的所有查询。 若设置值为 STATEMENT,本地会话仅用在语句执行上,对相同 SqlSession
  36. 的不同调用将不会共享数据。 -->
  37. <setting name="localCacheScope" value="SESSION" />
  38. <!-- 当没有为参数提供特定的 JDBC 类型时,为空值指定 JDBC 类型。 某些驱动需要指定列的 JDBC 类型,多数情况直接用一般类型即可,比如
  39. NULL、VARCHAR OTHER。 -->
  40. <setting name="jdbcTypeForNull" value="OTHER" />
  41. <!-- 指定哪个对象的方法触发一次延迟加载。 -->
  42. <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString" />
  43. </settings>
  44. <!-- 别名定义 -->
  45. <typeAliases>
  46. <typeAlias alias="pageAccessURL" type="com.lgm.mybatis.model.PageAccessURL" />
  47. </typeAliases>
  48. <!--自定义类型处理器 -->
  49. <typeHandlers>
  50. <!-- <typeHandler handler="com.xhm.util.BooleanTypeHandlder" /> -->
  51. <!--扫描整个包下的自定义类型处理器 -->
  52. <package name="com.xhm.util" />
  53. </typeHandlers>
  54. <!--plugins插件之 分页拦截器 -->
  55. <plugins>
  56. <plugin interceptor="com.xhm.util.PageInterceptor"></plugin>
  57. </plugins>
  58. <!--配置environment环境 -->
  59. <environments default="development">
  60. <!-- 环境配置1,每个SqlSessionFactory对应一个环境 -->
  61. <environment id="development1">
  62. <!-- 事务配置 type= JDBC、MANAGED 1.JDBC:这个配置直接简单使用了JDBC的提交和回滚设置。它依赖于从数据源得到的连接来管理事务范围。
  63. 2.MANAGED:这个配置几乎没做什么。它从来不提交或回滚一个连接。而它会让容器来管理事务的整个生命周期(比如Spring或JEE应用服务器的上下文)。
  64. 默认情况下它会关闭连接。然而一些容器并不希望这样,因此如果你需要从连接中停止它,将closeConnection属性设置为false -->
  65. <transactionManager type="JDBC" />
  66. <!-- <transactionManager type="MANAGED"> <property name="closeConnection"
  67. value="false"/> </transactionManager> -->
  68. <!-- 数据源类型:type = UNPOOLED、POOLED、JNDI 1.UNPOOLED:这个数据源的实现是每次被请求时简单打开和关闭连接。它有一点慢,这是对简单应用程序的一个很好的选择,因为它不需要及时的可用连接。
  69. 不同的数据库对这个的表现也是不一样的,所以对某些数据库来说配置数据源并不重要,这个配置也是闲置的 2.POOLED:这是JDBC连接对象的数据源连接池的实现,用来避免创建新的连接实例时必要的初始连接和认证时间。
  70. 这是一种当前Web应用程序用来快速响应请求很流行的方法。 3.JNDI:这个数据源的实现是为了使用如Spring或应用服务器这类的容器,容器可以集中或在外部配置数据源,然后放置一个JNDI上下文的引用 -->
  71. <dataSource type="UNPOOLED">
  72. <property name="driver" value="com.mysql.jdbc.Driver" />
  73. <property name="url" value="jdbc:mysql://localhost:3306/xhm" />
  74. <property name="username" value="root" />
  75. <property name="password" value="root" />
  76. <!-- 默认连接事务隔离级别 <property name="defaultTransactionIsolationLevel" value=""
  77. /> -->
  78. </dataSource>
  79. </environment>
  80. <!-- 环境配置2 -->
  81. <environment id="development2">
  82. <transactionManager type="JDBC" />
  83. <dataSource type="POOLED">
  84. <property name="driver" value="com.mysql.jdbc.Driver" />
  85. <property name="url" value="jdbc:mysql://localhost:3306/xhm" />
  86. <property name="username" value="root" />
  87. <property name="password" value="root" />
  88. <!-- 在任意时间存在的活动(也就是正在使用)连接的数量 -->
  89. <property name="poolMaximumActiveConnections" value="10" />
  90. <!-- 任意时间存在的空闲连接数 -->
  91. <property name="poolMaximumIdleConnections" value="5" />
  92. <!-- 在被强制返回之前,池中连接被检查的时间 -->
  93. <property name="poolMaximumCheckoutTime" value="20000" />
  94. <!-- 这是给连接池一个打印日志状态机会的低层次设置,还有重新尝试获得连接,这些情况下往往需要很长时间(为了避免连接池没有配置时静默失败) -->
  95. <property name="poolTimeToWait" value="20000" />
  96. <!-- 发送到数据的侦测查询,用来验证连接是否正常工作,并且准备接受请求。 -->
  97. <property name="poolPingQuery" value="NO PING QUERY SET" />
  98. <!-- 这是开启或禁用侦测查询。如果开启,你必须用一个合法的SQL语句(最好是很快速的)设置poolPingQuery属性 -->
  99. <property name="poolPingEnabled" value="false" />
  100. <!-- 这是用来配置poolPingQuery多次时间被用一次。这可以被设置匹配标准的数据库连接超时时间,来避免不必要的侦测 -->
  101. <property name="poolPingConnectionsNotUsedFor" value="0" />
  102. </dataSource>
  103. </environment>
  104. <!-- 环境配置3 -->
  105. <environment id="development3">
  106. <transactionManager type="JDBC" />
  107. <dataSource type="JNDI">
  108. <property name="data_source" value="java:comp/env/jndi/mybatis" />
  109. <property name="env.encoding" value="UTF8" />
  110. <!-- <property name="initial_context" value=""/> <property name="env.encoding"
  111. value="UTF8"/> -->
  112. </dataSource>
  113. </environment>
  114. </environments>
  115. <!-- 映射文件,mapper的配置文件 -->
  116. <mappers>
  117. <!--直接映射到相应的mapper文件 -->
  118. <mapper resource="com/xhm/mapper/UserMapper.xml" />
  119. <!--扫描包路径下所有xxMapper.xml文件 -->
  120. <package name="com.xhm.mapper" />
  121. </mappers>
  122. </configuration>

2.Mybatis配合文件节点详细解释

下面我们看看关于参数的配置情况,下面的信息是在mybatis配置文件中的每个配置大节点的详细解释(标注是红色的,需要重点知道的)

1672408-20190826113034137-2055096862.png

3.Mybatie配置文件中的setting节点下面配置信息的诠释。

1672408-20190826114111087-299963013.png

1672408-20190826114348094-1032890926.png

1672408-20190826114429848-2058789256.png

4.Mybatis中environments配置信息解释

  • environment 元素是配置一个数据源的开始,属性id是它的唯一标识
  • transactionManager 元素配置数据库事务,其中type属性有三种配置方式

    • jdbc,采用jdbc的方式管理事务;
    • managed,采用容器的方式管理事务,在JNDI数据源中使用;
    • 自定义,自定义数据库事务管理办法;
  • dataSource 元素配置数据源连接信息,type属性是连接数据库的方式配置,有四种配置方式

    • UNPOOLED 非连接池方式连接
    • POOLED 使用连接池连接
    • JNDI 使用JNDI数据源
    • 自定义数据源

5.数据库字段和java中bean的对应的类是:TypeHandlerReigser

6.Mapper.xml配置文件讲解

6.1具体标签的使用(这里的标签,需要都知道,既然学了,就要记住)

 cache – 给定命名空间的缓存配置。
 cache-ref – 其他命名空间缓存配置的引用。
 resultMap – 是最复杂也是最强大的元素,用来描述如何从数据库结果集中来加载对象。
 sql – 可被其他语句引用的可重用语句块。
 insert – 映射插入语句
 update – 映射更新语句
 delete – 映射删除语句
 select – 映射查询语句

下面我们看一个Mapper.xml的详细配置信息

ContractedBlock.gif ExpandedBlockStart.gif

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.youy.mybatis.mapper.TUserMapper">
  4. <resultMap id="BaseResultMap" type="com.youy.mybatis.entity.TUser">
  5. <!--
  6. WARNING - @mbggenerated
  7. This element is automatically generated by MyBatis Generator, do not modify.
  8. -->
  9. <id column="id" jdbcType="INTEGER" property="id" />
  10. <result column="user_name" jdbcType="VARCHAR" property="userName" />
  11. <result column="real_name" jdbcType="VARCHAR" property="realName" />
  12. <result column="sex" jdbcType="TINYINT" property="sex" />
  13. <result column="mobile" jdbcType="VARCHAR" property="mobile" />
  14. <result column="email" jdbcType="VARCHAR" property="email" />
  15. <result column="note" jdbcType="VARCHAR" property="note" />
  16. <result column="position_id" jdbcType="INTEGER" property="positionId" />
  17. </resultMap>
  18. <sql id="Base_Column_List">
  19. <!--
  20. WARNING - @mbggenerated
  21. This element is automatically generated by MyBatis Generator, do not modify.
  22. -->
  23. id, user_name, real_name, sex, mobile, email, note, position_id
  24. </sql>
  25. <resultMap id="userAndPosition1" type="com.youy.mybatis.entity.TUser" extends="BaseResultMap">
  26. <association property="tPosition" columnPrefix="post_"
  27. javaType="com.youy.mybatis.entity.TPosition">
  28. <id column="id" property="id"/>
  29. <result column="name" property="postName"/>
  30. <result column="note" property="note"/>
  31. </association>
  32. </resultMap>
  33. <resultMap id="userAndPosition2" type="com.youy.mybatis.entity.TUser" extends="BaseResultMap">
  34. <!-- property TUser 对象中的属性
  35. fetchType 加载的方式
  36. column 关联的字段
  37. select 关联到另外的mapper.xml的方法-->
  38. <association property="tPosition" fetchType="lazy" column="position_id" select="com.youy.mybatis.mapper.TPositionMapper.selectByPrimaryKey"/>
  39. </resultMap>
  40. <select id="selectUserPosition1" resultMap="userAndPosition1">
  41. select
  42. a.id,
  43. a.user_name,
  44. a.real_name,
  45. a.sex,
  46. a.mobile,
  47. a.email,
  48. a.note,
  49. b.id post_id,
  50. b.post_name,
  51. b.note post_note
  52. from
  53. t_user a,t_position b
  54. where
  55. a.position_id = b.id
  56. </select>
  57. <select id="selectUserPosition2" resultMap="userAndPosition2">
  58. select
  59. a.id,
  60. a.user_name,
  61. a.real_name,
  62. a.sex,
  63. a.mobile,
  64. a.email,
  65. a.note,
  66. a.position_id
  67. from
  68. t_user a
  69. </select>
  70. <select id="selectBeanByNameOrEmail" resultMap="BaseResultMap">
  71. select
  72. <include refid="Base_Column_List"/>
  73. from t_user
  74. <where>
  75. <choose>
  76. <when test="userName != null and userName != ''">
  77. and user_name = #{userName,jdbcType=VARCHAR}
  78. </when>
  79. <when test="email != null and email!= ''">
  80. and email like CONCAT('%',#{email,jdbcType=VARCHAR},'%')
  81. </when>
  82. <otherwise>
  83. and 1 = 1
  84. </otherwise>
  85. </choose>
  86. </where>
  87. </select>
  88. <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  89. <!--
  90. WARNING - @mbggenerated
  91. This element is automatically generated by MyBatis Generator, do not modify.
  92. -->
  93. select
  94. <include refid="Base_Column_List" />
  95. from t_user
  96. where id = #{id,jdbcType=INTEGER}
  97. </select>
  98. <resultMap id="userRoleInfo" extends= "BaseResultMap" type="com.youy.mybatis.entity.TUser">
  99. <collection property="roleList" column="id" columnPrefix="role_" ofType="com.youy.mybatis.entity.TRole">
  100. <id column="id" property="id"/>
  101. <result column="name" property="roleName"/>
  102. <result column="note" property="note"/>
  103. </collection>
  104. </resultMap>
  105. <select id="selectUserRole" resultMap="userRoleInfo">
  106. select a.id,
  107. a.user_name,
  108. a.real_name,
  109. a.sex,
  110. a.mobile,
  111. a.note,
  112. b.role_id,
  113. c.role_name,
  114. c.note role_note
  115. from t_user a,
  116. t_user_role b,
  117. t_role c
  118. where a.id = b.user_id AND
  119. b.role_id = c.id
  120. </select>
  121. <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
  122. <!--
  123. WARNING - @mbggenerated
  124. This element is automatically generated by MyBatis Generator, do not modify.
  125. -->
  126. delete from t_user
  127. where id = #{id,jdbcType=INTEGER}
  128. </delete>
  129. <insert id="insert" parameterType="com.youy.mybatis.entity.TUser">
  130. <!--
  131. WARNING - @mbggenerated
  132. This element is automatically generated by MyBatis Generator, do not modify.
  133. -->
  134. <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.Integer">
  135. SELECT LAST_INSERT_ID()
  136. </selectKey>
  137. insert into t_user (id, user_name, real_name,
  138. sex, mobile, email,
  139. note, position_id)
  140. values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{realName,jdbcType=VARCHAR},
  141. #{sex,jdbcType=TINYINT}, #{mobile,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR},
  142. #{note,jdbcType=VARCHAR}, #{positionId,jdbcType=INTEGER})
  143. </insert>
  144. <insert id="insertSelective" parameterType="com.youy.mybatis.entity.TUser">
  145. <!--
  146. WARNING - @mbggenerated
  147. This element is automatically generated by MyBatis Generator, do not modify.
  148. -->
  149. <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.Integer">
  150. SELECT LAST_INSERT_ID()
  151. </selectKey>
  152. insert into t_user
  153. <trim prefix="(" suffix=")" suffixOverrides=",">
  154. id,
  155. <if test="userName != null">
  156. user_name,
  157. </if>
  158. <if test="realName != null">
  159. real_name,
  160. </if>
  161. <if test="sex != null">
  162. sex,
  163. </if>
  164. <if test="mobile != null">
  165. mobile,
  166. </if>
  167. <if test="email != null">
  168. email,
  169. </if>
  170. <if test="note != null">
  171. note,
  172. </if>
  173. <if test="positionId != null">
  174. position_id,
  175. </if>
  176. </trim>
  177. <trim prefix="values (" suffix=")" suffixOverrides=",">
  178. #{id,jdbcType=INTEGER},
  179. <if test="userName != null">
  180. #{userName,jdbcType=VARCHAR},
  181. </if>
  182. <if test="realName != null">
  183. #{realName,jdbcType=VARCHAR},
  184. </if>
  185. <if test="sex != null">
  186. #{sex,jdbcType=TINYINT},
  187. </if>
  188. <if test="mobile != null">
  189. #{mobile,jdbcType=VARCHAR},
  190. </if>
  191. <if test="email != null">
  192. #{email,jdbcType=VARCHAR},
  193. </if>
  194. <if test="note != null">
  195. #{note,jdbcType=VARCHAR},
  196. </if>
  197. <if test="positionId != null">
  198. #{positionId,jdbcType=INTEGER},
  199. </if>
  200. </trim>
  201. </insert>
  202. <update id="updateByPrimaryKeySelective" parameterType="com.youy.mybatis.entity.TUser">
  203. <!--
  204. WARNING - @mbggenerated
  205. This element is automatically generated by MyBatis Generator, do not modify.
  206. -->
  207. update t_user
  208. <set>
  209. <if test="userName != null">
  210. user_name = #{userName,jdbcType=VARCHAR},
  211. </if>
  212. <if test="realName != null">
  213. real_name = #{realName,jdbcType=VARCHAR},
  214. </if>
  215. <if test="sex != null">
  216. sex = #{sex,jdbcType=TINYINT},
  217. </if>
  218. <if test="mobile != null">
  219. mobile = #{mobile,jdbcType=VARCHAR},
  220. </if>
  221. <if test="email != null">
  222. email = #{email,jdbcType=VARCHAR},
  223. </if>
  224. <if test="note != null">
  225. note = #{note,jdbcType=VARCHAR},
  226. </if>
  227. <if test="positionId != null">
  228. position_id = #{positionId,jdbcType=INTEGER},
  229. </if>
  230. </set>
  231. where id = #{id,jdbcType=INTEGER}
  232. </update>
  233. <update id="updateByPrimaryKey" parameterType="com.youy.mybatis.entity.TUser">
  234. <!--
  235. WARNING - @mbggenerated
  236. This element is automatically generated by MyBatis Generator, do not modify.
  237. -->
  238. update t_user
  239. set user_name = #{userName,jdbcType=VARCHAR},
  240. real_name = #{realName,jdbcType=VARCHAR},
  241. sex = #{sex,jdbcType=TINYINT},
  242. mobile = #{mobile,jdbcType=VARCHAR},
  243. email = #{email,jdbcType=VARCHAR},
  244. note = #{note,jdbcType=VARCHAR},
  245. position_id = #{positionId,jdbcType=INTEGER}
  246. where id = #{id,jdbcType=INTEGER}
  247. </update>
  248. </mapper>

6.1.1 select 标签的使用

我们看一下该标签里面涉及到的属性

1672408-20190826163346084-605325435.png

关于这个标签,我们需要西湖一的点

  1. 我们首先需要注意的是resultType返回是集合的时候,返回的是集合的元素类型。不是list。
  2. 使用自动映射的时候,也就是 resultType = “com.youy.mybatis.entity.TUser” ,这样的话,需要将setting配置节点中的属性 否则的话。带有下划线的列明,不能正常的对应高bean里面;
  3. 如果使用的是resultMap的话,在节点里面将属性对应好,不用配置驼峰信息的话,也是可以的;

总结:1.请求参数,如果参数少于 5个 的话,我们需要传递固定的参数

  1. 2.传入的参数要是大于5 个的话,就要使用的是bean,不要使用 map 作为入参,因为不好维护;
  2. 3.resultType,要是使用这个参数的话,返回值耦合性比较大,需要动地方比较多。
  3. 4.所以使用resultMap,这样的配置信息,易于维护。便于解耦。

转载于:https://www.cnblogs.com/lys-lyy/p/11411650.html

发表评论

表情:
评论列表 (有 0 条评论,215人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Mybatis配置文件详解

    配置文件和映射文件还有挺多的属性我还没有讲的,现在就把它们一一补全 映射文件 在mapper.xml文件中配置很多的sql语句,执行每个sql语句时,封装为Mapped