文档章节

初次使用mybatis Generator

大海
 大海
发布于 2016/07/10 17:14
字数 2136
阅读 392
收藏 0

1.新建一个maven webapp项目,在pom.xml文件内新增几个maven坐标

    //servlet的依赖一定要写,否则maven-web项目会报错
     <dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>javax.servlet-api</artifactId>
		<version>3.1.0</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
	//接下来是mybatis.generator的依赖,过会需要用命令执行这个jar
    <dependency>
	    <groupId>org.mybatis.generator</groupId>
	    <artifactId>mybatis-generator-core</artifactId>
	    <version>1.3.3</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    //mysql驱动
	<dependency>
	    <groupId>mysql</groupId>
	    <artifactId>mysql-connector-java</artifactId>
	    <version>5.1.38</version>
	</dependency>

2.新建一个mybatis-generator的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>
    //mysql驱动的文件的位置,一般来说在lib文件夹内
	<classPathEntry location="mysql-connector-java-5.1.38.jar"/>
	//targetRuntime="MyBatis3",这个貌似是固定写法
	<context id="example_table" targetRuntime="MyBatis3">
        //抑制注释和注释的时间为false,就是允许自动生成注释,并添加时间
		<commentGenerator>
			<property name="suppressDate" value="false"/>
			<property name="suppressAllComments" value="false"/>
		</commentGenerator>
        //jdbc数据库地址,用户名和密码
		<jdbcConnection driverClass="com.mysql.jdbc.Driver" 
			connectionURL="jdbc:mysql://localhost:3306/openfire" 
			userId="root" 
			password="root"> 
		</jdbcConnection>
        //这个是参考网上的写法,从英文名看是强制使用bigdecimal,可能是对生成的javabean里的float double字段进行强制转bigdecimal
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false"/>
		</javaTypeResolver>
		//java bean生成所在包,目标项目路径,这个好理解
        <javaModelGenerator targetPackage="com.wyk.model" targetProject="D:\myeclipse2015-workspace\mybatisGenerater\src\main\java">
			//子包是否可用
            <property name="enableSubPackages" value="true"/>  
            //get方法内是否使用trim方法,这个是我在查看生成的javabean里看到的,所有get方法都带有trim
            <property name="trimStrings" value="true"/>
		</javaModelGenerator>
        //sqlxml文件的生成路径
		<sqlMapGenerator targetPackage="com.wyk.mapping" targetProject="D:\myeclipse2015-workspace\mybatisGenerater\src\main\java">  
            <property name="enableSubPackages" value="true"/>  
        </sqlMapGenerator>
        //生成的接口所在包路径
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.wyk.dao" targetProject="D:\myeclipse2015-workspace\mybatisGenerater\src\main\java">  
            <property name="enableSubPackages" value="true"/>  
        </javaClientGenerator>  
        
        //最后就是配置表信息,表名,表对应的实体类的名称
        <!-- 要生成哪些表-->  
        <table tableName="ofextcomponentconf" 
	        domainObjectName="ofextcomponentconf" 
	        enableCountByExample="false" 
	        enableUpdateByExample="false" 
	        enableDeleteByExample="false" 
	        enableSelectByExample="false" 
	        selectByExampleQueryId="false"></table>  
	        
	        
	     <table tableName="ofgroup" 
	        domainObjectName="ofgroup" 
	        enableCountByExample="false" 
	        enableUpdateByExample="false" 
	        enableDeleteByExample="false" 
	        enableSelectByExample="false" 
	        selectByExampleQueryId="false"></table>
	        
	      <table tableName="ofextcomponentconf" 
	        domainObjectName="ofextcomponentconf" 
	        enableCountByExample="false" 
	        enableUpdateByExample="false" 
	        enableDeleteByExample="false" 
	        enableSelectByExample="false" 
	        selectByExampleQueryId="false"></table>
	        
	        <table tableName="ofgroupprop" 
	        domainObjectName="ofgroupprop" 
	        enableCountByExample="false" 
	        enableUpdateByExample="false" 
	        enableDeleteByExample="false" 
	        enableSelectByExample="false" 
	        selectByExampleQueryId="false"></table>
	        
	        
	        <table tableName="ofgroupuser" 
	        domainObjectName="ofgroupuser" 
	        enableCountByExample="false" 
	        enableUpdateByExample="false" 
	        enableDeleteByExample="false" 
	        enableSelectByExample="false" 
	        selectByExampleQueryId="false"></table>
	        
	        
	        <table tableName="ofextcomponentconf" 
	        domainObjectName="ofextcomponentconf" 
	        enableCountByExample="false" 
	        enableUpdateByExample="false" 
	        enableDeleteByExample="false" 
	        enableSelectByExample="false" 
	        selectByExampleQueryId="false"></table>
	        
	        
	        <table tableName="ofid" 
	        domainObjectName="ofid" 
	        enableCountByExample="false" 
	        enableUpdateByExample="false" 
	        enableDeleteByExample="false" 
	        enableSelectByExample="false" 
	        selectByExampleQueryId="false"></table>
	        
	        
	        <table tableName="ofmucaffiliation" 
	        domainObjectName="ofmucaffiliation" 
	        enableCountByExample="false" 
	        enableUpdateByExample="false" 
	        enableDeleteByExample="false" 
	        enableSelectByExample="false" 
	        selectByExampleQueryId="false"></table>
	        
	        
	        
	        
	        <table tableName="ofmucconversationlog" 
	        domainObjectName="ofmucconversationlog" 
	        enableCountByExample="false" 
	        enableUpdateByExample="false" 
	        enableDeleteByExample="false" 
	        enableSelectByExample="false" 
	        selectByExampleQueryId="false"></table>
	        
	        
	        <table tableName="ofmucmember" 
	        domainObjectName="ofmucmember" 
	        enableCountByExample="false" 
	        enableUpdateByExample="false" 
	        enableDeleteByExample="false" 
	        enableSelectByExample="false" 
	        selectByExampleQueryId="false"></table>
	        
	        <table tableName="ofmucroom" 
	        domainObjectName="ofmucroom" 
	        enableCountByExample="false" 
	        enableUpdateByExample="false" 
	        enableDeleteByExample="false" 
	        enableSelectByExample="false" 
	        selectByExampleQueryId="false"></table>
	        
	        
	        
	        <table tableName="ofmucroomprop" 
	        domainObjectName="ofmucroomprop" 
	        enableCountByExample="false" 
	        enableUpdateByExample="false" 
	        enableDeleteByExample="false" 
	        enableSelectByExample="false" 
	        selectByExampleQueryId="false"></table>
	        
	        
	        
	        <table tableName="ofmucservice" 
	        domainObjectName="ofmucservice" 
	        enableCountByExample="false" 
	        enableUpdateByExample="false" 
	        enableDeleteByExample="false" 
	        enableSelectByExample="false" 
	        selectByExampleQueryId="false"></table>
	        
	        
	        
	        <table tableName="ofmucserviceprop" 
	        domainObjectName="ofmucserviceprop" 
	        enableCountByExample="false" 
	        enableUpdateByExample="false" 
	        enableDeleteByExample="false" 
	        enableSelectByExample="false" 
	        selectByExampleQueryId="false"></table>
	        
	        
	        
	        <table tableName="ofoffline" 
	        domainObjectName="ofoffline" 
	        enableCountByExample="false" 
	        enableUpdateByExample="false" 
	        enableDeleteByExample="false" 
	        enableSelectByExample="false" 
	        selectByExampleQueryId="false"></table>
	        
	        <table tableName="ofextcomponentconf" 
	        domainObjectName="ofextcomponentconf" 
	        enableCountByExample="false" 
	        enableUpdateByExample="false" 
	        enableDeleteByExample="false" 
	        enableSelectByExample="false" 
	        selectByExampleQueryId="false"></table>
	        
	        
	        <table tableName="ofpresence" 
	        domainObjectName="ofpresence" 
	        enableCountByExample="false" 
	        enableUpdateByExample="false" 
	        enableDeleteByExample="false" 
	        enableSelectByExample="false" 
	        selectByExampleQueryId="false"></table>
	        
	</context>
	
