注意:以下几种方式可以组合使用。当遇到大型项目的时候这些方式都有局限性,
所以这里推荐使用sqlSessionFactory配置+mapperScannerConfConfigurer的方式来实现全局的自动配置。
第一种 使用类路径pei配置。配置dao接口的路径。
这种情况下,如果是非注解模式的话xml配置文件必须和这个类在同一级目录,且与Mapper类同名。
缺点:需要手动配置,文件多了之后不实用。
<configuration>
<mappers>
<!-- class 级别的指定 -->
<mapper class="com.bestcxx.stu.springmvc.mapper.UserModelMapper"/>
<mapper class="com.bestcxx.stu.springmvc.mapper.UserModelTwoMapper"/>
</mappers>
</configuration>
在存在 xml 配置文件的情况下,文件结构如下,没有 xml 配置文件的情况下,只有 Mapper 类
第二种 使用package包路径 配置 。
这种情况下,如果是非注解模式的话xml配置文件必须也处于同一级 package 下,且与Mapper类同名。
可以实现自动配置,但是局限于在同一个包之下,有局限性。
<configuration>
<mappers>
<package name="com.bestcxx.stu.springmvc.mapper"/>
</mappers>
</configuration>
文件结构
第三种 资源路径配置
是把 Mapper 的xml配置文件单独放置到 resources 中,和Mapper 类分开了
这种方式的好处是便于统一管理 xml 配置文件,不好的的地方是无法使用注解模式了,
缺点:需要手动引入映射文件,文件多了不实用。
<configuration>
<mappers>
<!-- 使用这个方案,可以单独指定Mapper的位置 -->
<mapper resource="mybatis/mappings/UserModelMapper.xml"/>
<mapper resource="mybatis/mappings/UserModelTwoMapper.xml"/>
</mappers>
</configuration>
然后Mapper类还是呆在该在的位置
但是mapper 的 xml文件的位置如下
上面的配置在mybitas-config.xml(名字是可以自定义的)中集中管理,Mapper 的 xml 文件需要关联
第一,是mybatis-config.xml 文件负责加载 Mapper的xml文件,即上面所说的配置是在这个mybatis-config.xml文件中
第二,mapper对应的xml 配置文件内部要指定对应的Mapper类
下面是单独测试mybatis的mybatas的配置文件的内容
<?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?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?) -->
<!-- 加载常量配置信息 -->
<!-- 可以覆盖重写,用于解密-http://www.mybatis.org/mybatis-3/zh/configuration.html
·在 properties 元素体内指定的属性首先被读取。
·然后根据 properties 元素中的 resource 属性读取类路径下属性文件或根据 url 属性指定的路径读取属性文件,并覆盖已读取的同名属性。
·最后读取作为方法参数传递的属性,并覆盖已读取的同名属性。 -->
<properties resource="config/jdbc.properties"/>
<!-- 为实体类定义别名 -->
<typeAliases>
<typeAlias alias="userModel" type="com.bestcxx.stu.springmvc.model.UserModel"/>
<typeAlias alias="userModelTwo" type="com.bestcxx.stu.springmvc.model.UserModelTwo"/>
</typeAliases>
<!-- 使用 spring 配置数据库的时候,下面就可以注释掉了 -->
<!-- 定义数据库信息,默认使用 development 数据库构建环境 -->
<environments default="development">
<environment id="development">
<!-- 采用 jdbc 事务管理 -->
<transactionManager type="JDBC"/>
<!-- 配置数据库链接信息 -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!-- 定义映射器 -->
<mappers>
<!-- 使用这个方案,可以单独指定Mapper的位置 -->
<!-- <mapper resource="mybatis/mappings/UserModelMapper.xml"/>
<mapper resource="mybatis/mappings/UserModelTwoMapper.xml"/> -->
<!-- 使用下面两个个方法,Mapper.xml 文件位置必须在和其内部 <mapper namespace="">的类在一起,当然,使用注解模式的话,Mapper.xml文件就没有必要存在了 -->
<!-- 直接指定一个包去扫描-内保包含多个Mapper配置- -->
<!-- <package name="com.bestcxx.stu.springmvc.mapper"/> -->
<!-- class 级别的指定 -->
<mapper class="com.bestcxx.stu.springmvc.mapper.UserModelMapper"/>
<mapper class="com.bestcxx.stu.springmvc.mapper.UserModelTwoMapper"/>
</mappers>
</configuration>