Mybatis逆向生成工具——MyBatis Genertor

来源:互联网 发布:无损音乐声谱软件 编辑:程序博客网 时间:2024/06/11 20:13
1.建一个Java工程 


2.在工程上新建一个generatorConfig.xml 文件 




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">


<generatorConfiguration>
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true" />
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>


<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="scott"
password="123456">
</jdbcConnection>


<!-- <jdbcConnection -->
<!-- driverClass="oracle.jdbc.OracleDriver" -->
<!-- connectionURL="jdbc:oracle:thin:@192.168.49.140:1521:DBDATA" -->
<!-- userId="ms_member" -->
<!-- password="ms1_member"> -->
<!-- </jdbcConnection> -->


<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 
和 NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>


<!-- targetProject:生成Model类的位置 -->
<javaModelGenerator targetPackage="com.pojo"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>


<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>


<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.mapper" targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>


<!-- 指定数据库表 ,生成对应表及类名,可以生成多个,复制下面这行代码改名 -->
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名 -->
<table tableName="SYSM_TD_USER" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>


</context>
</generatorConfiguration>


4.新建一个java类 GeneratorSqlmap.java 






5.新建一个lib文件,导入jar包 

数据库jdbc连接jar包

log4jar包

mybatis核心包

mybatis-generator-core包









6.在java类写入以下代码.注意,generatorConfig.xml文件就是我们上面新建的配置,名字 一样,要不然运行会找不到配置文件.




package org.lyj.myBatis;


import java.io.File;
import java.util.ArrayList;
import java.util.List;


import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;


public class GeneratorSqlmap {


public void generator() throws Exception {


List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
// 指定 逆向工程配置文件
File configFile = new File("src/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);


}


public static void main(String[] args) throws Exception {
try {
GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}


}
}










7.运行GeneratorSqlmap.java ,就会生成.刷新工程,就会看到.如果要再次生成,需要重新删除生成的代码.




UseMapper.java
package com.mapper;


import com.pojo.User;
import java.math.BigDecimal;


public interface UserMapper {
    int deleteByPrimaryKey(BigDecimal id);


    int insert(User record);


    int insertSelective(User record);


    User selectByPrimaryKey(BigDecimal id);


    int updateByPrimaryKeySelective(User record);


