Mybatis自动生成代码

         林炳文Evankaka原创作品。转载请注明出处https://blog.csdn.net/Evankaka/article/details/47023955

       摘要:本文将简要介绍怎样利用Mybatis Generator自动生成Mybatis的相关代码,Mybatis Generator是一个非常好用的工具,使用它可以大大节省开发的时间,并减少代码的编写量。

本文工程免费下载

一、构建一个环境


1. 首先创建一个表:


   
   
  1. CREATE TABLE
  2. t_user
  3. (
  4. USER_ID INT NOT NULL AUTO_INCREMENT,
  5. USER_NAME CHAR( 30) NOT NULL,
  6. USER_PASSWORD CHAR( 10) NOT NULL,
  7. USER_EMAIL CHAR( 30) NOT NULL,
  8. PRIMARY KEY (USER_ID)
  9. )
  10. ENGINE= InnoDB DEFAULT CHARSET=utf8;

2. 在 Mybatis 主页 http://code.google.com/p/mybatis/ 上下载 Mybatis mybatis-generator-core 或者在这里下载:http://download.csdn.net/detail/evankaka/8926999

二、xml文件编写

1、新建一个工程。然后新建如下包,都是空的


2、然后新建generator.xmll文件

内容如下:


   
   
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5. <generatorConfiguration>
  6. <!-- classPathEntry:数据库的JDBC驱动的jar包地址 -->
  7. <classPathEntry location="D:\Java\Jar\mysql-connector-java-5.1.22\mysql-connector-java-5.1.22-bin.jar" />
  8. <context id="DB2Tables" targetRuntime="MyBatis3">
  9. <commentGenerator>
  10. <!-- 抑制警告 -->
  11. <property name="suppressTypeWarnings" value="true" />
  12. <!-- 是否去除自动生成的注释 true:是 : false:否 -->
  13. <property name="suppressAllComments" value="false" />
  14. <!-- 是否生成注释代时间戳-->
  15. <property name="suppressDate" value="true" />
  16. </commentGenerator>
  17. <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
  18. <jdbcConnection driverClass="com.mysql.jdbc.Driver"
  19. connectionURL= "jdbc:mysql://localhost/learning" userId= "root"
  20. password= "christmas258@">
  21. </jdbcConnection>
  22. <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和
  23. NUMERIC 类型解析为java.math.BigDecimal -->
  24. <!-- <javaTypeResolver>
  25. <property name="forceBigDecimals" value="false" />
  26. </javaTypeResolver> -->
  27. <!--生成Model类存放位置 -->
  28. <javaModelGenerator targetPackage="com.lin.domain"
  29. targetProject= "D:\lunaJee-workspace\MyBatisLearningChapter7\src">
  30. <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
  31. <property name="enableSubPackages" value="false" />
  32. <!-- 是否针对string类型的字段在set的时候进行trim调用 -->
  33. <property name="trimStrings" value="true" />
  34. </javaModelGenerator>
  35. <!--生成映射文件存放位置 -->
  36. <sqlMapGenerator targetPackage="com.lin.mapper"
  37. targetProject= "D:\lunaJee-workspace\MyBatisLearningChapter7\src">
  38. <property name="enableSubPackages" value="true" />
  39. </sqlMapGenerator>
  40. <!--生成Dao类存放位置 -->
  41. <javaClientGenerator type="XMLMAPPER"
  42. targetPackage= "com.lin.dao" targetProject= "D:\lunaJee-workspace\MyBatisLearningChapter7\src">
  43. <property name="enableSubPackages" value="true" />
  44. </javaClientGenerator>
  45. <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->
  46. <table schema="general" tableName="T_USER" domainObjectName="User">
  47. <!--domain字段的命名规则,false:默认为驼峰命名 true:按数据库真实命名 -->
  48. <property name="useActualColumnNames" value="false"/>
  49. <!-- 忽略列,不生成bean 字段 -->
  50. <!-- <ignoreColumn column="FRED" /> -->
  51. <!-- 指定列的java数据类型 -->
  52. <!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> -->
  53. </table>
  54. </context>
  55. </generatorConfiguration>

