• mybatis怎么自动生成实体类,Mapper配置文件和Dao接口


    1.首先准备好jar包

    https://github.com/mybatis/generator/releases 下载MyBatis Generator  

    下载压缩包后,打开可以看到lib目录下有我们需要的jar包,添加到项目引用

    2.和Hibernate逆向生成一样,这里也需要一个配置文件:

    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>
        <!-- 数据库驱动包的位置,我这里是放到D盘的 -->      
      <classPathEntry location="D:mysql-connector-java-5.1.0-bin.jar" />      
          
      <context id="Mysql2Tables" targetRuntime="MyBatis3">    
      <!-- 数据库驱动,连接url,用户名,密码 -->  
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"      
            connectionURL="jdbc:mysql://192.168.144.100:3306/networkdept"      
            userId="root"      
            password="123456">      
        </jdbcConnection>      
          
        <javaTypeResolver >      
          <property name="forceBigDecimals" value="false" />      
        </javaTypeResolver>
              
          <!-- 生成实体类的包名和位置 -->   
        <javaModelGenerator targetPackage="cn.networkdepartment.pojo" targetProject="src">      
          <property name="enableSubPackages" value="true" />      
          <property name="trimStrings" value="true" />      
        </javaModelGenerator>      
          
          <!-- 生成dao接口的包名和位置 -->
        <sqlMapGenerator targetPackage="cn.networkdepartment.dao"  targetProject="src">      
          <property name="enableSubPackages" value="true" />      
        </sqlMapGenerator>   
           
          <!-- 生成的映射文件包名和位置 --> 
        <javaClientGenerator type="XMLMAPPER" targetPackage="cn.networkdepartment.dao"  targetProject="src">      
          <property name="enableSubPackages" value="true" />      
        </javaClientGenerator> 
        
         <!-- 要生成的那些表(更改tableName 和domainObjectName 就可以了) -->    
        <table schema="test" tableName="nd_class" domainObjectName="NClass" enableCountByExample="false" enableUpdateByExample="false"      
               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">      
        </table>
              
        <table schema="test" tableName="nd_integral" domainObjectName="Integral" enableCountByExample="false" enableUpdateByExample="false"      
           enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">      
       </table>
             
       <table schema="test" tableName="nd_log" domainObjectName="Nlog" enableCountByExample="false" enableUpdateByExample="false"      
           enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">      
       </table>
       
        <table schema="test" tableName="nd_maintain" domainObjectName="Maintain" enableCountByExample="false" enableUpdateByExample="false"      
           enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">      
       </table>
       
          <table schema="test" tableName="nd_member" domainObjectName="Member" enableCountByExample="false" enableUpdateByExample="false"      
           enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">      
       </table> 
          <table schema="test" tableName="nd_post" domainObjectName="Npost" enableCountByExample="false" enableUpdateByExample="false"      
           enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">      
       </table> 
          <table schema="test" tableName="nd_systemset" domainObjectName="Systemset" enableCountByExample="false" enableUpdateByExample="false"      
           enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">      
       </table>   
      </context>      
    </generatorConfiguration>  

     3.创建一个类通过main方法生成代码,运行这个类

    package test;  
    import java.io.File;    
    import java.io.IOException;    
    import java.sql.SQLException;    
    import java.util.ArrayList;    
    import java.util.List;    
        
    import org.mybatis.generator.api.MyBatisGenerator;    
    import org.mybatis.generator.config.Configuration;    
    import org.mybatis.generator.config.xml.ConfigurationParser;    
    import org.mybatis.generator.exception.InvalidConfigurationException;    
    import org.mybatis.generator.exception.XMLParserException;    
    import org.mybatis.generator.internal.DefaultShellCallback;    
    
    /**
     * 创建测试类,加载配置文件自动生成dao,Mapper文件
     * @author Bryce
     *
     */
    public class MybatisGeneratorUtil {    
        
        public static void main(String[] args) {    
            try {    
                System.out.println("start generator ...");    
                List<String> warnings = new ArrayList<String>();    
                boolean overwrite = true;    
                File configFile = new File(MybatisGeneratorUtil.class.getResource("/generator.xml").getFile());    
                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);    
                System.out.println("end generator!");    
            } catch (IOException e) {    
                e.printStackTrace();    
            } catch (XMLParserException e) {    
                e.printStackTrace();    
            } catch (InvalidConfigurationException e) {    
                e.printStackTrace();    
            } catch (SQLException e) {    
                e.printStackTrace();    
            } catch (InterruptedException e) {    
                e.printStackTrace();    
            }    
        }    
            
    }  

    然后就可以看到实体类和dao都生成了

  • 相关阅读:
    TP5 try{}catch{}异常捕获不到 解决办法
    layui2.5 开关在confirm确认了之后在关/开
    JQuery 表单textarea控制字数
    Navicat Premium从远程Mysql数据库复制到本地数据库的方法
    dedecmsV5.7 任意文件上传漏洞修复
    PHP 利用PHPExcel到处数据到Excel;还有导出数据乱码的解决方案。
    Mac Pro 2017款自带php与用brew重装PHP后的地址
    用js传递当前页面的url,丢失了&后面的参数 解决办法
    PHP 超全局变量之$_SERVER
    Linux while和for循环简单分析
  • 原文地址:https://www.cnblogs.com/sunmax/p/8310371.html
Copyright © 2020-2023  润新知