mybatis配置文件详解(二)

一、mybatis-config全局配置文件

1.1、配置文件内容

<?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>
        <!--各个标签的顺序不能变-->
        <properties resource="application.properties"></properties>

    <!-- 别名 -->
	<typeAliases>
		<typeAlias type="com.achuan.demo.entity.Student" value="student"></typeAlias>
		<package name="com.achuan.demo.entity" />
	</typeAliases>

        <!--延时加载: -->
        <!--1、直接加载:执行完对主加载对象的select语句,马上执行对关联对象的select查询。-->
        <!--2、侵入式延迟(按需加载):执行对主加载对象的查询时,不会执行对关联对象的查询。但当要访问主加载对象的详情时,就会马上执行关联对象的select查询。-->
        <!--3、深度延迟:执行对主加载对象的查询时,不会执行对关联对象的查询。访问主加载对象的详情时也不会执行关联对象的select查询。只有当真正访问关联对象的详情时,才会执行对关联对象的select查询。-->
        <settings>
            <setting name="lazyLoadingEnabled" value="true"/>
            <setting name="aggressiveLazyLoading" value="false"/>
        </settings>
     
        <environments default="demoMyBatis">
            <environment id="demoMyBatis">
                <transactionManager type="JDBC"></transactionManager>
                <dataSource type="POOLED">
                    <property name="driver" value="${jdbc.driverClassName}"></property>
                    <property name="url" value="${jdbc.url}"></property>
                    <property name="username" value="${jdbc.username}"></property>
                    <property name="password" value="${jdbc.password}"></property>
                </dataSource>
            </environment>
        </environments>

    <!-- 多数据库支持 -->
	<databaseIdProvider type="DB_VENDOR">
		<property name="MySQL" value="mysql"></property>
		<property name="Oracle" value="oracle"></property>
		<property name="SQL Server" value="sql server"></property>
	</databaseIdProvider>

        <mappers>
            <mapper resource="mapper/studentMapper.xml"/>
            <mapper resource="mapper/UserMapper.xml"/>
            <mapper resource="mapper/CountryMapper.xml"/>
            <mapper resource="mapper/MinisterMapper.xml"/>
            <mapper resource="mapper/NewsMapper.xml"/>
        </mappers>

</configuration>

1.2、全局配置各个标签内容详解

   1、configuration标签:包含properties、typeAliases、settings、environments、databaseIdProvider、mappers等标签并且标签顺序不可以改变;

   2、properties标签:引入外部配置文件;

   3、environments标签:environment标签中配置了数据源、数据源事务管理,environments标签中的default属性是用来选择environment标签的。transactionManager中type有两种:JDBC,代表使用jdbc的方式进行事物的提交和回滚;MANAGED代表使用j2ee的方式进行提交和回滚,dataSource为数据源,type为数据源类型:UNPOOLED|POOLED|JNDI,UNPOOLED;

   4、typeAliases标签:用来给制定的类声明一个别名以便后续引用;

   5、databaseIdProvider标签:dataBaseIdProvider是为全局设置多个数据源,可以设置多个数据库,如mysql,oracle,sql server等数据库;

   6、mappers标签:将自定义mapper.xml文件注入到全局配置文件中;

   7、settings标签:全局配置中最重要的标签,详细列表如下:

mybatis配置文件详解(二)

mybatis配置文件详解(二)

mybatis配置文件详解(二)

 

二、自定义mapper标签详解

