mybatis-generator-gui

代码在这里,自己构建一个
环境: 
OS: win7
jdk: java version "1.8.0_341"
mysql: 

1、修改pom.xml,把注释打开
<!-- Java 11请关闭下面注释 -->
        
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>11</version>
        </dependency>

2、参考README.md文件,依次执行以下命令即可构建成功,
构建成功的文件位于target/jfx/app下,文件名为: mybatis-generator-gui.jar 
  git clone https://github.com/zouzg/mybatis-generator-gui
  cd mybatis-generator-gui
  mvn jfx:jar
  cd target/jfx/app/
  java -jar mybatis-generator-gui.jar

3、如何运行
java -jar target/jfx/app/mybatis-generator-gui.jar

或者在IDE中直接运行com.zzg.mybatis.generator.MainUI

4、问题
1)、运行后创建mysql连接时,提示找不到mysql-connector-java-5.1.38.jar驱动
发现在com.zzg.mybatis.generator.model.DbType中,有这么一句:
MySQL("com.mysql.jdbc.Driver", "jdbc:mysql://%s:%s/%s?useUnicode=true&useSSL=false&characterEncoding=%s", "mysql-connector-java-5.1.38.jar"),
MySQL_8("com.mysql.cj.jdbc.Driver", "jdbc:mysql://%s:%s/%s?serverTimezone=UTC&useUnicode=true&useSSL=false&characterEncoding=%s","mysql-connector-java-8.0.11.jar"),
Oracle("oracle.jdbc.OracleDriver", "jdbc:oracle:thin:@//%s:%s/%s", "ojdbc6.jar"),
PostgreSQL("org.postgresql.Driver", "jdbc:postgresql://%s:%s/%s", "postgresql-9.4.1209.jar"),
SQL_Server("com.microsoft.sqlserver.jdbc.SQLServerDriver","jdbc:sqlserver://%s:%s;databaseName=%s", "sqljdbc4-4.0.jar"),
Sqlite("org.sqlite.JDBC", "jdbc:sqlite:%s", "sqlite-jdbc-3.19.3.jar")

这里指定了支持的各种数据库以及驱动包

解决:
修改了pom.xml,增加了mysql-connector-java-5.1.38配置,

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>

然后重新执行mvn jfx:jar,生成新的mybatis-generator-gui.jar
重新运行: java -jar mybatis-generator-gui.jar 
重新建立mysql连接,这次终于连接成功了


开始使用:
打开数据库连接,选择表,输入项目目录、实体类名包名、Mapper接口包名、映射XML文件包名,
实体类名包、Mapper接口包默认存放位置为src/main/java
XML文件存放目录默认为src/main/resources

然后点击“生成代码”

MyBatisBaseDao.java: 
package mapper;

import java.io.Serializable;

/**
 * DAO公共基类,由MybatisGenerator自动生成请勿修改
 * @param <Model> The Model Class 这里是泛型不是Model类
 * @param <PK> The Primary Key Class 如果是无主键,则可以用Model来跳过,如果是多主键则是Key类
 */
public interface MyBatisBaseDao<Model, PK extends Serializable> {
    int deleteByPrimaryKey(PK id);

    int insert(Model record);

    int insertSelective(Model record);

    Model selectByPrimaryKey(PK id);

    int updateByPrimaryKeySelective(Model record);

    int updateByPrimaryKey(Model record);
}


UserInfoDAO.java:
package mapper;

import model.UserInfo;
import org.springframework.stereotype.Repository;

/**
 * UserInfoDAO继承基类
 */
@Repository
public interface UserInfoDAO extends MyBatisBaseDao<UserInfo, Integer> {
}


UserInfo.java: 
package model;

import java.io.Serializable;

/**
 * @author 
 * 
 */
public class UserInfo implements Serializable {
    private Integer id;

    private String uid;

    private String name;

    private String city;

    private String province;

    private Byte status;

