Mybatis Generator(逆向工程)

Mybatis Generator(逆向工程)

编程的一部分意义就在于消除人类的重复劳动,所以重复的编程天然就不该存在;

mybatis的逆向工程解决了利用ssm框架编写Javaweb项目时每次都要要机械重复的编写pojo、dao、mapper的问题。

mybatis逆向工程通过简单的配置就可以快速生成我们每次新建ssm项目时pojo、dao、mapper层的通用基础模板;通过简单的改写,我们可以就快速进入业务编写的工作;

重要提示:使用Mybats Generator时,mysql-connector-java版本不能用太新的,否则会出现错误,下面版本测试通过;

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.42</version>
</dependency>

 

 

使用

  1. 首先引入所需jar包

    maven项目在pom文件中引入:

    <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
    <dependency>
     <groupId>org.mybatis.generator</groupId>
     <artifactId>mybatis-generator-core</artifactId>
     <version>1.3.7</version>
    </dependency>
    ​

    普通web项目可以去这里下载所需jar包;

     

  2. 编写配置文件

    配置文件的编写,及关于逆向工程的其它问题可以参考Mybatis Generator的官方文档;下面摘取介绍一下常用配置(完整xml在文末)

    首先配置所连数据库

    <jdbcConnection
          driverClass="com.mysql.jdbc.Driver"
           connectionURL="jdbc:mysql://localhost:3306/testdatabase"
           userId="username"
           password="password">
    </jdbcConnection>

    配置与数据库中单表所对应的Java对象(po、domain)层

    这层怎么取名的都有,不止我上面及下面列出来的这几个,看个人习惯吧,下面是阿里发布的Java开发手册中的建议;

    •  DO(Data Object):此对象与数据库表结构一一对应,通过 DAO 层向上传输数据源对象。
    •  DTO(Data Transfer Object):数据传输对象,Service 或 Manager 向外传输的对象。
    •  BO(Business Object):业务对象,由 Service 层输出的封装业务逻辑的对象。
    •  AO(Application Object):应用对象,在 Web 层与 Service 层之间抽象的复用对象模型,极为贴
    近展示层,复用度不高。
    •  VO(View Object):显示层对象,通常是 Web 向模板渲染引擎层传输的对象。
    •  Query:数据查询对象,各层接收上层的查询请求。注意超过 2 个参数的查询封装,禁止使用 Map 类
    来传输。

     

    <!--./src/main/java 表示当前项目下的src/main/java(maven项目的默认结构),targetPackage则是指定包名  -->
    <javaModelGenerator targetPackage="hoimsys.pojo" targetProject="./src/main/java">
          <property name="enableSubPackages" value="true" />
          <property name="trimStrings" value="true" />
    </javaModelGenerator>

     

    配置dao层接口的生成位置

    <javaClientGenerator type="XMLMAPPER" 
                         targetPackage="hoimsys.dao"
                         targetProject="./src/main/java">
          <property name="enableSubPackages" value="true" />
    </javaClientGenerator>

    配置mapper文件生成位置

    <sqlMapGenerator targetPackage="mapper"  targetProject="./src/main/resources">
          <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

    配置每个表的生成策略

    <!-- 简单配置一下表名与生成类名就能用 -->
    <table tableName="department" domainObjectName="Department" ></table>

    至此,在配置文件中配置这么多就可以用了,随便在那个测试中运行以下代码,刷新项目即可真香;

    public static void main(String[] args) throws Exception {
             List<String> warnings = new ArrayList<String>();
               boolean overwrite = true;
               File configFile = new File("MBG.xml");//将你上面配置好的配置文件在这里给它
               ConfigurationParser cp = new ConfigurationParser(warnings);
               Configuration config = cp.parseConfiguration(configFile);
               DefaultShellCallback callback = new DefaultShellCallback(overwrite);
               MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
               myBatisGenerator.generate(null);
        }

    一些建议的可选配置

     <!-- 配置这个设置后生成的代码就会清爽很多(就不会生成那些看不懂的英文注释了...) -->
    <commentGenerator>
      <property name="suppressAllComments" value="true" />
    </commentGenerator>
     <!-- 生成toString方法) -->
    <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
     <!-- 不想要example类以及相关内容可以在table中配置以下属性 -->
    <table tableName="department" domainObjectName="Department" 
                    enableCountByExample="false" 
                    enableUpdateByExample="false"
                    enableDeleteByExample="false" 
                    enableSelectByExample="false"
                    selectByExampleQueryId="false"></table>
    

    最后

    如果有N张表,就会生成2N个PO,N个mapper.java以及N个mapper.xml,因为它除了常规的PO之外还生成了用于设置条件的xxxExample,不喜欢用删了就好,没事最好还是研究一下怎么用;

    以下测试MBG.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="DB2Tables" targetRuntime="MyBatis3">
     <!-- 生成toString方法) -->
    <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
    		<commentGenerator>
    			<!-- 配置这个设置后生成的代码就会清爽很多(就不会生成那些看不懂的英文注释了...) -->
    			<property name="suppressAllComments" value="true" />
    		</commentGenerator>
    		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
    			connectionURL="jdbc:mysql://localhost:3306/hoimsys" userId="root"
    			password="123456">
    		</jdbcConnection>
    
    		<javaTypeResolver>
    			<property name="forceBigDecimals" value="false" />
    		</javaTypeResolver>
    
    		<!--./src/main/java 表示当前项目下的src/main/java(maven项目的默认结构),targetPackage则是指定包名 -->
    		<javaModelGenerator targetPackage="hoimsys.pojo"
    			targetProject="./src/main/java">
    			<property name="enableSubPackages" value="true" />
    			<property name="trimStrings" value="true" />
    		</javaModelGenerator>
    
    		<sqlMapGenerator targetPackage="mapper"
    			targetProject="./src/main/resources">
    			<property name="enableSubPackages" value="true" />
    		</sqlMapGenerator>
    
    		<javaClientGenerator type="XMLMAPPER"
    			targetPackage="hoimsys.dao" targetProject="./src/main/java">
    			<property name="enableSubPackages" value="true" />
    		</javaClientGenerator>
    
    		<!-- 简单配置一下表名与生成类名就能用 -->
    		<table tableName="department" domainObjectName="Department"></table>
    		<table tableName="doctor" domainObjectName="Doctor"></table>
    		<table tableName="medicine" domainObjectName="Medicine"></table>
    		<table tableName="patient" domainObjectName="Patient"></table>
    		<table tableName="prescription" domainObjectName="Prescription"></table>
    		<table tableName="registration" domainObjectName="Registration"></table>
    		<table tableName="storage" domainObjectName="Storage"></table>
    		<table tableName="title" domainObjectName="Title"></table>
    
    	</context>
    
    </generatorConfiguration>

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
mybatis也能方向生成代码,能方向生成实体类(po)、mapper接口和Mapper接口映射文件,能减少我们代码的工作量。详细步骤如下 1、下载mybatis生成架包工具MyBatis_Generator_1.3.1.zip,解压架包把features、plugins文件夹下的架包分别拷贝到eclipse安装目录下的features、plugins文件夹。重启eclipse就行。 解压后图片如下: Eclipse路径如图: 拷贝替换如图: 2、创建generatorConfig.xml文件,安装好mybatis 就能创建generatorConfig.xml 3、配置generatorConfig.xml配置文件,详细如下 [html] view plain copy <?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 location="D:\\rep\\mysql\\mysql-connector-java\\5.1.19\\mysql-connector-java-5.1.19.jar" /> --> <classPathEntry location="D:\\repo\\com\\oracle\\ojdbc14\\10.2.0.1.0\\ojdbc14-10.2.0.1.0.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true" /> <property name="suppressDate" value="true" /> </commentGenerator> <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@xxx.xxx.xxx.xxx:1521:orcl4" userId="xxx" password="xxxx" /> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> </javaTypeResolver> <javaModelGenerator targetPackage="com.pcmall.domain.sale.order" targetProject="pos-service/src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="mybatis.mapper.sale.order" targetProject="pos-service/src/main/resources"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <javaClientGenerator targetPackage="com.pcmall.dao.sale.order" targetProject="pos-service/src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <table tableName="hs_zxzflx" enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" selectByExampleQueryId="true" enableUpdateByExample="false"> <!-- <generatedKey column="ID" sqlStatement="oracle" identity="true" /> --> </table> </context> </generatorConfiguration> 4、右击generatorConfig.xml 点击Generate MyBatis/iBATIS Artifacts 生成对应接口、接口映射文件、实体类 5、查看结果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值