三、自动代码生成

自动代码生成有4种方法

1、直接cmd下命令行生成

命令如下:java -jar 电脑上mybatis-generator-core-1.3.0.jar的绝对路径  -configfile 电脑上generator.xml的绝对路径,这里的generator.xml不一定要放在工程的src文件中。

如我的这个项目就是:

运行的结果如下:

然后在eclipse中刷新一下:结果出来了


看看各个文件

(1)User.java


   
   
  1. package com.lin.domain;
  2. public class User {
  3. /**
  4. * This field was generated by MyBatis Generator.
  5. * This field corresponds to the database column t_user.USER_ID
  6. *
  7. * @mbggenerated
  8. */
  9. private Integer userId;
  10. /**
  11. * This field was generated by MyBatis Generator.
  12. * This field corresponds to the database column t_user.USER_NAME
  13. *
  14. * @mbggenerated
  15. */
  16. private String userName;
  17. /**
  18. * This field was generated by MyBatis Generator.
  19. * This field corresponds to the database column t_user.USER_PASSWORD
  20. *
  21. * @mbggenerated
  22. */
  23. private String userPassword;
  24. /**
  25. * This field was generated by MyBatis Generator.
  26. * This field corresponds to the database column t_user.USER_EMAIL
  27. *
  28. * @mbggenerated
  29. */
  30. private String userEmail;
  31. /**
  32. * This method was generated by MyBatis Generator.
  33. * This method returns the value of the database column t_user.USER_ID
  34. *
  35. * @return the value of t_user.USER_ID
  36. *
  37. * @mbggenerated
  38. */
  39. public Integer getUserId() {
  40. return userId;
  41. }
  42. /**
  43. * This method was generated by MyBatis Generator.
  44. * This method sets the value of the database column t_user.USER_ID
  45. *
  46. * @param userId the value for t_user.USER_ID
  47. *
  48. * @mbggenerated
  49. */
  50. public void setUserId(Integer userId) {
  51. this.userId = userId;
  52. }
  53. /**
  54. * This method was generated by MyBatis Generator.
  55. * This method returns the value of the database column t_user.USER_NAME
  56. *
  57. * @return the value of t_user.USER_NAME
  58. *
  59. * @mbggenerated
  60. */
  61. public String getUserName() {
  62. return userName;
  63. }
  64. /**
  65. * This method was generated by MyBatis Generator.
  66. * This method sets the value of the database column t_user.USER_NAME
  67. *
  68. * @param userName the value for t_user.USER_NAME
  69. *
  70. * @mbggenerated
  71. */
  72. public void setUserName(String userName) {
  73. this.userName = userName == null ? null : userName.trim();
  74. }
  75. /**
  76. * This method was generated by MyBatis Generator.
  77. * This method returns the value of the database column t_user.USER_PASSWORD
  78. *
  79. * @return the value of t_user.USER_PASSWORD
  80. *
  81. * @mbggenerated
  82. */
  83. public String getUserPassword() {
  84. return userPassword;
  85. }
  86. /**
  87. * This method was generated by MyBatis Generator.
  88. * This method sets the value of the database column t_user.USER_PASSWORD
  89. *
  90. * @param userPassword the value for t_user.USER_PASSWORD
  91. *
  92. * @mbggenerated
  93. */
  94. public void setUserPassword(String userPassword) {
  95. this.userPassword = userPassword == null ? null : userPassword.trim();
  96. }
  97. /**
  98. * This method was generated by MyBatis Generator.
  99. * This method returns the value of the database column t_user.USER_EMAIL
  100. *
  101. * @return the value of t_user.USER_EMAIL
  102. *
  103. * @mbggenerated
  104. */
  105. public String getUserEmail() {
  106. return userEmail;
  107. }
  108. /**
  109. * This method was generated by MyBatis Generator.
  110. * This method sets the value of the database column t_user.USER_EMAIL
  111. *
  112. * @param userEmail the value for t_user.USER_EMAIL
  113. *
  114. * @mbggenerated
  115. */
  116. public void setUserEmail(String userEmail) {
  117. this.userEmail = userEmail == null ? null : userEmail.trim();
  118. }
  119. }