</generatorConfiguration>

3.使用cmd命令生成代码

java -jar mybatis-generator-core-1.3.3.jar -configfile mybatis-generater.xml -overwrite

这里要注意路径是在tomcat-webapp-project-lib里,而且mysql驱动包和配置文件必须在一起

执行这行命令会出现提示:

Existing file D:\myeclipse2015-workspace\mybatisGenerater\src\main\java\com\wyk\model\ofextcomponentconf.java was overwritten
Existing file D:\myeclipse2015-workspace\mybatisGenerater\src\main\java\com\wyk\dao\ofextcomponentconfMapper.java was overwritten
Existing file D:\myeclipse2015-workspace\mybatisGenerater\src\main\java\com\wyk\model\ofextcomponentconf.java was overwritten
Existing file D:\myeclipse2015-workspace\mybatisGenerater\src\main\java\com\wyk\dao\ofextcomponentconfMapper.java was overwritten
Existing file D:\myeclipse2015-workspace\mybatisGenerater\src\main\java\com\wyk\model\ofextcomponentconf.java was overwritten
Existing file D:\myeclipse2015-workspace\mybatisGenerater\src\main\java\com\wyk\dao\ofextcomponentconfMapper.java was overwritten

MyBatis Generator finished successfully, there were warnings.

说明执行成功

