MyBatis:由数据库自动生成实体类

需要的工具jar包

mybatis-generator-core-1.3.2.jar

mysql-connector-java-5.0.4.jar

第一步:编写一个MybatisGeneratorUtil类

<pre name="code" class="java">package com.test.utils;
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;  
  
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();  
        }  
    }  
      
}


 
第二步:编写一个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:\mysql-connector-java-5.0.4.jar" />    
    
  <context id="Mysql2Tables" targetRuntime="MyBatis3">    
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"    
        connectionURL="jdbc:mysql://localhost:3306/datebase"    
        userId="root"    
        password="root">    
    </jdbcConnection>    
    
    <javaTypeResolver >    
      <property name="forceBigDecimals" value="false" />    
    </javaTypeResolver>    
    
    <javaModelGenerator targetPackage="com.test.model" targetProject="src">    
      <property name="enableSubPackages" value="true" />    
      <property name="trimStrings" value="true" />    
    </javaModelGenerator>    
    
    <sqlMapGenerator targetPackage="com.test.mapper"  targetProject="src">    
      <property name="enableSubPackages" value="true" />    
    </sqlMapGenerator>    
    
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.test.dao"  targetProject="src">    
      <property name="enableSubPackages" value="true" />    
    </javaClientGenerator>    
    <table schema="test" tableName="t_student" domainObjectName="Tstudent" enableCountByExample="false" enableUpdateByExample="false"    
           enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">    
    </table>    
    <table schema="test" tableName="t_class" domainObjectName="Tclass" enableCountByExample="false" enableUpdateByExample="false"    
       enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">    
   </table>    
   <table schema="test" tableName="t_stu_class" domainObjectName="Tstclass" enableCountByExample="false" enableUpdateByExample="false"    
       enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">    
   </table>  
  </context>    
</generatorConfiguration> 
其中<table>中的tableName代表数据库中的表名、domainObjectName表示你要生成的实体类的名字;

<classPathEntry>中的location代表你的mysql-connector-java-5.0.4.jar地址;

targetPackage便是你要生成的文件的路径.



生成之后就是这样子

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值