首页 热点资讯 义务教育 高等教育 出国留学 考研考公
您的当前位置:首页正文

Eclipse 下 Mybatis 自动生成实体类,Mappin

2024-12-20 来源:化拓教育网
  1. 在 Eclipse Marketplace 下载 “MyBatis Generator”,最新版本是 1.3.5。

  2. 在项目中新建一个"My Batis Generator Configuration File"。
    "New->Other->MyBatis-> MyBatis Generator Configuration File"


    image.png
  3. 以下便是 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>
  <context id="context1">
    <jdbcConnection connectionURL="???" driverClass="???" password="???" userId="???" />
    <javaModelGenerator targetPackage="???" targetProject="???" />
    <sqlMapGenerator targetPackage="???" targetProject="???" />
    <javaClientGenerator targetPackage="???" targetProject="???" type="XMLMAPPER" />
    <table schema="???" tableName="???">
      <columnOverride column="???" property="???" />
    </table>
  </context>
</generatorConfiguration>

4.这个是我修改配置后的内容

<?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>
<!-- classPathEntry:数据库的JDBC驱动的jar包地址--> 
<classPathEntry 
      location="C:/Users/h144198/Downloads/postgresql-42.1.1.jar" />
  <context id="context1">
  <property name="mergeable" value="true"></property>
   <commentGenerator> 
    <!-- 是否去除自动生成的注释 true:是 : false:否 --> 
    <property name="suppressDate" value="true"/>
    </commentGenerator> 
   <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> 
<jdbcConnection driverClass="org.postgresql.Driver"
            connectionURL="jdbc:postgresql://localhost:5432/postgres?currentSchema=public"
            userId="postgres" 
            password="AAaa1111">
</jdbcConnection>
         
<javaModelGenerator targetPackage="com.honey.windy.entity" targetProject="mybatisDemo\src\main\java">
            <!-- 如果true,MBG会根据catalog和schema来生成子包,如果false就会直接用targetPackage属性,默认为false -->
            <property name="enableSubPackages" value="false"></property>
</javaModelGenerator>

<sqlMapGenerator targetPackage="mybatis" targetProject="mybatisDemo\src\main\resources" >
            <!-- 如果true,MBG会根据catalog和schema来生成子包,如果false就会直接用targetPackage属性,默认为false -->
            <property name="enableSubPackages" value="false"></property>
</sqlMapGenerator>
        
<!-- 如果不配置该元素,就不会生成  DAO 接口 -->
<javaClientGenerator targetPackage="com.honey.home.message.dao" type="XMLMAPPER"  targetProject="mybatisDemo\src\main\java">
            <!-- 如果true,MBG会根据catalog和schema来生成子包,如果false就会直接用targetPackage属性,默认为false -->
            <property name="enableSubPackages" value="false"></property>
</javaClientGenerator>
    
    
    <table schema="public"  tableName="tbl_customer" domainObjectName="Customer" 
    enableCountByExample="false"
    enableUpdateByExample="false"
    enableDeleteByExample="false"
    enableSelectByExample="false" 
    selectByExampleQueryId="false"  >
       
    </table>
  </context>
  
</generatorConfiguration>
  1. 配置文件中大部分有了注解,提一下注意几点:
  1. targetproject 属性问题,这个不能从字面意义上去理解,(或许我这个Java的初学者理解有误),这里的设置并不是project而是一个真正的路径。
  2. 覆盖问题:使用以上的配置有个问题就是: MyBatis Generator "自觉"的问我们加入了很多注释:
public class Customer {

    /**
     * This field was generated by MyBatis Generator. This field corresponds to the database column public.tbl_customer.id
     * @mbg.generated
     */
    private BigDecimal id;
    /**
     * This field was generated by MyBatis Generator. This field corresponds to the database column public.tbl_customer.Name
     * @mbg.generated
     */
    private String name;

    /**
     * This method was generated by MyBatis Generator. This method returns the value of the database column public.tbl_customer.id
     * @return  the value of public.tbl_customer.id
     * @mbg.generated
     */
    public BigDecimal getId() {
        return id;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of the database column public.tbl_customer.id
     * @param id  the value for public.tbl_customer.id
     * @mbg.generated
     */
    public void setId(BigDecimal id) {
        this.id = id;
    }

    /**
     * This method was generated by MyBatis Generator. This method returns the value of the database column public.tbl_customer.Name
     * @return  the value of public.tbl_customer.Name
     * @mbg.generated
     */
    public String getName() {
        return name;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of the database column public.tbl_customer.Name
     * @param name  the value for public.tbl_customer.Name
     * @mbg.generated
     */
    public void setName(String name) {
        this.name = name;
    }
}

我们并不需要啊,好吧我们可以设置"suppressAllComments" 这个属性,

 <context id="context1">
   
   <commentGenerator> 
    <!-- 是否去除自动生成的注释 true:是 : false:否 --> 
    <property name="suppressDate" value="true"/>
    <property name="suppressAllComments" value="true"/>
    </commentGenerator> 
  1. 其他生成方法:使用Maven插件
<plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>

然后设置一下 Debug/Run Config

image.png

这种做法与上一种大同小异,不过似乎没有追加文本的问题,推荐使用。

显示全文