ToB企服应用市场:ToB评测及商务社交产业平台

标题: MyBatis笔记 [打印本页]

作者: 不到断气不罢休    时间: 2022-9-23 22:09
标题: MyBatis笔记
MyBatis

MyBatis特性

下载

下载地址


持久化层技术对比

开发环境

idea:2019.3.5
MySQL:8.0
MyBatis:3.5.10
Maven:3.6.1
开始写代码


1.创建Maven工程

2.配置打包方式为jar

3.引入依赖

pom.xml
  1. <dependencies>
  2.        
  3.         <dependency>
  4.                 <groupId>org.mybatis</groupId>
  5.                 <artifactId>mybatis</artifactId>
  6.                 <version>3.5.10</version>
  7.         </dependency>
  8.        
  9.         <dependency>
  10.                 <groupId>junit</groupId>
  11.                 <artifactId>junit</artifactId>
  12.                 <version>4.12</version>
  13.                 <scope>test</scope>
  14.         </dependency>
  15.        
  16.         <dependency>
  17.                 <groupId>mysql</groupId>
  18.                 <artifactId>mysql-connector-java</artifactId>
  19.                 <version>8.0.30</version>
  20.                 </dependency>
  21. </dependencies>
复制代码
4.mybatis配置文件

mybatis-config.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. <settings>
  4. <plugins>
  5.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  6. </plugins>
  7. <plugins>
  8.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  9. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  10. </settings>PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  11. <settings>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins>
  15. <plugins>
  16.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  17. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  18. </settings>"http://mybatis.org/dtd/mybatis-3-config.dtd">
  19. <configuration>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins>
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins><environments default="development">
  26. <settings>
  27. <plugins>
  28.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  29. </plugins>
  30. <plugins>
  31.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  32. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  33. </settings><environment id="development">
  34. <settings>
  35. <plugins>
  36.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  37. </plugins>
  38. <plugins>
  39.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  40. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  41. </settings><plugins>
  42.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  43. </plugins><transactionManager type="JDBC"/>
  44. <settings>
  45. <plugins>
  46.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  47. </plugins>
  48. <plugins>
  49.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  50. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  51. </settings><plugins>
  52.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  53. </plugins><dataSource type="POOLED">
  54. <settings>
  55. <plugins>
  56.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  57. </plugins>
  58. <plugins>
  59.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  60. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  61. </settings><settings>
  62. <plugins>
  63.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  64. </plugins>
  65. <plugins>
  66.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  67. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  68. </settings><property name="driver" value="com.mysql.cj.jdbc.Driver"/>
  69. <settings>
  70. <plugins>
  71.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  72. </plugins>
  73. <plugins>
  74.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  75. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  76. </settings><settings>
  77. <plugins>
  78.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  79. </plugins>
  80. <plugins>
  81.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  82. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  83. </settings><property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
  84. <settings>
  85. <plugins>
  86.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  87. </plugins>
  88. <plugins>
  89.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  90. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  91. </settings><settings>
  92. <plugins>
  93.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  94. </plugins>
  95. <plugins>
  96.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  97. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  98. </settings><property name="username" value="root"/>
  99. <settings>
  100. <plugins>
  101.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  102. </plugins>
  103. <plugins>
  104.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  105. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  106. </settings><settings>
  107. <plugins>
  108.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  109. </plugins>
  110. <plugins>
  111.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  112. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  113. </settings><property name="password" value="xxxxxxxxx"/>
  114. <settings>
  115. <plugins>
  116.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  117. </plugins>
  118. <plugins>
  119.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  120. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  121. </settings><plugins>
  122.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  123. </plugins></dataSource>
  124. <settings>
  125. <plugins>
  126.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  127. </plugins>
  128. <plugins>
  129.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  130. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  131. </settings></environment>
  132. <plugins>
  133.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  134. </plugins></environments>
  135. <plugins>
  136.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  137. </plugins>
  138. <plugins>
  139.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  140. </plugins><mappers>
  141. <settings>
  142. <plugins>
  143.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  144. </plugins>
  145. <plugins>
  146.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  147. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  148. </settings><mapper resource="mappers/UserMapper.xml"/>
  149. <plugins>
  150.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  151. </plugins></mappers>
  152. </configuration>
复制代码
5.创建数据库建表(略)并在Java代码中创建对应实体类

Users.java
  1. package com.xust.mybatis.pojo;
  2. public class Users {
  3. <plugins>
  4.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  5. </plugins>private Integer id;
  6. <plugins>
  7.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  8. </plugins>private String username;
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>private String password;
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins>private Integer age;
  15. <plugins>
  16.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  17. </plugins>private String sex;
  18. <plugins>
  19.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  20. </plugins>private String email;
  21. <plugins>
  22.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  23. </plugins>public Users() {
  24. <plugins>
  25.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  26. </plugins>}
  27. <plugins>
  28.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  29. </plugins>public Users(Integer id, String username, String password, Integer age, String sex, String email) {
  30. <settings>
  31. <plugins>
  32.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  33. </plugins>
  34. <plugins>
  35.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  36. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  37. </settings>this.id = id;
  38. <settings>
  39. <plugins>
  40.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  41. </plugins>
  42. <plugins>
  43.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  44. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  45. </settings>this.username = username;
  46. <settings>
  47. <plugins>
  48.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  49. </plugins>
  50. <plugins>
  51.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  52. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  53. </settings>this.password = password;
  54. <settings>
  55. <plugins>
  56.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  57. </plugins>
  58. <plugins>
  59.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  60. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  61. </settings>this.age = age;
  62. <settings>
  63. <plugins>
  64.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  65. </plugins>
  66. <plugins>
  67.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  68. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  69. </settings>this.sex = sex;
  70. <settings>
  71. <plugins>
  72.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  73. </plugins>
  74. <plugins>
  75.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  76. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  77. </settings>this.email = email;
  78. <plugins>
  79.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  80. </plugins>}
  81. <plugins>
  82.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  83. </plugins>public Integer getId() {
  84. <settings>
  85. <plugins>
  86.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  87. </plugins>
  88. <plugins>
  89.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  90. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  91. </settings>return id;
  92. <plugins>
  93.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  94. </plugins>}
  95. <plugins>
  96.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  97. </plugins>public void setId(Integer id) {
  98. <settings>
  99. <plugins>
  100.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  101. </plugins>
  102. <plugins>
  103.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  104. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  105. </settings>this.id = id;
  106. <plugins>
  107.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  108. </plugins>}
  109. <plugins>
  110.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  111. </plugins>public String getUsername() {
  112. <settings>
  113. <plugins>
  114.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  115. </plugins>
  116. <plugins>
  117.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  118. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  119. </settings>return username;
  120. <plugins>
  121.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  122. </plugins>}
  123. <plugins>
  124.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  125. </plugins>public void setUsername(String username) {
  126. <settings>
  127. <plugins>
  128.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  129. </plugins>
  130. <plugins>
  131.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  132. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  133. </settings>this.username = username;
  134. <plugins>
  135.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  136. </plugins>}
  137. <plugins>
  138.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  139. </plugins>public String getPassword() {
  140. <settings>
  141. <plugins>
  142.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  143. </plugins>
  144. <plugins>
  145.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  146. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  147. </settings>return password;
  148. <plugins>
  149.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  150. </plugins>}
  151. <plugins>
  152.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  153. </plugins>public void setPassword(String password) {
  154. <settings>
  155. <plugins>
  156.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  157. </plugins>
  158. <plugins>
  159.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  160. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  161. </settings>this.password = password;
  162. <plugins>
  163.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  164. </plugins>}
  165. <plugins>
  166.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  167. </plugins>public Integer getAge() {
  168. <settings>
  169. <plugins>
  170.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  171. </plugins>
  172. <plugins>
  173.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  174. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  175. </settings>return age;
  176. <plugins>
  177.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  178. </plugins>}
  179. <plugins>
  180.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  181. </plugins>public void setAge(Integer age) {
  182. <settings>
  183. <plugins>
  184.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  185. </plugins>
  186. <plugins>
  187.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  188. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  189. </settings>this.age = age;
  190. <plugins>
  191.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  192. </plugins>}
  193. <plugins>
  194.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  195. </plugins>public String getSex() {
  196. <settings>
  197. <plugins>
  198.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  199. </plugins>
  200. <plugins>
  201.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  202. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  203. </settings>return sex;
  204. <plugins>
  205.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  206. </plugins>}
  207. <plugins>
  208.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  209. </plugins>public void setSex(String sex) {
  210. <settings>
  211. <plugins>
  212.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  213. </plugins>
  214. <plugins>
  215.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  216. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  217. </settings>this.sex = sex;
  218. <plugins>
  219.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  220. </plugins>}
  221. <plugins>
  222.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  223. </plugins>public String getEmail() {
  224. <settings>
  225. <plugins>
  226.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  227. </plugins>
  228. <plugins>
  229.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  230. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  231. </settings>return email;
  232. <plugins>
  233.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  234. </plugins>}
  235. <plugins>
  236.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  237. </plugins>public void setEmail(String email) {
  238. <settings>
  239. <plugins>
  240.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  241. </plugins>
  242. <plugins>
  243.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  244. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  245. </settings>this.email = email;
  246. <plugins>
  247.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  248. </plugins>}
  249. <plugins>
  250.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  251. </plugins>@Override
  252. <plugins>
  253.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  254. </plugins>public String toString() {
  255. <settings>
  256. <plugins>
  257.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  258. </plugins>
  259. <plugins>
  260.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  261. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  262. </settings>return "Users{" +
  263. <settings>
  264. <plugins>
  265.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  266. </plugins>
  267. <plugins>
  268.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  269. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  270. </settings><settings>
  271. <plugins>
  272.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  273. </plugins>
  274. <plugins>
  275.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  276. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  277. </settings>"id=" + id +
  278. <settings>
  279. <plugins>
  280.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  281. </plugins>
  282. <plugins>
  283.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  284. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  285. </settings><settings>
  286. <plugins>
  287.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  288. </plugins>
  289. <plugins>
  290.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  291. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  292. </settings>", username='" + username + '\'' +
  293. <settings>
  294. <plugins>
  295.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  296. </plugins>
  297. <plugins>
  298.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  299. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  300. </settings><settings>
  301. <plugins>
  302.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  303. </plugins>
  304. <plugins>
  305.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  306. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  307. </settings>", password='" + password + '\'' +
  308. <settings>
  309. <plugins>
  310.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  311. </plugins>
  312. <plugins>
  313.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  314. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  315. </settings><settings>
  316. <plugins>
  317.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  318. </plugins>
  319. <plugins>
  320.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  321. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  322. </settings>", age=" + age +
  323. <settings>
  324. <plugins>
  325.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  326. </plugins>
  327. <plugins>
  328.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  329. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  330. </settings><settings>
  331. <plugins>
  332.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  333. </plugins>
  334. <plugins>
  335.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  336. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  337. </settings>", sex='" + sex + '\'' +
  338. <settings>
  339. <plugins>
  340.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  341. </plugins>
  342. <plugins>
  343.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  344. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  345. </settings><settings>
  346. <plugins>
  347.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  348. </plugins>
  349. <plugins>
  350.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  351. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  352. </settings>", email='" + email + '\'' +
  353. <settings>
  354. <plugins>
  355.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  356. </plugins>
  357. <plugins>
  358.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  359. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  360. </settings><settings>
  361. <plugins>
  362.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  363. </plugins>
  364. <plugins>
  365.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  366. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  367. </settings>'}';
  368. <plugins>
  369.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  370. </plugins>}
  371. }
复制代码
6.创建mapper接口(不需要实现类)

