使用mybatis generator 逆向生成数据库代码

两个星期前,跑一个案例的时候尝试用mybatis generator逆向生产数据库代码,没有做记录,现在用到,完了,什么多记不起了,只好在摸索一遍

1、先建好一个maven工程

2、配置pom.xml,添加mybatis generator的依赖

<build>
        <finalName>SaprkWeb</finalName>
        <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>
    </build>
</project>

3、在resources下建jdbc.properties

jdbc_driverLocation=/home/hadoop/mysql/mysql-connector-java-5.1.40-bin.jar   //mysql驱动包所在位置
jdbc_driverClassName=com.mysql.jdbc.Driver //mysql驱动
jdbc_url=jdbc:mysql://localhost:3306/movierecommend?useUnicode=true&amp;characterEncoding=utf-8  //数据库及编码
jdbc_username=root  //用户
jdbc_password=123456 //密码

4、编写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>
    <!--导入属性配置,引入jdbc.propertie-->
    <properties resource="conf/jdbc.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_driverClassName}"
                connectionURL="${jdbc_url}"
                userId="${jdbc_username}"
                password="${jdbc_password}">
        </jdbcConnection>
        <!--<jdbcConnection driverClass="com.mysql.jdbc.Driver"-->
                        <!--connectionURL="jdbc:mysql://localhost:3306/movierecommend?useUnicode=true&characterEncoding=UTF-8"-->
                        <!--userId="root"-->
                        <!--password="123456" />-->



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


        <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
            targetPackage     指定生成的model生成所在的包名
            targetProject     指定在该项目下所在的路径
        -->
        <javaModelGenerator targetPackage="com.report.model"
                            targetProject="src/main/java">

            <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="false"/>
            <!-- 是否对model添加 构造函数 -->
            <property name="constructo  rBased" value="true"/>
            <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
            <property name="trimStrings" value="false"/>
            <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
            <property name="immutable" value="false"/>
        </javaModelGenerator>

        <!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
        <sqlMapGenerator targetPackage="main.resources.mapper" targetProject="src">
            <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.report.dao"
                             targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>


        <table tableName="userinfo" domainObjectName="UserInfo"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false" enableInsert="false">
            <!--useactualcolumnnames 使用实际的名字,默认是false,即为驼峰-->
            <property name="useActualColumnNames" value="true"/>
        </table>

        <table tableName="movieinfo" domainObjectName="MovieInfo"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false" enableInsert="false">
            <!--useactualcolumnnames 使用实际的名字,默认是false,即为驼峰-->
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="ratings" domainObjectName="Ratings"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false" enableInsert="false">
            <!--useactualcolumnnames 使用实际的名字,默认是false,即为驼峰-->
            <property name="useActualColumnNames" value="true"/>
        </table>
    </context>
</generatorConfiguration>

5、配置运行generatorConfig.xml

-------------------------------------------------------------------------------------------------------------------------------------------------------

方法一:如图

方法二:Run —>Edit Configuration...

------------------------------------------------------------------------------------------------------------------------------------------------

接下来

点击 + ——>Maven

填写参数

name---这个文件的名称

working directory---工程路径

Commend line 填写mybatis-generator:generate -e

点击Apply

ok

6、点击运行,便可以在相应的路径上查看到逆向生成的文件

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值