Mybatis的配置文件参数详解
1.Myatis配置文件主要是mybatis-config.xml
我们来看一下这里的详细的配置和需要注意的地方:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 参数设置 -->
<settings>
<!-- 这个配置使全局的映射器启用或禁用缓存 -->
<setting name="cacheEnabled" value="true" />
<!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载 -->
<setting name="aggressiveLazyLoading" value="true" />
<!-- 允许或不允许多种结果集从一个单独的语句中返回(需要适合的驱动) -->
<setting name="multipleResultSetsEnabled" value="true" />
<!-- 使用列标签代替列名。不同的驱动在这方便表现不同。参考驱动文档或充分测试两种方法来决定所使用的驱动 -->
<setting name="useColumnLabel" value="true" />
<!-- 允许JDBC支持生成的键。需要适合的驱动。如果设置为true则这个设置强制生成的键被使用,尽管一些驱动拒绝兼容但仍然有效(比如Derby) -->
<setting name="useGeneratedKeys" value="true" />
<!-- 指定MyBatis如何自动映射列到字段/属性。PARTIAL只会自动映射简单,没有嵌套的结果。FULL会自动映射任意复杂的结果(嵌套的或其他情况) -->
<setting name="autoMappingBehavior" value="PARTIAL" />
<!--当检测出未知列(或未知属性)时,如何处理,默认情况下没有任何提示,这在测试的时候很不方便,不容易找到错误。 NONE : 不做任何处理
(默认值) WARNING : 警告日志形式的详细信息 FAILING : 映射失败,抛出异常和详细信息 -->
<setting name="autoMappingUnknownColumnBehavior" value="WARNING" />
<!-- 配置默认的执行器。SIMPLE执行器没有什么特别之处。REUSE执行器重用预处理语句。BATCH执行器重用语句和批量更新 -->
<setting name="defaultExecutorType" value="SIMPLE" />
<!-- 设置超时时间,它决定驱动等待一个数据库响应的时间 -->
<setting name="defaultStatementTimeout" value="25000" />
<!--设置查询返回值数量,可以被查询数值覆盖 -->
<setting name="defaultFetchSize" value="100" />
<!-- 允许在嵌套语句中使用分页 -->
<setting name="safeRowBoundsEnabled" value="false" />
<!--是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN 到经典 Java 属性名 aColumn
的类似映射。 -->
<setting name="mapUnderscoreToCamelCase" value="false" />
<!--MyBatis 利用本地缓存机制(Local Cache)防止循环引用(circular references)和加速重复嵌套查询。
默认值为 SESSION,这种情况下会缓存一个会话中执行的所有查询。 若设置值为 STATEMENT,本地会话仅用在语句执行上,对相同 SqlSession
的不同调用将不会共享数据。 -->
<setting name="localCacheScope" value="SESSION" />
<!-- 当没有为参数提供特定的 JDBC 类型时,为空值指定 JDBC 类型。 某些驱动需要指定列的 JDBC 类型,多数情况直接用一般类型即可,比如
NULL、VARCHAR OTHER。 -->
<setting name="jdbcTypeForNull" value="OTHER" />
<!-- 指定哪个对象的方法触发一次延迟加载。 -->
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString" />
</settings>
<!-- 别名定义 -->
<typeAliases>
<typeAlias alias="pageAccessURL" type="com.lgm.mybatis.model.PageAccessURL" />
</typeAliases>
<!--自定义类型处理器 -->
<typeHandlers>
<!-- <typeHandler handler="com.xhm.util.BooleanTypeHandlder" /> -->
<!--扫描整个包下的自定义类型处理器 -->
<package name="com.xhm.util" />
</typeHandlers>
<!--plugins插件之 分页拦截器 -->
<plugins>
<plugin interceptor="com.xhm.util.PageInterceptor"></plugin>
</plugins>
<!--配置environment环境 -->
<environments default="development">
<!-- 环境配置1,每个SqlSessionFactory对应一个环境 -->
<environment id="development1">
<!-- 事务配置 type= JDBC、MANAGED 1.JDBC:这个配置直接简单使用了JDBC的提交和回滚设置。它依赖于从数据源得到的连接来管理事务范围。
2.MANAGED:这个配置几乎没做什么。它从来不提交或回滚一个连接。而它会让容器来管理事务的整个生命周期(比如Spring或JEE应用服务器的上下文)。
默认情况下它会关闭连接。然而一些容器并不希望这样,因此如果你需要从连接中停止它,将closeConnection属性设置为false -->
<transactionManager type="JDBC" />
<!-- <transactionManager type="MANAGED"> <property name="closeConnection"
value="false"/> </transactionManager> -->
<!-- 数据源类型:type = UNPOOLED、POOLED、JNDI 1.UNPOOLED:这个数据源的实现是每次被请求时简单打开和关闭连接。它有一点慢,这是对简单应用程序的一个很好的选择,因为它不需要及时的可用连接。
不同的数据库对这个的表现也是不一样的,所以对某些数据库来说配置数据源并不重要,这个配置也是闲置的 2.POOLED:这是JDBC连接对象的数据源连接池的实现,用来避免创建新的连接实例时必要的初始连接和认证时间。
这是一种当前Web应用程序用来快速响应请求很流行的方法。 3.JNDI:这个数据源的实现是为了使用如Spring或应用服务器这类的容器,容器可以集中或在外部配置数据源,然后放置一个JNDI上下文的引用 -->
<dataSource type="UNPOOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/xhm" />
<property name="username" value="root" />
<property name="password" value="root" />
<!-- 默认连接事务隔离级别 <property name="defaultTransactionIsolationLevel" value=""
/> -->
</dataSource>
</environment>
<!-- 环境配置2 -->
<environment id="development2">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/xhm" />
<property name="username" value="root" />
<property name="password" value="root" />
<!-- 在任意时间存在的活动(也就是正在使用)连接的数量 -->
<property name="poolMaximumActiveConnections" value="10" />
<!-- 任意时间存在的空闲连接数 -->
<property name="poolMaximumIdleConnections" value="5" />
<!-- 在被强制返回之前,池中连接被检查的时间 -->
<property name="poolMaximumCheckoutTime" value="20000" />
<!-- 这是给连接池一个打印日志状态机会的低层次设置,还有重新尝试获得连接,这些情况下往往需要很长时间(为了避免连接池没有配置时静默失败) -->
<property name="poolTimeToWait" value="20000" />
<!-- 发送到数据的侦测查询,用来验证连接是否正常工作,并且准备接受请求。 -->
<property name="poolPingQuery" value="NO PING QUERY SET" />
<!-- 这是开启或禁用侦测查询。如果开启,你必须用一个合法的SQL语句(最好是很快速的)设置poolPingQuery属性 -->
<property name="poolPingEnabled" value="false" />
<!-- 这是用来配置poolPingQuery多次时间被用一次。这可以被设置匹配标准的数据库连接超时时间,来避免不必要的侦测 -->
<property name="poolPingConnectionsNotUsedFor" value="0" />
</dataSource>
</environment>
<!-- 环境配置3 -->
<environment id="development3">
<transactionManager type="JDBC" />
<dataSource type="JNDI">
<property name="data_source" value="java:comp/env/jndi/mybatis" />
<property name="env.encoding" value="UTF8" />
<!-- <property name="initial_context" value=""/> <property name="env.encoding"
value="UTF8"/> -->
</dataSource>
</environment>
</environments>
<!-- 映射文件,mapper的配置文件 -->
<mappers>
<!--直接映射到相应的mapper文件 -->
<mapper resource="com/xhm/mapper/UserMapper.xml" />
<!--扫描包路径下所有xxMapper.xml文件 -->
<package name="com.xhm.mapper" />
</mappers>
</configuration>
2.Mybatis配合文件节点详细解释
下面我们看看关于参数的配置情况,下面的信息是在mybatis配置文件中的每个配置大节点的详细解释(标注是红色的,需要重点知道的)
3.Mybatie配置文件中的setting节点下面配置信息的诠释。
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的详细配置信息
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.youy.mybatis.mapper.TUserMapper">
<resultMap id="BaseResultMap" type="com.youy.mybatis.entity.TUser">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<id column="id" jdbcType="INTEGER" property="id" />
<result column="user_name" jdbcType="VARCHAR" property="userName" />
<result column="real_name" jdbcType="VARCHAR" property="realName" />
<result column="sex" jdbcType="TINYINT" property="sex" />
<result column="mobile" jdbcType="VARCHAR" property="mobile" />
<result column="email" jdbcType="VARCHAR" property="email" />
<result column="note" jdbcType="VARCHAR" property="note" />
<result column="position_id" jdbcType="INTEGER" property="positionId" />
</resultMap>
<sql id="Base_Column_List">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
id, user_name, real_name, sex, mobile, email, note, position_id
</sql>
<resultMap id="userAndPosition1" type="com.youy.mybatis.entity.TUser" extends="BaseResultMap">
<association property="tPosition" columnPrefix="post_"
javaType="com.youy.mybatis.entity.TPosition">
<id column="id" property="id"/>
<result column="name" property="postName"/>
<result column="note" property="note"/>
</association>
</resultMap>
<resultMap id="userAndPosition2" type="com.youy.mybatis.entity.TUser" extends="BaseResultMap">
<!-- property TUser 对象中的属性
fetchType 加载的方式
column 关联的字段
select 关联到另外的mapper.xml的方法-->
<association property="tPosition" fetchType="lazy" column="position_id" select="com.youy.mybatis.mapper.TPositionMapper.selectByPrimaryKey"/>
</resultMap>
<select id="selectUserPosition1" resultMap="userAndPosition1">
select
a.id,
a.user_name,
a.real_name,
a.sex,
a.mobile,
a.email,
a.note,
b.id post_id,
b.post_name,
b.note post_note
from
t_user a,t_position b
where
a.position_id = b.id
</select>
<select id="selectUserPosition2" resultMap="userAndPosition2">
select
a.id,
a.user_name,
a.real_name,
a.sex,
a.mobile,
a.email,
a.note,
a.position_id
from
t_user a
</select>
<select id="selectBeanByNameOrEmail" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_user
<where>
<choose>
<when test="userName != null and userName != ''">
and user_name = #{userName,jdbcType=VARCHAR}
</when>
<when test="email != null and email!= ''">
and email like CONCAT('%',#{email,jdbcType=VARCHAR},'%')
</when>
<otherwise>
and 1 = 1
</otherwise>
</choose>
</where>
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
select
<include refid="Base_Column_List" />
from t_user
where id = #{id,jdbcType=INTEGER}
</select>
<resultMap id="userRoleInfo" extends= "BaseResultMap" type="com.youy.mybatis.entity.TUser">
<collection property="roleList" column="id" columnPrefix="role_" ofType="com.youy.mybatis.entity.TRole">
<id column="id" property="id"/>
<result column="name" property="roleName"/>
<result column="note" property="note"/>
</collection>
</resultMap>
<select id="selectUserRole" resultMap="userRoleInfo">
select a.id,
a.user_name,
a.real_name,
a.sex,
a.mobile,
a.note,
b.role_id,
c.role_name,
c.note role_note
from t_user a,
t_user_role b,
t_role c
where a.id = b.user_id AND
b.role_id = c.id
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
delete from t_user
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.youy.mybatis.entity.TUser">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<selectKey keyProperty="id" order="BEFORE" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into t_user (id, user_name, real_name,
sex, mobile, email,
note, position_id)
values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{realName,jdbcType=VARCHAR},
#{sex,jdbcType=TINYINT}, #{mobile,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR},
#{note,jdbcType=VARCHAR}, #{positionId,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.youy.mybatis.entity.TUser">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<selectKey keyProperty="id" order="BEFORE" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into t_user
<trim prefix="(" suffix=")" suffixOverrides=",">
id,
<if test="userName != null">
user_name,
</if>
<if test="realName != null">
real_name,
</if>
<if test="sex != null">
sex,
</if>
<if test="mobile != null">
mobile,
</if>
<if test="email != null">
email,
</if>
<if test="note != null">
note,
</if>
<if test="positionId != null">
position_id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
#{id,jdbcType=INTEGER},
<if test="userName != null">
#{userName,jdbcType=VARCHAR},
</if>
<if test="realName != null">
#{realName,jdbcType=VARCHAR},
</if>
<if test="sex != null">
#{sex,jdbcType=TINYINT},
</if>
<if test="mobile != null">
#{mobile,jdbcType=VARCHAR},
</if>
<if test="email != null">
#{email,jdbcType=VARCHAR},
</if>
<if test="note != null">
#{note,jdbcType=VARCHAR},
</if>
<if test="positionId != null">
#{positionId,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.youy.mybatis.entity.TUser">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update t_user
<set>
<if test="userName != null">
user_name = #{userName,jdbcType=VARCHAR},
</if>
<if test="realName != null">
real_name = #{realName,jdbcType=VARCHAR},
</if>
<if test="sex != null">
sex = #{sex,jdbcType=TINYINT},
</if>
<if test="mobile != null">
mobile = #{mobile,jdbcType=VARCHAR},
</if>
<if test="email != null">
email = #{email,jdbcType=VARCHAR},
</if>
<if test="note != null">
note = #{note,jdbcType=VARCHAR},
</if>
<if test="positionId != null">
position_id = #{positionId,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.youy.mybatis.entity.TUser">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update t_user
set user_name = #{userName,jdbcType=VARCHAR},
real_name = #{realName,jdbcType=VARCHAR},
sex = #{sex,jdbcType=TINYINT},
mobile = #{mobile,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR},
note = #{note,jdbcType=VARCHAR},
position_id = #{positionId,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
6.1.1 select 标签的使用
我们看一下该标签里面涉及到的属性
关于这个标签,我们需要西湖一的点
- 我们首先需要注意的是resultType返回是集合的时候,返回的是集合的元素类型。不是list。
- 使用自动映射的时候,也就是 resultType = “com.youy.mybatis.entity.TUser” ,这样的话,需要将setting配置节点中的属性
否则的话。带有下划线的列明,不能正常的对应高bean里面; - 如果使用的是resultMap的话,在节点里面将属性对应好,不用配置驼峰信息的话,也是可以的;
总结:1.请求参数,如果参数少于 5个 的话,我们需要传递固定的参数
2.传入的参数要是大于5 个的话,就要使用的是bean,不要使用 map 作为入参,因为不好维护;
3.resultType,要是使用这个参数的话,返回值耦合性比较大,需要动地方比较多。
4.所以使用resultMap,这样的配置信息,易于维护。便于解耦。
转载于//www.cnblogs.com/lys-lyy/p/11411650.html
还没有评论,来说两句吧...