4.查看生成的文件:

public interface ofextcomponentconfMapper {
    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table ofextcomponentconf
     *
     * @mbggenerated Sun Jul 10 16:13:05 CST 2016
     */
    int deleteByPrimaryKey(String subdomain);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table ofextcomponentconf
     *
     * @mbggenerated Sun Jul 10 16:13:05 CST 2016
     */
    int insert(ofextcomponentconf record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table ofextcomponentconf
     *
     * @mbggenerated Sun Jul 10 16:13:05 CST 2016
     */
    int insertSelective(ofextcomponentconf record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table ofextcomponentconf
     *
     * @mbggenerated Sun Jul 10 16:13:05 CST 2016
     */
    ofextcomponentconf selectByPrimaryKey(String subdomain);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table ofextcomponentconf
     *
     * @mbggenerated Sun Jul 10 16:13:05 CST 2016
     */
    int updateByPrimaryKeySelective(ofextcomponentconf record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table ofextcomponentconf
     *
     * @mbggenerated Sun Jul 10 16:13:05 CST 2016
     */
    int updateByPrimaryKey(ofextcomponentconf record);
}
<?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.wyk.dao.ofextcomponentconfMapper">
  <resultMap id="BaseResultMap" type="com.wyk.model.ofextcomponentconf">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 10 16:13:05 CST 2016.
    -->
    <id column="subdomain" jdbcType="VARCHAR" property="subdomain" />
    <result column="wildcard" jdbcType="TINYINT" property="wildcard" />
    <result column="secret" jdbcType="VARCHAR" property="secret" />
    <result column="permission" jdbcType="VARCHAR" property="permission" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 10 16:13:05 CST 2016.
    -->
    subdomain, wildcard, secret, permission
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 10 16:13:05 CST 2016.
    -->
    select 
    <include refid="Base_Column_List" />
    from ofextcomponentconf
    where subdomain = #{subdomain,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 10 16:13:05 CST 2016.
    -->
    delete from ofextcomponentconf
    where subdomain = #{subdomain,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com.wyk.model.ofextcomponentconf">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 10 16:13:05 CST 2016.
    -->
    insert into ofextcomponentconf (subdomain, wildcard, secret, 
      permission)
    values (#{subdomain,jdbcType=VARCHAR}, #{wildcard,jdbcType=TINYINT}, #{secret,jdbcType=VARCHAR}, 
      #{permission,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.wyk.model.ofextcomponentconf">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 10 16:13:05 CST 2016.
    -->
    insert into ofextcomponentconf
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="subdomain != null">
        subdomain,
      </if>
      <if test="wildcard != null">
        wildcard,
      </if>
      <if test="secret != null">
        secret,
      </if>
      <if test="permission != null">
        permission,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="subdomain != null">
        #{subdomain,jdbcType=VARCHAR},
      </if>
      <if test="wildcard != null">
        #{wildcard,jdbcType=TINYINT},
      </if>
      <if test="secret != null">
        #{secret,jdbcType=VARCHAR},
      </if>
      <if test="permission != null">
        #{permission,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.wyk.model.ofextcomponentconf">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 10 16:13:05 CST 2016.
    -->
    update ofextcomponentconf
    <set>
      <if test="wildcard != null">
        wildcard = #{wildcard,jdbcType=TINYINT},
      </if>
      <if test="secret != null">
        secret = #{secret,jdbcType=VARCHAR},
      </if>
      <if test="permission != null">
        permission = #{permission,jdbcType=VARCHAR},
      </if>
    </set>
    where subdomain = #{subdomain,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.wyk.model.ofextcomponentconf">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 10 16:13:05 CST 2016.
    -->
    update ofextcomponentconf
    set wildcard = #{wildcard,jdbcType=TINYINT},
      secret = #{secret,jdbcType=VARCHAR},
      permission = #{permission,jdbcType=VARCHAR}
    where subdomain = #{subdomain,jdbcType=VARCHAR}
  </update>
</mapper>
package com.wyk.model;

public class ofextcomponentconf {
    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column ofextcomponentconf.subdomain
     *
     * @mbggenerated Sun Jul 10 16:13:05 CST 2016
     */
    private String subdomain;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column ofextcomponentconf.wildcard
     *
     * @mbggenerated Sun Jul 10 16:13:05 CST 2016
     */
    private Byte wildcard;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column ofextcomponentconf.secret
     *
     * @mbggenerated Sun Jul 10 16:13:05 CST 2016
     */
    private String secret;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column ofextcomponentconf.permission
     *
     * @mbggenerated Sun Jul 10 16:13:05 CST 2016
     */
    private String permission;

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column ofextcomponentconf.subdomain
     *
     * @return the value of ofextcomponentconf.subdomain
     *
     * @mbggenerated Sun Jul 10 16:13:05 CST 2016
     */
    public String getSubdomain() {
        return subdomain;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column ofextcomponentconf.subdomain
     *
     * @param subdomain the value for ofextcomponentconf.subdomain
     *
     * @mbggenerated Sun Jul 10 16:13:05 CST 2016
     */
    public void setSubdomain(String subdomain) {
        this.subdomain = subdomain == null ? null : subdomain.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column ofextcomponentconf.wildcard
     *
     * @return the value of ofextcomponentconf.wildcard
     *
     * @mbggenerated Sun Jul 10 16:13:05 CST 2016
     */
    public Byte getWildcard() {
        return wildcard;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column ofextcomponentconf.wildcard
     *
     * @param wildcard the value for ofextcomponentconf.wildcard
     *
     * @mbggenerated Sun Jul 10 16:13:05 CST 2016
     */
    public void setWildcard(Byte wildcard) {
        this.wildcard = wildcard;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column ofextcomponentconf.secret
     *
     * @return the value of ofextcomponentconf.secret
     *
     * @mbggenerated Sun Jul 10 16:13:05 CST 2016
     */
    public String getSecret() {
        return secret;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column ofextcomponentconf.secret
     *
     * @param secret the value for ofextcomponentconf.secret
     *
     * @mbggenerated Sun Jul 10 16:13:05 CST 2016
     */
    public void setSecret(String secret) {
        this.secret = secret == null ? null : secret.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column ofextcomponentconf.permission
     *
     * @return the value of ofextcomponentconf.permission
     *
     * @mbggenerated Sun Jul 10 16:13:05 CST 2016
     */
    public String getPermission() {
        return permission;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column ofextcomponentconf.permission
     *
     * @param permission the value for ofextcomponentconf.permission
     *
     * @mbggenerated Sun Jul 10 16:13:05 CST 2016
     */
    public void setPermission(String permission) {
        this.permission = permission == null ? null : permission.trim();
    }
}

只取了其中3个文件作为演示,实际上一共生成了30多个文件,因为我只指定生成数据库中10张表。用这个方法就可以减少很多手工代码的工作量了

© 著作权归作者所有

大海
粉丝 2
博文 84
码字总数 32431
作品 0
杭州
程序员
私信 提问
加载中

评论(1)

i
itxx2016
推荐国内最著名的mybatis代码生成网站 --- fwjava.com
无需任何安装配置,直接在线生成,且十分规范好用.
MyBatis Generator java生成方式小记

数据连接等相关的配置 xxx.properties 将xxx.properties放在项目的类路径下。如: config/xxx.properties generatorConfig.xml 编写(最简形式) java编写...

who7708
05/13
28
0
mybatis generator 生成检索条件问题

使用mybatis generator生成检索条件,想生成如下sql语句: select * from t_base t where t.aaa <> t.bbb 疑问:我用generator自动生成的Example类中的Criteria,仅能生成对[值]的查询条件,...

sg4567
2018/10/16
385
1
MyBatis insert 返回主键的方法

数据库:SqlServer2005,表结构: /==============================================================// Table: Dic_City //============================================================......

凯文加内特
2015/03/04
491
0
基于MybatisGenerator扩展的 Model & Mapper & Service & C

基于MybatisGenerator扩展的 Model & Mapper & Service & C Harries Blog™2018-10-25170 阅读 gitJavaIOjavaAppcatGitHubhttpshttp...

Harries Blog™
2018/10/25
0
0
关于mybatis下的继承关系映射

假设我有这样3张表 使用mybatis generator工具的话如何才能实现自动生成这样的继承关系,还是生成后需要自己手动去添加的? 另外,对于数据库设计,这样的抽象式的设计值得推荐吗?另外,mybatis需...

yongzhong
2014/12/02
2.3K
4

没有更多内容

加载失败,请刷新页面

加载更多

c语言实现Sqlite3的创建db和增删改查db操作

SQLite,是一款轻型的数据库,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中使用广泛,现在准备学习一下sqlite3的使用方法并写一个测试demo,后面在项目智能...

jorin_zou
10分钟前
2
0
【2019年8月版本】OCP 071认证考试最新版本的考试原题-第2题

choose three Which three are true about the CREATE TABLE command? A) It can include the CREATE...INDEX statement for creating an index to enforce the primary key constraint. B) ......

oschina_5359
13分钟前
3
0
如何在二维码中循环批量插入图片

现在二维码种类比较多,为了突出二维码的个性及吸引客户,很多朋友都喜欢在二维码上插入图片。想要每个二维码都与众不同,但是有的时候需要批量插入图片数量有限,如果制作的二维码比较多的话...

中琅软件
14分钟前
2
0
LTR那点事—AUC及其与线上点击率的关联详解

LTR(Learning To Rank)学习排序是一种监督学习(SupervisedLearning)的排序方法,现已经广泛应用于信息索引,内容推荐,自然语言处理等多个领域。以推荐系统为例,推荐一般使用多个子策略...

达观数据
14分钟前
3
0
IntelliJ 如何显示代码的代码 docs

希望能够在 IntelliJ 代码上面显示方法的 docs。 如何进行显示? 你可以使用 Ctrl + Q 这个快捷键来查看方法的 Docs。 https://blog.ossez.com/archives/3061...

honeymoose
17分钟前
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部