Mybatis的功能之一:数据库表自动生成对应的model类,dao接口和Mapper.xml文件

步骤:

下载地址:https://github.com/mybatis/generator/releases

官网:http://www.mybatis.org/generator/index.html

1. 导入 jar 包,逆向工程和数据库链接

2. 复制粘贴 generatorConfig.xml

3. 修改内容:

3.1 数据库链接

3.2 包,共有三个:model,interface,mapper

3.3 表名字,使用 5 个 false

4. 复制粘贴 Test 内容

5. run as java application

6. 项目名 右键 refresh

示例图片:

Mybatis的功能之一:数据库表自动生成对应的model类,dao接口和Mapper.xml文件_第1张图片

1. generatorConfig.xml 文件代码:




<generatorConfiguration>
    <context id="testTables" targetRuntime="MyBatis3">
        <commentGenerator>
            
            <property name="suppressAllComments" value="true" />
        commentGenerator>
        
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/zyonlineforum" userId="root"
            password="123456">
        jdbcConnection>

        
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        javaTypeResolver>

        ****
        <javaModelGenerator targetPackage="com.zhiyou100.model"
            targetProject=".\src">

            
            <property name="enableSubPackages" value="false" />

            
            <property name="trimStrings" value="true" />
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="com.zhiyou100.mapper"
            targetProject=".\src">
            
            <property name="enableSubPackages" value="false" />
        sqlMapGenerator>
        
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.zhiyou100.mapper" targetProject=".\src">
            
            <property name="enableSubPackages" value="false" />
        javaClientGenerator>
        
        <table tableName="topics" enableCountByExample="false"
            enableUpdateByExample="false" enableDeleteByExample="false"
            enableSelectByExample="false" selectByExampleQueryId="false">table>

        <table tableName="reply" enableCountByExample="false"
            enableUpdateByExample="false" enableDeleteByExample="false"
            enableSelectByExample="false" selectByExampleQueryId="false">table>

        <table tableName="category" enableCountByExample="false"
            enableUpdateByExample="false" enableDeleteByExample="false"
            enableSelectByExample="false" selectByExampleQueryId="false">table>

        <table tableName="user" enableCountByExample="false"
            enableUpdateByExample="false" enableDeleteByExample="false"
            enableSelectByExample="false" selectByExampleQueryId="false">
        table>


        
    context>
generatorConfiguration>

2. Test.java文件: 运行即可

@JAVA
package com.zhiyou100.test;

import java.io.File;
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.internal.DefaultShellCallback;

public class Test {

    public static void generator() throws Exception{

        List warnings = new ArrayList();
        boolean overwrite = true;
        //指定逆向工程配置文件
        File configFile = new File("src/generatorConfig.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);
    } 

    public static void main(String[] args) throws Exception {
        try {
            generator();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

你可能感兴趣的:(mybatis)