找回密码
 立即注册
首页 业界区 业界 MyBatis与其使用方法讲解

MyBatis与其使用方法讲解

蓝娅萍 2025-6-4 22:40:38
ORM

在讲解Mybatis之前,我们需了解一个概念ORM(Object-Relational Mapping)对象关系映射,其是数据库与Java对象进行映射的一个技术.通过使用ORM,我们可以不用编写负责的Sql语句,而是通过操作对象来实现增删改查操作
缺优分析


  • 优点

    • 提高开发效率,减少代码的重复性和维护成本
    • 增加代码的可读性,降低复杂度
    • 对数据库查询的细节进行抽象,隐藏了sql语句

  • 缺点

    • 在进行多表联查时,或存在where条件时,ORM语句会变得复杂

MyBatis


  • mybatis是一个支持自定义SQL的持久层框架,通过XML文件来实现SQL配置和数据映射,MyBatis允许开发者手动编写SQL语句,提高灵活性
Mybatis通过mapper文件,将sql查询和Java对象绑定到一起,简化了JDBC代码的编写,手动设置参数,获取结果集的工作
MyBatis的工作流程


  • 其分为以下几步
1.png

MyBatis的基本使用

环境准备


  • 引入依赖包:
  1. <dependency>
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.example.mapper.UserMapper">
  6.    
  7. </mapper><groupId>mysql</groupId>
  8. <?xml version="1.0" encoding="UTF-8"?>
  9. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  10.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  11. <mapper namespace="com.example.mapper.UserMapper">
  12.    
  13. </mapper>mysql-connector-java</artifactId>
  14. <?xml version="1.0" encoding="UTF-8"?>
  15. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  16.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  17. <mapper namespace="com.example.mapper.UserMapper">
  18.    
  19. </mapper><version>8.0.23</version>
  20. </dependency>
复制代码

  • 创建mybatis配置文件(mybatis-config.xml)
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
  3. <configuration>
  4. <?xml version="1.0" encoding="UTF-8"?>
  5. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  6.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  7. <mapper namespace="com.example.mapper.UserMapper">
  8.    
  9. </mapper>
  10. <?xml version="1.0" encoding="UTF-8"?>
  11. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  12.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  13. <mapper namespace="com.example.mapper.UserMapper">
  14.    
  15. </mapper><environments default="mysql">
  16. <?xml version="1.0" encoding="UTF-8"?>
  17. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  18.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  19. <mapper namespace="com.example.mapper.UserMapper">
  20.    
  21. </mapper><?xml version="1.0" encoding="UTF-8"?>
  22. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  23.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  24. <mapper namespace="com.example.mapper.UserMapper">
  25.    
  26. </mapper><environment id="mysql">
  27. <?xml version="1.0" encoding="UTF-8"?>
  28. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  29.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  30. <mapper namespace="com.example.mapper.UserMapper">
  31.    
  32. </mapper><?xml version="1.0" encoding="UTF-8"?>
  33. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  34.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  35. <mapper namespace="com.example.mapper.UserMapper">
  36.    
  37. </mapper><?xml version="1.0" encoding="UTF-8"?>
  38. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  39.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  40. <mapper namespace="com.example.mapper.UserMapper">
  41.    
  42. </mapper>
  43. <?xml version="1.0" encoding="UTF-8"?>
  44. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  45.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  46. <mapper namespace="com.example.mapper.UserMapper">
  47.    
  48. </mapper><?xml version="1.0" encoding="UTF-8"?>
  49. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  50.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  51. <mapper namespace="com.example.mapper.UserMapper">
  52.    
  53. </mapper><?xml version="1.0" encoding="UTF-8"?>
  54. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  55.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  56. <mapper namespace="com.example.mapper.UserMapper">
  57.    
  58. </mapper><transactionManager type="JDBC"></transactionManager>
  59. <?xml version="1.0" encoding="UTF-8"?>
  60. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  61.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  62. <mapper namespace="com.example.mapper.UserMapper">
  63.    
  64. </mapper><?xml version="1.0" encoding="UTF-8"?>
  65. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  66.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  67. <mapper namespace="com.example.mapper.UserMapper">
  68.    
  69. </mapper><?xml version="1.0" encoding="UTF-8"?>
  70. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  71.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  72. <mapper namespace="com.example.mapper.UserMapper">
  73.    
  74. </mapper>
  75. <?xml version="1.0" encoding="UTF-8"?>
  76. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  77.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  78. <mapper namespace="com.example.mapper.UserMapper">
  79.    
  80. </mapper><?xml version="1.0" encoding="UTF-8"?>
  81. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  82.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  83. <mapper namespace="com.example.mapper.UserMapper">
  84.    
  85. </mapper><?xml version="1.0" encoding="UTF-8"?>
  86. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  87.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  88. <mapper namespace="com.example.mapper.UserMapper">
  89.    
  90. </mapper><dataSource type="POOLED">
  91. <?xml version="1.0" encoding="UTF-8"?>
  92. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  93.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  94. <mapper namespace="com.example.mapper.UserMapper">
  95.    
  96. </mapper><?xml version="1.0" encoding="UTF-8"?>
  97. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  98.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  99. <mapper namespace="com.example.mapper.UserMapper">
  100.    
  101. </mapper><?xml version="1.0" encoding="UTF-8"?>
  102. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  103.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  104. <mapper namespace="com.example.mapper.UserMapper">
  105.    
  106. </mapper><?xml version="1.0" encoding="UTF-8"?>
  107. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  108.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  109. <mapper namespace="com.example.mapper.UserMapper">
  110.    
  111. </mapper><property name="driver" value="com.mysql.cj.jdbc.Driver"/>
  112. <?xml version="1.0" encoding="UTF-8"?>
  113. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  114.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  115. <mapper namespace="com.example.mapper.UserMapper">
  116.    
  117. </mapper><?xml version="1.0" encoding="UTF-8"?>
  118. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  119.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  120. <mapper namespace="com.example.mapper.UserMapper">
  121.    
  122. </mapper><?xml version="1.0" encoding="UTF-8"?>
  123. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  124.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  125. <mapper namespace="com.example.mapper.UserMapper">
  126.    
  127. </mapper><?xml version="1.0" encoding="UTF-8"?>
  128. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  129.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  130. <mapper namespace="com.example.mapper.UserMapper">
  131.    
  132. </mapper><property name="url" value="jdbc:mysql://localhost:3306/数据库名称?userSSL=false&serverTimezone=Asia/Shanghai"/>
  133. <?xml version="1.0" encoding="UTF-8"?>
  134. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  135.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  136. <mapper namespace="com.example.mapper.UserMapper">
  137.    
  138. </mapper><?xml version="1.0" encoding="UTF-8"?>
  139. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  140.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  141. <mapper namespace="com.example.mapper.UserMapper">
  142.    
  143. </mapper><?xml version="1.0" encoding="UTF-8"?>
  144. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  145.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  146. <mapper namespace="com.example.mapper.UserMapper">
  147.    
  148. </mapper><?xml version="1.0" encoding="UTF-8"?>
  149. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  150.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  151. <mapper namespace="com.example.mapper.UserMapper">
  152.    
  153. </mapper><property name="username" value="帐号"/>
  154. <?xml version="1.0" encoding="UTF-8"?>
  155. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  156.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  157. <mapper namespace="com.example.mapper.UserMapper">
  158.    
  159. </mapper><?xml version="1.0" encoding="UTF-8"?>
  160. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  161.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  162. <mapper namespace="com.example.mapper.UserMapper">
  163.    
  164. </mapper><?xml version="1.0" encoding="UTF-8"?>
  165. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  166.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  167. <mapper namespace="com.example.mapper.UserMapper">
  168.    
  169. </mapper><?xml version="1.0" encoding="UTF-8"?>
  170. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  171.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  172. <mapper namespace="com.example.mapper.UserMapper">
  173.    
  174. </mapper><property name="password" value="密码"/>
  175. <?xml version="1.0" encoding="UTF-8"?>
  176. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  177.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  178. <mapper namespace="com.example.mapper.UserMapper">
  179.    
  180. </mapper><?xml version="1.0" encoding="UTF-8"?>
  181. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  182.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  183. <mapper namespace="com.example.mapper.UserMapper">
  184.    
  185. </mapper><?xml version="1.0" encoding="UTF-8"?>
  186. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  187.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  188. <mapper namespace="com.example.mapper.UserMapper">
  189.    
  190. </mapper></dataSource>
  191. <?xml version="1.0" encoding="UTF-8"?>
  192. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  193.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  194. <mapper namespace="com.example.mapper.UserMapper">
  195.    
  196. </mapper><?xml version="1.0" encoding="UTF-8"?>
  197. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  198.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  199. <mapper namespace="com.example.mapper.UserMapper">
  200.    
  201. </mapper></environment>
  202. <?xml version="1.0" encoding="UTF-8"?>
  203. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  204.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  205. <mapper namespace="com.example.mapper.UserMapper">
  206.    
  207. </mapper></environments>
  208. <?xml version="1.0" encoding="UTF-8"?>
  209. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  210.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  211. <mapper namespace="com.example.mapper.UserMapper">
  212.    
  213. </mapper>
  214. <?xml version="1.0" encoding="UTF-8"?>
  215. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  216.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  217. <mapper namespace="com.example.mapper.UserMapper">
  218.    
  219. </mapper>
  220. <?xml version="1.0" encoding="UTF-8"?>
  221. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  222.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  223. <mapper namespace="com.example.mapper.UserMapper">
  224.    
  225. </mapper><mappers>
  226. <?xml version="1.0" encoding="UTF-8"?>
  227. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  228.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  229. <mapper namespace="com.example.mapper.UserMapper">
  230.    
  231. </mapper><?xml version="1.0" encoding="UTF-8"?>
  232. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  233.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  234. <mapper namespace="com.example.mapper.UserMapper">
  235.    
  236. </mapper><mapper resource="映射配置文件全路径"></mapper>
  237. <?xml version="1.0" encoding="UTF-8"?>
  238. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  239.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  240. <mapper namespace="com.example.mapper.UserMapper">
  241.    
  242. </mapper></mappers>
  243. </configuration>
