mybatis-generator反向工程数据库生成代码

Posted wyx100

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis-generator反向工程数据库生成代码相关的知识,希望对你有一定的参考价值。

 

mytest
utf8 -- UTF-8 Unicode
utf8_unicode_ci

mytest数据表sys_users

CREATE TABLE `sys_users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>mybatis_generator</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>mybatis_generator</name>
  <build>
   <plugins>
    <plugin>
       <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
         <version>1.3.6</version>
    </plugin>
   </plugins>
  </build> 
</project>

generatorConfig.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>
 
    <!--数据库驱动jar -->
    <classPathEntry
        location="D:\\software\\mysql-connector-java-8.0.22.jar" />
 
    <context id="Tables" targetRuntime="MyBatis3">
        <!--去除注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
 
        <!--数据库连接 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/mytest?useUnicode=true&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC" userId="root" password="root">
        </jdbcConnection>
        <!--默认false Java type resolver will always use java.math.BigDecimal if 
            the database column is of type DECIMAL or NUMERIC. -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
 
		<!-- ************************* targetProject 最好使用相对路径,否则会报错(Project does not exist) **************** -->
 
        <!--生成实体类 指定包名 以及生成的地址 (可以自定义地址,但是路径不存在不会自动创建 使用Maven生成在target目录下,会自动创建) -->
        <javaModelGenerator targetPackage="com.junlenet.mybatis.generator.model" 
            targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        
        <!--生成SQLMAP文件 -->
        <sqlMapGenerator targetPackage="com.junlenet.mybatis.generator.mapper"
            targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!--生成Dao文件 可以配置 type="XMLMAPPER"生成xml的dao实现 context id="DB2Tables" 修改targetRuntime="MyBatis3" -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.junlenet.mybatis.generator.dao"
            targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
 
        <!--对应数据库表 mysql可以加入主键自增 字段命名 忽略某字段等 -->
        <table tableName="sys_users" domainObjectName="sys_users"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
            
    </context>
</generatorConfiguration>

mybatis-generator:generate

sys_usersMapper.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="com.junlenet.mybatis.generator.dao.sys_usersMapper">
  <resultMap id="BaseResultMap" type="com.junlenet.mybatis.generator.model.sys_users">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
  </resultMap>
  <sql id="Base_Column_List">
    id, name
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from sys_users
    where id = #id,jdbcType=INTEGER
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from sys_users
    where id = #id,jdbcType=INTEGER
  </delete>
  <insert id="insert" parameterType="com.junlenet.mybatis.generator.model.sys_users">
    insert into sys_users (id, name)
    values (#id,jdbcType=INTEGER, #name,jdbcType=VARCHAR)
  </insert>
  <insert id="insertSelective" parameterType="com.junlenet.mybatis.generator.model.sys_users">
    insert into sys_users
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="name != null">
        name,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #id,jdbcType=INTEGER,
      </if>
      <if test="name != null">
        #name,jdbcType=VARCHAR,
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.junlenet.mybatis.generator.model.sys_users">
    update sys_users
    <set>
      <if test="name != null">
        name = #name,jdbcType=VARCHAR,
      </if>
    </set>
    where id = #id,jdbcType=INTEGER
  </update>
  <update id="updateByPrimaryKey" parameterType="com.junlenet.mybatis.generator.model.sys_users">
    update sys_users
    set name = #name,jdbcType=VARCHAR
    where id = #id,jdbcType=INTEGER
  </update>
</mapper>

 

sys_usersMapper.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="com.junlenet.mybatis.generator.dao.sys_usersMapper">
  <resultMap id="BaseResultMap" type="com.junlenet.mybatis.generator.model.sys_users">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
  </resultMap>
  <sql id="Base_Column_List">
    id, name
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from sys_users
    where id = #id,jdbcType=INTEGER
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from sys_users
    where id = #id,jdbcType=INTEGER
  </delete>
  <insert id="insert" parameterType="com.junlenet.mybatis.generator.model.sys_users">
    insert into sys_users (id, name)
    values (#id,jdbcType=INTEGER, #name,jdbcType=VARCHAR)
  </insert>
  <insert id="insertSelective" parameterType="com.junlenet.mybatis.generator.model.sys_users">
    insert into sys_users
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="name != null">
        name,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #id,jdbcType=INTEGER,
      </if>
      <if test="name != null">
        #name,jdbcType=VARCHAR,
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.junlenet.mybatis.generator.model.sys_users">
    update sys_users
    <set>
      <if test="name != null">
        name = #name,jdbcType=VARCHAR,
      </if>
    </set>
    where id = #id,jdbcType=INTEGER
  </update>
  <update id="updateByPrimaryKey" parameterType="com.junlenet.mybatis.generator.model.sys_users">
    update sys_users
    set name = #name,jdbcType=VARCHAR
    where id = #id,jdbcType=INTEGER
  </update>
</mapper>

 

sys_users.java

package com.junlenet.mybatis.generator.model;

public class sys_users 
    private Integer id;

    private String name;

    public Integer getId() 
        return id;
    

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

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name == null ? null : name.trim();
    

 

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building mybatis_generator 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- mybatis-generator-maven-plugin:1.3.6:generate (default-cli) @ mybatis_generator ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.840 s
[INFO] Finished at: 2021-02-21T00:16:39+08:00
[INFO] Final Memory: 11M/99M
[INFO] ------------------------------------------------------------------------

 

参考

https://blog.csdn.net/huweijun_2012/article/details/80622539

以上是关于mybatis-generator反向工程数据库生成代码的主要内容,如果未能解决你的问题,请参考以下文章

搭建Mybatis-generator工程

数据库表反向生成mybatis-generator与IDEA的集成

mybatis-generator

MyEclipse下安装MyBatis Generator代码反向生成工具

在Java聚合工程里使用mybatis-generator

MyBatis-Generator 逆向工程(生成异常缺少部分的方法)