UserExample.java这个文件可以控制是否生成


   
   
  1. package com.lin.domain;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. public class UserExample {
  5. /**
  6. * This field was generated by MyBatis Generator.
  7. * This field corresponds to the database table t_user
  8. *
  9. * @mbggenerated
  10. */
  11. protected String orderByClause;
  12. /**
  13. * This field was generated by MyBatis Generator.
  14. * This field corresponds to the database table t_user
  15. *
  16. * @mbggenerated
  17. */
  18. protected boolean distinct;
  19. /**
  20. * This field was generated by MyBatis Generator.
  21. * This field corresponds to the database table t_user
  22. *
  23. * @mbggenerated
  24. */
  25. protected List<Criteria> oredCriteria;
  26. /**
  27. * This method was generated by MyBatis Generator.
  28. * This method corresponds to the database table t_user
  29. *
  30. * @mbggenerated
  31. */
  32. public UserExample() {
  33. oredCriteria = new ArrayList<Criteria>();
  34. }
  35. /**
  36. * This method was generated by MyBatis Generator.
  37. * This method corresponds to the database table t_user
  38. *
  39. * @mbggenerated
  40. */
  41. public void setOrderByClause(String orderByClause) {
  42. this.orderByClause = orderByClause;
  43. }
  44. /**
  45. * This method was generated by MyBatis Generator.
  46. * This method corresponds to the database table t_user
  47. *
  48. * @mbggenerated
  49. */
  50. public String getOrderByClause() {
  51. return orderByClause;
  52. }
  53. /**
  54. * This method was generated by MyBatis Generator.
  55. * This method corresponds to the database table t_user
  56. *
  57. * @mbggenerated
  58. */
  59. public void setDistinct(boolean distinct) {
  60. this.distinct = distinct;
  61. }
  62. /**
  63. * This method was generated by MyBatis Generator.
  64. * This method corresponds to the database table t_user
  65. *
  66. * @mbggenerated
  67. */
  68. public boolean isDistinct() {
  69. return distinct;
  70. }
  71. /**
  72. * This method was generated by MyBatis Generator.
  73. * This method corresponds to the database table t_user
  74. *
  75. * @mbggenerated
  76. */
  77. public List<Criteria> getOredCriteria() {
  78. return oredCriteria;
  79. }
  80. /**
  81. * This method was generated by MyBatis Generator.
  82. * This method corresponds to the database table t_user
  83. *
  84. * @mbggenerated
  85. */
  86. public void or(Criteria criteria) {
  87. oredCriteria.add(criteria);
  88. }
  89. /**
  90. * This method was generated by MyBatis Generator.
  91. * This method corresponds to the database table t_user
  92. *
  93. * @mbggenerated
  94. */
  95. public Criteria or() {
  96. Criteria criteria = createCriteriaInternal();
  97. oredCriteria.add(criteria);
  98. return criteria;
  99. }
  100. /**
  101. * This method was generated by MyBatis Generator.
  102. * This method corresponds to the database table t_user
  103. *
  104. * @mbggenerated
  105. */
  106. public Criteria createCriteria() {
  107. Criteria criteria = createCriteriaInternal();
  108. if (oredCriteria.size() == 0) {
  109. oredCriteria.add(criteria);
  110. }
  111. return criteria;
  112. }
  113. /**
  114. * This method was generated by MyBatis Generator.
  115. * This method corresponds to the database table t_user
  116. *
  117. * @mbggenerated
  118. */
  119. protected Criteria createCriteriaInternal() {
  120. Criteria criteria = new Criteria();
  121. return criteria;
  122. }
  123. /**
  124. * This method was generated by MyBatis Generator.
  125. * This method corresponds to the database table t_user
  126. *
  127. * @mbggenerated
  128. */
  129. public void clear() {
  130. oredCriteria.clear();
  131. orderByClause = null;
  132. distinct = false;
  133. }
  134. /**
  135. * This class was generated by MyBatis Generator.
  136. * This class corresponds to the database table t_user
  137. *
  138. * @mbggenerated
  139. */
  140. protected abstract static class GeneratedCriteria {
  141. protected List<Criterion> criteria;
  142. protected GeneratedCriteria() {
  143. super();
  144. criteria = new ArrayList<Criterion>();
  145. }
  146. public boolean isValid() {
  147. return criteria.size() > 0;
  148. }
  149. public List<Criterion> getCriteria() {
  150. return criteria;
  151. }
  152. protected void addCriterion(String condition) {
  153. if (condition == null) {
  154. throw new RuntimeException( "Value for condition cannot be null");
  155. }
  156. criteria.add( new Criterion(condition));
  157. }
  158. protected void addCriterion(String condition, Object value, String property) {
  159. if (value == null) {
  160. throw new RuntimeException( "Value for " + property + " cannot be null");
  161. }
  162. criteria.add( new Criterion(condition, value));
  163. }
  164. protected void addCriterion(String condition, Object value1, Object value2, String property) {
  165. if (value1 == null || value2 == null) {
  166. throw new RuntimeException( "Between values for " + property + " cannot be null");
  167. }
  168. criteria.add( new Criterion(condition, value1, value2));
  169. }
  170. public Criteria andUserIdIsNull() {
  171. addCriterion( "USER_ID is null");
  172. return (Criteria) this;
  173. }
  174. public Criteria andUserIdIsNotNull() {
  175. addCriterion( "USER_ID is not null");
  176. return (Criteria) this;
  177. }
  178. public Criteria andUserIdEqualTo(Integer value) {
  179. addCriterion( "USER_ID =", value, "userId");
  180. return (Criteria) this;
  181. }
  182. public Criteria andUserIdNotEqualTo(Integer value) {
  183. addCriterion( "USER_ID <>", value, "userId");
  184. return (Criteria) this;
  185. }
  186. public Criteria andUserIdGreaterThan(Integer value) {
  187. addCriterion( "USER_ID >", value, "userId");
  188. return (Criteria) this;
  189. }
  190. public Criteria andUserIdGreaterThanOrEqualTo(Integer value) {
  191. addCriterion( "USER_ID >=", value, "userId");
  192. return (Criteria) this;
  193. }
  194. public Criteria andUserIdLessThan(Integer value) {
  195. addCriterion( "USER_ID <", value, "userId");
  196. return (Criteria) this;
  197. }
  198. public Criteria andUserIdLessThanOrEqualTo(Integer value) {
  199. addCriterion( "USER_ID <=", value, "userId");
  200. return (Criteria) this;
  201. }
  202. public Criteria andUserIdIn(List<Integer> values) {
  203. addCriterion( "USER_ID in", values, "userId");
  204. return (Criteria) this;
  205. }
  206. public Criteria andUserIdNotIn(List<Integer> values) {
  207. addCriterion( "USER_ID not in", values, "userId");
  208. return (Criteria) this;
  209. }
  210. public Criteria andUserIdBetween(Integer value1, Integer value2) {
  211. addCriterion( "USER_ID between", value1, value2, "userId");
  212. return (Criteria) this;
  213. }
  214. public Criteria andUserIdNotBetween(Integer value1, Integer value2) {
  215. addCriterion( "USER_ID not between", value1, value2, "userId");
  216. return (Criteria) this;
  217. }
  218. public Criteria andUserNameIsNull() {
  219. addCriterion( "USER_NAME is null");
  220. return (Criteria) this;
  221. }
  222. public Criteria andUserNameIsNotNull() {
  223. addCriterion( "USER_NAME is not null");
  224. return (Criteria) this;
  225. }
  226. public Criteria andUserNameEqualTo(String value) {
  227. addCriterion( "USER_NAME =", value, "userName");
  228. return (Criteria) this;
  229. }
  230. public Criteria andUserNameNotEqualTo(String value) {
  231. addCriterion( "USER_NAME <>", value, "userName");
  232. return (Criteria) this;
  233. }
  234. public Criteria andUserNameGreaterThan(String value) {
  235. addCriterion( "USER_NAME >", value, "userName");
  236. return (Criteria) this;
  237. }
  238. public Criteria andUserNameGreaterThanOrEqualTo(String value) {
  239. addCriterion( "USER_NAME >=", value, "userName");
  240. return (Criteria) this;
  241. }
  242. public Criteria andUserNameLessThan(String value) {
  243. addCriterion( "USER_NAME <", value, "userName");
  244. return (Criteria) this;
  245. }
  246. public Criteria andUserNameLessThanOrEqualTo(String value) {
  247. addCriterion( "USER_NAME <=", value, "userName");
  248. return (Criteria) this;
  249. }
  250. public Criteria andUserNameLike(String value) {
  251. addCriterion( "USER_NAME like", value, "userName");
  252. return (Criteria) this;
  253. }
  254. public Criteria andUserNameNotLike(String value) {
  255. addCriterion( "USER_NAME not like", value, "userName");
  256. return (Criteria) this;
  257. }
  258. public Criteria andUserNameIn(List<String> values) {
  259. addCriterion( "USER_NAME in", values, "userName");
  260. return (Criteria) this;
  261. }
  262. public Criteria andUserNameNotIn(List<String> values) {
  263. addCriterion( "USER_NAME not in", values, "userName");
  264. return (Criteria) this;
  265. }
  266. public Criteria andUserNameBetween(String value1, String value2) {
  267. addCriterion( "USER_NAME between", value1, value2, "userName");
  268. return (Criteria) this;
  269. }
  270. public Criteria andUserNameNotBetween(String value1, String value2) {
  271. addCriterion( "USER_NAME not between", value1, value2, "userName");
  272. return (Criteria) this;
  273. }
  274. public Criteria andUserPasswordIsNull() {
  275. addCriterion( "USER_PASSWORD is null");
  276. return (Criteria) this;
  277. }
  278. public Criteria andUserPasswordIsNotNull() {
  279. addCriterion( "USER_PASSWORD is not null");
  280. return (Criteria) this;
  281. }
  282. public Criteria andUserPasswordEqualTo(String value) {
  283. addCriterion( "USER_PASSWORD =", value, "userPassword");
  284. return (Criteria) this;
  285. }
  286. public Criteria andUserPasswordNotEqualTo(String value) {
  287. addCriterion( "USER_PASSWORD <>", value, "userPassword");
  288. return (Criteria) this;
  289. }
  290. public Criteria andUserPasswordGreaterThan(String value) {
  291. addCriterion( "USER_PASSWORD >", value, "userPassword");
  292. return (Criteria) this;
  293. }
  294. public Criteria andUserPasswordGreaterThanOrEqualTo(String value) {
  295. addCriterion( "USER_PASSWORD >=", value, "userPassword");
  296. return (Criteria) this;
  297. }
  298. public Criteria andUserPasswordLessThan(String value) {
  299. addCriterion( "USER_PASSWORD <", value, "userPassword");
  300. return (Criteria) this;
  301. }
  302. public Criteria andUserPasswordLessThanOrEqualTo(String value) {
  303. addCriterion( "USER_PASSWORD <=", value, "userPassword");
  304. return (Criteria) this;
  305. }
  306. public Criteria andUserPasswordLike(String value) {
  307. addCriterion( "USER_PASSWORD like", value, "userPassword");
  308. return (Criteria) this;
  309. }
  310. public Criteria andUserPasswordNotLike(String value) {
  311. addCriterion( "USER_PASSWORD not like", value, "userPassword");
  312. return (Criteria) this;
  313. }
  314. public Criteria andUserPasswordIn(List<String> values) {
  315. addCriterion( "USER_PASSWORD in", values, "userPassword");
  316. return (Criteria) this;
  317. }
  318. public Criteria andUserPasswordNotIn(List<String> values) {
  319. addCriterion( "USER_PASSWORD not in", values, "userPassword");
  320. return (Criteria) this;
  321. }
  322. public Criteria andUserPasswordBetween(String value1, String value2) {
  323. addCriterion( "USER_PASSWORD between", value1, value2, "userPassword");
  324. return (Criteria) this;
  325. }
  326. public Criteria andUserPasswordNotBetween(String value1, String value2) {
  327. addCriterion( "USER_PASSWORD not between", value1, value2, "userPassword");
  328. return (Criteria) this;
  329. }
  330. public Criteria andUserEmailIsNull() {
  331. addCriterion( "USER_EMAIL is null");
  332. return (Criteria) this;
  333. }
  334. public Criteria andUserEmailIsNotNull() {
  335. addCriterion( "USER_EMAIL is not null");
  336. return (Criteria) this;
  337. }
  338. public Criteria andUserEmailEqualTo(String value) {
  339. addCriterion( "USER_EMAIL =", value, "userEmail");
  340. return (Criteria) this;
  341. }
  342. public Criteria andUserEmailNotEqualTo(String value) {
  343. addCriterion( "USER_EMAIL <>", value, "userEmail");
  344. return (Criteria) this;
  345. }
  346. public Criteria andUserEmailGreaterThan(String value) {
  347. addCriterion( "USER_EMAIL >", value, "userEmail");
  348. return (Criteria) this;
  349. }
  350. public Criteria andUserEmailGreaterThanOrEqualTo(String value) {
  351. addCriterion( "USER_EMAIL >=", value, "userEmail");
  352. return (Criteria) this;
  353. }
  354. public Criteria andUserEmailLessThan(String value) {
  355. addCriterion( "USER_EMAIL <", value, "userEmail");
  356. return (Criteria) this;
  357. }
  358. public Criteria andUserEmailLessThanOrEqualTo(String value) {
  359. addCriterion( "USER_EMAIL <=", value, "userEmail");
  360. return (Criteria) this;
  361. }
  362. public Criteria andUserEmailLike(String value) {
  363. addCriterion( "USER_EMAIL like", value, "userEmail");
  364. return (Criteria) this;
  365. }
  366. public Criteria andUserEmailNotLike(String value) {
  367. addCriterion( "USER_EMAIL not like", value, "userEmail");
  368. return (Criteria) this;
  369. }
  370. public Criteria andUserEmailIn(List<String> values) {
  371. addCriterion( "USER_EMAIL in", values, "userEmail");
  372. return (Criteria) this;
  373. }
  374. public Criteria andUserEmailNotIn(List<String> values) {
  375. addCriterion( "USER_EMAIL not in", values, "userEmail");
  376. return (Criteria) this;
  377. }
  378. public Criteria andUserEmailBetween(String value1, String value2) {
  379. addCriterion( "USER_EMAIL between", value1, value2, "userEmail");
  380. return (Criteria) this;
  381. }
  382. public Criteria andUserEmailNotBetween(String value1, String value2) {
  383. addCriterion( "USER_EMAIL not between", value1, value2, "userEmail");
  384. return (Criteria) this;
  385. }
  386. }
  387. /**
  388. * This class was generated by MyBatis Generator.
  389. * This class corresponds to the database table t_user
  390. *
  391. * @mbggenerated do_not_delete_during_merge
  392. */
  393. public static class Criteria extends GeneratedCriteria {
  394. protected Criteria() {
  395. super();
  396. }
  397. }
  398. /**
  399. * This class was generated by MyBatis Generator.
  400. * This class corresponds to the database table t_user
  401. *
  402. * @mbggenerated
  403. */
  404. public static class Criterion {
  405. private String condition;
  406. private Object value;
  407. private Object secondValue;
  408. private boolean noValue;
  409. private boolean singleValue;
  410. private boolean betweenValue;
  411. private boolean listValue;
  412. public String getCondition() {
  413. return condition;
  414. }
  415. public Object getValue() {
  416. return value;
  417. }
  418. public Object getSecondValue() {
  419. return secondValue;
  420. }
  421. public boolean isNoValue() {
  422. return noValue;
  423. }
  424. public boolean isSingleValue() {
  425. return singleValue;
  426. }
  427. public boolean isBetweenValue() {
  428. return betweenValue;
  429. }
  430. public boolean isListValue() {
  431. return listValue;
  432. }
  433. protected Criterion(String condition) {
  434. super();
  435. this.condition = condition;
  436. this.noValue = true;
  437. }
  438. protected Criterion(String condition, Object value) {
  439. super();
  440. this.condition = condition;
  441. this.value = value;
  442. if (value instanceof List<?>) {
  443. this.listValue = true;
  444. } else {
  445. this.singleValue = true;
  446. }
  447. }
  448. protected Criterion(String condition, Object value, Object secondValue) {
  449. super();
  450. this.condition = condition;
  451. this.value = value;
  452. this.secondValue = secondValue;
  453. this.betweenValue = true;
  454. }
  455. }
  456. }
