本人微信公众号"aeolian"~

MybatisGenerator生成SSM的dao层

官网下载

mybatis generator

下载generator的release版本mybatis-generator-core-1.4.0-bundle.zip

https://github.com/mybatis/generator/releases

mybatis generator

generator.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>  
      
    <classPathEntry  location="D:\workspace6_5\HjzzAuthPlatform\mysql-connector-java-5.1.22-bin.jar"/>  
    
    <context id="mysql"  targetRuntime="MyBatis3">  
        <commentGenerator>  
            <property name="suppressDate" value="true"/>  
              
            <property name="suppressAllComments" value="true"/>  
        commentGenerator>  
        
          
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" 
            connectionURL="jdbc:mysql://222.222.221.138:3306/authplatform?useUnicode\=true&characterEncoding\=UTF-8" 
          userId="root" password="root">  
        jdbcConnection>  
        
        <javaTypeResolver>  
            <property name="forceBigDecimals" value="false"/>  
        javaTypeResolver>  
          
        <javaModelGenerator targetPackage="com.autumn.model" targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
            <property name="trimStrings" value="true"/>  
        javaModelGenerator>  
          
        <sqlMapGenerator targetPackage="com.autumn.mapper" targetProject="src">
            <property name="enableSubPackages" value="true"/>  
        sqlMapGenerator>  
          
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.autumn.mapper" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        javaClientGenerator>
        
        
        
         
        <table tableName="account" domainObjectName="Account" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">table>
        <table tableName="auth_role" domainObjectName="Auth_role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">table>
        <table tableName="auth_system" domainObjectName="Auth_system" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="true">table>
        
    context>  
generatorConfiguration>

把下载的mybatis-generator-core-1.4.0.jar、generator.xml(需要指定mysql-connector-java-5.1.22-bin.jar)放入项目跟目录下面。

《MybatisGenerator生成SSM的dao层》

java命令

用java命令生成dao、mapper、pojo

java -jar mybatis-generator-core-1.4.0.jar -configfile generator.xml -overwrite

《MybatisGenerator生成SSM的dao层》

《MybatisGenerator生成SSM的dao层》

Maven

如果是maven,generator.xml放入项目下面,pom.xml中用如下配置,maven的targetProject和普通的myeclipse项目不一样,需要更改targetProject为src/main/java

      <dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-coreartifactId>
            <version>1.3.2version>
      dependency>
      <plugin>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-maven-pluginartifactId>
            <version>1.3.2version>
      plugin>

多次运行jar生成mapper后,接口类会被覆盖,mapper.xml会以追加的形式放进去。

IDEA插件(推荐)

Setting -> Plugins中安装mybatisGenerator。

《MybatisGenerator生成SSM的dao层》

 

把如下generator.xml放在项目根目录下面,使用插件可以不指定表名,只需配置数据库连接和生成的mapper与pojo路径即可。

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:\workspace6_5\HjzzAuthPlatform\mysql-connector-java-5.1.22-bin.jar"/>
    <context id="mysql"  targetRuntime="MyBatis3">  
        <commentGenerator>  
            <property name="suppressDate" value="true"/>  
              
            <property name="suppressAllComments" value="true"/>  
        commentGenerator>  
        
          
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" 
            connectionURL="jdbc:mysql://222.222.221.138:3306/authplatform?useUnicode\=true&characterEncoding\=UTF-8" 
          userId="root" password="root">  
        jdbcConnection>  
        
        <javaTypeResolver>  
            <property name="forceBigDecimals" value="false"/>  
        javaTypeResolver>  
          
        <javaModelGenerator targetPackage="com.autumn.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>  
            <property name="trimStrings" value="true"/>  
        javaModelGenerator>  
          
        <sqlMapGenerator targetPackage="com.autumn.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>  
        sqlMapGenerator>  
          
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.autumn.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        javaClientGenerator>

    context>  
generatorConfiguration>

右击generator.xml,点击MybatisGenerate后,选择需要逆向的表。确定即可。

《MybatisGenerator生成SSM的dao层》

其他注意事项

表一定不要忘记设主键,否则所有根据主键的方法都不会生成,忘记设主键生成的代码如下.

《MybatisGenerator生成SSM的dao层》

点赞

Leave a Reply

Your email address will not be published. Required fields are marked *