2.1、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.achuan.demo.dao.StudentDao">

    <select id="selectById" parameterType="int" resultMap="student">
           select * from student where id = #{id}
    </select>

    <select id="selectAll" resultMap="student">
           select * from student;
    </select>

    <select id="selectBySex" parameterType="string" resultMap="student">
           select * from student where sex = #{sex};
    </select>

    <!--useGeneratedKeys:开启主键回写
        keyColumn:指定数据库的主键
        keyProperty:主键对应的pojo属性名-->
    <insert id="insert" parameterType="com.achuan.demo.entity.Student" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
           insert into student(
           id,
           user_name,
           password,
           sex,
           local_address,
           age
           )values (
           null,
           #{username},
           #{password},
           #{sex},
           #{localAddress},
           #{age}
           );
    </insert>

    <update id="update" parameterType="com.achuan.demo.entity.Student">
        update student
        <trim prefix="set" suffixOverrides=",">
            <if test="username!=null">user_name = #{username},</if>
            <if test="password!=null">password = #{password},</if>
            <if test="sex!=null">sex = #{sex},</if>
            <if test="localAddress!=null">local_address = #{localAddress},</if>
            <if test="age!=null">age = #{age}</if>
        </trim>
        where id = #{id};
    </update>

    <delete id="deleteById" parameterType="int">
        delete from student where id = #{id};
    </delete>

    <resultMap id="student" type="com.achuan.demo.entity.Student">
        <!--jdbcType的值必须得大写-->
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="user_name" jdbcType="VARCHAR" property="username"/>
        <result column="password" jdbcType="VARCHAR" property="password"/>
        <result column="local_address" jdbcType="VARCHAR" property="localAddress"/>
        <result column="sex" jdbcType="VARCHAR" property="sex"/>
        <result column="age" jdbcType="INTEGER" property="age"/>
    </resultMap>



映射文件是以<mapper>作为根节点,在根节点中支持9个元素,分别为insert、update、delete、select(增删改查);cache、cache-ref、resultMap、parameterMap、sql。

2.2、select标签

属性 描述
id 在命名空间中唯一的标识符,可以被用来引用这条语句。
parameterType 将会传入这条语句的参数类的完全限定名或别名。这个属性是可选的,因为 MyBatis 可以通过 TypeHandler 推断出具体传入语句的参数,默认值为 unset。
resultType 从这条语句中返回的期望类型的类的完全限定名或别名。注意如果是集合情形,那应该是集合可以包含的类型,而不能是集合本身。使用 resultType 或 resultMap,但不能同时使用。
resultMap 外部 resultMap 的命名引用。结果集的映射是 MyBatis 最强大的特性,对其有一个很好的理解的话,许多复杂映射的情形都能迎刃而解。使用 resultMap 或 resultType,但不能同时使用。
flushCache 将其设置为 true,任何时候只要语句被调用,都会导致本地缓存和二级缓存都会被清空,默认值:false。
useCache 将其设置为 true,将会导致本条语句的结果被二级缓存,默认值:对 select 元素为 true。
timeout 这个设置是在抛出异常之前,驱动程序等待数据库返回请求结果的秒数。默认值为 unset(依赖驱动)。
fetchSize 这是尝试影响驱动程序每次批量返回的结果行数和这个设置值相等。默认值为 unset(依赖驱动)。
statementType STATEMENT,PREPARED 或 CALLABLE 的一个。这会让 MyBatis 分别使用 Statement,PreparedStatement 或 CallableStatement,默认值:PREPARED。
resultSetType FORWARD_ONLY,SCROLL_SENSITIVE 或 SCROLL_INSENSITIVE 中的一个,默认值为 unset (依赖驱动)。
databaseId 如果配置了 databaseIdProvider,MyBatis 会加载所有的不带 databaseId 或匹配当前 databaseId 的语句;如果带或者不带的语句都有,则不带的会被忽略。
resultOrdered 这个设置仅针对嵌套结果 select 语句适用:如果为 true,就是假设包含了嵌套结果集或是分组了,这样的话当返回一个主结果行的时候,就不会发生有对前面结果集的引用的情况。这就使得在获取嵌套的结果集的时候不至于导致内存不够用。默认值:false。
resultSets 这个设置仅对多结果集的情况适用,它将列出语句执行后返回的结果集并每个结果集给一个名称,名称是逗号分隔的。

2.3、增、删、改标签