    int updateByPrimaryKey(User record);
}
UseMapper.xml




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.UserMapper">
  <resultMap id="BaseResultMap" type="com.pojo.User">
    <id column="ID" jdbcType="DECIMAL" property="id" />
    <result column="EMAIL" jdbcType="VARCHAR" property="email" />
    <result column="USERNAME" jdbcType="VARCHAR" property="username" />
    <result column="USERPWD" jdbcType="VARCHAR" property="userpwd" />
    <result column="GANDER" jdbcType="VARCHAR" property="gander" />
    <result column="MOBILE" jdbcType="VARCHAR" property="mobile" />
    <result column="SECURE" jdbcType="VARCHAR" property="secure" />
    <result column="ANSWER" jdbcType="VARCHAR" property="answer" />
    <result column="EMAIL2" jdbcType="VARCHAR" property="email2" />
    <result column="ADDRESS" jdbcType="VARCHAR" property="address" />
  </resultMap>
  <sql id="Base_Column_List">
    ID, EMAIL, USERNAME, USERPWD, GANDER, MOBILE, SECURE, ANSWER, EMAIL2, ADDRESS
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.math.BigDecimal" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from SYSM_TD_USER
    where ID = #{id,jdbcType=DECIMAL}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.math.BigDecimal">
    delete from SYSM_TD_USER
    where ID = #{id,jdbcType=DECIMAL}
  </delete>
  <insert id="insert" parameterType="com.pojo.User">
    insert into SYSM_TD_USER (ID, EMAIL, USERNAME, 
      USERPWD, GANDER, MOBILE, 
      SECURE, ANSWER, EMAIL2, 
      ADDRESS)
    values (#{id,jdbcType=DECIMAL}, #{email,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, 
      #{userpwd,jdbcType=VARCHAR}, #{gander,jdbcType=VARCHAR}, #{mobile,jdbcType=VARCHAR}, 
      #{secure,jdbcType=VARCHAR}, #{answer,jdbcType=VARCHAR}, #{email2,jdbcType=VARCHAR}, 
      #{address,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.pojo.User">
    insert into SYSM_TD_USER
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        ID,
      </if>
      <if test="email != null">
        EMAIL,
      </if>
      <if test="username != null">
        USERNAME,
      </if>
      <if test="userpwd != null">
        USERPWD,
      </if>
      <if test="gander != null">
        GANDER,
      </if>
      <if test="mobile != null">
        MOBILE,
      </if>
      <if test="secure != null">
        SECURE,
      </if>
      <if test="answer != null">
        ANSWER,
      </if>
      <if test="email2 != null">
        EMAIL2,
      </if>
      <if test="address != null">
        ADDRESS,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=DECIMAL},
      </if>
      <if test="email != null">
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="username != null">
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="userpwd != null">
        #{userpwd,jdbcType=VARCHAR},
      </if>
      <if test="gander != null">
        #{gander,jdbcType=VARCHAR},
      </if>
      <if test="mobile != null">
        #{mobile,jdbcType=VARCHAR},
      </if>
      <if test="secure != null">
        #{secure,jdbcType=VARCHAR},
      </if>
      <if test="answer != null">
        #{answer,jdbcType=VARCHAR},
      </if>
      <if test="email2 != null">
        #{email2,jdbcType=VARCHAR},
      </if>
      <if test="address != null">
        #{address,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.pojo.User">
    update SYSM_TD_USER
    <set>
      <if test="email != null">
        EMAIL = #{email,jdbcType=VARCHAR},
      </if>
      <if test="username != null">
        USERNAME = #{username,jdbcType=VARCHAR},
      </if>
      <if test="userpwd != null">
        USERPWD = #{userpwd,jdbcType=VARCHAR},
      </if>
      <if test="gander != null">
        GANDER = #{gander,jdbcType=VARCHAR},
      </if>
      <if test="mobile != null">
        MOBILE = #{mobile,jdbcType=VARCHAR},
      </if>
      <if test="secure != null">
        SECURE = #{secure,jdbcType=VARCHAR},
      </if>
      <if test="answer != null">
        ANSWER = #{answer,jdbcType=VARCHAR},
      </if>
      <if test="email2 != null">
        EMAIL2 = #{email2,jdbcType=VARCHAR},
      </if>
      <if test="address != null">
        ADDRESS = #{address,jdbcType=VARCHAR},
      </if>
    </set>
    where ID = #{id,jdbcType=DECIMAL}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.pojo.User">
    update SYSM_TD_USER
    set EMAIL = #{email,jdbcType=VARCHAR},
      USERNAME = #{username,jdbcType=VARCHAR},
      USERPWD = #{userpwd,jdbcType=VARCHAR},
      GANDER = #{gander,jdbcType=VARCHAR},
      MOBILE = #{mobile,jdbcType=VARCHAR},
      SECURE = #{secure,jdbcType=VARCHAR},
      ANSWER = #{answer,jdbcType=VARCHAR},
      EMAIL2 = #{email2,jdbcType=VARCHAR},
      ADDRESS = #{address,jdbcType=VARCHAR}
    where ID = #{id,jdbcType=DECIMAL}
  </update>
  <resultMap id="BaseResultMap" type="com.pojo.User">
    <id column="ID" jdbcType="DECIMAL" property="id" />
    <result column="EMAIL" jdbcType="VARCHAR" property="email" />
    <result column="USERNAME" jdbcType="VARCHAR" property="username" />
    <result column="USERPWD" jdbcType="VARCHAR" property="userpwd" />
    <result column="GANDER" jdbcType="VARCHAR" property="gander" />
    <result column="MOBILE" jdbcType="VARCHAR" property="mobile" />
    <result column="SECURE" jdbcType="VARCHAR" property="secure" />
    <result column="ANSWER" jdbcType="VARCHAR" property="answer" />
    <result column="EMAIL2" jdbcType="VARCHAR" property="email2" />
    <result column="ADDRESS" jdbcType="VARCHAR" property="address" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    ID, EMAIL, USERNAME, USERPWD, GANDER, MOBILE, SECURE, ANSWER, EMAIL2, ADDRESS
  </sql>
  <select id="selectByExample" parameterType="com.pojo.UserExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from SYSM_TD_USER
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.math.BigDecimal" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from SYSM_TD_USER
    where ID = #{id,jdbcType=DECIMAL}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.math.BigDecimal">
    delete from SYSM_TD_USER
    where ID = #{id,jdbcType=DECIMAL}
  </delete>
  <delete id="deleteByExample" parameterType="com.pojo.UserExample">
    delete from SYSM_TD_USER
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.pojo.User">
    insert into SYSM_TD_USER (ID, EMAIL, USERNAME, 
      USERPWD, GANDER, MOBILE, 
      SECURE, ANSWER, EMAIL2, 
      ADDRESS)
    values (#{id,jdbcType=DECIMAL}, #{email,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, 
      #{userpwd,jdbcType=VARCHAR}, #{gander,jdbcType=VARCHAR}, #{mobile,jdbcType=VARCHAR}, 
      #{secure,jdbcType=VARCHAR}, #{answer,jdbcType=VARCHAR}, #{email2,jdbcType=VARCHAR}, 
      #{address,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.pojo.User">
    insert into SYSM_TD_USER
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        ID,
      </if>
      <if test="email != null">
        EMAIL,
      </if>
      <if test="username != null">
        USERNAME,
      </if>
      <if test="userpwd != null">
        USERPWD,
      </if>
      <if test="gander != null">
        GANDER,
      </if>
      <if test="mobile != null">
        MOBILE,
      </if>
      <if test="secure != null">
        SECURE,
      </if>
      <if test="answer != null">
        ANSWER,
      </if>
      <if test="email2 != null">
        EMAIL2,
      </if>
      <if test="address != null">
        ADDRESS,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=DECIMAL},
      </if>
      <if test="email != null">
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="username != null">
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="userpwd != null">
        #{userpwd,jdbcType=VARCHAR},
      </if>
      <if test="gander != null">
        #{gander,jdbcType=VARCHAR},
      </if>
      <if test="mobile != null">
        #{mobile,jdbcType=VARCHAR},
      </if>
      <if test="secure != null">
        #{secure,jdbcType=VARCHAR},
      </if>
      <if test="answer != null">
        #{answer,jdbcType=VARCHAR},
      </if>
      <if test="email2 != null">
        #{email2,jdbcType=VARCHAR},
      </if>
      <if test="address != null">
        #{address,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.pojo.UserExample" resultType="java.lang.Integer">
    select count(*) from SYSM_TD_USER
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update SYSM_TD_USER
    <set>
      <if test="record.id != null">
        ID = #{record.id,jdbcType=DECIMAL},
      </if>
      <if test="record.email != null">
        EMAIL = #{record.email,jdbcType=VARCHAR},
      </if>
      <if test="record.username != null">
        USERNAME = #{record.username,jdbcType=VARCHAR},
      </if>
      <if test="record.userpwd != null">
        USERPWD = #{record.userpwd,jdbcType=VARCHAR},
      </if>
      <if test="record.gander != null">
        GANDER = #{record.gander,jdbcType=VARCHAR},
      </if>
      <if test="record.mobile != null">
        MOBILE = #{record.mobile,jdbcType=VARCHAR},
      </if>
      <if test="record.secure != null">
        SECURE = #{record.secure,jdbcType=VARCHAR},
      </if>
      <if test="record.answer != null">
        ANSWER = #{record.answer,jdbcType=VARCHAR},
      </if>
      <if test="record.email2 != null">
        EMAIL2 = #{record.email2,jdbcType=VARCHAR},
      </if>
      <if test="record.address != null">
        ADDRESS = #{record.address,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update SYSM_TD_USER
    set ID = #{record.id,jdbcType=DECIMAL},
      EMAIL = #{record.email,jdbcType=VARCHAR},
      USERNAME = #{record.username,jdbcType=VARCHAR},
      USERPWD = #{record.userpwd,jdbcType=VARCHAR},
      GANDER = #{record.gander,jdbcType=VARCHAR},
      MOBILE = #{record.mobile,jdbcType=VARCHAR},
      SECURE = #{record.secure,jdbcType=VARCHAR},
      ANSWER = #{record.answer,jdbcType=VARCHAR},
      EMAIL2 = #{record.email2,jdbcType=VARCHAR},
      ADDRESS = #{record.address,jdbcType=VARCHAR}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.pojo.User">
    update SYSM_TD_USER
    <set>
      <if test="email != null">
        EMAIL = #{email,jdbcType=VARCHAR},
      </if>
      <if test="username != null">
        USERNAME = #{username,jdbcType=VARCHAR},
      </if>
      <if test="userpwd != null">
        USERPWD = #{userpwd,jdbcType=VARCHAR},
      </if>
      <if test="gander != null">
        GANDER = #{gander,jdbcType=VARCHAR},
      </if>
      <if test="mobile != null">
        MOBILE = #{mobile,jdbcType=VARCHAR},
      </if>
      <if test="secure != null">
        SECURE = #{secure,jdbcType=VARCHAR},
      </if>
      <if test="answer != null">
        ANSWER = #{answer,jdbcType=VARCHAR},
      </if>
      <if test="email2 != null">
        EMAIL2 = #{email2,jdbcType=VARCHAR},
      </if>
      <if test="address != null">
        ADDRESS = #{address,jdbcType=VARCHAR},
      </if>
    </set>
    where ID = #{id,jdbcType=DECIMAL}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.pojo.User">
    update SYSM_TD_USER
    set EMAIL = #{email,jdbcType=VARCHAR},
      USERNAME = #{username,jdbcType=VARCHAR},
      USERPWD = #{userpwd,jdbcType=VARCHAR},
      GANDER = #{gander,jdbcType=VARCHAR},
      MOBILE = #{mobile,jdbcType=VARCHAR},
      SECURE = #{secure,jdbcType=VARCHAR},
      ANSWER = #{answer,jdbcType=VARCHAR},
      EMAIL2 = #{email2,jdbcType=VARCHAR},
      ADDRESS = #{address,jdbcType=VARCHAR}
    where ID = #{id,jdbcType=DECIMAL}
  </update>
</mapper>




Pojo类   User实体类


package com.pojo;


import java.math.BigDecimal;


public class User {
    private BigDecimal id;


