分享

Mybatis自动生成代码详细步骤

 指尖幻城 2017-05-20

         林炳文Evankaka原创作品。转载请注明出处http://blog.csdn.net/evankaka

       摘要:本文将简要介绍怎样利用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 "-////DTD MyBatis Generator Configuration 1.0//EN"    
  4.   "http:///dtd/mybatis-generator-config_1_0.dtd">  
  5.   
  6. <generatorConfiguration>  
  7.     <!-- classPathEntry:数据库的JDBC驱动的jar包地址 -->  
  8.     <classPathEntry location="D:\Java\Jar\mysql-connector-java-5.1.22\mysql-connector-java-5.1.22-bin.jar" />  
  9.     <context id="DB2Tables" targetRuntime="MyBatis3">  
  10.         <commentGenerator>  
  11.         <!-- 抑制警告 -->  
  12.         <property name="suppressTypeWarnings" value="true" />  
  13.         <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
  14.         <property name="suppressAllComments" value="false" />  
  15.         <!-- 是否生成注释代时间戳-->    
  16.         <property name="suppressDate" value="true" />   
  17.         </commentGenerator>  
  18.   
  19.         <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->  
  20.         <jdbcConnection driverClass="com.mysql.jdbc.Driver"  
  21.             connectionURL="jdbc:mysql://localhost/learning" userId="root"  
  22.             password="christmas258@">  
  23.         </jdbcConnection>  
  24.         <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和   
  25.             NUMERIC 类型解析为java.math.BigDecimal -->  
  26.     <!--     <javaTypeResolver>  
  27.             <property name="forceBigDecimals" value="false" />  
  28.         </javaTypeResolver> -->  
  29.         <!--生成Model类存放位置 -->  
  30.         <javaModelGenerator targetPackage="com.lin.domain"  
  31.             targetProject="D:\lunaJee-workspace\MyBatisLearningChapter7\src">  
  32.              <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->    
  33.             <property name="enableSubPackages" value="false" />  
  34.              <!-- 是否针对string类型的字段在set的时候进行trim调用 -->    
  35.             <property name="trimStrings" value="true" />  
  36.         </javaModelGenerator>  
  37.         <!--生成映射文件存放位置 -->  
  38.         <sqlMapGenerator targetPackage="com.lin.mapper"  
  39.             targetProject="D:\lunaJee-workspace\MyBatisLearningChapter7\src">  
  40.             <property name="enableSubPackages" value="true" />  
  41.         </sqlMapGenerator>  
  42.         <!--生成Dao类存放位置 -->  
  43.         <javaClientGenerator type="XMLMAPPER"  
  44.             targetPackage="com.lin.dao" targetProject="D:\lunaJee-workspace\MyBatisLearningChapter7\src">  
  45.             <property name="enableSubPackages" value="true" />  
  46.         </javaClientGenerator>  
  47.           
  48.   
  49.         <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->    
  50.         <table schema="general" tableName="T_USER" domainObjectName="User">  
  51.          <!--domain字段的命名规则,false:默认为驼峰命名 true:按数据库真实命名  -->  
  52.         <property name="useActualColumnNames" value="false"/>  
  53.         <!-- 忽略列,不生成bean 字段 -->    
  54.         <!--     <ignoreColumn column="FRED" />   -->  
  55.         <!-- 指定列的java数据类型 -->    
  56.         <!--     <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />   -->  
  57.         </table>  
  58.     </context>  
  59. </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.   
  3. public class User {  
  4.     /** 
  5.      * This field was generated by MyBatis Generator. 
  6.      * This field corresponds to the database column t_user.USER_ID 
  7.      * 
  8.      * @mbggenerated 
  9.      */  
  10.     private Integer userId;  
  11.   
  12.     /** 
  13.      * This field was generated by MyBatis Generator. 
  14.      * This field corresponds to the database column t_user.USER_NAME 
  15.      * 
  16.      * @mbggenerated 
  17.      */  
  18.     private String userName;  
  19.   
  20.     /** 
  21.      * This field was generated by MyBatis Generator. 
  22.      * This field corresponds to the database column t_user.USER_PASSWORD 
  23.      * 
  24.      * @mbggenerated 
  25.      */  
  26.     private String userPassword;  
  27.   
  28.     /** 
  29.      * This field was generated by MyBatis Generator. 
  30.      * This field corresponds to the database column t_user.USER_EMAIL 
  31.      * 
  32.      * @mbggenerated 
  33.      */  
  34.     private String userEmail;  
  35.   
  36.     /** 
  37.      * This method was generated by MyBatis Generator. 
  38.      * This method returns the value of the database column t_user.USER_ID 
  39.      * 
  40.      * @return the value of t_user.USER_ID 
  41.      * 
  42.      * @mbggenerated 
  43.      */  
  44.     public Integer getUserId() {  
  45.         return userId;  
  46.     }  
  47.   
  48.     /** 
  49.      * This method was generated by MyBatis Generator. 
  50.      * This method sets the value of the database column t_user.USER_ID 
  51.      * 
  52.      * @param userId the value for t_user.USER_ID 
  53.      * 
  54.      * @mbggenerated 
  55.      */  
  56.     public void setUserId(Integer userId) {  
  57.         this.userId = userId;  
  58.     }  
  59.   
  60.     /** 
  61.      * This method was generated by MyBatis Generator. 
  62.      * This method returns the value of the database column t_user.USER_NAME 
  63.      * 
  64.      * @return the value of t_user.USER_NAME 
  65.      * 
  66.      * @mbggenerated 
  67.      */  
  68.     public String getUserName() {  
  69.         return userName;  
  70.     }  
  71.   
  72.     /** 
  73.      * This method was generated by MyBatis Generator. 
  74.      * This method sets the value of the database column t_user.USER_NAME 
  75.      * 
  76.      * @param userName the value for t_user.USER_NAME 
  77.      * 
  78.      * @mbggenerated 
  79.      */  
  80.     public void setUserName(String userName) {  
  81.         this.userName = userName == null ? null : userName.trim();  
  82.     }  
  83.   
  84.     /** 
  85.      * This method was generated by MyBatis Generator. 
  86.      * This method returns the value of the database column t_user.USER_PASSWORD 
  87.      * 
  88.      * @return the value of t_user.USER_PASSWORD 
  89.      * 
  90.      * @mbggenerated 
  91.      */  
  92.     public String getUserPassword() {  
  93.         return userPassword;  
  94.     }  
  95.   
  96.     /** 
  97.      * This method was generated by MyBatis Generator. 
  98.      * This method sets the value of the database column t_user.USER_PASSWORD 
  99.      * 
  100.      * @param userPassword the value for t_user.USER_PASSWORD 
  101.      * 
  102.      * @mbggenerated 
  103.      */  
  104.     public void setUserPassword(String userPassword) {  
  105.         this.userPassword = userPassword == null ? null : userPassword.trim();  
  106.     }  
  107.   
  108.     /** 
  109.      * This method was generated by MyBatis Generator. 
  110.      * This method returns the value of the database column t_user.USER_EMAIL 
  111.      * 
  112.      * @return the value of t_user.USER_EMAIL 
  113.      * 
  114.      * @mbggenerated 
  115.      */  
  116.     public String getUserEmail() {  
  117.         return userEmail;  
  118.     }  
  119.   
  120.     /** 
  121.      * This method was generated by MyBatis Generator. 
  122.      * This method sets the value of the database column t_user.USER_EMAIL 
  123.      * 
  124.      * @param userEmail the value for t_user.USER_EMAIL 
  125.      * 
  126.      * @mbggenerated 
  127.      */  
  128.     public void setUserEmail(String userEmail) {  
  129.         this.userEmail = userEmail == null ? null : userEmail.trim();  
  130.     }  
  131. }  

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

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

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

