• (转)淘淘商城系列——SSM框架整合之逆向工程


    http://blog.csdn.net/yerenyuan_pku/article/details/72758590

    我们知道在开发中有些工作是非常耗时但是又没有什么技术含量的,比如创建mapper文件、pojo、dao等,我们更关心的是业务逻辑的处理,而不是这些无关紧要的东西,因此如果能有工具帮我们自动生成代码将是非常棒的一件事情,这些mybatis官方已经为我们考虑到了,mybatis官方提供了逆向生成代码工程,我们只需要修改下配置文件便可以非常方便的生成接口、pojo、dao。如果有同学对mybatis的逆向生成代码工程不熟悉,可参考我的文章——MyBatis框架的学习(七)——MyBatis逆向工程自动生成代码,这篇文章我就已经讲的很详细了,但我在此再讲一遍,希望读者能多多留心。 
    下面是我所使用的mybatis官方提供的逆向生成代码工程。 

    接着我们把该工程导入到我们的workspace目录下,该过程省略,相信大家都知道。import完成之后,我们可以看到逆向工程的整个目录结构如下图所示。 
     
    接下来,我们修改generatorConfig.xml配置文件来帮我们自动生成代码。 打开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="testTables" targetRuntime="MyBatis3">
            <commentGenerator>
                <!-- 是否去除自动生成的注释 true:是 : false:否 -->
                <property name="suppressAllComments" value="true" />
            </commentGenerator>
            <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
            <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/taotao" userId="root"
                password="yezi">
            </jdbcConnection>
            <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 
                NUMERIC 类型解析为java.math.BigDecimal -->
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
    
            <!-- targetProject:生成PO类的位置 -->
            <javaModelGenerator targetPackage="com.taotao.pojo"
                targetProject=".src">
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false" />
                <!-- 从数据库返回的值被清理前后的空格 -->
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
            <!-- targetProject:mapper映射文件生成的位置 -->
            <sqlMapGenerator targetPackage="com.taotao.mapper" 
                targetProject=".src">
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false" />
            </sqlMapGenerator>
            <!-- targetPackage:mapper接口生成的位置 -->
            <javaClientGenerator type="XMLMAPPER"
                targetPackage="com.taotao.mapper" 
                targetProject=".src">
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false" />
            </javaClientGenerator>
            <!-- 指定数据库表 -->
            <table schema="" tableName="tb_content"></table>
            <table schema="" tableName="tb_content_category"></table>
            <table schema="" tableName="tb_item"></table>
            <table schema="" tableName="tb_item_cat"></table>
            <table schema="" tableName="tb_item_desc"></table>
            <table schema="" tableName="tb_item_param"></table>
            <table schema="" tableName="tb_item_param_item"></table>
            <table schema="" tableName="tb_order"></table>
            <table schema="" tableName="tb_order_item"></table>
            <table schema="" tableName="tb_order_shipping"></table>
            <table schema="" tableName="tb_user"></table>
    
        </context>
    </generatorConfiguration>

    我们打开src目录下的”GeneratorSqlmap.Java”类,该类有个main方法,我们运行这个main方法就可以自动生成代码,如下图所示。 

    我们刷新整个工程,自动生成的代码如下,可以看到接口、mapper文件、dao都成功生成了。 
     
    注意:逆向工程执行的时候,只要执行一遍就够了,不要执行两遍,执行两遍之后,它不会把原来的文件覆盖,而是会在原来文件的内容里面再追加。 
    下面我们要做的便是把自动生成的代码整合到我们的工程当中去,其中pojo很明显,我们应该放到pojo工程,我们直接复制com.taotao.pojo整个文件夹,然后粘贴到taotao-manager-pojo工程的src/main/java目录下,如下图所示。 
     
    mapper文件和dao都是操作数据库所必须的,很显然,它们都应该放到taotao-manager-dao工程的src/main/java目录下,如下图所示。 

  • 相关阅读:
    PHP之项目环境变量设置
    nginx相关服务实践
    模拟器的基本使用
    Redis常见问题汇总
    用OpenResty搭建高性能服务端
    Lua代码规范
    Lua之基础篇
    如何设计一个高性能短链系统?
    通过双 key 来解决缓存并发问题
    Golang常见问题汇总
  • 原文地址:https://www.cnblogs.com/telwanggs/p/6934083.html
Copyright © 2020-2023  润新知