    private String email;


    private String username;


    private String userpwd;


    private String gander;


    private String mobile;


    private String secure;


    private String answer;


    private String email2;


    private String address;


    public BigDecimal getId() {
        return id;
    }


    public void setId(BigDecimal id) {
        this.id = id;
    }


    public String getEmail() {
        return email;
    }


    public void setEmail(String email) {
        this.email = email == null ? null : email.trim();
    }


    public String getUsername() {
        return username;
    }


    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }


    public String getUserpwd() {
        return userpwd;
    }


    public void setUserpwd(String userpwd) {
        this.userpwd = userpwd == null ? null : userpwd.trim();
    }


    public String getGander() {
        return gander;
    }


    public void setGander(String gander) {
        this.gander = gander == null ? null : gander.trim();
    }


    public String getMobile() {
        return mobile;
    }


    public void setMobile(String mobile) {
        this.mobile = mobile == null ? null : mobile.trim();
    }


    public String getSecure() {
        return secure;
    }


    public void setSecure(String secure) {
        this.secure = secure == null ? null : secure.trim();
    }


    public String getAnswer() {
        return answer;
    }


    public void setAnswer(String answer) {
        this.answer = answer == null ? null : answer.trim();
    }


    public String getEmail2() {
        return email2;
    }


    public void setEmail2(String email2) {
        this.email2 = email2 == null ? null : email2.trim();
    }


    public String getAddress() {
        return address;
    }


    public void setAddress(String address) {
        this.address = address == null ? null : address.trim();
    }
}











1 0
原创粉丝点击