• mybatis-generator 自动生成


    mybatis-generator可以帮助我们通过表反向生成pojo类,mapper文件,mapper.xml。

    我使用的软件:IDEA 数据库:mysql  我使用的数据库名字:jiaji

    先在数据库中建立本次使用的数据库和表。我建了5张表

     

    打开IDEA,新建一个maven项目(我的叫gongcheng)。接下来开始配置。pom.xml中(把下面的复制过去,改一下项目名)

    <?xml version="1.0" encoding="UTF-8"?>
    <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xhj</groupId>
    <artifactId>gongcheng</artifactId> <!-- 项目名-->
    <version>1.0-SNAPSHOT</version>
    <!--- mybatis-generator 用的jar包版本-->
    <properties>
    <mysql-connector-version>5.1.38</mysql-connector-version>
    </properties>

    <dependencies>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>${mysql-connector-version}</version>
    </dependency>
    </dependencies>

    <build>
    <finalName>zsxt</finalName>
    <plugins>
    <plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
    <verbose>true</verbose>
    <overwrite>true</overwrite>
    </configuration>
    </plugin>
    </plugins>
    </build>
    </project>

     现在的目录结构

     在resources下建立generator.properties 

    找到你的maven仓库地址 把C:/Users/lenovo/.m2/repository换成你自己的

    jiaji为我的数据库名   换成你自己的 

    uerId 用户名 password 密码

    jdbc.driverLocation=C:/Users/lenovo/.m2/repository/mysql/mysql-connector-java/5.1.38/mysql-connector-java-5.1.38.jar
    jdbc.driverClass=com.mysql.jdbc.Driver
    jdbc.connectionURL=jdbc:mysql://127.0.0.1:3306/jiaji
    jdbc.userId=root
    jdbc.password=root

    建立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>
    <!--导入属性配置-->
    <properties resource="generator.properties"></properties>

    <!--指定特定数据库的jdbc驱动jar包的位置-->
    <classPathEntry location="${jdbc.driverLocation}"/>

    <context id="default" targetRuntime="MyBatis3">

    <!-- optional,旨在创建class时,对注释进行控制 -->
    <commentGenerator>
    <property name="suppressDate" value="true"/>
    <property name="suppressAllComments" value="true"/>
    </commentGenerator>

    <!--jdbc的数据库连接 -->
    <jdbcConnection
    driverClass="${jdbc.driverClass}"
    connectionURL="${jdbc.connectionURL}"
    userId="${jdbc.userId}"
    password="${jdbc.password}">
    </jdbcConnection>


    <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
    <javaTypeResolver>
    <property name="forceBigDecimals" value="true"/>
    </javaTypeResolver>


    <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
    targetPackage 指定生成的model生成所在的包名
    targetProject 指定在该项目下所在的路径
    cn.fraudmetrix.crest.dal.object
    -->
    <javaModelGenerator targetPackage="com.clps.pojo"
    targetProject="src/main/java">
    <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
    <property name="enableSubPackages" value="false"/>
    <!-- 是否对model添加 构造函数 -->
    <property name="constructorBased" value="true"/>
    <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
    <property name="trimStrings" value="true"/>
    <!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 -->
    <property name="immutable" value="false"/>
    </javaModelGenerator>

    <!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件(生成xml文件) -->
    <sqlMapGenerator targetPackage="com.clps.dao"
    targetProject="src/main/resources">
    <property name="enableSubPackages" value="true"/>
    </sqlMapGenerator>

    <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
    type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
    type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
    type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
    -->
    <javaClientGenerator targetPackage="com.clps.dao"
    targetProject="src/main/java" type="XMLMAPPER">
    <property name="enableSubPackages" value="true"/>
    </javaClientGenerator>
    <!--数据库中建立了几张表,写几个table tableName对应表名 domainObjectName对应自动生成的pojo类 -->

    <table tableName="users" domainObjectName="UserDo"
    enableCountByExample="false" enableUpdateByExample="false"
    enableDeleteByExample="false" enableSelectByExample="false"
    selectByExampleQueryId="false">
    </table>
    <table tableName="stu" domainObjectName="StuDo"
    enableCountByExample="false" enableUpdateByExample="false"
    enableDeleteByExample="false" enableSelectByExample="false"
    selectByExampleQueryId="false">
    </table>
    <table tableName="dept" domainObjectName="DeptDo"
    enableCountByExample="false" enableUpdateByExample="false"
    enableDeleteByExample="false" enableSelectByExample="false"
    selectByExampleQueryId="false">
    </table>
    <table tableName="emp" domainObjectName="EmpDo"
    enableCountByExample="false" enableUpdateByExample="false"
    enableDeleteByExample="false" enableSelectByExample="false"
    selectByExampleQueryId="false">
    </table>
    <table tableName="salgrade" domainObjectName="salgradeDo"
    enableCountByExample="false" enableUpdateByExample="false"
    enableDeleteByExample="false" enableSelectByExample="false"
    selectByExampleQueryId="false">
    </table>
    </context>
    </generatorConfiguration>
    配置结束,Idea中设置运行条件

     或者eclipse中添加运行条件 mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate

     
  • 相关阅读:
    Confluence 6 从外部目录中同步数据手动同步缓存
    Confluence 6 从外部目录中同步数据如何工作
    Confluence 6 从外部目录中同步数据支持的目录类型
    Confluence 6 嵌套用户组的备注
    Confluence 6 嵌套用户组的示例
    Confluence 6 嵌套用户组的影响
    Confluence 6 启用嵌套用户组
    Confluence 6 目录序列将会影响
    初识Continuation
    HBase原理–所有Region切分的细节都在这里了
  • 原文地址:https://www.cnblogs.com/yxj808/p/12196445.html
Copyright © 2020-2023  润新知