用Mybatis自动生成代码

在校时的项目一般持久层框架都是用的Mybatis,最近做到一个数据属性稍多的一个项目,故查找了一下Mybatis自动生成代码的方法,现总结如下。

1.所需jar包

1.Mybatis的jar包
2.数据库连接的jar包
3.生成器的jar包

这里写图片描述

2.配置generator.xml文件

generator.xml是我们对生成规则进行配置的文件,在MySql数据库的各个表中,如果词之间用”_“或者”-“分隔,在自动生成时,后面第一个字母会自动转化成大写(驼峰)。其余的有关说明在注释中。其配置如下:

<?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:\Code\maven3.2.5\repository\mysql\mysql-connector-java\5.1.30\mysql-connector-java-5.1.30.jar"/>    
    <context id="MySQL2Tables"  targetRuntime="MyBatis3">    
        <commentGenerator>    
            <property name="suppressDate" value="true"/>    
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->    
            <property name="suppressAllComments" value="false"/>    
        </commentGenerator>    
        <!--数据库链接URL,用户名、密码 -->    
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/springmvc" userId="root" password=""></jdbcConnection>    
        <javaTypeResolver>    
            <property name="forceBigDecimals" value="false"/>    
        </javaTypeResolver>    
        <!-- 生成模型的包名和位置-->    
        <javaModelGenerator targetPackage="com.cie.entity" targetProject="src/main/java">    
            <property name="enableSubPackages" value="true"/>    
            <property name="trimStrings" value="true"/>    
        </javaModelGenerator>    
        <!-- 生成映射文件的包名和位置-->    
        <sqlMapGenerator targetPackage="mappers" targetProject="src/main/resources">    
            <property name="enableSubPackages" value="true"/>    
        </sqlMapGenerator>    
        <!-- 生成DAO的包名和位置-->    
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.cie.model.dao" targetProject="src/main/java">    
            <property name="enableSubPackages" value="true"/>    
        </javaClientGenerator>    
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
        <!-- tableName="%" 表示对所有的表都生成对应的文件 -->    
        <!-- <table tableName="%" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>   -->
        <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>  
        <table tableName="test" domainObjectName="Test" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>  
    </context>    
</generatorConfiguration>    

3.生成方法

完成配置文件之后,则根据配置文件生成相应的代码,生成的方法有两种。

1.进入上述截图中的目录,在该目录下运行命令行窗口,执行下面语句,则可以自动生成。

Java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml –overwrite

2.如果是用Maven构建的项目,则在POM.xml中加入插件依赖:

<!-- Mybatis自动生成实体、dao和mapper的插件 -->
      <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>

然后,将generatorConfig.xml放在resources目录中,目录结构如下:

这里写图片描述

右键项目——Run As——Maven Build在goals中输入mybatis-generator:generate,然后apply——run,刷新项目,即可看到生成的代码。

注意:生成的mapper文件一般都会以“***Dao”命名,习惯上改为“***mapper”。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值