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张表。用这个方法就可以减少很多手工代码的工作量了