复制代码

  • springboot中的application.yml
  1. mybatis:
  2.   # mapper配置文件
  3.   mapper-locations: classpath:mapper/*.xml
  4.   # resultType别名,没有这个配置resultType包名要写全,配置后只要写类名
  5.   type-aliases-package: com.mashang.xiaomistore.domain
  6.   configuration:
  7. <?xml version="1.0" encoding="UTF-8"?>
  8. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  9.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  10. <mapper namespace="com.example.mapper.UserMapper">
  11.    
  12. </mapper>#下划线自动转驼峰
  13. <?xml version="1.0" encoding="UTF-8"?>
  14. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  15.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  16. <mapper namespace="com.example.mapper.UserMapper">
  17.    
  18. </mapper>map-underscore-to-camel-case: true
复制代码

  • 创建Mapper映射文件
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.company.mapper.StudentMapper">
  4. <?xml version="1.0" encoding="UTF-8"?>
  5. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  6.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  7. <mapper namespace="com.example.mapper.UserMapper">
  8.    
  9. </mapper><select id="queryAll" resultType="com.company.entity.Student">
  10. <?xml version="1.0" encoding="UTF-8"?>
  11. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  12.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  13. <mapper namespace="com.example.mapper.UserMapper">
  14.    
  15. </mapper><?xml version="1.0" encoding="UTF-8"?>
  16. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  17.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  18. <mapper namespace="com.example.mapper.UserMapper">
  19.    
  20. </mapper>SELECT * FROM student
  21. <?xml version="1.0" encoding="UTF-8"?>
  22. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  23.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  24. <mapper namespace="com.example.mapper.UserMapper">
  25.    
  26. </mapper></select>
  27. </mapper>
复制代码
2.png


  • 创建Mapper接口:
  1. public interface StudentMapper {
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.example.mapper.UserMapper">
  6.    
  7. </mapper>List<Student> queryAll();
  8. }
复制代码
3.png

MyBatis日志配置


  • 引入SpringBoot中的log4j
  1. <dependency>
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.example.mapper.UserMapper">
  6.    
  7. </mapper><groupId>org.springframework.boot</groupId>
  8. <?xml version="1.0" encoding="UTF-8"?>
  9. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  10.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  11. <mapper namespace="com.example.mapper.UserMapper">
  12.    
  13. </mapper>spring-boot-starter-log4j2</artifactId>
  14. <?xml version="1.0" encoding="UTF-8"?>
  15. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  16.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  17. <mapper namespace="com.example.mapper.UserMapper">
  18.    
  19. </mapper><version>3.4.3</version>
  20. </dependency>
复制代码

  • 在SpringBoot中在application.yml中mybatis配置项中进行配置
  1. mybatis:
  2.   configuration:
  3. <?xml version="1.0" encoding="UTF-8"?>
  4. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  5.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  6. <mapper namespace="com.example.mapper.UserMapper">
  7.    
  8. </mapper>log-impl: org.apache.ibatis.logging.log4j.Log4jImpl
复制代码

  • 配置log4j.properties文件
  1. ### 设置###
  2. log4j.rootLogger = debug,stdout,D,E
  3. ### 输出信息到控制抬 ###
  4. log4j.appender.stdout = org.apache.log4j.ConsoleAppender
  5. log4j.appender.stdout.Target = System.out
  6. log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
  7. log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
  8. ### 输出DEBUG 级别以上的日志到=D://logs/error.log ###
  9. log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
  10. log4j.appender.D.File = D://logs/log.log
  11. log4j.appender.D.Append = true
  12. log4j.appender.D.Threshold = DEBUG
  13. log4j.appender.D.layout = org.apache.log4j.PatternLayout
  14. log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
  15. ### 输出ERROR 级别以上的日志到=D://logs/error.log ###
  16. log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
  17. log4j.appender.E.File =D://logs/error.log
  18. log4j.appender.E.Append = true
  19. log4j.appender.E.Threshold = ERROR
  20. log4j.appender.E.layout = org.apache.log4j.PatternLayout
  21. log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
复制代码

  • 一个基本的mybatis的XML模板如下
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="com.example.mapper.UserMapper">
    5.    
    6. </mapper>
    复制代码
CURD实现

select查询


  • 使用标签实现基本查询
    1. <select id ="getUserById" resultType="com.company.domain.entity.User">
    2.                 SELECT * FORM user
    3. </select>
    复制代码

    • id:对应Mapper接口中的方法名,必须一致
    • resultType:指定返回结果映射到哪个Java类

  • 传参#{}与${}的区别

    • #{}的特点

      • 事先进行预编译:使用#{}的参数会被Mybatis当作JDBC中的?占位符
      • 防止sql注入:由于会事先进行预编译,Mybatis能够防止Sql注入
      • 类型转换:会根据参数类型进行适当的类型转换

    • ${}的特点

      • 字符串拼接:&{}直接将字符串进行替换,相当于在Sql中直接拼接传入的参数
      • 存在sql注入的风险:没有预编译,会引发sql注入问题


  • 多条件查询
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="com.example.mapper.UserMapper">
    5.    
    6. </mapper>SELECT * FROM user<?xml version="1.0" encoding="UTF-8"?>
    7. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    8.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    9. <mapper namespace="com.example.mapper.UserMapper">
    10.    
    11. </mapper> WHERE name = #{name} AND age = #{age}
    复制代码
  • 模糊查询
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="com.example.mapper.UserMapper">
    5.    
    6. </mapper><?xml version="1.0" encoding="UTF-8"?>
    7. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    8.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    9. <mapper namespace="com.example.mapper.UserMapper">
    10.    
    11. </mapper>SELECT *<?xml version="1.0" encoding="UTF-8"?>
    12. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    13.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    14. <mapper namespace="com.example.mapper.UserMapper">
    15.    
    16. </mapper><?xml version="1.0" encoding="UTF-8"?>
    17. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    18.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    19. <mapper namespace="com.example.mapper.UserMapper">
    20.    
    21. </mapper>FROM user<?xml version="1.0" encoding="UTF-8"?>
    22. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    23.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    24. <mapper namespace="com.example.mapper.UserMapper">
    25.    
    26. </mapper><?xml version="1.0" encoding="UTF-8"?>
    27. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    28.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    29. <mapper namespace="com.example.mapper.UserMapper">
    30.    
    31. </mapper>WHERE name LIKE CONCAT('%', #{name}, '%')
    复制代码

    • 使用LIKE关键字和CONCAT()函数进行查询

insert插入


  • 使用标签实现基本插入操作
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="com.example.mapper.UserMapper">
    5.    
    6. </mapper><?xml version="1.0" encoding="UTF-8"?>
    7. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    8.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    9. <mapper namespace="com.example.mapper.UserMapper">
    10.    
    11. </mapper><?xml version="1.0" encoding="UTF-8"?>
    12. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    13.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    14. <mapper namespace="com.example.mapper.UserMapper">
    15.    
    16. </mapper>INSERT INTO user(name, age)<?xml version="1.0" encoding="UTF-8"?>
    17. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    18.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    19. <mapper namespace="com.example.mapper.UserMapper">
    20.    
    21. </mapper><?xml version="1.0" encoding="UTF-8"?>
    22. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    23.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    24. <mapper namespace="com.example.mapper.UserMapper">
    25.    
    26. </mapper>VALUES (#{name}, #{age})<?xml version="1.0" encoding="UTF-8"?>
    27. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    28.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    29. <mapper namespace="com.example.mapper.UserMapper">
    30.    
    31. </mapper>
    复制代码

    • parameterType:表示入参类型

  • 实现回填自增主键

    • 使用userGeneratedKeys和keyProperty实现
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="com.example.mapper.UserMapper">
    5.    
    6. </mapper>INSERT INTO user (name, age)<?xml version="1.0" encoding="UTF-8"?>
    7. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    8.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    9. <mapper namespace="com.example.mapper.UserMapper">
    10.    
    11. </mapper> VALUES (#{name}, #{age})
    复制代码

    • userGeneratedKeys:表示是否启动自增
    • keyProperty:表示将生成的主键赋值给哪个java对象的哪个属性如user.id

update更新


  • 使用标签实现基本更新
    1.         <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="com.example.mapper.UserMapper">
    5.    
    6. </mapper><?xml version="1.0" encoding="UTF-8"?>
    7. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    8.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    9. <mapper namespace="com.example.mapper.UserMapper">
    10.    
    11. </mapper> UPDATE user<?xml version="1.0" encoding="UTF-8"?>
    12. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    13.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    14. <mapper namespace="com.example.mapper.UserMapper">
    15.    
    16. </mapper><?xml version="1.0" encoding="UTF-8"?>
    17. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    18.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    19. <mapper namespace="com.example.mapper.UserMapper">
    20.    
    21. </mapper>SET name=#{name},<?xml version="1.0" encoding="UTF-8"?>
    22. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    23.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    24. <mapper namespace="com.example.mapper.UserMapper">
    25.    
    26. </mapper><?xml version="1.0" encoding="UTF-8"?>
    27. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    28.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    29. <mapper namespace="com.example.mapper.UserMapper">
    30.    
    31. </mapper><?xml version="1.0" encoding="UTF-8"?>
    32. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    33.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    34. <mapper namespace="com.example.mapper.UserMapper">
    35.    
    36. </mapper>age =#{age}<?xml version="1.0" encoding="UTF-8"?>
    37. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    38.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    39. <mapper namespace="com.example.mapper.UserMapper">
    40.    
    41. </mapper><?xml version="1.0" encoding="UTF-8"?>
    42. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    43.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    44. <mapper namespace="com.example.mapper.UserMapper">
    45.    
    46. </mapper>WHERE id = #{id}<?xml version="1.0" encoding="UTF-8"?>
    47. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    48.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    49. <mapper namespace="com.example.mapper.UserMapper">
    50.    
    51. </mapper>
    复制代码
delete删除


  • 使用标签基本查询
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="com.example.mapper.UserMapper">
    5.    
    6. </mapper><?xml version="1.0" encoding="UTF-8"?>
    7. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    8.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    9. <mapper namespace="com.example.mapper.UserMapper">
    10.    
    11. </mapper><?xml version="1.0" encoding="UTF-8"?>
    12. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    13.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    14. <mapper namespace="com.example.mapper.UserMapper">
    15.    
    16. </mapper>DELETE<?xml version="1.0" encoding="UTF-8"?>
    17. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    18.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    19. <mapper namespace="com.example.mapper.UserMapper">
    20.    
    21. </mapper><?xml version="1.0" encoding="UTF-8"?>
    22. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    23.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    24. <mapper namespace="com.example.mapper.UserMapper">
    25.    
    26. </mapper>FROM user<?xml version="1.0" encoding="UTF-8"?>
    27. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    28.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    29. <mapper namespace="com.example.mapper.UserMapper">
    30.    
    31. </mapper><?xml version="1.0" encoding="UTF-8"?>
    32. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    33.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    34. <mapper namespace="com.example.mapper.UserMapper">
    35.    
    36. </mapper>WHERE id = #{id}<?xml version="1.0" encoding="UTF-8"?>
    37. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    38.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    39. <mapper namespace="com.example.mapper.UserMapper">
    40.    
    41. </mapper>
    复制代码
传参方式


  • 多参数传参(使用@Param)

    • 当方法有多个参数时,使用@Param注解明确参数名
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="com.example.mapper.UserMapper">
    5.    
    6. </mapper>SELECT * FROM user<?xml version="1.0" encoding="UTF-8"?>
    7. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    8.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    9. <mapper namespace="com.example.mapper.UserMapper">
    10.    
    11. </mapper> WHERE name = #{name} AND age = #{age}
    复制代码

    • Mapper接口
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="com.example.mapper.UserMapper">
    5.    
    6. </mapper>User getUserByNameAndAge(<?xml version="1.0" encoding="UTF-8"?>
    7. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    8.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    9. <mapper namespace="com.example.mapper.UserMapper">
    10.    
    11. </mapper><?xml version="1.0" encoding="UTF-8"?>
    12. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    13.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    14. <mapper namespace="com.example.mapper.UserMapper">
    15.    
    16. </mapper><?xml version="1.0" encoding="UTF-8"?>
    17. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    18.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    19. <mapper namespace="com.example.mapper.UserMapper">
    20.    
    21. </mapper>@Param("name") String name,<?xml version="1.0" encoding="UTF-8"?>
    22. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    23.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    24. <mapper namespace="com.example.mapper.UserMapper">
    25.    
    26. </mapper><?xml version="1.0" encoding="UTF-8"?>
    27. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    28.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    29. <mapper namespace="com.example.mapper.UserMapper">
    30.    
    31. </mapper><?xml version="1.0" encoding="UTF-8"?>
    32. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    33.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    34. <mapper namespace="com.example.mapper.UserMapper">
    35.    
    36. </mapper>@Param("age") Integer age);
    复制代码
  • 对象参数

    • 当参数为一个Java对象时,MyBatis自动将对象属性映射到Sql语句中的占位符
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="com.example.mapper.UserMapper">
    5.    
    6. </mapper>INSERT INTO user (name, age)<?xml version="1.0" encoding="UTF-8"?>
    7. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    8.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    9. <mapper namespace="com.example.mapper.UserMapper">
    10.    
    11. </mapper> VALUES (#{name}, #{age})
    复制代码

    • #{name}对应user.name,#{age}对应user.age
    • Mapper接口
    1. void insertUser(User user);
    复制代码
  • Map参数

    • 通过Map传递多个参数或动态参数
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="com.example.mapper.UserMapper">
    5.    
    6. </mapper>SELECT * FROM user<?xml version="1.0" encoding="UTF-8"?>
    7. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    8.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    9. <mapper namespace="com.example.mapper.UserMapper">
    10.    
    11. </mapper> WHERE name = #{name} AND age = #{age}
    复制代码

    • Mapper接口
    1. User getUserByMap(Map<String, Object> params);
    复制代码
  • 集合/数组参数

    • 适用于批量查询,比如WHERE id IN (…)
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="com.example.mapper.UserMapper">
    5.    
    6. </mapper>SELECT * FROM user<?xml version="1.0" encoding="UTF-8"?>
    7. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    8.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    9. <mapper namespace="com.example.mapper.UserMapper">
    10.    
    11. </mapper> WHERE id IN<?xml version="1.0" encoding="UTF-8"?>
    12. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    13.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    14. <mapper namespace="com.example.mapper.UserMapper">
    15.    
    16. </mapper><?xml version="1.0" encoding="UTF-8"?>
    17. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    18.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    19. <mapper namespace="com.example.mapper.UserMapper">
    20.    
    21. </mapper><?xml version="1.0" encoding="UTF-8"?>
    22. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    23.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    24. <mapper namespace="com.example.mapper.UserMapper">
    25.    
    26. </mapper>#{id}<?xml version="1.0" encoding="UTF-8"?>
    27. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    28.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    29. <mapper namespace="com.example.mapper.UserMapper">
    30.    
    31. </mapper>
    复制代码

    • 是动态sql中的知识点等下会系统讲解
    • Mapper接口
    1. List<User> getUsersByIds(@Param("ids") List<Integer> ids);
    复制代码
动态sql

与标签


  • :生成WHERE子句,并自动判断去掉开头多余的AND/OR关键字,使sql更简洁
  • :用于判断传参条件,根据条件决定是否拼接某段SQL语句,适用于传参条件不固定,只有在满足条件时接入某个子串
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.example.mapper.UserMapper">
  5.    
  6. </mapper>SELECT * FROM user<?xml version="1.0" encoding="UTF-8"?>
  7. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  8.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  9. <mapper namespace="com.example.mapper.UserMapper">
  10.    
  11. </mapper><?xml version="1.0" encoding="UTF-8"?>
  12. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  13.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  14. <mapper namespace="com.example.mapper.UserMapper">
  15.    
  16. </mapper><?xml version="1.0" encoding="UTF-8"?>
  17. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  18.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  19. <mapper namespace="com.example.mapper.UserMapper">
  20.    
  21. </mapper><?xml version="1.0" encoding="UTF-8"?>
  22. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  23.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  24. <mapper namespace="com.example.mapper.UserMapper">
  25.    
  26. </mapper><?xml version="1.0" encoding="UTF-8"?>
  27. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  28.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  29. <mapper namespace="com.example.mapper.UserMapper">
  30.    
  31. </mapper><?xml version="1.0" encoding="UTF-8"?>
  32. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  33.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  34. <mapper namespace="com.example.mapper.UserMapper">
  35.    
  36. </mapper>AND name LIKE CONCAT('%', #{name}, '%')<?xml version="1.0" encoding="UTF-8"?>
  37. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  38.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  39. <mapper namespace="com.example.mapper.UserMapper">
  40.    
  41. </mapper><?xml version="1.0" encoding="UTF-8"?>
  42. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  43.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  44. <mapper namespace="com.example.mapper.UserMapper">
  45.    
  46. </mapper><?xml version="1.0" encoding="UTF-8"?>
  47. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  48.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  49. <mapper namespace="com.example.mapper.UserMapper">
  50.    
  51. </mapper><?xml version="1.0" encoding="UTF-8"?>
  52. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  53.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  54. <mapper namespace="com.example.mapper.UserMapper">
  55.    
  56. </mapper><?xml version="1.0" encoding="UTF-8"?>
  57. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  58.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  59. <mapper namespace="com.example.mapper.UserMapper">
  60.    
  61. </mapper><?xml version="1.0" encoding="UTF-8"?>
  62. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  63.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  64. <mapper namespace="com.example.mapper.UserMapper">
  65.    
  66. </mapper><?xml version="1.0" encoding="UTF-8"?>
  67. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  68.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  69. <mapper namespace="com.example.mapper.UserMapper">
  70.    
  71. </mapper>AND age = #{age}<?xml version="1.0" encoding="UTF-8"?>
  72. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  73.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  74. <mapper namespace="com.example.mapper.UserMapper">
  75.    
  76. </mapper><?xml version="1.0" encoding="UTF-8"?>
  77. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  78.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  79. <mapper namespace="com.example.mapper.UserMapper">
  80.    
  81. </mapper><?xml version="1.0" encoding="UTF-8"?>
  82. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  83.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  84. <mapper namespace="com.example.mapper.UserMapper">
  85.    
  86. </mapper>
复制代码

  • 当name为非空,会添加AND name LIKE CONCAT('%', #{name}, '%') 当age为非空时会添加 AND age = #{age}
  • 结合标签使用,能自动处理首个AND,使得SQL语句正确



  • 其作用在于动态拼接SQL片段前添加或去除特点字符,比如前缀,后缀,以及多余的分隔符如,
  • 常用于INSERT和UPDATE语句,避免出现多余逗号
INSERT语句
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.example.mapper.UserMapper">
  5.    
  6. </mapper>INSERT INTO user<?xml version="1.0" encoding="UTF-8"?>
  7. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  8.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  9. <mapper namespace="com.example.mapper.UserMapper">
  10.    
  11. </mapper><?xml version="1.0" encoding="UTF-8"?>
  12. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  13.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  14. <mapper namespace="com.example.mapper.UserMapper">
  15.    
  16. </mapper><?xml version="1.0" encoding="UTF-8"?>
  17. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  18.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  19. <mapper namespace="com.example.mapper.UserMapper">
  20.    
  21. </mapper>name,<?xml version="1.0" encoding="UTF-8"?>
  22. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  23.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  24. <mapper namespace="com.example.mapper.UserMapper">
  25.    
  26. </mapper><?xml version="1.0" encoding="UTF-8"?>
  27. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  28.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  29. <mapper namespace="com.example.mapper.UserMapper">
  30.    
  31. </mapper>age,<?xml version="1.0" encoding="UTF-8"?>
  32. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  33.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  34. <mapper namespace="com.example.mapper.UserMapper">
  35.    
  36. </mapper><?xml version="1.0" encoding="UTF-8"?>
  37. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  38.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  39. <mapper namespace="com.example.mapper.UserMapper">
  40.    
  41. </mapper>email,<?xml version="1.0" encoding="UTF-8"?>
  42. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  43.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  44. <mapper namespace="com.example.mapper.UserMapper">
  45.    
  46. </mapper><?xml version="1.0" encoding="UTF-8"?>
  47. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  48.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  49. <mapper namespace="com.example.mapper.UserMapper">
  50.    
  51. </mapper>VALUES<?xml version="1.0" encoding="UTF-8"?>
  52. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  53.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  54. <mapper namespace="com.example.mapper.UserMapper">
  55.    
  56. </mapper><?xml version="1.0" encoding="UTF-8"?>
  57. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  58.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  59. <mapper namespace="com.example.mapper.UserMapper">
  60.    
  61. </mapper><?xml version="1.0" encoding="UTF-8"?>
  62. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  63.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  64. <mapper namespace="com.example.mapper.UserMapper">
  65.    
  66. </mapper>#{name},<?xml version="1.0" encoding="UTF-8"?>
  67. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  68.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  69. <mapper namespace="com.example.mapper.UserMapper">
  70.    
  71. </mapper><?xml version="1.0" encoding="UTF-8"?>
  72. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  73.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  74. <mapper namespace="com.example.mapper.UserMapper">
  75.    
  76. </mapper>#{age},<?xml version="1.0" encoding="UTF-8"?>
  77. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  78.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  79. <mapper namespace="com.example.mapper.UserMapper">
  80.    
  81. </mapper><?xml version="1.0" encoding="UTF-8"?>
  82. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  83.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  84. <mapper namespace="com.example.mapper.UserMapper">
  85.    
  86. </mapper>#{email},<?xml version="1.0" encoding="UTF-8"?>
  87. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  88.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  89. <mapper namespace="com.example.mapper.UserMapper">
  90.    
  91. </mapper>
复制代码

  • 标签包裹字段列表和对应值部分
  • suffixOverrides=”,” 表示自动去除多余的逗号,确保sql语法正确
UPDATE语句

  • 标签是的特性化
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.example.mapper.UserMapper">
  5.    
  6. </mapper>UPDATE user<?xml version="1.0" encoding="UTF-8"?>
  7. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  8.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  9. <mapper namespace="com.example.mapper.UserMapper">
  10.    
  11. </mapper><?xml version="1.0" encoding="UTF-8"?>
  12. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  13.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  14. <mapper namespace="com.example.mapper.UserMapper">
  15.    
  16. </mapper><?xml version="1.0" encoding="UTF-8"?>
  17. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  18.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  19. <mapper namespace="com.example.mapper.UserMapper">
  20.    
  21. </mapper>name = #{name},<?xml version="1.0" encoding="UTF-8"?>
  22. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  23.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  24. <mapper namespace="com.example.mapper.UserMapper">
  25.    
  26. </mapper><?xml version="1.0" encoding="UTF-8"?>
  27. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  28.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  29. <mapper namespace="com.example.mapper.UserMapper">
  30.    
  31. </mapper>age = #{age},<?xml version="1.0" encoding="UTF-8"?>
  32. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  33.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  34. <mapper namespace="com.example.mapper.UserMapper">
  35.    
  36. </mapper><?xml version="1.0" encoding="UTF-8"?>
  37. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  38.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  39. <mapper namespace="com.example.mapper.UserMapper">
  40.    
  41. </mapper>email = #{email},<?xml version="1.0" encoding="UTF-8"?>
  42. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  43.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  44. <mapper namespace="com.example.mapper.UserMapper">
  45.    
  46. </mapper><?xml version="1.0" encoding="UTF-8"?>
  47. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  48.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  49. <mapper namespace="com.example.mapper.UserMapper">
  50.    
  51. </mapper>WHERE id = #{id}
复制代码

  • 标签内部原理类似,会自动去除多余逗号



  • 用于遍历集合,数组和Map,常用于批量操作或动态生成IN子句
  • 其主要属性

    • collection:集合或数组名称(可用@Param()指定对应名称,默认为list或array)
    • item:循环时每个元素的别名
    • open:循环生成sql片段的前缀
    • separator:循环时的分隔符
    • close:循环生成sql片段的后缀

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.example.mapper.UserMapper">
  5.    
  6. </mapper>SELECT * FROM user<?xml version="1.0" encoding="UTF-8"?>
  7. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  8.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  9. <mapper namespace="com.example.mapper.UserMapper">
  10.    
  11. </mapper><?xml version="1.0" encoding="UTF-8"?>
  12. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  13.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  14. <mapper namespace="com.example.mapper.UserMapper">
  15.    
  16. </mapper><?xml version="1.0" encoding="UTF-8"?>
  17. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  18.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  19. <mapper namespace="com.example.mapper.UserMapper">
  20.    
  21. </mapper><?xml version="1.0" encoding="UTF-8"?>
  22. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  23.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  24. <mapper namespace="com.example.mapper.UserMapper">
  25.    
  26. </mapper><?xml version="1.0" encoding="UTF-8"?>
  27. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  28.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  29. <mapper namespace="com.example.mapper.UserMapper">
  30.    
  31. </mapper><?xml version="1.0" encoding="UTF-8"?>
  32. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  33.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  34. <mapper namespace="com.example.mapper.UserMapper">
  35.    
  36. </mapper>AND id IN<?xml version="1.0" encoding="UTF-8"?>
  37. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  38.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  39. <mapper namespace="com.example.mapper.UserMapper">
  40.    
  41. </mapper><?xml version="1.0" encoding="UTF-8"?>
  42. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  43.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  44. <mapper namespace="com.example.mapper.UserMapper">
  45.    
  46. </mapper><?xml version="1.0" encoding="UTF-8"?>
  47. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  48.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  49. <mapper namespace="com.example.mapper.UserMapper">
  50.    
  51. </mapper><?xml version="1.0" encoding="UTF-8"?>
  52. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  53.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  54. <mapper namespace="com.example.mapper.UserMapper">
  55.    
  56. </mapper><?xml version="1.0" encoding="UTF-8"?>
  57. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  58.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  59. <mapper namespace="com.example.mapper.UserMapper">
  60.    
  61. </mapper><?xml version="1.0" encoding="UTF-8"?>
  62. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  63.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  64. <mapper namespace="com.example.mapper.UserMapper">
  65.    
  66. </mapper><?xml version="1.0" encoding="UTF-8"?>
  67. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  68.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  69. <mapper namespace="com.example.mapper.UserMapper">
  70.    
  71. </mapper>#{id}<?xml version="1.0" encoding="UTF-8"?>
  72. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  73.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  74. <mapper namespace="com.example.mapper.UserMapper">
  75.    
  76. </mapper><?xml version="1.0" encoding="UTF-8"?>
  77. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  78.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  79. <mapper namespace="com.example.mapper.UserMapper">
  80.    
  81. </mapper><?xml version="1.0" encoding="UTF-8"?>
  82. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  83.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  84. <mapper namespace="com.example.mapper.UserMapper">
  85.    
  86. </mapper><?xml version="1.0" encoding="UTF-8"?>
  87. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  88.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  89. <mapper namespace="com.example.mapper.UserMapper">
  90.    
  91. </mapper><?xml version="1.0" encoding="UTF-8"?>
  92. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  93.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  94. <mapper namespace="com.example.mapper.UserMapper">
  95.    
  96. </mapper><?xml version="1.0" encoding="UTF-8"?>
  97. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  98.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  99. <mapper namespace="com.example.mapper.UserMapper">
  100.    
  101. </mapper>
复制代码

  • 当ids不为null时,进入生成的sql片段为 SELECT * FROM AND  WHERE id IN(#{id},#{id},…)
  • 其中标签遍历集合ids,用逗号进行分隔,并在开头添加(括号,结尾添加)括号
  • 最终标签会去除第一个AND,使sql合法SELECT * FROM id WHERE IN(#{id},#{id},…)
MyBatis的映射

基本映射

用于单一的字段对应


  • 假设有一个user表,其中有字段id,name,age其在Java中有个简单的对应类User,其属性分别也是id,name,age那么在Mapper.xml进行select查询时
    1. SELECT id, name, age FROM user WHERE id = #{id}
    复制代码
  • MyBatis会将查询的结果中每一列值自动赋值给User对象中相同的属性

    • 数据库列表的id→User对象的id
    • 数据库列表的name→User对象的name
    • 数据库列表的age→User对象的age

  • 这样可能就会出现一种情况,数据库列表的列名与对象的属性名不一致,通常使用开启驼峰转换来解决→在application.yml的mybatis配置中添加如下配置:
    1. mybatis:<?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="com.example.mapper.UserMapper">
    5.    
    6. </mapper>map-underscore-to-camel-case: true
    复制代码
一对一映射

当查询中需要查询一个对象时


  • 现假设,数据库有两张表一个user(用户)表,另一个user_detail(用户详细信息)表,在Java中我们可以创建两个类User和UserDetail类
  • 然后再创建一个UserVo类,其中包含User的属性和一个UserDatail对象
  1. public class User {<?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.example.mapper.UserMapper">
  5.    
  6. </mapper>private Integer id;<?xml version="1.0" encoding="UTF-8"?>
  7. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  8.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  9. <mapper namespace="com.example.mapper.UserMapper">
  10.    
  11. </mapper>private String name;<?xml version="1.0" encoding="UTF-8"?>
  12. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  13.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  14. <mapper namespace="com.example.mapper.UserMapper">
  15.    
  16. </mapper>private Integer age;}public class UserDetail {<?xml version="1.0" encoding="UTF-8"?>
  17. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  18.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  19. <mapper namespace="com.example.mapper.UserMapper">
  20.    
  21. </mapper>private Integer detailId;<?xml version="1.0" encoding="UTF-8"?>
  22. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  23.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  24. <mapper namespace="com.example.mapper.UserMapper">
  25.    
  26. </mapper>private String address;<?xml version="1.0" encoding="UTF-8"?>
  27. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  28.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  29. <mapper namespace="com.example.mapper.UserMapper">
  30.    
  31. </mapper>private String phone;}public class UserVo {<?xml version="1.0" encoding="UTF-8"?>
  32. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  33.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  34. <mapper namespace="com.example.mapper.UserMapper">
  35.    
  36. </mapper>private Integer id;<?xml version="1.0" encoding="UTF-8"?>
  37. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  38.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  39. <mapper namespace="com.example.mapper.UserMapper">
  40.    
  41. </mapper>private String name;<?xml version="1.0" encoding="UTF-8"?>
  42. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  43.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  44. <mapper namespace="com.example.mapper.UserMapper">
  45.    
  46. </mapper>private Integer age;<?xml version="1.0" encoding="UTF-8"?>
  47. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  48.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  49. <mapper namespace="com.example.mapper.UserMapper">
  50.    
  51. </mapper>private UserDetail userDetail;  // 一对一关系:一个用户对应一份详细信息 }
复制代码
XML配置如下:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.example.mapper.UserMapper">
  5.    
  6. </mapper><?xml version="1.0" encoding="UTF-8"?>
  7. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  8.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  9. <mapper namespace="com.example.mapper.UserMapper">
  10.    
  11. </mapper><?xml version="1.0" encoding="UTF-8"?>
  12. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  13.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  14. <mapper namespace="com.example.mapper.UserMapper">
  15.    
  16. </mapper><?xml version="1.0" encoding="UTF-8"?>
  17. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  18.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  19. <mapper namespace="com.example.mapper.UserMapper">
  20.    
  21. </mapper><?xml version="1.0" encoding="UTF-8"?>
  22. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  23.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  24. <mapper namespace="com.example.mapper.UserMapper">
  25.    
  26. </mapper><?xml version="1.0" encoding="UTF-8"?>
  27. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  28.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  29. <mapper namespace="com.example.mapper.UserMapper">
  30.    
  31. </mapper><?xml version="1.0" encoding="UTF-8"?>
  32. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  33.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  34. <mapper namespace="com.example.mapper.UserMapper">
  35.    
  36. </mapper><?xml version="1.0" encoding="UTF-8"?>
  37. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  38.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  39. <mapper namespace="com.example.mapper.UserMapper">
  40.    
  41. </mapper><?xml version="1.0" encoding="UTF-8"?>
  42. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  43.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  44. <mapper namespace="com.example.mapper.UserMapper">
  45.    
  46. </mapper><?xml version="1.0" encoding="UTF-8"?>
  47. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  48.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  49. <mapper namespace="com.example.mapper.UserMapper">
  50.    
  51. </mapper><?xml version="1.0" encoding="UTF-8"?>
  52. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  53.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  54. <mapper namespace="com.example.mapper.UserMapper">
  55.    
  56. </mapper><?xml version="1.0" encoding="UTF-8"?>
  57. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  58.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  59. <mapper namespace="com.example.mapper.UserMapper">
  60.    
  61. </mapper><?xml version="1.0" encoding="UTF-8"?>
  62. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  63.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  64. <mapper namespace="com.example.mapper.UserMapper">
  65.    
  66. </mapper>SELECT u.id, u.name, u.age, ud.detail_id, ud.address, ud.phone<?xml version="1.0" encoding="UTF-8"?>
  67. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  68.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  69. <mapper namespace="com.example.mapper.UserMapper">
  70.    
  71. </mapper>FROM user u<?xml version="1.0" encoding="UTF-8"?>
  72. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  73.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  74. <mapper namespace="com.example.mapper.UserMapper">
  75.    
  76. </mapper>LEFT JOIN user_detail ud ON u.id = ud.user_id<?xml version="1.0" encoding="UTF-8"?>
  77. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  78.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  79. <mapper namespace="com.example.mapper.UserMapper">
  80.    
  81. </mapper>WHERE u.id = #{id}
复制代码

  • 标签

    • 用于定义一组映射规则,将查询的结果转换为一个指定类型的Java对象
    • 属性

      • id:为该映射指定一个唯一标识,供其在XML中引用使用,如
      • type:指定映射结果对应的Java类型(对象的全路径)


  • 标签

    • 用于将数据库列映射到Java对象的属性,在中使用
    • 属性

      • property:Java对象中的属性名称,如其表示将查询到的user_name列的值赋值给userName属性
      • cloumn:数据库查询结果中的列名,如column="user_name”表示sql查询列名为user_name的值


  • 标签

    • 类似于主要用于映射主键字段
    • 属性

      • property:与相同,映射到Java对象的主键属性
      • column:对应数据库中的主键列名


  • 标签

    • 表示一个一对一关连
    • 当查询到结果时,MyBatis会将用户的基本字段{id,name,age}直接映射到UserVo中,同时将详细信息{detail_id,address,phone}封装为一个UserDetail对象,并赋值到UserVo的userDetail对象中

一对多映射

提供用于有列表对象的查询


  • 现假设一个老师(Teacher)类和一个学生(student)类,一个老师可以对应多个学生,在Java中我们可以设计Teacher类, 使用List属性来存放老师的所有学生
    1. public class Teacher {<?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="com.example.mapper.UserMapper">
    5.    
    6. </mapper>private Integer id;<?xml version="1.0" encoding="UTF-8"?>
    7. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    8.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    9. <mapper namespace="com.example.mapper.UserMapper">
    10.    
    11. </mapper>private String teacherName;<?xml version="1.0" encoding="UTF-8"?>
    12. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    13.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    14. <mapper namespace="com.example.mapper.UserMapper">
    15.    
    16. </mapper>private Integer age;<?xml version="1.0" encoding="UTF-8"?>
    17. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    18.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    19. <mapper namespace="com.example.mapper.UserMapper">
    20.    
    21. </mapper>private List students; // 一对多关系:一个老师对应多个学生}public class Student {<?xml version="1.0" encoding="UTF-8"?>
    22. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    23.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    24. <mapper namespace="com.example.mapper.UserMapper">
    25.    
    26. </mapper>private Integer id;<?xml version="1.0" encoding="UTF-8"?>
    27. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    28.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    29. <mapper namespace="com.example.mapper.UserMapper">
    30.    
    31. </mapper>private String name;<?xml version="1.0" encoding="UTF-8"?>
    32. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    33.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    34. <mapper namespace="com.example.mapper.UserMapper">
    35.    
    36. </mapper>private Integer age;}
    复制代码
    XML配置如下:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="com.example.mapper.UserMapper">
    5.    
    6. </mapper><?xml version="1.0" encoding="UTF-8"?>
    7. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    8.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    9. <mapper namespace="com.example.mapper.UserMapper">
    10.    
    11. </mapper><?xml version="1.0" encoding="UTF-8"?>
    12. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    13.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    14. <mapper namespace="com.example.mapper.UserMapper">
    15.    
    16. </mapper><?xml version="1.0" encoding="UTF-8"?>
    17. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    18.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    19. <mapper namespace="com.example.mapper.UserMapper">
    20.    
    21. </mapper><?xml version="1.0" encoding="UTF-8"?>
    22. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    23.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    24. <mapper namespace="com.example.mapper.UserMapper">
    25.    
    26. </mapper><?xml version="1.0" encoding="UTF-8"?>
    27. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    28.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    29. <mapper namespace="com.example.mapper.UserMapper">
    30.    
    31. </mapper><?xml version="1.0" encoding="UTF-8"?>
    32. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    33.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    34. <mapper namespace="com.example.mapper.UserMapper">
    35.    
    36. </mapper><?xml version="1.0" encoding="UTF-8"?>
    37. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    38.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    39. <mapper namespace="com.example.mapper.UserMapper">
    40.    
    41. </mapper><?xml version="1.0" encoding="UTF-8"?>
    42. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    43.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    44. <mapper namespace="com.example.mapper.UserMapper">
    45.    
    46. </mapper><?xml version="1.0" encoding="UTF-8"?>
    47. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    48.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    49. <mapper namespace="com.example.mapper.UserMapper">
    50.    
    51. </mapper><?xml version="1.0" encoding="UTF-8"?>
    52. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    53.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    54. <mapper namespace="com.example.mapper.UserMapper">
    55.    
    56. </mapper><?xml version="1.0" encoding="UTF-8"?>
    57. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    58.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    59. <mapper namespace="com.example.mapper.UserMapper">
    60.    
    61. </mapper><?xml version="1.0" encoding="UTF-8"?>
    62. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    63.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    64. <mapper namespace="com.example.mapper.UserMapper">
    65.    
    66. </mapper>SELECT t.id as teacher_id, t.teacher_name, t.age as teacher_age,<?xml version="1.0" encoding="UTF-8"?>
    67. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    68.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    69. <mapper namespace="com.example.mapper.UserMapper">
    70.    
    71. </mapper><?xml version="1.0" encoding="UTF-8"?>
    72. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    73.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    74. <mapper namespace="com.example.mapper.UserMapper">
    75.    
    76. </mapper>   s.id as student_id, s.name as student_name, s.age as student_age<?xml version="1.0" encoding="UTF-8"?>
    77. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    78.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    79. <mapper namespace="com.example.mapper.UserMapper">
    80.    
    81. </mapper>FROM teacher t<?xml version="1.0" encoding="UTF-8"?>
    82. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    83.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    84. <mapper namespace="com.example.mapper.UserMapper">
    85.    
    86. </mapper>LEFT JOIN student s ON t.id = s.teacher_id<?xml version="1.0" encoding="UTF-8"?>
    87. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    88.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    89. <mapper namespace="com.example.mapper.UserMapper">
    90.    
    91. </mapper>WHERE t.id = #{id}
    复制代码

    • 标签:用于表示一对多关系,把查询结果中的学生记录封装成一个列表,并赋值到Teacher对象中的student属性


来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册