Mybatis03__配置文件简介
Mybatis配置文件详解
在mabatis中有两个配置文件,一个是全局配置文件,它是指导Mybatis如何运行的(官方的名称是applicationContext.xml),另外一个配置文件时dao接口的实现文件(xxxmapper.xml),它是指导哪个接口中的哪个方法是如何运行的.
一、全局配置文件(mybatis-conf.xml)
1、properties配置:mybatis用来加载外部properties配置文件用的,mybatis与spring整合之后会采用spring的方式来加载配置文件,具体用法如下:
01、jdbc.properties
1 2 3 4 | jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql: //localhost:3306/mybatis?serverTimezone=UTC jdbc.username=root jdbc.password=root |
02、全局配置文件引入jdbc.properties配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <configuration> // 加载类路径下的jdbc.properties配置文件 <properties resource= "jdbc.properties" ></properties> <environments default = "development" > <environment id= "development" > <transactionManager type= "JDBC" /> <dataSource type= "POOLED" > // jdbc.properties中的属性 <property name= "driver" value= "${jdbc.driver}" /> <property name= "url" value= "${jdbc.url}" /> <property name= "username" value= "${jdbc.username}" /> <property name= "password" value= "${jdbc.password}" /> </dataSource> </environment> </environments> </configuration> |
2、settings
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <settings> <setting name= "cacheEnabled" value= "true" /> <setting name= "lazyLoadingEnabled" value= "true" /> <setting name= "multipleResultSetsEnabled" value= "true" /> <setting name= "useColumnLabel" value= "true" /> <setting name= "useGeneratedKeys" value= "false" /> <setting name= "autoMappingBehavior" value= "PARTIAL" /> <setting name= "autoMappingUnknownColumnBehavior" value= "WARNING" /> <setting name= "defaultExecutorType" value= "SIMPLE" /> <setting name= "defaultStatementTimeout" value= "25" /> <setting name= "defaultFetchSize" value= "100" /> <setting name= "safeRowBoundsEnabled" value= "false" /> // 开启驼峰命名规则,默认值是false,当设置为true时,可以将数据库中 USER_NAME 的列名与实体类中的 userName 进行映射 <setting name= "mapUnderscoreToCamelCase" value= "false" /> <setting name= "localCacheScope" value= "SESSION" /> <setting name= "jdbcTypeForNull" value= "OTHER" /> <setting name= "lazyLoadTriggerMethods" value= "equals,clone,hashCode,toString" /> </settings> |
3、typeAliases:类型别名
1 2 3 4 5 6 7 8 | <typeAliases> // 如果不注明alias的值,那么别名默认使用的就是类名,并且对大小写不敏感,Atuhor和author均可以 <typeAlias type= "domain.blog.Author" /> // 别名设置为Blog,那么在xxxMapper.xml中使用Blog等同于domain.blog.Blog <typeAlias alias= "Blog" type= "domain.blog.Blog" /> // com.mybatis.entity下面所有的类都支持类型别名,由于没有指明alias属性,所以User、user都可以 < package name= "com.mybatis.entity" ></ package > </typeAliases> |
虽然支配各种方式来配置类型别名,但是还是建议在xxxMapper.xml中配置完整类名
1 2 3 4 5 6 | <mapper namespace= "com.mybatis.dao.UserMapper" > // 但是还是建议配置完整的包类路径,这样可以很好的识别映射的实体类 <select id= "queryUserById" resultType= "com.mybatis.entity.User" > select id,user_name as userName,age from user where id = #{id} </select> </mapper> |
4、Mappers映射器
mappers标签中有两个标签
1、mapper标签里面有三个取值
01、url:用来加载物理磁盘上或者网络上的配置文件
02、resource:用来加载类路径下的配置文件
03、class:基本不用
如果使用mapper一次只能加载一个mapper.xml文件,如果有大量的mapper.xml文件需要被加载就要写很多的mapper标签,针对这种情况我们使用package标签
2、package标签扫描包的形式批量引入xxxMapper.xml映射文件
用来加载某一个包下面的配置文件,但是需要将配置文件和接口放在同一个地方,并且接口的名字和xml的名字相同(原理是通过接口的名字,找到相同名字同一路径下的xml文件,然后加载xml文件)
但是实际情况下我们习惯把xxxMapper接口放置在 src/main/java下,而习惯把xxxMapper.xml配置文件放置在 src/main/resources,如何实现使xxxMapper接口和xxxMapper.xml编译之后能放置在一起呢?
具体步骤如下:(我这里是Maven工程)
1、项目的pom.xml中加上如下配置(当然其它方式还有很多)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>** /*.properties</include> <include>**/ *.xml</include> <include>** /*.tld</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/ *.properties</include> <include>** /*.xml</include> <include>**/ *.tld</include> </includes> <filtering> false </filtering> </resource> </resources> </build> |
2、在 src/main/resources下建立和xxxMapper文件一样的包结构(com.mybatis.dao,记住文件夹要一个一个的建),如左图,建立完以后,编译项目,如果编译之后发现xxxMapper接口和xxxMapper.xml都在同一目录下,那么就OK了
二、映射配置文件(xxxMapper.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?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.mybatis.dao.EmployeeMapper" > <insert id= "insertEmployee" useGeneratedKeys= "true" keyProperty= "employeeId" > INSERT INTO employee VALUES ( null ,#{employeeName},#{employeePassword},#{employeeAge},#{departmentId}) </insert> <select id= "queryEmployeeByEmployeeId" resultType= "com.mybatis.entity.Employee" > SELECT EMPLOYEE_ID,EMPLOYEE_NAME,EMPLOYEE_PASSWORD,EMPLOYEE_AGE,DEPARTMENT_ID FROM employee WHERE EMPLOYEE_ID=#{employeeId} </select> </mapper> 整个mapper.xml文件中只有一个标签mapper,下面这些就是mapper标签中的子标签 namespace:告诉mybatis这个配置文件是要实现哪一个接口 cache:和缓存有关 cache-ref:和缓存有关 parameterMap:参数map:废弃了,原本是来做复杂参数映射; resultMap:结果映射:自定义结果集的封装规则; sql:抽取可重用的sql; insert:添加 delete:删除 update:修改 select:查询 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?