初次使用mybatis Generator
為什么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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 树莓派debian配置lamp【解决ap
- 下一篇: 【poi xlsx报错】使用POI创建