(3)Mapper.xml文件:

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "http:///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.     <!--  
  107.       WARNING - @mbggenerated  
  108.       This element is automatically generated by MyBatis Generator, do not modify.  
  109.     -->  
  110.     select   
  111.     <include refid="Base_Column_List" />  
  112.     from t_user  
  113.     where USER_ID = #{userId,jdbcType=INTEGER}  
  114.   </select>  
  115.   <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >  
  116.     <!--  
  117.       WARNING - @mbggenerated  
  118.       This element is automatically generated by MyBatis Generator, do not modify.  
  119.     -->  
  120.     delete from t_user  
  121.     where USER_ID = #{userId,jdbcType=INTEGER}  
  122.   </delete>  
  123.   <delete id="deleteByExample" parameterType="com.lin.domain.UserExample" >  
  124.     <!--  
  125.       WARNING - @mbggenerated  
  126.       This element is automatically generated by MyBatis Generator, do not modify.  
  127.     -->  
  128.     delete from t_user  
  129.     <if test="_parameter != null" >  
  130.       <include refid="Example_Where_Clause" />  
  131.     </if>  
  132.   </delete>  
  133.   <insert id="insert" parameterType="com.lin.domain.User" >  
  134.     <!--  
  135.       WARNING - @mbggenerated  
  136.       This element is automatically generated by MyBatis Generator, do not modify.  
  137.     -->  
  138.     insert into t_user (USER_ID, USER_NAME, USER_PASSWORD,   
  139.       USER_EMAIL)  
  140.     values (#{userId,jdbcType=INTEGER}, #{userName,jdbcType=CHAR}, #{userPassword,jdbcType=CHAR},   
  141.       #{userEmail,jdbcType=CHAR})  
  142.   </insert>  
  143.   <insert id="insertSelective" parameterType="com.lin.domain.User" >  
  144.     <!--  
  145.       WARNING - @mbggenerated  
  146.       This element is automatically generated by MyBatis Generator, do not modify.  
  147.     -->  
  148.     insert into t_user  
  149.     <trim prefix="(" suffix=")" suffixOverrides="," >  
  150.       <if test="userId != null" >  
  151.         USER_ID,  
  152.       </if>  
  153.       <if test="userName != null" >  
  154.         USER_NAME,  
  155.       </if>  
  156.       <if test="userPassword != null" >  
  157.         USER_PASSWORD,  
  158.       </if>  
  159.       <if test="userEmail != null" >  
  160.         USER_EMAIL,  
  161.       </if>  
  162.     </trim>  
  163.     <trim prefix="values (" suffix=")" suffixOverrides="," >  
  164.       <if test="userId != null" >  
  165.         #{userId,jdbcType=INTEGER},  
  166.       </if>  
  167.       <if test="userName != null" >  
  168.         #{userName,jdbcType=CHAR},  
  169.       </if>  
  170.       <if test="userPassword != null" >  
  171.         #{userPassword,jdbcType=CHAR},  
  172.       </if>  
  173.       <if test="userEmail != null" >  
  174.         #{userEmail,jdbcType=CHAR},  
  175.       </if>  
  176.     </trim>  
  177.   </insert>  
  178.   <select id="countByExample" parameterType="com.lin.domain.UserExample" resultType="java.lang.Integer" >  
  179.     <!--  
  180.       WARNING - @mbggenerated  
  181.       This element is automatically generated by MyBatis Generator, do not modify.  
  182.     -->  
  183.     select count(*) from t_user  
  184.     <if test="_parameter != null" >  
  185.       <include refid="Example_Where_Clause" />  
  186.     </if>  
  187.   </select>  
  188.   <update id="updateByExampleSelective" parameterType="map" >  
  189.     <!--  
  190.       WARNING - @mbggenerated  
  191.       This element is automatically generated by MyBatis Generator, do not modify.  
  192.     -->  
  193.     update t_user  
  194.     <set >  
  195.       <if test="record.userId != null" >  
  196.         USER_ID = #{record.userId,jdbcType=INTEGER},  
  197.       </if>  
  198.       <if test="record.userName != null" >  
  199.         USER_NAME = #{record.userName,jdbcType=CHAR},  
  200.       </if>  
  201.       <if test="record.userPassword != null" >  
  202.         USER_PASSWORD = #{record.userPassword,jdbcType=CHAR},  
  203.       </if>  
  204.       <if test="record.userEmail != null" >  
  205.         USER_EMAIL = #{record.userEmail,jdbcType=CHAR},  
  206.       </if>  
  207.     </set>  
  208.     <if test="_parameter != null" >  
  209.       <include refid="Update_By_Example_Where_Clause" />  
  210.     </if>  
  211.   </update>  
  212.   <update id="updateByExample" parameterType="map" >  
  213.     <!--  
  214.       WARNING - @mbggenerated  
  215.       This element is automatically generated by MyBatis Generator, do not modify.  
  216.     -->  
  217.     update t_user  
  218.     set USER_ID = #{record.userId,jdbcType=INTEGER},  
  219.       USER_NAME = #{record.userName,jdbcType=CHAR},  
  220.       USER_PASSWORD = #{record.userPassword,jdbcType=CHAR},  
  221.       USER_EMAIL = #{record.userEmail,jdbcType=CHAR}  
  222.     <if test="_parameter != null" >  
  223.       <include refid="Update_By_Example_Where_Clause" />  
  224.     </if>  
  225.   </update>  
  226.   <update id="updateByPrimaryKeySelective" parameterType="com.lin.domain.User" >  
  227.     <!--  
  228.       WARNING - @mbggenerated  
  229.       This element is automatically generated by MyBatis Generator, do not modify.  
  230.     -->  
  231.     update t_user  
  232.     <set >  
  233.       <if test="userName != null" >  
  234.         USER_NAME = #{userName,jdbcType=CHAR},  
  235.       </if>  
  236.       <if test="userPassword != null" >  
  237.         USER_PASSWORD = #{userPassword,jdbcType=CHAR},  
  238.       </if>  
  239.       <if test="userEmail != null" >  
  240.         USER_EMAIL = #{userEmail,jdbcType=CHAR},  
  241.       </if>  
  242.     </set>  
  243.     where USER_ID = #{userId,jdbcType=INTEGER}  
  244.   </update>  
  245.   <update id="updateByPrimaryKey" parameterType="com.lin.domain.User" >  
  246.     <!--  
  247.       WARNING - @mbggenerated  
  248.       This element is automatically generated by MyBatis Generator, do not modify.  
  249.     -->  
  250.     update t_user  
  251.     set USER_NAME = #{userName,jdbcType=CHAR},  
  252.       USER_PASSWORD = #{userPassword,jdbcType=CHAR},  
  253.       USER_EMAIL = #{userEmail,jdbcType=CHAR}  
  254.     where USER_ID = #{userId,jdbcType=INTEGER}  
  255.   </update>  
  256. </mapper>  

这样就好了,serivce层的文件自己再去写写就好了。

如果不想要UserExample文件怎么办呢?

那就把

  1. <table schema="general" tableName="T_USER" domainObjectName="User">  
  2.  <!--domain字段的命名规则,false:默认为驼峰命名 true:按数据库真实命名  -->  
  3. <property name="useActualColumnNames" value="false"/>  
  4. </table>  
换成:

  1. <table schema="general" tableName="T_USER" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false"  
  2.        enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">  
  3.  <!--domain字段的命名规则,false:默认为驼峰命名 true:按数据库真实命名  -->  
  4. <property name="useActualColumnNames" value="false"/>  
  5. </table>  
这样就可以了
2、java代码读取xml文件生成

首先要导入如下的包:

然后在工程里写一个文件如下:

  1. package Test;  
  2. import java.io.File;  
  3. import java.io.IOException;  
  4. import java.sql.SQLException;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.   
  8. import org.mybatis.generator.api.MyBatisGenerator;  
  9. import org.mybatis.generator.config.Configuration;  
  10. import org.mybatis.generator.config.xml.ConfigurationParser;  
  11. import org.mybatis.generator.exception.InvalidConfigurationException;  
  12. import org.mybatis.generator.exception.XMLParserException;  
  13. import org.mybatis.generator.internal.DefaultShellCallback;  
  14. public class BuildFile {  
  15.     public static void main(String[] args) throws InvalidConfigurationException, IOException, XMLParserException, SQLException, InterruptedException {  
  16.         List<String> warnings = new ArrayList<String>();    
  17.           boolean overwrite = true;    
  18.           File configFile = new File("D:\\lunaJee-workspace\\MyBatisLearningChapter7\\src\\generator.xml");  //输入绝对路径  
  19.           ConfigurationParser cp = new ConfigurationParser(warnings);    
  20.           Configuration config=null;  
  21.           config = cp.parseConfiguration(configFile);  
  22.           DefaultShellCallback callback = new DefaultShellCallback(overwrite);    
  23.           MyBatisGenerator myBatisGenerator = null;  
  24.           myBatisGenerator = new MyBatisGenerator(config, callback, warnings);  
  25.           myBatisGenerator.generate(null);  
  26.     }  
  27. }  

然后运行,再刷新下就可以了


这里说一下,generator.xml文件也是可以随便放的,我这里为了方便,把它和整个工程放在一起了

本文工程免费下载

3、eclipse插入mybatis generator生成

待续~~

4、maven生成

待续~~

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多