UserMapper.java
  1. package com.xust.mybatis.mapper;
  2. /**
  3. * mybatis面向接口编程两个一致
  4. * 1.映射文件的namespace要和mapper的全类名保持一致
  5. * 2.映射文件中SQL语句的id要和mapper接口中的方法名一致
  6. */
  7. public interface UserMapper {
  8. <plugins>
  9.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  10. </plugins>/**
  11. <plugins>
  12.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  13. </plugins> * 添加用户信息
  14. <plugins>
  15.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  16. </plugins> */
  17. <plugins>
  18.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  19. </plugins>int insertUser();
  20. }
复制代码
7.创建MyBatis映射文件

UserMapper.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. <settings>
  4. <plugins>
  5.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  6. </plugins>
  7. <plugins>
  8.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  9. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  10. </settings>PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  11. <settings>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins>
  15. <plugins>
  16.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  17. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  18. </settings>"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  19. <mapper namespace="com.xust.mybatis.mapper.UserMapper">
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins>
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins><insert id="insertUser">
  26.                 insert into t_user values(null,'张三','123',23,'女','123456@qq.com')
  27.         </insert>
  28. </mapper>
复制代码
8.junit测试

SqlSession:代表Java程序和数据库之间的会话。HttpSession是Java程序和浏览器之间的会话
SqlSessionFactory:是生产SqlSession的工厂
MyBatisTest.java
  1. package com.xust.mybatis.test;
  2. import com.xust.mybatis.mapper.UserMapper;
  3. import org.apache.ibatis.io.Resources;
  4. import org.apache.ibatis.session.SqlSession;
  5. import org.apache.ibatis.session.SqlSessionFactory;
  6. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  7. import org.junit.Test;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. public class MyBatisTest {
  11. <plugins>
  12.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  13. </plugins>@Test
  14. <plugins>
  15.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  16. </plugins>public void testMyBatis() throws IOException {
  17. <settings>
  18. <plugins>
  19.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  20. </plugins>
  21. <plugins>
  22.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  23. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  24. </settings>//加载核心配置文件
  25. <settings>
  26. <plugins>
  27.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  28. </plugins>
  29. <plugins>
  30.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  31. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  32. </settings>InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
  33. <settings>
  34. <plugins>
  35.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  36. </plugins>
  37. <plugins>
  38.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  39. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  40. </settings>//获取SqlSessionFactoryBuilder
  41. <settings>
  42. <plugins>
  43.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  44. </plugins>
  45. <plugins>
  46.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  47. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  48. </settings>SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();
  49. <settings>
  50. <plugins>
  51.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  52. </plugins>
  53. <plugins>
  54.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  55. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  56. </settings>//获取SqlSessionFactory
  57. <settings>
  58. <plugins>
  59.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  60. </plugins>
  61. <plugins>
  62.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  63. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  64. </settings>SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(is);
  65. <settings>
  66. <plugins>
  67.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  68. </plugins>
  69. <plugins>
  70.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  71. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  72. </settings>//获取SqlSession
  73. <settings>
  74. <plugins>
  75.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  76. </plugins>
  77. <plugins>
  78.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  79. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  80. </settings>SqlSession sqlSession=sqlSessionFactory.openSession();
  81. <settings>
  82. <plugins>
  83.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  84. </plugins>
  85. <plugins>
  86.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  87. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  88. </settings>//获取mapper接口对象
  89. <settings>
  90. <plugins>
  91.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  92. </plugins>
  93. <plugins>
  94.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  95. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  96. </settings>UserMapper mapper=sqlSession.getMapper(UserMapper.class);
  97. <settings>
  98. <plugins>
  99.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  100. </plugins>
  101. <plugins>
  102.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  103. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  104. </settings>//测试功能
  105. <settings>
  106. <plugins>
  107.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  108. </plugins>
  109. <plugins>
  110.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  111. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  112. </settings>int result=mapper.insertUser();
  113. <settings>
  114. <plugins>
  115.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  116. </plugins>
  117. <plugins>
  118.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  119. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  120. </settings>//提交事务
  121. <settings>
  122. <plugins>
  123.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  124. </plugins>
  125. <plugins>
  126.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  127. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  128. </settings>sqlSession.commit();
  129. <settings>
  130. <plugins>
  131.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  132. </plugins>
  133. <plugins>
  134.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  135. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  136. </settings>System.out.println("result:"+result);
  137. <plugins>
  138.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  139. </plugins>}
  140. }
复制代码
上面是手动提交事务,SqlSession默认不自动提交事务,自动提交可以在获取sqlSession对象时,使用SqlSession sqlSession = sqlSessionFactory.openSession(true),传入一个Boolean类型的参数,值为true,这样就可以自动提交。
9.加入log4j日志功能

引入依赖
  1. <dependency>
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins><groupId>log4j</groupId>
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins><artifactId>log4j</artifactId>
  8. <plugins>
  9.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  10. </plugins><version>1.2.17</version>
  11. </dependency>
复制代码
log4j.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
  3. <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  4. <plugins>
  5.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  6. </plugins><appender name="STDOUT" >
  7. <settings>
  8. <plugins>
  9.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  10. </plugins>
  11. <plugins>
  12.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  13. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  14. </settings><param name="Encoding" value="UTF-8" />
  15. <settings>
  16. <plugins>
  17.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  18. </plugins>
  19. <plugins>
  20.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  21. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  22. </settings><layout >
  23.                         <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" />
  24. <settings>
  25. <plugins>
  26.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  27. </plugins>
  28. <plugins>
  29.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  30. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  31. </settings></layout>
  32. <plugins>
  33.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  34. </plugins></appender>
  35. <plugins>
  36.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  37. </plugins><logger name="java.sql">
  38. <settings>
  39. <plugins>
  40.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  41. </plugins>
  42. <plugins>
  43.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  44. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  45. </settings><level value="debug" />
  46. <plugins>
  47.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  48. </plugins></logger>
  49. <plugins>
  50.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  51. </plugins><logger name="org.apache.ibatis">
  52. <settings>
  53. <plugins>
  54.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  55. </plugins>
  56. <plugins>
  57.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  58. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  59. </settings><level value="info" />
  60. <plugins>
  61.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  62. </plugins></logger>
  63. <plugins>
  64.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  65. </plugins><root>
  66. <settings>
  67. <plugins>
  68.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  69. </plugins>
  70. <plugins>
  71.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  72. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  73. </settings><level value="debug" />
  74. <settings>
  75. <plugins>
  76.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  77. </plugins>
  78. <plugins>
  79.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  80. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  81. </settings><appender-ref ref="STDOUT" />
  82. <plugins>
  83.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  84. </plugins></root>
  85. </log4j:configuration>
复制代码
日志的级别:FATAL(致命)>ERROR(错误)>WARN(警告)>INFO(信息)>DEBUG(调试)
日志级别越低,打印越详细
增删改查

查询功能必须指定resultType或resultMap
resultType:设置默认映射(字段名和属性名一致)
resultMap:设置自定义映射(一对多,多对一,字段名和属性名不一致)
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. <settings>
  4. <plugins>
  5.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  6. </plugins>
  7. <plugins>
  8.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  9. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  10. </settings>PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  11. <settings>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins>
  15. <plugins>
  16.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  17. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  18. </settings>"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  19. <mapper namespace="com.xust.mybatis.mapper.UserMapper">
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins>
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins><insert id="insertUser">
  26.                 insert into t_user values(null,'张三','123',23,'女','123456@qq.com')
  27.         </insert>
  28. </mapper><settings>
  29. <plugins>
  30.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  31. </plugins>
  32. <plugins>
  33.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  34. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  35. </settings><settings>
  36. <plugins>
  37.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  38. </plugins>
  39. <plugins>
  40.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  41. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  42. </settings>update t_user set username='李四' where id=1<settings>
  43. <plugins>
  44.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  45. </plugins>
  46. <plugins>
  47.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  48. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  49. </settings><settings>
  50. <plugins>
  51.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  52. </plugins>
  53. <plugins>
  54.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  55. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  56. </settings><plugins>
  57.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  58. </plugins>delete from t_user where id=2<settings>
  59. <plugins>
  60.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  61. </plugins>
  62. <plugins>
  63.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  64. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  65. </settings><settings>
  66. <plugins>
  67.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  68. </plugins>
  69. <plugins>
  70.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  71. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  72. </settings><plugins>
  73.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  74. </plugins><select id="getAllUser" resultType="users">
  75. <select id="getAllUserToMap" resultType="map">
  76. <select id="getAllUserToMap1" resultType="map">
  77. <plugins>
  78.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  79. </plugins>select * from t_user
  80. </select>
  81. </select>
  82. </select> where id=3<settings>
  83. <plugins>
  84.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  85. </plugins>
  86. <plugins>
  87.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  88. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  89. </settings><settings>
  90. <plugins>
  91.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  92. </plugins>
  93. <plugins>
  94.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  95. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  96. </settings><select id="getAllUser" resultType="users">
  97. <select id="getAllUserToMap" resultType="map">
  98. <select id="getAllUserToMap1" resultType="map">
  99. <plugins>
  100.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  101. </plugins>select * from t_user
  102. </select>
  103. </select>
  104. </select><plugins>
  105.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  106. </plugins>
复制代码
MyBatis核心配置文件

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


MyBatis中mapper获取参数值的两种方式

${}:字符串拼接的方式拼接sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单引号
{}:使用占位符赋值的方式拼接sql,此时为字符串类型或日期类型的字段进行赋值时,可以自动添加单引号

单个字面量类型的参数

{}中username可替换为任意值,${}要手动添加''
Test.java
  1. public class ParameterTest {
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>@Test
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>public void testGetUserByUsername(){
  8. <settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
  16. <settings>
  17. <plugins>
  18.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  19. </plugins>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  23. </settings>ParameterMapper mapper=sqlSession.getMapper(ParameterMapper.class);
  24. <settings>
  25. <plugins>
  26.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  27. </plugins>
  28. <plugins>
  29.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  30. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  31. </settings>Users user=mapper.GetUserByUsername("李四");
  32. <settings>
  33. <plugins>
  34.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  35. </plugins>
  36. <plugins>
  37.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  38. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  39. </settings>System.out.println(user);
  40. <plugins>
  41.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  42. </plugins>}
  43. }
复制代码
#{}
  1. <select id="GetUserByUsername" resultType="users">
  2. <select id="getAllUser" resultType="users">
  3. <select id="getAllUserToMap" resultType="map">
  4. <select id="getAllUserToMap1" resultType="map">
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>select * from t_user
  8. </select>
  9. </select>
  10. </select> where username=#{username}
  11. </select>
复制代码
${}
  1. <select id="GetUserByUsername" resultType="users">
  2. <select id="getAllUser" resultType="users">
  3. <select id="getAllUserToMap" resultType="map">
  4. <select id="getAllUserToMap1" resultType="map">
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>select * from t_user
  8. </select>
  9. </select>
  10. </select> where username='${username}'
  11. </select>
复制代码
多个字面量类型的参数

若mapper接口中的方法参数为多个时,此时MyBatis会自动将这些参数放在一个map集合中
{}中必须按照上面的参数写
Test.java
  1. public class ParameterTest {
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>@Test
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>public void testCheckLogin(){
  8. <settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
  16. <settings>
  17. <plugins>
  18.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  19. </plugins>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  23. </settings>ParameterMapper mapper=sqlSession.getMapper(ParameterMapper.class);
  24. <settings>
  25. <plugins>
  26.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  27. </plugins>
  28. <plugins>
  29.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  30. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  31. </settings>Users user=mapper.checkLogin("李四","123");
  32. <settings>
  33. <plugins>
  34.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  35. </plugins>
  36. <plugins>
  37.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  38. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  39. </settings>System.out.println(user);
  40. <plugins>
  41.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  42. </plugins>}
  43. }