    private static final long serialVersionUID = 1L;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public Byte getStatus() {
        return status;
    }

    public void setStatus(Byte status) {
        this.status = status;
    }
}

UserInfoDAO.xml:
<?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="mapper.UserInfoDAO">
  <resultMap id="BaseResultMap" type="model.UserInfo">
    <id column="user_info_id" jdbcType="INTEGER" property="id" />
    <result column="user_info_uid" jdbcType="VARCHAR" property="uid" />
    <result column="user_info_name" jdbcType="VARCHAR" property="name" />
    <result column="user_info_city" jdbcType="VARCHAR" property="city" />
    <result column="user_info_province" jdbcType="VARCHAR" property="province" />
    <result column="user_info_status" jdbcType="TINYINT" property="status" />
  </resultMap>
  <sql id="Base_Column_List">
    user_info.id as user_info_id, user_info.`uid` as `user_info_uid`, user_info.`name` as `user_info_name`, 
    user_info.city as user_info_city, user_info.province as user_info_province, user_info.`status` as `user_info_status`
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from user_info user_info
    where user_info.id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from user_info
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="model.UserInfo">
    insert into user_info (id, `uid`, `name`, 
      city, province, `status`
      )
    values (#{id,jdbcType=INTEGER}, #{uid,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, 
      #{city,jdbcType=VARCHAR}, #{province,jdbcType=VARCHAR}, #{status,jdbcType=TINYINT}
      )
  </insert>
  <insert id="insertSelective" parameterType="model.UserInfo">
    insert into user_info
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="uid != null">
        `uid`,
      </if>
      <if test="name != null">
        `name`,
      </if>
      <if test="city != null">
        city,
      </if>
      <if test="province != null">
        province,
      </if>
      <if test="status != null">
        `status`,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="uid != null">
        #{uid,jdbcType=VARCHAR},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="city != null">
        #{city,jdbcType=VARCHAR},
      </if>
      <if test="province != null">
        #{province,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=TINYINT},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="model.UserInfo">
    update user_info
    <set>
      <if test="uid != null">
        `uid` = #{uid,jdbcType=VARCHAR},
      </if>
      <if test="name != null">
        `name` = #{name,jdbcType=VARCHAR},
      </if>
      <if test="city != null">
        city = #{city,jdbcType=VARCHAR},
      </if>
      <if test="province != null">
        province = #{province,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        `status` = #{status,jdbcType=TINYINT},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="model.UserInfo">
    update user_info
    set `uid` = #{uid,jdbcType=VARCHAR},
      `name` = #{name,jdbcType=VARCHAR},
      city = #{city,jdbcType=VARCHAR},
      province = #{province,jdbcType=VARCHAR},
      `status` = #{status,jdbcType=TINYINT}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>


第一次使用generator-gui工具生成mybatis框架代码,后续也会尝试使用非gui工具,
比如:mybatis-generator或mybatis-plus
先记录下maven生成quickstart和webapp项目的命令,后续可能通过它们使用mbg和mybatis-plus

quickstart: 
mvn  -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.1 -DgroupId=com.dev -DartifactId=mbg-mvn-quickstart -Dversion=0.0.1-SNAPSHOT -Dpackage=com.dev.mbg.mvn.quickstart -DoutputDirectory=E:\data\programs\sts4 -Dstyle.color=always -gs E:\maven-3.9.0\conf\settings.xml -s E:\maven-3.9.0\conf\settings.xml -U archetype:generate


webapp:
mvn  -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.0 -DgroupId=com.dev -DartifactId=mbg-mvn-webapp -Dversion=0.0.1-SNAPSHOT -Dpackage=com.dev.mbg.mvn.webapp -DoutputDirectory=E:\data\programs\sts4 -B -Dstyle.color=always -gs E:\maven-3.9.0\conf\settings.xml -s E:\maven-3.9.0\conf\settings.xml -U archetype:generate