为什么80%的码农都做不了架构师?>>>   

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 generatorConfigurationPUBLIC "-//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 - @mbggeneratedThis 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 - @mbggeneratedThis 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 - @mbggeneratedThis 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 ofextcomponentconfwhere subdomain = #{subdomain,jdbcType=VARCHAR}</select><delete id="deleteByPrimaryKey" parameterType="java.lang.String"><!--WARNING - @mbggeneratedThis 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 ofextcomponentconfwhere subdomain = #{subdomain,jdbcType=VARCHAR}</delete><insert id="insert" parameterType="com.wyk.model.ofextcomponentconf"><!--WARNING - @mbggeneratedThis 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 - @mbggeneratedThis 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 - @mbggeneratedThis 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 - @mbggeneratedThis element is automatically generated by MyBatis Generator, do not modify.This element was generated on Sun Jul 10 16:13:05 CST 2016.-->update ofextcomponentconfset 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张表。用这个方法就可以减少很多手工代码的工作量了

转载于:https://my.oschina.net/wwwd/blog/709243

初次使用mybatis Generator相关推荐

  1. MyBatis学习总结(9)——使用MyBatis Generator自动创建代码

    2019独角兽企业重金招聘Python工程师标准>>> 由于MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所 ...

  2. SpringBoot如何自动生成实体类和Dao层以及映射文件(mybatis generator 自动生成代码)

    一.首先添加自动生成代码插件 <!-- mybatis generator 自动生成代码插件 生成时解除注释 --><plugin><groupId>org.myb ...

  3. idea mybatis generator插件_在idea中使用mybatis generator逆向工程生成代码

    用maven工具生成mybatis的代码和映射文件. 1.配置pom.xml文件 在pom.xml下添加插件如下: <build> <finalName>zsxt</fi ...

  4. Spring Boot项目利用MyBatis Generator进行数据层代码自动生成

    概 述 MyBatis Generator (简称 MBG) 是一个用于 MyBatis和 iBATIS的代码生成器.它可以为 MyBatis的所有版本以及 2.2.0之后的 iBATIS版本自动生成 ...

  5. 从命令行及java程序运行MyBatis Generator 1.3.x生成MyBatis3.x代码

    为什么80%的码农都做不了架构师?>>>    近期因为项目需要,调研了myBatis 3.x的使用,当然,顺便也就研究了一下使用Generator来通过逆向工程生成pojo,map ...

  6. mysql分页取数每一页生成xml_让MyBatis Generator产生的代码支持分页

    本文提供一种方法,让MyBatis Generator产生的代码支持分页,  适用于MySQL. 分析 如果要获取分页信息,使用MySQL语句,我们需要怎么做呢? select * from t_us ...

  7. MyBatis Generator 详解

    MyBatis Generator中文文档 MyBatis Generator中文文档地址: http://mbg.cndocs.tk/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看 ...

  8. Maven多模块项目使用MyBatis Generator

    开发环境: JDK:8u102 Maven:3.3.9 MySQL:5.7.10 MySQL Connector:5.1.40 IDE:IntelliJ IDEA 2016 MyBatis:3.4.1 ...

  9. MyBatis Generator模板

    注:注意替换红色部分 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorCo ...

最新文章

  1. ConfigurationProperties
  2. CF510 D - Fox And Jumping(GCD问题)
  3. 用神经网络学习Fe原子光谱并反向求导计算权重
  4. 如何为Apache Kylin快速开发新数据源?
  5. js Date 对象用于处理日期和时间。
  6. 常用 Dos 命令+杂项-常用的命令符+常用的公式
  7. DELPHI基础教程 第七章 剪贴板和动态数据交换
  8. Swift 个人学习笔记 - 01: A Swift Tour
  9. 计算机公式最小值,excel最小值函数
  10. python实现机器学习算法——K均值聚类算法
  11. win10系统登录服务器密码存储位置,win10远程服务器登录密码
  12. #500-7 [编程作业]3_4 念整数
  13. 【自动化测试】Web自动化测试框架01
  14. ARINC619 COP(chapter3)简介
  15. nginx安装crt证书
  16. QGIS离线GeoJSON数据,使用Cesium加载并根据楼层高度拉伸(weixin公众号【图说GIS】)
  17. 数字化办公,就选流畅、清晰的华为云桌面
  18. 《中级数据库系统工程师》
  19. ZCMU--5154: 体育运动
  20. mysql 建立超级用户_mysql创建超级用户

热门文章

  1. VTK:图表之VertexSize
  2. c++判断数字是否为3的倍数的算法实现(附完整源码)
  3. QT绘制散点图(2)
  4. QML基础类型之palette
  5. C++对C的加强之struct类型加强
  6. pytorch 时间序列预测,梯度裁剪
  7. Spark SQL概述,DataFrames,创建DataFrames的案例,DataFrame常用操作(DSL风格语法),sql风格语法
  8. 流式计算strom,Strom解决的问题,实现实时计算系统要解决那些问题,离线计算是什么,流式计算什么,离线和实时计算区别,strom应用场景,Strorm架构图和编程模型(来自学习资料)
  9. C语言二分查找法(指针和数组实现)
  10. Java中几种输出数组的for循环,以及循环中使用标签(非goto)