复制代码
#{}
  1. <select id="checkLogin" resultType="users">
  2. <select id="getAllUser" resultType="users">
  3. <select id="getAllUserToMap" resultType="map">
  4. <select id="getAllUserToMap1" resultType="map">
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>select * from t_user
  8. </select>
  9. </select>
  10. </select> where username=#{arg0} and password=#{arg1}
  11. </select>
复制代码
${}
  1. <select id="checkLogin" resultType="User">
  2.         select * from t_user where username ='${param1}' and password ='${param2}'
  3. </select>
复制代码
map集合类型的参数

mapper接口中的方法需要的参数为多个时,此时可以手动创建map集合,将这些数据放在map中只需要通过${}和#{}访问map集合的键就可以获取相对应的值
Test.java
  1. public class ParameterTest {
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>@Test
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>public void testCheckLoginByMap(){
  8. <settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
  16. <settings>
  17. <plugins>
  18.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  19. </plugins>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  23. </settings>ParameterMapper mapper=sqlSession.getMapper(ParameterMapper.class);
  24. <settings>
  25. <plugins>
  26.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  27. </plugins>
  28. <plugins>
  29.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  30. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  31. </settings>Map<String,Object> map=new HashMap<>();
  32. <settings>
  33. <plugins>
  34.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  35. </plugins>
  36. <plugins>
  37.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  38. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  39. </settings>map.put("username","李四");
  40. <settings>
  41. <plugins>
  42.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  43. </plugins>
  44. <plugins>
  45.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  46. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  47. </settings>map.put("password","123");
  48. <settings>
  49. <plugins>
  50.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  51. </plugins>
  52. <plugins>
  53.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  54. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  55. </settings>Users user=mapper.checkUserByMap(map);
  56. <settings>
  57. <plugins>
  58.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  59. </plugins>
  60. <plugins>
  61.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  62. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  63. </settings>System.out.println(user);
  64. <plugins>
  65.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  66. </plugins>}
  67. }
复制代码
#{}
  1. <select id="GetUserByUsername" resultType="users">
  2. <select id="getAllUser" resultType="users">
  3. <select id="getAllUserToMap" resultType="map">
  4. <select id="getAllUserToMap1" resultType="map">
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>select * from t_user
  8. </select>
  9. </select>
  10. </select> where username=#{username}
  11. </select> and password=#{password}
复制代码
${}略
实体类类型的参数

通过访问实体类对象中的属性名获取属性值
Test.java
  1. public class ParameterTest {
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>@Test
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>public void testInsertUser(){
  8. <settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
  16. <settings>
  17. <plugins>
  18.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  19. </plugins>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  23. </settings>ParameterMapper mapper=sqlSession.getMapper(ParameterMapper.class);
  24. <settings>
  25. <plugins>
  26.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  27. </plugins>
  28. <plugins>
  29.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  30. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  31. </settings>int result=mapper.insertUser(new Users(null,"王五","123",23,"男","123@q.com"));
  32. <settings>
  33. <plugins>
  34.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  35. </plugins>
  36. <plugins>
  37.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  38. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  39. </settings>System.out.println(result);
  40. <plugins>
  41.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  42. </plugins>}
  43. }