(2)dao层文件。它自动取名为UserMapper.java,可以自己手动写成UserDao.java


   
   
  1. package com.lin.dao;
  2. import com.lin.domain.User;
  3. import com.lin.domain.UserExample;
  4. import java.util.List;
  5. import org.apache.ibatis.annotations.Param;
  6. public interface UserMapper {
  7. /**
  8. * This method was generated by MyBatis Generator.
  9. * This method corresponds to the database table t_user
  10. *
  11. * @mbggenerated
  12. */
  13. int countByExample(UserExample example);
  14. /**
  15. * This method was generated by MyBatis Generator.
  16. * This method corresponds to the database table t_user
  17. *
  18. * @mbggenerated
  19. */
  20. int deleteByExample(UserExample example);
  21. /**
  22. * This method was generated by MyBatis Generator.
  23. * This method corresponds to the database table t_user
  24. *
  25. * @mbggenerated
  26. */
  27. int deleteByPrimaryKey(Integer userId);
  28. /**
  29. * This method was generated by MyBatis Generator.
  30. * This method corresponds to the database table t_user
  31. *
  32. * @mbggenerated
  33. */
  34. int insert(User record);
  35. /**
  36. * This method was generated by MyBatis Generator.
  37. * This method corresponds to the database table t_user
  38. *
  39. * @mbggenerated
  40. */
  41. int insertSelective(User record);
  42. /**
  43. * This method was generated by MyBatis Generator.
  44. * This method corresponds to the database table t_user
  45. *
  46. * @mbggenerated
  47. */
  48. List<User> selectByExample(UserExample example);
  49. /**
  50. * This method was generated by MyBatis Generator.
  51. * This method corresponds to the database table t_user
  52. *
  53. * @mbggenerated
  54. */
  55. User selectByPrimaryKey(Integer userId);
  56. /**
  57. * This method was generated by MyBatis Generator.
  58. * This method corresponds to the database table t_user
  59. *
  60. * @mbggenerated
  61. */
  62. int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);
  63. /**
  64. * This method was generated by MyBatis Generator.
  65. * This method corresponds to the database table t_user
  66. *
  67. * @mbggenerated
  68. */
  69. int updateByExample(@Param("record") User record, @Param("example") UserExample example);
  70. /**
  71. * This method was generated by MyBatis Generator.
  72. * This method corresponds to the database table t_user
  73. *
  74. * @mbggenerated
  75. */
  76. int updateByPrimaryKeySelective(User record);
  77. /**
  78. * This method was generated by MyBatis Generator.
  79. * This method corresponds to the database table t_user
  80. *
  81. * @mbggenerated
  82. */
  83. int updateByPrimaryKey(User record);
  84. }

