为什么分离

对于Maven项目,IntelliJ IDEA默认是不处理src/main/java中的非java文件的,不专门在pom.xml中配置<resources>是会报错的,参考这里

所以src/main/java中最好不要出现非java文件。实际上,将mapper.xml放在src/main/resources中比较合适。

如何分离

首先,mapper肯定是不能配在mybatis-config.xml的<Mappers>里了,因为里面的方式都需要接口和xml在统一文件夹下

    <!-- 将sql映射注册到全局配置中-->
<mappers> <!--
mapper 单个注册(mapper如果多的话,不太可能用这种方式)
resource:引用类路径下的文件
url:引用磁盘路径下的资源
class,引用接口
package 批量注册(基本上使用这种方式)
name:mapper接口与mapper.xml所在的包名
--> <!-- 第一种:注册sql映射文件-->
<mapper resource="com/spldeolin/mapper/UserMapper.xml" /> <!-- 第二种:注册接口 sql映射文件必须与接口同名,并且放在同一目录下-->
<!--<mapper class="com.spldeolin.mapper.UserMapper" />--> <!-- 第三种:注册基于注解的接口 基于注解 没有sql映射文件,所有的sql都是利用注解写在接口上-->
<!--<mapper class="com.spldeolin.mapper.TeacherMapper" />--> <!-- 第四种:批量注册 需要将sql配置文件和接口放到同一目录下-->
<package name="com.spldeolin.mapper" /> </mappers>

所以不在mybatis-config.xml中进行全局配置。

其次,在sqlSessionFactory.mapperLocations指定mapper.xml的路径,在mapperScannerConfigurer.basePackage指定mapper接口的包名。

最终,目录结构、spring-mybatis.xml和mybatis.xml大致是这样的。

目录结构(其他内容省略)

spring-mybatis.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!--DB配置文件-->
<context:property-placeholder location="classpath:db.properties"
ignore-unresolvable="true" /> <!--数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean> <!--qlSessionFactory-->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--Mybatis配置文件-->
<property name="configLocation"
value="classpath:mybatis-config.xml" />
<!--mapper.xml所在位置-->
<property name="mapperLocations" value="classpath:mapper/*Mapper.xml" />
<!--指定需要使用别名的PO类所在的包-->
<property name="typeAliasesPackage"
value="com.spldeolin.demoapp.po" />
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--mapper接口所在的包-->
<property name="basePackage" value="com.spldeolin.demoapp.dao" />
</bean> </beans>

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="logImpl" value="LOG4J2" />
<setting name="cacheEnabled" value="true" />
</settings> <!--全局分页插件-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql" />
<property name="offsetAsPageNum" value="true" />
<property name="rowBoundsWithCount" value="true" />
<property name="pageSizeZero" value="true" />
<property name="reasonable" value="false" />
<property name="returnPageInfo" value="check" />
<property name="params" value="pageNum=start;pageSize=limit;" />
</plugin>
</plugins> </configuration>

最新文章

  1. [LeetCode] Nested List Weight Sum 嵌套链表权重和
  2. Redis-cli命令最新总结
  3. 上传图片插件鼠标手cursor:pointer;不生效
  4. PHP常用函数封装
  5. String使用equals方法和==分别比较的是什么?
  6. Test Bench基础知识笔记
  7. TEdit,TMemo背景透明
  8. TreeView中右击直接获取节点的方法
  9. Ror初学笔记
  10. leetcode第17题--4Sum
  11. Java 枚举7常见种用法(转)
  12. sum(case when ct.tradeTotal &gt;= 0 then 1 else 0 end)的意思
  13. VUE项目 npm run build卡住不动,也不报错
  14. HDU6341 Let Sudoku Rotate (杭电多校4J)
  15. 20165206 2017-2018-2 《Java程序设计》第9周学习总结
  16. Javascript Module pattern template. Shows a class with a constructor and public/private methods/properties. Also shows compatibility with CommonJS(eg Node.JS) and AMD (eg requireJS) as well as in a br
  17. Linux实践二:模块
  18. crontab在/var/log/目录下没有cron.log文件
  19. MySQL 安全整理
  20. mysql 列转行处理

热门文章

  1. 记录用到的mssql的几个方法
  2. 网页包抓取工具Fiddler工具简单设置
  3. python day5 lambda,内置函数,文件操作,冒泡排序以及装饰器
  4. 原生JS-----一个剪刀石头布游戏
  5. React 中的 定义组件的 两种方式
  6. vue-cli脚手架——3.0版本项目案例
  7. 【kafka】一键启动kafka脚本
  8. redhat6.7 yum网络源配置
  9. GAE相关
  10. python3 queue队列