复制代码
#{}
  1. <insert id="insertUser">
  2. <insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
  3. <plugins>
  4.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  5. </plugins>insert into t_user values(null,#{username},#{password},#{age},#{sex},#{email})
  6. </insert>
  7. </insert>
复制代码
${}略
使用@Param注解命名参数

可以通过@Param注解标识mapper接口中的方法参数,此时,会将这些参数放在map集合中
Test.java
  1. public class ParameterTest {
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>@Test
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>public void testChesckLoginByParam(){
  8. <settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
  16. <settings>
  17. <plugins>
  18.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  19. </plugins>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  23. </settings>ParameterMapper mapper=sqlSession.getMapper(ParameterMapper.class);
  24. <settings>
  25. <plugins>
  26.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  27. </plugins>
  28. <plugins>
  29.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  30. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  31. </settings>Users user=mapper.checkLoginByParam("李四","123");
  32. <settings>
  33. <plugins>
  34.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  35. </plugins>
  36. <plugins>
  37.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  38. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  39. </settings>System.out.println(user);
  40. <plugins>
  41.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  42. </plugins>}
  43. }
复制代码
mapper.java
  1. Users checkLogdinByParam(@Param("username") String username,@Param("password") String password);
复制代码
mapper.xml
  1. <select id="GetUserByUsername" resultType="users">
  2. <select id="getAllUser" resultType="users">
  3. <select id="getAllUserToMap" resultType="map">
  4. <select id="getAllUserToMap1" resultType="map">
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>select * from t_user
  8. </select>
  9. </select>
  10. </select> where username=#{username}
  11. </select> and password=#{password}
复制代码
MyBatis查询功能

查询一个实体类对象
  1. Users getUserById(@Param("id") Integer id);
复制代码
  1. <select id="getUserById" resultType="users">
  2. <select id="getAllUser" resultType="users">
  3. <select id="getAllUserToMap" resultType="map">
  4. <select id="getAllUserToMap1" resultType="map">
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>select * from t_user
  8. </select>
  9. </select>
  10. </select> where id=#{id}
  11. </select>
复制代码
  1. public class SelectMapperTest {
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>@Test
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>public void getUsereById(){
  8. <settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
  16. <settings>
  17. <plugins>
  18.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  19. </plugins>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  23. </settings>SelectMapper mapper=sqlSession.getMapper(SelectMapper.class);
  24. <settings>
  25. <plugins>
  26.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  27. </plugins>
  28. <plugins>
  29.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  30. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  31. </settings>System.out.println(mapper.getUserById(3));
  32. <plugins>
  33.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  34. </plugins>}
  35. }
复制代码
查询List集合
  1. List<Users> getAllUser();
复制代码
  1. <select id="getAllUser" resultType="users">
  2. <select id="getAllUserToMap" resultType="map">
  3. <select id="getAllUserToMap1" resultType="map">
  4. <plugins>
  5.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  6. </plugins>select * from t_user
  7. </select>
  8. </select>
  9. </select>
复制代码
  1. public class SelectMapperTest {
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>@Test
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>public void getAllUsers(){
  8. <settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
  16. <settings>
  17. <plugins>
  18.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  19. </plugins>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  23. </settings>SelectMapper mapper=sqlSession.getMapper(SelectMapper.class);
  24. <settings>
  25. <plugins>
  26.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  27. </plugins>
  28. <plugins>
  29.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  30. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  31. </settings>System.out.println(mapper.getAllUser());
  32. <plugins>
  33.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  34. </plugins>}
  35. }
复制代码
查询单个数据
  1. Integer getCount();
复制代码
  1. <select id="getCount" resultType="java.lang.Integer">
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>select count(*) from t_user
  5. </select>
复制代码
  1. public class SelectMapperTest {
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>@Test
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>public void testGetCount(){
  8. <settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
  16. <settings>
  17. <plugins>
  18.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  19. </plugins>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  23. </settings>SelectMapper mapper=sqlSession.getMapper(SelectMapper.class);
  24. <settings>
  25. <plugins>
  26.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  27. </plugins>
  28. <plugins>
  29.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  30. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  31. </settings>System.out.println(mapper.getCount());
  32. <plugins>
  33.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  34. </plugins>}
  35. }
复制代码
查询一条数据为map集合
  1. Map<String,Object> getUserByIdToMap(@Param("id") Integer id);
复制代码
  1. <select id="getUserById" resultType="users">
  2. <select id="getAllUser" resultType="users">
  3. <select id="getAllUserToMap" resultType="map">
  4. <select id="getAllUserToMap1" resultType="map">
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>select * from t_user
  8. </select>
  9. </select>
  10. </select> where id=#{id}
  11. </select>
复制代码
  1. public class SelectMapperTest {
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>@Test
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>public void testGetUserByIdToMap(){
  8. <settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
  16. <settings>
  17. <plugins>
  18.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  19. </plugins>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  23. </settings>SelectMapper mapper=sqlSession.getMapper(SelectMapper.class);
  24. <settings>
  25. <plugins>
  26.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  27. </plugins>
  28. <plugins>
  29.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  30. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  31. </settings>System.out.println(mapper.getUserByIdToMap(3));
  32. <plugins>
  33.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  34. </plugins>}
  35. }
复制代码
查询多条数据为map集合

法一:
  1. List<Map<String,Object>> getAllUserToMap();
复制代码
  1. <select id="getAllUser" resultType="users">
  2. <select id="getAllUserToMap" resultType="map">
  3. <select id="getAllUserToMap1" resultType="map">
  4. <plugins>
  5.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  6. </plugins>select * from t_user
  7. </select>
  8. </select>
  9. </select>
复制代码
  1. public class SelectMapperTest {
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>@Test
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>public void testGetAllUserToMap(){
  8. <settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
  16. <settings>
  17. <plugins>
  18.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  19. </plugins>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  23. </settings>SelectMapper mapper=sqlSession.getMapper(SelectMapper.class);
  24. <settings>
  25. <plugins>
  26.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  27. </plugins>
  28. <plugins>
  29.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  30. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  31. </settings>System.out.println(mapper.getAllUserToMap());
  32. <plugins>
  33.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  34. </plugins>}
  35. }
复制代码
法二:
  1. @MapKey("id")
  2. Map<String,Object> getAllUserToMap1();
复制代码
  1. <select id="getAllUser" resultType="users">
  2. <select id="getAllUserToMap" resultType="map">
  3. <select id="getAllUserToMap1" resultType="map">
  4. <plugins>
  5.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  6. </plugins>select * from t_user
  7. </select>
  8. </select>
  9. </select>
复制代码
  1. public class SelectMapperTest {
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>@Test
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>public void testGetAllUserToMap1(){
  8. <settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
  16. <settings>
  17. <plugins>
  18.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  19. </plugins>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  23. </settings>SelectMapper mapper=sqlSession.getMapper(SelectMapper.class);
  24. <settings>
  25. <plugins>
  26.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  27. </plugins>
  28. <plugins>
  29.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  30. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  31. </settings>System.out.println(mapper.getAllUserToMap1());
  32. <plugins>
  33.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  34. </plugins>}
  35. }
复制代码
特殊SQL的执行

模糊查询

第三种方法最常用
  1. List<Users> getUserByLike(@Param("username") String username);
复制代码
  1. <settings>
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  8. </settings><select id="getAllUser" resultType="users">
  9. <select id="getAllUserToMap" resultType="map">
  10. <select id="getAllUserToMap1" resultType="map">
  11. <plugins>
  12.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  13. </plugins>select * from t_user
  14. </select>
  15. </select>
  16. </select> where username like "%"#{username}"%"
复制代码
  1. public class SQLMapperTest {
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>@Test
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>public void testGetUserByLike(){
  8. <settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
  16. <settings>
  17. <plugins>
  18.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  19. </plugins>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  23. </settings>SQLMapper mapper = sqlSession.getMapper(SQLMapper.class);
  24. <settings>
  25. <plugins>
  26.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  27. </plugins>
  28. <plugins>
  29.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  30. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  31. </settings>List<Users> list=mapper.getUserByLike("a");
  32. <settings>
  33. <plugins>
  34.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  35. </plugins>
  36. <plugins>
  37.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  38. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  39. </settings>System.out.println(list);
  40. <plugins>
  41.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  42. </plugins>}
  43. }
复制代码
批量删除
  1. int deleteMore(@Param("ids") String ids);
复制代码
  1. <delete id="deleteMore">
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>delete from t_user where id in (${ids})
  5. </delete>
复制代码
  1. public class SQLMapperTest {
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>@Test
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>public void testDeleteMore(){
  8. <settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
  16. <settings>
  17. <plugins>
  18.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  19. </plugins>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  23. </settings>SQLMapper mapper = sqlSession.getMapper(SQLMapper.class);
  24. <settings>
  25. <plugins>
  26.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  27. </plugins>
  28. <plugins>
  29.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  30. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  31. </settings>int result=mapper.deleteMore("1,2,3");
  32. <settings>
  33. <plugins>
  34.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  35. </plugins>
  36. <plugins>
  37.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  38. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  39. </settings>System.out.println(result);
  40. <plugins>
  41.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  42. </plugins>}
  43. }
复制代码
动态设置表名
  1. List<Users> getUserByTableName(@Param("tableName") String tableName);
复制代码
  1. <select id="getUserByTableName" resultType="users">
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>select * from ${tableName}
  5. </select>
复制代码
  1. public class SQLMapperTest {
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>@Test
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>public void testGetUserByTableName(){
  8. <settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
  16. <settings>
  17. <plugins>
  18.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  19. </plugins>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  23. </settings>SQLMapper mapper = sqlSession.getMapper(SQLMapper.class);
  24. <settings>
  25. <plugins>
  26.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  27. </plugins>
  28. <plugins>
  29.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  30. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  31. </settings>List<Users> list=mapper.getUserByTableName("t_user");
  32. <settings>
  33. <plugins>
  34.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  35. </plugins>
  36. <plugins>
  37.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  38. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  39. </settings>System.out.println(list);
  40. <plugins>
  41.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  42. </plugins>}
  43. }
复制代码
添加功能获取自增的主键

在插入一条数据的同时就可以获得对应的id,不需要插入后再去查询对应数据的id
  1. void insertUser(Users user);
复制代码
  1. <insert id="insertUser">
  2. <insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
  3. <plugins>
  4.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  5. </plugins>insert into t_user values(null,#{username},#{password},#{age},#{sex},#{email})
  6. </insert>
  7. </insert>
复制代码
  1. public class SQLMapperTest {
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>@Test
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>public void testInsertUser(){
  8. <settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings>SqlSession sqlSession= SqlSessionUtils.getSqlSession();
  16. <settings>
  17. <plugins>
  18.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  19. </plugins>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  23. </settings>SQLMapper mapper = sqlSession.getMapper(SQLMapper.class);
  24. <settings>
  25. <plugins>
  26.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  27. </plugins>
  28. <plugins>
  29.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  30. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  31. </settings>Users user=new Users(null,"赵六","123",23,"男","123@qq.com");
  32. <settings>
  33. <plugins>
  34.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  35. </plugins>
  36. <plugins>
  37.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  38. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  39. </settings>mapper.insertUser(user);
  40. <settings>
  41. <plugins>
  42.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  43. </plugins>
  44. <plugins>
  45.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  46. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  47. </settings>System.out.println(user);
  48. <plugins>
  49.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  50. </plugins>}
  51. }
复制代码
自定义映射resultMap

字段名与属性名不一致

数据库

Emp.java
  1. package com.xust.mybatis.pojo;
  2. public class Emp {
  3. <plugins>
  4.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  5. </plugins>private Integer eid;
  6. <plugins>
  7.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  8. </plugins>private String empName;
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>private Integer age;
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins>private String sex;
  15. <plugins>
  16.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  17. </plugins>private String email;
  18. <plugins>
  19.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  20. </plugins>public Emp() {
  21. <plugins>
  22.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  23. </plugins>}
  24. <plugins>
  25.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  26. </plugins>public Emp(Integer eid, String empName, Integer age, String sex, String email) {
  27. <settings>
  28. <plugins>
  29.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  30. </plugins>
  31. <plugins>
  32.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  33. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  34. </settings>this.eid = eid;
  35. <settings>
  36. <plugins>
  37.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  38. </plugins>
  39. <plugins>
  40.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  41. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  42. </settings>this.empName = empName;
  43. <settings>
  44. <plugins>
  45.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  46. </plugins>
  47. <plugins>
  48.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  49. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  50. </settings>this.age = age;
  51. <settings>
  52. <plugins>
  53.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  54. </plugins>
  55. <plugins>
  56.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  57. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  58. </settings>this.sex = sex;
  59. <settings>
  60. <plugins>
  61.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  62. </plugins>
  63. <plugins>
  64.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  65. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  66. </settings>this.email = email;
  67. <plugins>
  68.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  69. </plugins>}
  70. <plugins>
  71.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  72. </plugins>public Integer getEid() {
  73. <settings>
  74. <plugins>
  75.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  76. </plugins>
  77. <plugins>
  78.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  79. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  80. </settings>return eid;
  81. <plugins>
  82.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  83. </plugins>}
  84. <plugins>
  85.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  86. </plugins>public void setEid(Integer eid) {
  87. <settings>
  88. <plugins>
  89.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  90. </plugins>
  91. <plugins>
  92.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  93. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  94. </settings>this.eid = eid;
  95. <plugins>
  96.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  97. </plugins>}
  98. <plugins>
  99.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  100. </plugins>public String getEmpName() {
  101. <settings>
  102. <plugins>
  103.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  104. </plugins>
  105. <plugins>
  106.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  107. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  108. </settings>return empName;
  109. <plugins>
  110.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  111. </plugins>}
  112. <plugins>
  113.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  114. </plugins>public void setEmpName(String empName) {
  115. <settings>
  116. <plugins>
  117.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  118. </plugins>
  119. <plugins>
  120.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  121. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  122. </settings>this.empName = empName;
  123. <plugins>
  124.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  125. </plugins>}
  126. <plugins>
  127.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  128. </plugins>public Integer getAge() {
  129. <settings>
  130. <plugins>
  131.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  132. </plugins>
  133. <plugins>
  134.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  135. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  136. </settings>return age;
  137. <plugins>
  138.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  139. </plugins>}
  140. <plugins>
  141.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  142. </plugins>public void setAge(Integer age) {
  143. <settings>
  144. <plugins>
  145.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  146. </plugins>
  147. <plugins>
  148.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  149. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  150. </settings>this.age = age;
  151. <plugins>
  152.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  153. </plugins>}
  154. <plugins>
  155.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  156. </plugins>public String getSex() {
  157. <settings>
  158. <plugins>
  159.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  160. </plugins>
  161. <plugins>
  162.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  163. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  164. </settings>return sex;
  165. <plugins>
  166.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  167. </plugins>}
  168. <plugins>
  169.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  170. </plugins>public void setSex(String sex) {
  171. <settings>
  172. <plugins>
  173.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  174. </plugins>
  175. <plugins>
  176.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  177. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  178. </settings>this.sex = sex;
  179. <plugins>
  180.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  181. </plugins>}
  182. <plugins>
  183.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  184. </plugins>public String getEmail() {
  185. <settings>
  186. <plugins>
  187.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  188. </plugins>
  189. <plugins>
  190.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  191. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  192. </settings>return email;
  193. <plugins>
  194.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  195. </plugins>}
  196. <plugins>
  197.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  198. </plugins>public void setEmail(String email) {
  199. <settings>
  200. <plugins>
  201.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  202. </plugins>
  203. <plugins>
  204.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  205. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  206. </settings>this.email = email;
  207. <plugins>
  208.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  209. </plugins>}
  210. <plugins>
  211.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  212. </plugins>@Override
  213. <plugins>
  214.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  215. </plugins>public String toString() {
  216. <settings>
  217. <plugins>
  218.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  219. </plugins>
  220. <plugins>
  221.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  222. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  223. </settings>return "Emp{" +
  224. <settings>
  225. <plugins>
  226.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  227. </plugins>
  228. <plugins>
  229.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  230. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  231. </settings><settings>
  232. <plugins>
  233.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  234. </plugins>
  235. <plugins>
  236.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  237. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  238. </settings>"eid=" + eid +
  239. <settings>
  240. <plugins>
  241.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  242. </plugins>
  243. <plugins>
  244.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  245. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  246. </settings><settings>
  247. <plugins>
  248.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  249. </plugins>
  250. <plugins>
  251.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  252. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  253. </settings>", empName='" + empName + '\'' +
  254. <settings>
  255. <plugins>
  256.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  257. </plugins>
  258. <plugins>
  259.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  260. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  261. </settings><settings>
  262. <plugins>
  263.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  264. </plugins>
  265. <plugins>
  266.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  267. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  268. </settings>", age=" + age +
  269. <settings>
  270. <plugins>
  271.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  272. </plugins>
  273. <plugins>
  274.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  275. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  276. </settings><settings>
  277. <plugins>
  278.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  279. </plugins>
  280. <plugins>
  281.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  282. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  283. </settings>", sex='" + sex + '\'' +
  284. <settings>
  285. <plugins>
  286.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  287. </plugins>
  288. <plugins>
  289.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  290. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  291. </settings><settings>
  292. <plugins>
  293.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  294. </plugins>
  295. <plugins>
  296.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  297. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  298. </settings>", email='" + email + '\'' +
  299. <settings>
  300. <plugins>
  301.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  302. </plugins>
  303. <plugins>
  304.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  305. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  306. </settings><settings>
  307. <plugins>
  308.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  309. </plugins>
  310. <plugins>
  311.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  312. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  313. </settings>'}';
  314. <plugins>
  315.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  316. </plugins>}
  317. }
复制代码
  1. public class ResultMapTest {
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>@Test
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>public void testGetAllEmp(){
  8. <settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings>SqlSession sqlSessaion= SqlSessionUtils.getSqlSession();
  16. <settings>
  17. <plugins>
  18.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  19. </plugins>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  23. </settings>EmpMapper mapper=sqlSessaion.getMapper(EmpMapper.class);
  24. <settings>
  25. <plugins>
  26.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  27. </plugins>
  28. <plugins>
  29.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  30. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  31. </settings>List<Emp> list=mapper.getAllEmp();
  32. <settings>
  33. <plugins>
  34.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  35. </plugins>
  36. <plugins>
  37.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  38. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  39. </settings>list.forEach(emp-> System.out.println(emp));
  40. <plugins>
  41.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  42. </plugins>}
  43. }
复制代码
方法一:为字段起别名,使之与属性名称相同

EmpMapper.xml
  1. <select id="getAllEmp" resultType="emp">
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>select eid,emp_name empNaem,age,sex,email from t_emp
  5. </select>
复制代码
方法二:配置映射

在mybatis-config.xml中添加配置后自动将下划线映射为驼峰
mybatis-config.xml
  1. <settings>
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  8. </settings>
复制代码
EmpMapper.xml
  1. <select id="getAllEmp" resultType="emp">
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>select * from t_emp
  5. </select>
复制代码
方法三:自定义映射resultMap

所有属性都要列出来
EmpMapper.xml
  1. <settings>
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  8. </settings><settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings><settings>
  16. <plugins>
  17.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  18. </plugins>
  19. <plugins>
  20.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  21. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  22. </settings><select id="getAllEmp" resultType="emp">
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins>select * from t_emp
  26. </select>
复制代码
多对一映射

方法一:级联操作

Emp.java
  1. public class Emp {
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>private Integer eid;
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>private String empName;
  8. <plugins>
  9.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  10. </plugins>private Integer age;
  11. <plugins>
  12.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  13. </plugins>private String sex;
  14. <plugins>
  15.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  16. </plugins>private String email;
  17. <plugins>
  18.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  19. </plugins>private Dept dept;
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins>......
  23. }
复制代码
Dept.java
  1. package com.xust.mybatis.pojo;
  2. public class Dept {
  3. <plugins>
  4.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  5. </plugins>private Integer did;
  6. <plugins>
  7.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  8. </plugins>private String deptName;
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>......
  12. }
复制代码
EmpMapper.xml
  1. <settings>
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  8. </settings><settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings><settings>
  16. <plugins>
  17.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  18. </plugins>
  19. <plugins>
  20.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  21. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  22. </settings><settings>
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins>
  26. <plugins>
  27.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  28. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  29. </settings>select * from t_emp left join t_dept on t_emp.did=t_dept.did where t_emp.eid=#{eid}
复制代码
方法二:association

association用来处理多对一的映射关系
javaType中是类型名
EmpMapper.xml
  1. <settings>
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  8. </settings><settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings><settings>
  16. <plugins>
  17.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  18. </plugins>
  19. <plugins>
  20.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  21. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  22. </settings><settings>
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins>
  26. <plugins>
  27.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  28. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  29. </settings><settings>
  30. <plugins>
  31.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  32. </plugins>
  33. <plugins>
  34.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  35. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  36. </settings><plugins>
  37.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  38. </plugins>
复制代码
方法三:分步查询

EmpMapper.xml
  1. <settings>
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  8. </settings><settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings><settings>
  16. <plugins>
  17.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  18. </plugins>
  19. <plugins>
  20.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  21. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  22. </settings><settings>
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins>
  26. <plugins>
  27.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  28. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  29. </settings>select * from t_emp where eid=#{eid}
复制代码
DeptMapper.xml
  1. <select id="getEmpAndDeptByStepTwo" resultType="Dept">
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>select * from t_dept where did=#{did}
  5. </select>
复制代码
  1. public class ResultMapTest {<plugins>
  2.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  3. </plugins>@Test<plugins>
  4.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  5. </plugins>public void testGetEmpAndDeptByStep(){<settings>
  6. <plugins>
  7.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  8. </plugins>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  12. </settings>SqlSession sqlSessaion= SqlSessionUtils.getSqlSession();<settings>
  13. <plugins>
  14.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  15. </plugins>
  16. <plugins>
  17.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  18. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  19. </settings>EmpMapper mapper=sqlSessaion.getMapper(EmpMapper.class);<settings>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins>
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  26. </settings>Emp emp=mapper.getEmpAndDeptByStepOne(1);<settings>
  27. <plugins>
  28.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  29. </plugins>
  30. <plugins>
  31.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  32. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  33. </settings>System.out.println(emp);<plugins>
  34.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  35. </plugins>}}
复制代码
一对多映射

Dept.java
  1. public class Dept {
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>private Integer did;
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins>private String deptName;
  8. <plugins>
  9.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  10. </plugins>private List<Emp> emps;
  11. <plugins>
  12.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  13. </plugins>......
  14. }
复制代码
方法一:collection

collection:用来处理一对多的映射关系
ofType:表示该属性对饮的集合中存储的数据的类型
  1. <settings>
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  8. </settings><settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings><settings>
  16. <plugins>
  17.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  18. </plugins>
  19. <plugins>
  20.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  21. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  22. </settings><settings>
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins>
  26. <plugins>
  27.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  28. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  29. </settings><settings>
  30. <plugins>
  31.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  32. </plugins>
  33. <plugins>
  34.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  35. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  36. </settings><settings>
  37. <plugins>
  38.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  39. </plugins>
  40. <plugins>
  41.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  42. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  43. </settings><settings>
  44. <plugins>
  45.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  46. </plugins>
  47. <plugins>
  48.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  49. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  50. </settings><plugins>
  51.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  52. </plugins>select * from t_dept left join t_emp on t_dept.did=t_emp.did where t_dept.did=#{did}
复制代码
  1. public class ResultMapTest {<plugins>
  2.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  3. </plugins>@Test<plugins>
  4.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  5. </plugins>public void testGetDeptAndEmp(){<settings>
  6. <plugins>
  7.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  8. </plugins>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  12. </settings>SqlSession sqlSessaion= SqlSessionUtils.getSqlSession();<settings>
  13. <plugins>
  14.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  15. </plugins>
  16. <plugins>
  17.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  18. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  19. </settings>DeptMapper mapper=sqlSessaion.getMapper(DeptMapper.class);<settings>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins>
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  26. </settings>Dept dept=mapper.getDeptAndEmp(1);<settings>
  27. <plugins>
  28.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  29. </plugins>
  30. <plugins>
  31.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  32. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  33. </settings>System.out.println(dept);<plugins>
  34.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  35. </plugins>}}
复制代码
方法二:分步查询

DeptMapper.xml
  1. <settings>
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  8. </settings><settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings>select * from t_dept where did=#{did}
复制代码
EmpMapper.xml
  1. <select id="getAllEmp" resultType="emp">
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>select * from t_emp
  5. </select> where did=#{did}
复制代码
  1. public class ResultMapTest {<plugins>
  2.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  3. </plugins>@Test<plugins>
  4.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  5. </plugins>public void testGetDeptAndEmpByStep(){<settings>
  6. <plugins>
  7.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  8. </plugins>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  12. </settings>SqlSession sqlSessaion= SqlSessionUtils.getSqlSession();<settings>
  13. <plugins>
  14.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  15. </plugins>
  16. <plugins>
  17.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  18. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  19. </settings>DeptMapper mapper=sqlSessaion.getMapper(DeptMapper.class);<settings>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins>
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  26. </settings>Dept dept=mapper.getDeptAndEmpByStepOne(1);<settings>
  27. <plugins>
  28.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  29. </plugins>
  30. <plugins>
  31.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  32. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  33. </settings>System.out.println(dept);<plugins>
  34.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  35. </plugins>}}
复制代码
动态SQL

Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了解决拼接SQL语句字符串时的痛点问题
if标签

if标签可以对test中接收到的属性值进行判断,为true则拼接if标签下的内容
where后加1=1为了更好实现拼接效果,防止出现拼接错误
  1. List<Emp> getEmpByCondition(Emp emp);
复制代码
  1. <select id="getAllEmp" resultType="emp">
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>select * from t_emp
  5. </select> where 1=1<settings>
  6. <plugins>
  7.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  8. </plugins>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  12. </settings><plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins>and emp_name=#{empName}<settings>
  15. <plugins>
  16.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  17. </plugins>
  18. <plugins>
  19.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  20. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  21. </settings><settings>
  22. <plugins>
  23.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  24. </plugins>
  25. <plugins>
  26.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  27. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  28. </settings>and age=#{age}<settings>
  29. <plugins>
  30.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  31. </plugins>
  32. <plugins>
  33.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  34. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  35. </settings><settings>
  36. <plugins>
  37.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  38. </plugins>
  39. <plugins>
  40.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  41. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  42. </settings>and sex=#{sex}<settings>
  43. <plugins>
  44.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  45. </plugins>
  46. <plugins>
  47.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  48. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  49. </settings><settings>
  50. <plugins>
  51.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  52. </plugins>
  53. <plugins>
  54.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  55. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  56. </settings>and email=#{email}<plugins>
  57.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  58. </plugins>
复制代码
  1. public class DynamicSQLMapperTest {<plugins>
  2.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  3. </plugins>@Test<plugins>
  4.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  5. </plugins>public void testGetEmpByCondition(){<settings>
  6. <plugins>
  7.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  8. </plugins>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  12. </settings>SqlSession sqlSessaion= SqlSessionUtils.getSqlSession();<settings>
  13. <plugins>
  14.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  15. </plugins>
  16. <plugins>
  17.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  18. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  19. </settings>DynamicSQLMapper mapper=sqlSessaion.getMapper(DynamicSQLMapper.class);<settings>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins>
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  26. </settings>List list=mapper.getEmpByCondition(new Emp(null,"张三",12,"男","123@qq.com"));<settings>
  27. <plugins>
  28.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  29. </plugins>
  30. <plugins>
  31.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  32. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  33. </settings>System.out.println(list);<plugins>
  34.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  35. </plugins>}}
复制代码
where标签

相比直接使用if标签,where标签会自动清除多余的and和or,条件全不满足时where不起作用,不会出现拼接错误
但是where标签只能将内容之前多余的and和or清除,如果and和or放在拼接的语句之后,则不能清除
  1. <select id="getAllEmp" resultType="emp">
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>select * from t_emp
  5. </select><settings>
  6. <plugins>
  7.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  8. </plugins>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  12. </settings><settings>
  13. <plugins>
  14.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  15. </plugins>
  16. <plugins>
  17.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  18. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  19. </settings><settings>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins>
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  26. </settings>emp_name=#{empName}<settings>
  27. <plugins>
  28.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  29. </plugins>
  30. <plugins>
  31.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  32. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  33. </settings><settings>
  34. <plugins>
  35.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  36. </plugins>
  37. <plugins>
  38.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  39. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  40. </settings><settings>
  41. <plugins>
  42.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  43. </plugins>
  44. <plugins>
  45.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  46. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  47. </settings><plugins>
  48.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  49. </plugins>and age=#{age}<settings>
  50. <plugins>
  51.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  52. </plugins>
  53. <plugins>
  54.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  55. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  56. </settings><settings>
  57. <plugins>
  58.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  59. </plugins>
  60. <plugins>
  61.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  62. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  63. </settings><settings>
  64. <plugins>
  65.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  66. </plugins>
  67. <plugins>
  68.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  69. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  70. </settings><plugins>
  71.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  72. </plugins>and sex=#{sex}<settings>
  73. <plugins>
  74.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  75. </plugins>
  76. <plugins>
  77.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  78. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  79. </settings><settings>
  80. <plugins>
  81.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  82. </plugins>
  83. <plugins>
  84.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  85. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  86. </settings><settings>
  87. <plugins>
  88.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  89. </plugins>
  90. <plugins>
  91.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  92. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  93. </settings><plugins>
  94.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  95. </plugins>and email=#{email}<settings>
  96. <plugins>
  97.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  98. </plugins>
  99. <plugins>
  100.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  101. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  102. </settings><plugins>
  103.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  104. </plugins>
复制代码
trim标签

prefix/suffix:将trim标签中内容前面/后面添加指定内容
prefixOverrides/suffixOverrides:将trim标签中内容前面/后面去掉指定内容
  1. <select id="getAllEmp" resultType="emp">
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>select * from t_emp
  5. </select><settings>
  6. <plugins>
  7.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  8. </plugins>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  12. </settings><settings>
  13. <plugins>
  14.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  15. </plugins>
  16. <plugins>
  17.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  18. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  19. </settings><settings>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins>
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  26. </settings>emp_name = #{empName} and<settings>
  27. <plugins>
  28.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  29. </plugins>
  30. <plugins>
  31.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  32. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  33. </settings><settings>
  34. <plugins>
  35.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  36. </plugins>
  37. <plugins>
  38.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  39. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  40. </settings><settings>
  41. <plugins>
  42.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  43. </plugins>
  44. <plugins>
  45.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  46. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  47. </settings><plugins>
  48.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  49. </plugins>age = #{age} and<settings>
  50. <plugins>
  51.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  52. </plugins>
  53. <plugins>
  54.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  55. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  56. </settings><settings>
  57. <plugins>
  58.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  59. </plugins>
  60. <plugins>
  61.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  62. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  63. </settings><settings>
  64. <plugins>
  65.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  66. </plugins>
  67. <plugins>
  68.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  69. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  70. </settings><plugins>
  71.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  72. </plugins>sex = #{sex} or<settings>
  73. <plugins>
  74.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  75. </plugins>
  76. <plugins>
  77.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  78. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  79. </settings><settings>
  80. <plugins>
  81.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  82. </plugins>
  83. <plugins>
  84.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  85. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  86. </settings><settings>
  87. <plugins>
  88.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  89. </plugins>
  90. <plugins>
  91.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  92. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  93. </settings><plugins>
  94.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  95. </plugins>email = #{email}<settings>
  96. <plugins>
  97.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  98. </plugins>
  99. <plugins>
  100.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  101. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  102. </settings><plugins>
  103.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  104. </plugins>
复制代码
choose,when,otherwise标签

相当于if...else if...else,只会执行一个
when至少要有一个,otherwise至多只有一个
  1. <select id="getAllEmp" resultType="emp">
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>select * from t_emp
  5. </select><settings>
  6. <plugins>
  7.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  8. </plugins>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  12. </settings><settings>
  13. <plugins>
  14.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  15. </plugins>
  16. <plugins>
  17.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  18. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  19. </settings><settings>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins>
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  26. </settings><settings>
  27. <plugins>
  28.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  29. </plugins>
  30. <plugins>
  31.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  32. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  33. </settings><settings>
  34. <plugins>
  35.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  36. </plugins>
  37. <plugins>
  38.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  39. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  40. </settings>emp_name=#{empName}<settings>
  41. <plugins>
  42.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  43. </plugins>
  44. <plugins>
  45.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  46. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  47. </settings><settings>
  48. <plugins>
  49.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  50. </plugins>
  51. <plugins>
  52.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  53. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  54. </settings><settings>
  55. <plugins>
  56.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  57. </plugins>
  58. <plugins>
  59.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  60. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  61. </settings><settings>
  62. <plugins>
  63.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  64. </plugins>
  65. <plugins>
  66.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  67. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  68. </settings><settings>
  69. <plugins>
  70.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  71. </plugins>
  72. <plugins>
  73.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  74. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  75. </settings>age=#{age}<settings>
  76. <plugins>
  77.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  78. </plugins>
  79. <plugins>
  80.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  81. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  82. </settings><settings>
  83. <plugins>
  84.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  85. </plugins>
  86. <plugins>
  87.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  88. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  89. </settings><settings>
  90. <plugins>
  91.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  92. </plugins>
  93. <plugins>
  94.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  95. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  96. </settings><settings>
  97. <plugins>
  98.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  99. </plugins>
  100. <plugins>
  101.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  102. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  103. </settings><settings>
  104. <plugins>
  105.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  106. </plugins>
  107. <plugins>
  108.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  109. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  110. </settings>sex=#{sex}<settings>
  111. <plugins>
  112.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  113. </plugins>
  114. <plugins>
  115.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  116. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  117. </settings><settings>
  118. <plugins>
  119.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  120. </plugins>
  121. <plugins>
  122.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  123. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  124. </settings><settings>
  125. <plugins>
  126.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  127. </plugins>
  128. <plugins>
  129.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  130. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  131. </settings><settings>
  132. <plugins>
  133.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  134. </plugins>
  135. <plugins>
  136.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  137. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  138. </settings><settings>
  139. <plugins>
  140.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  141. </plugins>
  142. <plugins>
  143.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  144. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  145. </settings>email=#{email}<settings>
  146. <plugins>
  147.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  148. </plugins>
  149. <plugins>
  150.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  151. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  152. </settings><settings>
  153. <plugins>
  154.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  155. </plugins>
  156. <plugins>
  157.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  158. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  159. </settings><settings>
  160. <plugins>
  161.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  162. </plugins>
  163. <plugins>
  164.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  165. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  166. </settings><settings>
  167. <plugins>
  168.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  169. </plugins>
  170. <plugins>
  171.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  172. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  173. </settings><settings>
  174. <plugins>
  175.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  176. </plugins>
  177. <plugins>
  178.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  179. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  180. </settings>did=1<settings>
  181. <plugins>
  182.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  183. </plugins>
  184. <plugins>
  185.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  186. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  187. </settings><settings>
  188. <plugins>
  189.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  190. </plugins>
  191. <plugins>
  192.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  193. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  194. </settings><settings>
  195. <plugins>
  196.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  197. </plugins>
  198. <plugins>
  199.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  200. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  201. </settings>
复制代码
foreach标签

collection:设置要循环的数组或集合
item:表示集合或数组中的每一个数据
separator:设置循环体之间的分隔符,分隔符前后默认有一个空格
open:开始符
close:结束符
  1. int deleteMoreByArray(@Param("eids") Integer[] eids);
复制代码
  1. <plugins>
  2.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  3. </plugins>delete from t_emp where eid in<settings>
  4. <plugins>
  5.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  6. </plugins>
  7. <plugins>
  8.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  9. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  10. </settings><settings>
  11. <plugins>
  12.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  13. </plugins>
  14. <plugins>
  15.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  16. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  17. </settings><plugins>
  18.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  19. </plugins>#{eid}<settings>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins>
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  26. </settings>
复制代码
  1. public class DynamicSQLMapperTest {<plugins>
  2.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  3. </plugins>@Test<plugins>
  4.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  5. </plugins>public void testDeleteMoreByArray(){<settings>
  6. <plugins>
  7.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  8. </plugins>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  12. </settings>SqlSession sqlSessaion= SqlSessionUtils.getSqlSession();<settings>
  13. <plugins>
  14.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  15. </plugins>
  16. <plugins>
  17.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  18. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  19. </settings>DynamicSQLMapper mapper=sqlSessaion.getMapper(DynamicSQLMapper.class);<settings>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins>
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  26. </settings>int result=mapper.deleteMoreByArray(new Integer[]{6,7,8});<settings>
  27. <plugins>
  28.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  29. </plugins>
  30. <plugins>
  31.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  32. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  33. </settings>System.out.println(result);<plugins>
  34.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  35. </plugins>}}
复制代码
批量删除

,换成or
  1. <plugins>
  2.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  3. </plugins>delete from t_emp where eid in<settings>
  4. <plugins>
  5.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  6. </plugins>
  7. <plugins>
  8.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  9. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  10. </settings><settings>
  11. <plugins>
  12.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  13. </plugins>
  14. <plugins>
  15.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  16. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  17. </settings><plugins>
  18.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  19. </plugins>#{eid}<settings>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins>
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  26. </settings>
复制代码
批量添加
  1. int insertMoreByList(@Param("emps") List<Emp> emps);
复制代码
  1. <plugins>
  2.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  3. </plugins>insert into t_emp values<settings>
  4. <plugins>
  5.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  6. </plugins>
  7. <plugins>
  8.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  9. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  10. </settings><plugins>
  11.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  12. </plugins>(null,#{emp.empName},#{emp.age},#{emp.sex},#{emp.email},null)<plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins>
复制代码
  1. public class DynamicSQLMapperTest {<plugins>
  2.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  3. </plugins>@Test<plugins>
  4.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  5. </plugins>public void insertMoreByList() {<settings>
  6. <plugins>
  7.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  8. </plugins>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  12. </settings>SqlSession sqlSession = SqlSessionUtils.getSqlSession();<settings>
  13. <plugins>
  14.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  15. </plugins>
  16. <plugins>
  17.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  18. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  19. </settings>DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);<settings>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins>
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  26. </settings>Emp emp1 = new Emp(null,"a",1,"男","123@qq.com");<settings>
  27. <plugins>
  28.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  29. </plugins>
  30. <plugins>
  31.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  32. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  33. </settings>Emp emp2 = new Emp(null,"b",1,"男","123@321.com");<settings>
  34. <plugins>
  35.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  36. </plugins>
  37. <plugins>
  38.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  39. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  40. </settings>Emp emp3 = new Emp(null,"c",1,"男","123@321.com");<settings>
  41. <plugins>
  42.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  43. </plugins>
  44. <plugins>
  45.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  46. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  47. </settings>List emps = Arrays.asList(emp1, emp2, emp3);<settings>
  48. <plugins>
  49.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  50. </plugins>
  51. <plugins>
  52.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  53. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  54. </settings>mapper.insertMoreByList(emps);<settings>
  55. <plugins>
  56.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  57. </plugins>
  58. <plugins>
  59.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  60. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  61. </settings>System.out.println(emps);<plugins>
  62.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  63. </plugins>}}
复制代码
SQL标签

sql片段,可以记录一段公共sql片段,在使用的地方通过include标签进行引入
  1. <sql id="empColumns">eid,emp_name,age,sex,email</sql>
  2. <select id="getEmpByCondition" resultType="Emp">
  3.         select <include refid="empColumns"></include> from t_emp
  4. </select>
复制代码
MyBatis缓存

MyBatis一级缓存

一级缓存是SqlSession级别的,通过同一个SqlSession查询的数据会被缓存,下次查询相同的数据,就会从缓存中直接获取,不会从数据库重新访问
使一级缓存失效的四种情况:
MyBatis二级缓存

二级缓存是SqlSessionFactory级别,通过同一个SqlSessionFactory创建的SqlSession查询的结果会被缓存;此后若再次执行相同的查询语句,结果就会从缓存中获取
二级缓存开启的条件:
二级缓存相关配置

在mapper配置文件中添加的cache标签可以设置一些属性:
eviction属性:缓存回收策略
flushInterval属性:刷新间隔,单位毫秒
size属性:引用数目,正整数
readOnly属性:只读,true/false
MyBatis缓存查询顺序

整合第三方缓存

向pom.xml中添加依赖
  1. <dependency>
  2.         <groupId>org.mybatis.caches</groupId>
  3.         <artifactId>mybatis-ehcache</artifactId>
  4.         <version>1.2.1</version>
  5. </dependency>
  6. <dependency>
  7.         <groupId>ch.qos.logback</groupId>
  8.         <artifactId>logback-classic</artifactId>
  9.         <version>1.2.3</version>
  10. </dependency>
复制代码
resources下创建ehcache.xml文件
  1. <settings>
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  8. </settings><settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings>
复制代码
设置二级缓存类型

mapper.xml的cache标签中
  1. [/code][size=3]加入logback日志[/size]
  2. [code]<settings>
  3. <plugins>
  4.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  5. </plugins>
  6. <plugins>
  7.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  8. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  9. </settings><settings>
  10. <plugins>
  11.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  12. </plugins>
  13. <plugins>
  14.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  15. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  16. </settings><settings>
  17. <plugins>
  18.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  19. </plugins>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  23. </settings><settings>
  24. <plugins>
  25.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  26. </plugins>
  27. <plugins>
  28.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  29. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  30. </settings><settings>
  31. <plugins>
  32.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  33. </plugins>
  34. <plugins>
  35.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  36. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  37. </settings><settings>
  38. <plugins>
  39.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  40. </plugins>
  41. <plugins>
  42.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  43. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  44. </settings><plugins>
  45.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  46. </plugins>[%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger] [%msg]%n<settings>
  47. <plugins>
  48.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  49. </plugins>
  50. <plugins>
  51.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  52. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  53. </settings><settings>
  54. <plugins>
  55.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  56. </plugins>
  57. <plugins>
  58.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  59. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  60. </settings><settings>
  61. <plugins>
  62.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  63. </plugins>
  64. <plugins>
  65.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  66. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  67. </settings><settings>
  68. <plugins>
  69.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  70. </plugins>
  71. <plugins>
  72.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  73. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  74. </settings><settings>
  75. <plugins>
  76.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  77. </plugins>
  78. <plugins>
  79.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  80. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  81. </settings><settings>
  82. <plugins>
  83.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  84. </plugins>
  85. <plugins>
  86.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  87. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  88. </settings><plugins>
  89.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  90. </plugins>
复制代码
MyBatis逆向工程

正向工程:先创建Java实体类,由框架负责根据实体类生成数据库表。Hibernate是支持正向工程的。
逆向工程:先创建数据库表,由框架负责根据数据库表,反向生成Java实体类,Mapper接口,Mapper映射文件。
创建逆向工程的步骤

1.添加依赖和插件

pom.xml
  1. jar<settings>
  2. <plugins>
  3.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>
  5. <plugins>
  6.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  7. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  8. </settings><settings>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  15. </settings>org.mybatis<settings>
  16. <plugins>
  17.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  18. </plugins>
  19. <plugins>
  20.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  21. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  22. </settings>mybatis<settings>
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins>
  26. <plugins>
  27.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  28. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  29. </settings>3.5.10<settings>
  30. <plugins>
  31.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  32. </plugins>
  33. <plugins>
  34.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  35. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  36. </settings><settings>
  37. <plugins>
  38.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  39. </plugins>
  40. <plugins>
  41.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  42. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  43. </settings><plugins>
  44.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  45. </plugins>junit<settings>
  46. <plugins>
  47.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  48. </plugins>
  49. <plugins>
  50.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  51. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  52. </settings>junit<settings>
  53. <plugins>
  54.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  55. </plugins>
  56. <plugins>
  57.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  58. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  59. </settings>4.12<settings>
  60. <plugins>
  61.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  62. </plugins>
  63. <plugins>
  64.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  65. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  66. </settings>test<settings>
  67. <plugins>
  68.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  69. </plugins>
  70. <plugins>
  71.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  72. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  73. </settings><settings>
  74. <plugins>
  75.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  76. </plugins>
  77. <plugins>
  78.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  79. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  80. </settings><plugins>
  81.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  82. </plugins>mysql<settings>
  83. <plugins>
  84.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  85. </plugins>
  86. <plugins>
  87.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  88. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  89. </settings>mysql-connector-java<settings>
  90. <plugins>
  91.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  92. </plugins>
  93. <plugins>
  94.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  95. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  96. </settings>8.0.30<settings>
  97. <plugins>
  98.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  99. </plugins>
  100. <plugins>
  101.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  102. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  103. </settings><settings>
  104. <plugins>
  105.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  106. </plugins>
  107. <plugins>
  108.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  109. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  110. </settings><plugins>
  111.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  112. </plugins>log4j<settings>
  113. <plugins>
  114.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  115. </plugins>
  116. <plugins>
  117.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  118. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  119. </settings>log4j<settings>
  120. <plugins>
  121.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  122. </plugins>
  123. <plugins>
  124.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  125. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  126. </settings>1.2.17<settings>
  127. <plugins>
  128.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  129. </plugins>
  130. <plugins>
  131.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  132. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  133. </settings><settings>
  134. <plugins>
  135.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  136. </plugins>
  137. <plugins>
  138.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  139. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  140. </settings><settings>
  141. <plugins>
  142.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  143. </plugins>
  144. <plugins>
  145.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  146. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  147. </settings><settings>
  148. <plugins>
  149.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  150. </plugins>
  151. <plugins>
  152.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  153. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  154. </settings><settings>
  155. <plugins>
  156.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  157. </plugins>
  158. <plugins>
  159.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  160. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  161. </settings>org.mybatis.generator<settings>
  162. <plugins>
  163.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  164. </plugins>
  165. <plugins>
  166.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  167. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  168. </settings><plugins>
  169.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  170. </plugins>mybatis-generator-maven-plugin<settings>
  171. <plugins>
  172.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  173. </plugins>
  174. <plugins>
  175.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  176. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  177. </settings><plugins>
  178.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  179. </plugins>1.3.0<settings>
  180. <plugins>
  181.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  182. </plugins>
  183. <plugins>
  184.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  185. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  186. </settings><settings>
  187. <plugins>
  188.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  189. </plugins>
  190. <plugins>
  191.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  192. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  193. </settings><settings>
  194. <plugins>
  195.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  196. </plugins>
  197. <plugins>
  198.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  199. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  200. </settings><settings>
  201. <plugins>
  202.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  203. </plugins>
  204. <plugins>
  205.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  206. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  207. </settings><settings>
  208. <plugins>
  209.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  210. </plugins>
  211. <plugins>
  212.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  213. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  214. </settings><settings>
  215. <plugins>
  216.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  217. </plugins>
  218. <plugins>
  219.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  220. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  221. </settings><settings>
  222. <plugins>
  223.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  224. </plugins>
  225. <plugins>
  226.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  227. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  228. </settings><settings>
  229. <plugins>
  230.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  231. </plugins>
  232. <plugins>
  233.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  234. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  235. </settings><settings>
  236. <plugins>
  237.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  238. </plugins>
  239. <plugins>
  240.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  241. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  242. </settings><plugins>
  243.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  244. </plugins>org.mybatis.generator<settings>
  245. <plugins>
  246.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  247. </plugins>
  248. <plugins>
  249.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  250. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  251. </settings><settings>
  252. <plugins>
  253.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  254. </plugins>
  255. <plugins>
  256.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  257. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  258. </settings><plugins>
  259.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  260. </plugins>mybatis-generator-core<settings>
  261. <plugins>
  262.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  263. </plugins>
  264. <plugins>
  265.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  266. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  267. </settings><settings>
  268. <plugins>
  269.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  270. </plugins>
  271. <plugins>
  272.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  273. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  274. </settings><plugins>
  275.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  276. </plugins>1.3.2<settings>
  277. <plugins>
  278.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  279. </plugins>
  280. <plugins>
  281.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  282. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  283. </settings><settings>
  284. <plugins>
  285.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  286. </plugins>
  287. <plugins>
  288.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  289. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  290. </settings><settings>
  291. <plugins>
  292.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  293. </plugins>
  294. <plugins>
  295.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  296. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  297. </settings><settings>
  298. <plugins>
  299.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  300. </plugins>
  301. <plugins>
  302.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  303. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  304. </settings><settings>
  305. <plugins>
  306.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  307. </plugins>
  308. <plugins>
  309.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  310. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  311. </settings><settings>
  312. <plugins>
  313.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  314. </plugins>
  315. <plugins>
  316.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  317. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  318. </settings><settings>
  319. <plugins>
  320.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  321. </plugins>
  322. <plugins>
  323.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  324. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  325. </settings><settings>
  326. <plugins>
  327.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  328. </plugins>
  329. <plugins>
  330.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  331. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  332. </settings><plugins>
  333.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  334. </plugins>com.mchange<settings>
  335. <plugins>
  336.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  337. </plugins>
  338. <plugins>
  339.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  340. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  341. </settings><settings>
  342. <plugins>
  343.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  344. </plugins>
  345. <plugins>
  346.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  347. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  348. </settings><plugins>
  349.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  350. </plugins>c3p0<settings>
  351. <plugins>
  352.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  353. </plugins>
  354. <plugins>
  355.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  356. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  357. </settings><settings>
  358. <plugins>
  359.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  360. </plugins>
  361. <plugins>
  362.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  363. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  364. </settings><plugins>
  365.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  366. </plugins>0.9.2<settings>
  367. <plugins>
  368.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  369. </plugins>
  370. <plugins>
  371.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  372. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  373. </settings><settings>
  374. <plugins>
  375.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  376. </plugins>
  377. <plugins>
  378.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  379. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  380. </settings><settings>
  381. <plugins>
  382.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  383. </plugins>
  384. <plugins>
  385.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  386. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  387. </settings><settings>
  388. <plugins>
  389.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  390. </plugins>
  391. <plugins>
  392.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  393. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  394. </settings><settings>
  395. <plugins>
  396.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  397. </plugins>
  398. <plugins>
  399.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  400. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  401. </settings><settings>
  402. <plugins>
  403.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  404. </plugins>
  405. <plugins>
  406.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  407. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  408. </settings><settings>
  409. <plugins>
  410.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  411. </plugins>
  412. <plugins>
  413.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  414. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  415. </settings><settings>
  416. <plugins>
  417.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  418. </plugins>
  419. <plugins>
  420.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  421. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  422. </settings><plugins>
  423.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  424. </plugins>mysql<settings>
  425. <plugins>
  426.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  427. </plugins>
  428. <plugins>
  429.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  430. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  431. </settings><settings>
  432. <plugins>
  433.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  434. </plugins>
  435. <plugins>
  436.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  437. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  438. </settings><plugins>
  439.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  440. </plugins>mysql-connector-java<settings>
  441. <plugins>
  442.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  443. </plugins>
  444. <plugins>
  445.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  446. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  447. </settings><settings>
  448. <plugins>
  449.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  450. </plugins>
  451. <plugins>
  452.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  453. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  454. </settings><plugins>
  455.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  456. </plugins>8.0.27<settings>
  457. <plugins>
  458.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  459. </plugins>
  460. <plugins>
  461.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  462. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  463. </settings><settings>
  464. <plugins>
  465.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  466. </plugins>
  467. <plugins>
  468.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  469. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  470. </settings><settings>
  471. <plugins>
  472.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  473. </plugins>
  474. <plugins>
  475.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  476. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  477. </settings><settings>
  478. <plugins>
  479.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  480. </plugins>
  481. <plugins>
  482.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  483. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  484. </settings><settings>
  485. <plugins>
  486.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  487. </plugins>
  488. <plugins>
  489.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  490. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  491. </settings>
复制代码
2.创建MyBatis核心配置文件

Mybatis.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. <settings>
  4. <plugins>
  5.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  6. </plugins>
  7. <plugins>
  8.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  9. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  10. </settings>PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  11. <settings>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins>
  15. <plugins>
  16.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  17. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  18. </settings>"http://mybatis.org/dtd/mybatis-3-config.dtd">
  19. <configuration>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins>
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins><environments default="development">
  26. <settings>
  27. <plugins>
  28.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  29. </plugins>
  30. <plugins>
  31.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  32. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  33. </settings><environment id="development">
  34. <settings>
  35. <plugins>
  36.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  37. </plugins>
  38. <plugins>
  39.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  40. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  41. </settings><plugins>
  42.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  43. </plugins><transactionManager type="JDBC"/>
  44. <settings>
  45. <plugins>
  46.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  47. </plugins>
  48. <plugins>
  49.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  50. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  51. </settings><plugins>
  52.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  53. </plugins><dataSource type="POOLED">
  54. <settings>
  55. <plugins>
  56.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  57. </plugins>
  58. <plugins>
  59.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  60. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  61. </settings><settings>
  62. <plugins>
  63.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  64. </plugins>
  65. <plugins>
  66.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  67. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  68. </settings><property name="driver" value="com.mysql.cj.jdbc.Driver"/>
  69. <settings>
  70. <plugins>
  71.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  72. </plugins>
  73. <plugins>
  74.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  75. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  76. </settings><settings>
  77. <plugins>
  78.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  79. </plugins>
  80. <plugins>
  81.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  82. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  83. </settings><property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
  84. <settings>
  85. <plugins>
  86.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  87. </plugins>
  88. <plugins>
  89.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  90. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  91. </settings><settings>
  92. <plugins>
  93.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  94. </plugins>
  95. <plugins>
  96.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  97. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  98. </settings><property name="username" value="root"/>
  99. <settings>
  100. <plugins>
  101.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  102. </plugins>
  103. <plugins>
  104.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  105. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  106. </settings><settings>
  107. <plugins>
  108.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  109. </plugins>
  110. <plugins>
  111.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  112. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  113. </settings><property name="password" value="xxxxxxxxx"/>
  114. <settings>
  115. <plugins>
  116.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  117. </plugins>
  118. <plugins>
  119.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  120. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  121. </settings><plugins>
  122.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  123. </plugins></dataSource>
  124. <settings>
  125. <plugins>
  126.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  127. </plugins>
  128. <plugins>
  129.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  130. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  131. </settings></environment>
  132. <plugins>
  133.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  134. </plugins></environments>
  135. <plugins>
  136.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  137. </plugins>
  138. <plugins>
  139.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  140. </plugins><mappers>
  141. <settings>
  142. <plugins>
  143.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  144. </plugins>
  145. <plugins>
  146.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  147. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  148. </settings><mapper resource="mappers/UserMapper.xml"/>
  149. <plugins>
  150.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  151. </plugins></mappers>
  152. </configuration><settings>
  153. <plugins>
  154.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  155. </plugins>
  156. <plugins>
  157.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  158. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  159. </settings><plugins>
  160.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  161. </plugins>
复制代码
3.将log4j.xml和jdbc.properties复制过来

4.创建逆向工程配置文件

文件名必须是generatorConfig.xml
MyBatis3Simple: 生成基本的CRUD
MyBatis3: 生成带条件的CRUD(使用最多)
generatorConfig.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. <settings>
  4. <plugins>
  5.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  6. </plugins>
  7. <plugins>
  8.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  9. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  10. </settings>PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  11. <settings>
  12. <plugins>
  13.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  14. </plugins>
  15. <plugins>
  16.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  17. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  18. </settings>"http://mybatis.org/dtd/mybatis-3-config.dtd">
  19. <configuration>
  20. <plugins>
  21.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  22. </plugins>
  23. <plugins>
  24.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  25. </plugins><environments default="development">
  26. <settings>
  27. <plugins>
  28.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  29. </plugins>
  30. <plugins>
  31.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  32. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  33. </settings><environment id="development">
  34. <settings>
  35. <plugins>
  36.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  37. </plugins>
  38. <plugins>
  39.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  40. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  41. </settings><plugins>
  42.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  43. </plugins><transactionManager type="JDBC"/>
  44. <settings>
  45. <plugins>
  46.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  47. </plugins>
  48. <plugins>
  49.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  50. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  51. </settings><plugins>
  52.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  53. </plugins><dataSource type="POOLED">
  54. <settings>
  55. <plugins>
  56.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  57. </plugins>
  58. <plugins>
  59.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  60. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  61. </settings><settings>
  62. <plugins>
  63.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  64. </plugins>
  65. <plugins>
  66.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  67. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  68. </settings><property name="driver" value="com.mysql.cj.jdbc.Driver"/>
  69. <settings>
  70. <plugins>
  71.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  72. </plugins>
  73. <plugins>
  74.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  75. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  76. </settings><settings>
  77. <plugins>
  78.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  79. </plugins>
  80. <plugins>
  81.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  82. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  83. </settings><property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
  84. <settings>
  85. <plugins>
  86.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  87. </plugins>
  88. <plugins>
  89.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  90. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  91. </settings><settings>
  92. <plugins>
  93.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  94. </plugins>
  95. <plugins>
  96.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  97. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  98. </settings><property name="username" value="root"/>
  99. <settings>
  100. <plugins>
  101.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  102. </plugins>
  103. <plugins>
  104.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  105. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  106. </settings><settings>
  107. <plugins>
  108.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  109. </plugins>
  110. <plugins>
  111.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  112. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  113. </settings><property name="password" value="xxxxxxxxx"/>
  114. <settings>
  115. <plugins>
  116.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  117. </plugins>
  118. <plugins>
  119.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  120. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  121. </settings><plugins>
  122.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  123. </plugins></dataSource>
  124. <settings>
  125. <plugins>
  126.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  127. </plugins>
  128. <plugins>
  129.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  130. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  131. </settings></environment>
  132. <plugins>
  133.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  134. </plugins></environments>
  135. <plugins>
  136.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  137. </plugins>
  138. <plugins>
  139.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  140. </plugins><mappers>
  141. <settings>
  142. <plugins>
  143.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  144. </plugins>
  145. <plugins>
  146.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  147. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  148. </settings><mapper resource="mappers/UserMapper.xml"/>
  149. <plugins>
  150.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  151. </plugins></mappers>
  152. </configuration><settings>
  153. <plugins>
  154.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  155. </plugins>
  156. <plugins>
  157.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  158. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  159. </settings><settings>
  160. <plugins>
  161.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  162. </plugins>
  163. <plugins>
  164.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  165. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  166. </settings><settings>
  167. <plugins>
  168.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  169. </plugins>
  170. <plugins>
  171.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  172. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  173. </settings><settings>
  174. <plugins>
  175.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  176. </plugins>
  177. <plugins>
  178.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  179. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  180. </settings><settings>
  181. <plugins>
  182.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  183. </plugins>
  184. <plugins>
  185.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  186. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  187. </settings><settings>
  188. <plugins>
  189.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  190. </plugins>
  191. <plugins>
  192.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  193. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  194. </settings><settings>
  195. <plugins>
  196.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  197. </plugins>
  198. <plugins>
  199.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  200. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  201. </settings><plugins>
  202.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  203. </plugins>[table]<settings>
  204. <plugins>
  205.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  206. </plugins>
  207. <plugins>
  208.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  209. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  210. </settings>[table]<plugins>
  211.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  212. </plugins>
复制代码
5.执行MBG插件的generate目标

双击mybatis-generator:generate
6.通过Example进行查询演示
  1. public class MBGTest {<plugins>
  2.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  3. </plugins>@Test<plugins>
  4.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  5. </plugins>public void testMBG(){<settings>
  6. <plugins>
  7.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  8. </plugins>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  12. </settings>try {<settings>
  13. <plugins>
  14.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  15. </plugins>
  16. <plugins>
  17.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  18. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  19. </settings><plugins>
  20.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  21. </plugins>InputStream is= Resources.getResourceAsStream("mybatis-config.xml");<settings>
  22. <plugins>
  23.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  24. </plugins>
  25. <plugins>
  26.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  27. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  28. </settings><plugins>
  29.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  30. </plugins>SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);<settings>
  31. <plugins>
  32.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  33. </plugins>
  34. <plugins>
  35.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  36. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  37. </settings><plugins>
  38.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  39. </plugins>SqlSession sqlSession=sqlSessionFactory.openSession(true);<settings>
  40. <plugins>
  41.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  42. </plugins>
  43. <plugins>
  44.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  45. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  46. </settings><plugins>
  47.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  48. </plugins>EmpMapper mapper=sqlSession.getMapper(EmpMapper.class);<settings>
  49. <plugins>
  50.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  51. </plugins>
  52. <plugins>
  53.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  54. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  55. </settings><plugins>
  56.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  57. </plugins>//查询所有//<settings>
  58. <plugins>
  59.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  60. </plugins>
  61. <plugins>
  62.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  63. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  64. </settings><plugins>
  65.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  66. </plugins>List list=mapper.selectByExample(null);//<settings>
  67. <plugins>
  68.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  69. </plugins>
  70. <plugins>
  71.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  72. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  73. </settings><plugins>
  74.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  75. </plugins>list.forEach(emp-> System.out.println(emp));<settings>
  76. <plugins>
  77.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  78. </plugins>
  79. <plugins>
  80.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  81. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  82. </settings><plugins>
  83.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  84. </plugins>//条件查询<settings>
  85. <plugins>
  86.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  87. </plugins>
  88. <plugins>
  89.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  90. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  91. </settings><plugins>
  92.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  93. </plugins>EmpExample example=new EmpExample();<settings>
  94. <plugins>
  95.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  96. </plugins>
  97. <plugins>
  98.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  99. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  100. </settings><plugins>
  101.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  102. </plugins>example.createCriteria().andEmpNameEqualTo("张三").andAgeGreaterThanOrEqualTo(11);<settings>
  103. <plugins>
  104.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  105. </plugins>
  106. <plugins>
  107.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  108. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  109. </settings><plugins>
  110.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  111. </plugins>List list=mapper.selectByExample(example);<settings>
  112. <plugins>
  113.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  114. </plugins>
  115. <plugins>
  116.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  117. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  118. </settings><plugins>
  119.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  120. </plugins>list.forEach(emp-> System.out.println(emp));<settings>
  121. <plugins>
  122.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  123. </plugins>
  124. <plugins>
  125.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  126. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  127. </settings>} catch (IOException e) {<settings>
  128. <plugins>
  129.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  130. </plugins>
  131. <plugins>
  132.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  133. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  134. </settings><plugins>
  135.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  136. </plugins>e.printStackTrace();<settings>
  137. <plugins>
  138.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  139. </plugins>
  140. <plugins>
  141.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  142. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  143. </settings>}<plugins>
  144.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  145. </plugins>}}
复制代码
分页插件

步骤

添加依赖
  1. <dependency>
  2.         <groupId>com.github.pagehelper</groupId>
  3.         <artifactId>pagehelper</artifactId>
  4.         <version>5.2.0</version>
  5. </dependency>
复制代码
配置插件
  1. <plugins>
  2.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  3. </plugins>
复制代码
开启分页功能
  1. public class PageHelperTest {<plugins>
  2.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  3. </plugins>@Test<plugins>
  4.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  5. </plugins>public void testPageHelper(){<settings>
  6. <plugins>
  7.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  8. </plugins>
  9. <plugins>
  10.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  11. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  12. </settings>try {<settings>
  13. <plugins>
  14.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  15. </plugins>
  16. <plugins>
  17.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  18. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  19. </settings><plugins>
  20.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  21. </plugins>InputStream is= Resources.getResourceAsStream("mybatis-config.xml");<settings>
  22. <plugins>
  23.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  24. </plugins>
  25. <plugins>
  26.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  27. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  28. </settings><plugins>
  29.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  30. </plugins>SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);<settings>
  31. <plugins>
  32.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  33. </plugins>
  34. <plugins>
  35.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  36. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  37. </settings><plugins>
  38.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  39. </plugins>SqlSession sqlSession=sqlSessionFactory.openSession(true);<settings>
  40. <plugins>
  41.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  42. </plugins>
  43. <plugins>
  44.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  45. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  46. </settings><plugins>
  47.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  48. </plugins>EmpMapper mapper=sqlSession.getMapper(EmpMapper.class);<settings>
  49. <plugins>
  50.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  51. </plugins>
  52. <plugins>
  53.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  54. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  55. </settings><plugins>
  56.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  57. </plugins>PageHelper.startPage(1,4);<settings>
  58. <plugins>
  59.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  60. </plugins>
  61. <plugins>
  62.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  63. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  64. </settings><plugins>
  65.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  66. </plugins>List list = mapper.selectByExample(null);<settings>
  67. <plugins>
  68.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  69. </plugins>
  70. <plugins>
  71.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  72. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  73. </settings><plugins>
  74.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  75. </plugins>list.forEach(emp-> System.out.println(emp));<settings>
  76. <plugins>
  77.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  78. </plugins>
  79. <plugins>
  80.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  81. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  82. </settings>} catch (IOException e) {<settings>
  83. <plugins>
  84.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  85. </plugins>
  86. <plugins>
  87.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  88. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  89. </settings><plugins>
  90.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  91. </plugins>e.printStackTrace();<settings>
  92. <plugins>
  93.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  94. </plugins>
  95. <plugins>
  96.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  97. </plugins><setting name="mapUnderscoreToCamelCase" value="true"/>
  98. </settings>}<plugins>
  99.     <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  100. </plugins>}}
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4