(3)Mapper.xml文件:


   
   
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.lin.dao.UserMapper" >
  4. <resultMap id="BaseResultMap" type="com.lin.domain.User" >
  5. <!--
  6. WARNING - @mbggenerated
  7. This element is automatically generated by MyBatis Generator, do not modify.
  8. -->
  9. <id column="USER_ID" property="userId" jdbcType="INTEGER" />
  10. <result column="USER_NAME" property="userName" jdbcType="CHAR" />
  11. <result column="USER_PASSWORD" property="userPassword" jdbcType="CHAR" />
  12. <result column="USER_EMAIL" property="userEmail" jdbcType="CHAR" />
  13. </resultMap>
  14. <sql id="Example_Where_Clause" >
  15. <!--
  16. WARNING - @mbggenerated
  17. This element is automatically generated by MyBatis Generator, do not modify.
  18. -->
  19. <where >
  20. <foreach collection="oredCriteria" item="criteria" separator="or" >
  21. <if test="criteria.valid" >
  22. <trim prefix="(" suffix=")" prefixOverrides="and" >
  23. <foreach collection="criteria.criteria" item="criterion" >
  24. <choose >
  25. <when test="criterion.noValue" >
  26. and ${criterion.condition}
  27. </when>
  28. <when test="criterion.singleValue" >
  29. and ${criterion.condition} #{criterion.value}
  30. </when>
  31. <when test="criterion.betweenValue" >
  32. and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
  33. </when>
  34. <when test="criterion.listValue" >
  35. and ${criterion.condition}
  36. <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
  37. #{listItem}
  38. </foreach>
  39. </when>
  40. </choose>
  41. </foreach>
  42. </trim>
  43. </if>
  44. </foreach>
  45. </where>
  46. </sql>
  47. <sql id="Update_By_Example_Where_Clause" >
  48. <!--
  49. WARNING - @mbggenerated
  50. This element is automatically generated by MyBatis Generator, do not modify.
  51. -->
  52. <where >
  53. <foreach collection="example.oredCriteria" item="criteria" separator="or" >
  54. <if test="criteria.valid" >
  55. <trim prefix="(" suffix=")" prefixOverrides="and" >
  56. <foreach collection="criteria.criteria" item="criterion" >
  57. <choose >
  58. <when test="criterion.noValue" >
  59. and ${criterion.condition}
  60. </when>
  61. <when test="criterion.singleValue" >
  62. and ${criterion.condition} #{criterion.value}
  63. </when>
  64. <when test="criterion.betweenValue" >
  65. and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
  66. </when>
  67. <when test="criterion.listValue" >
  68. and ${criterion.condition}
  69. <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
  70. #{listItem}
  71. </foreach>
  72. </when>
  73. </choose>
  74. </foreach>
  75. </trim>
  76. </if>
  77. </foreach>
  78. </where>
  79. </sql>
  80. <sql id="Base_Column_List" >
  81. <!--
  82. WARNING - @mbggenerated
  83. This element is automatically generated by MyBatis Generator, do not modify.
  84. -->
  85. USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL
  86. </sql>
  87. <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.lin.domain.UserExample" >
  88. <!--
  89. WARNING - @mbggenerated
  90. This element is automatically generated by MyBatis Generator, do not modify.
  91. -->
  92. select
  93. <if test="distinct" >
  94. distinct
  95. </if>
  96. <include refid="Base_Column_List" />
  97. from t_user
  98. <if test="_parameter != null" >
  99. <include refid="Example_Where_Clause" />
  100. </if>
  101. <if test="orderByClause != null" >
  102. order by ${orderByClause}
  103. </if>
  104. </select>
  105. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
  106. <!--
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值