一、使用说明
1)配置数据库连接配置 - 根据连接数据库类型(mysql或oracle)对generator.xml进行不同驱动及数据库账号密码配置
<?xml version="1.0" encoding="UTF-8"?>@b@<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">@b@<generatorConfiguration>@b@ <!-- 数据库驱动包位置 -->@b@ <classPathEntry location=".\mysql-connector-java-8.0.13.jar" /> @b@ <context id="DB2Tables" targetRuntime="MyBatis3">@b@ <commentGenerator>@b@ <property name="suppressAllComments" value="true" />@b@ </commentGenerator>@b@ <!-- 数据库链接URL、用户名、密码 -->@b@ <!--<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/localtest" userId="root" password="123456"> -->@b@ <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/localtest?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT" userId="root" password="123456"> @b@ <!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">-->@b@ </jdbcConnection>@b@ <javaTypeResolver>@b@ <property name="forceBigDecimals" value="false" />@b@ </javaTypeResolver>@b@ <!-- 生成模型的包名和位置 -->@b@ <javaModelGenerator targetPackage="com.xwood.model" targetProject=".\src">@b@ <property name="enableSubPackages" value="true" />@b@ <property name="trimStrings" value="true" />@b@ </javaModelGenerator>@b@ <!-- 生成的映射文件包名和位置 -->@b@ <sqlMapGenerator targetPackage="com.xwood.mapping" targetProject=".\src">@b@ <property name="enableSubPackages" value="true" />@b@ </sqlMapGenerator>@b@ <!-- 生成DAO的包名和位置 -->@b@ <javaClientGenerator type="XMLMAPPER" targetPackage="com.xwood.dao" targetProject=".\src">@b@ <property name="enableSubPackages" value="true" />@b@ </javaClientGenerator>@b@ <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->@b@ <table tableName="demo_first" domainObjectName="DemoFirst" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />@b@ @b@ </context>@b@</generatorConfiguration>
2) 运行“generatorCode.bat”提示代码生成成功,具体生成目标代码见src目录下面,如下图所示
F:\BaiduNetdiskDownload\generator>java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite@b@MyBatis Generator finished successfully.
3)生成dao&mapping&model代码如下图所示
package com.xwood.dao;@b@@b@import com.xwood.model.DemoFirst;@b@@b@public interface DemoFirstMapper {@b@ int deleteByPrimaryKey(Long id);@b@@b@ int insert(DemoFirst record);@b@@b@ int insertSelective(DemoFirst record);@b@@b@ DemoFirst selectByPrimaryKey(Long id);@b@@b@ int updateByPrimaryKeySelective(DemoFirst record);@b@@b@ int updateByPrimaryKeyWithBLOBs(DemoFirst record);@b@@b@ int updateByPrimaryKey(DemoFirst record);@b@}
<?xml version="1.0" encoding="UTF-8" ?>@b@<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >@b@<mapper namespace="com.xwood.dao.DemoFirstMapper" >@b@ <resultMap id="BaseResultMap" type="com.xwood.model.DemoFirst" >@b@ <id column="id" property="id" jdbcType="BIGINT" />@b@ <result column="name" property="name" jdbcType="VARCHAR" />@b@ <result column="code" property="code" jdbcType="VARCHAR" />@b@ <result column="content" property="content" jdbcType="VARCHAR" />@b@ <result column="varchar_one" property="varcharOne" jdbcType="VARCHAR" />@b@ </resultMap>@b@ <resultMap id="ResultMapWithBLOBs" type="com.xwood.model.DemoFirst" extends="BaseResultMap" >@b@ <result column="content_ext" property="contentExt" jdbcType="LONGVARCHAR" />@b@ </resultMap>@b@ <sql id="Base_Column_List" >@b@ id, name, code, content, varchar_one@b@ </sql>@b@ <sql id="Blob_Column_List" >@b@ content_ext@b@ </sql>@b@ <select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Long" >@b@ select @b@ <include refid="Base_Column_List" />@b@ ,@b@ <include refid="Blob_Column_List" />@b@ from demo_first@b@ where id = #{id,jdbcType=BIGINT}@b@ </select>@b@ <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >@b@ delete from demo_first@b@ where id = #{id,jdbcType=BIGINT}@b@ </delete>@b@ <insert id="insert" parameterType="com.xwood.model.DemoFirst" >@b@ insert into demo_first (id, name, code, @b@ content, varchar_one, content_ext@b@ )@b@ values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, #{code,jdbcType=VARCHAR}, @b@ #{content,jdbcType=VARCHAR}, #{varcharOne,jdbcType=VARCHAR}, #{contentExt,jdbcType=LONGVARCHAR}@b@ )@b@ </insert>@b@ <insert id="insertSelective" parameterType="com.xwood.model.DemoFirst" >@b@ insert into demo_first@b@ <trim prefix="(" suffix=")" suffixOverrides="," >@b@ <if test="id != null" >@b@ id,@b@ </if>@b@ <if test="name != null" >@b@ name,@b@ </if>@b@ <if test="code != null" >@b@ code,@b@ </if>@b@ <if test="content != null" >@b@ content,@b@ </if>@b@ <if test="varcharOne != null" >@b@ varchar_one,@b@ </if>@b@ <if test="contentExt != null" >@b@ content_ext,@b@ </if>@b@ </trim>@b@ <trim prefix="values (" suffix=")" suffixOverrides="," >@b@ <if test="id != null" >@b@ #{id,jdbcType=BIGINT},@b@ </if>@b@ <if test="name != null" >@b@ #{name,jdbcType=VARCHAR},@b@ </if>@b@ <if test="code != null" >@b@ #{code,jdbcType=VARCHAR},@b@ </if>@b@ <if test="content != null" >@b@ #{content,jdbcType=VARCHAR},@b@ </if>@b@ <if test="varcharOne != null" >@b@ #{varcharOne,jdbcType=VARCHAR},@b@ </if>@b@ <if test="contentExt != null" >@b@ #{contentExt,jdbcType=LONGVARCHAR},@b@ </if>@b@ </trim>@b@ </insert>@b@ <update id="updateByPrimaryKeySelective" parameterType="com.xwood.model.DemoFirst" >@b@ update demo_first@b@ <set >@b@ <if test="name != null" >@b@ name = #{name,jdbcType=VARCHAR},@b@ </if>@b@ <if test="code != null" >@b@ code = #{code,jdbcType=VARCHAR},@b@ </if>@b@ <if test="content != null" >@b@ content = #{content,jdbcType=VARCHAR},@b@ </if>@b@ <if test="varcharOne != null" >@b@ varchar_one = #{varcharOne,jdbcType=VARCHAR},@b@ </if>@b@ <if test="contentExt != null" >@b@ content_ext = #{contentExt,jdbcType=LONGVARCHAR},@b@ </if>@b@ </set>@b@ where id = #{id,jdbcType=BIGINT}@b@ </update>@b@ <update id="updateByPrimaryKeyWithBLOBs" parameterType="com.xwood.model.DemoFirst" >@b@ update demo_first@b@ set name = #{name,jdbcType=VARCHAR},@b@ code = #{code,jdbcType=VARCHAR},@b@ content = #{content,jdbcType=VARCHAR},@b@ varchar_one = #{varcharOne,jdbcType=VARCHAR},@b@ content_ext = #{contentExt,jdbcType=LONGVARCHAR}@b@ where id = #{id,jdbcType=BIGINT}@b@ </update>@b@ <update id="updateByPrimaryKey" parameterType="com.xwood.model.DemoFirst" >@b@ update demo_first@b@ set name = #{name,jdbcType=VARCHAR},@b@ code = #{code,jdbcType=VARCHAR},@b@ content = #{content,jdbcType=VARCHAR},@b@ varchar_one = #{varcharOne,jdbcType=VARCHAR}@b@ where id = #{id,jdbcType=BIGINT}@b@ </update>@b@</mapper>
package com.xwood.model;@b@@b@public class DemoFirst {@b@ private Long id;@b@@b@ private String name;@b@@b@ private String code;@b@@b@ private String content;@b@@b@ private String varcharOne;@b@@b@ private String contentExt;@b@@b@ public Long getId() {@b@ return id;@b@ }@b@@b@ public void setId(Long id) {@b@ this.id = id;@b@ }@b@@b@ public String getName() {@b@ return name;@b@ }@b@@b@ public void setName(String name) {@b@ this.name = name == null ? null : name.trim();@b@ }@b@@b@ public String getCode() {@b@ return code;@b@ }@b@@b@ public void setCode(String code) {@b@ this.code = code == null ? null : code.trim();@b@ }@b@@b@ public String getContent() {@b@ return content;@b@ }@b@@b@ public void setContent(String content) {@b@ this.content = content == null ? null : content.trim();@b@ }@b@@b@ public String getVarcharOne() {@b@ return varcharOne;@b@ }@b@@b@ public void setVarcharOne(String varcharOne) {@b@ this.varcharOne = varcharOne == null ? null : varcharOne.trim();@b@ }@b@@b@ public String getContentExt() {@b@ return contentExt;@b@ }@b@@b@ public void setContentExt(String contentExt) {@b@ this.contentExt = contentExt == null ? null : contentExt.trim();@b@ }@b@}
