世界那么好,机会那么多

这里除了干货,什么都没有

mybatis-generator自动生成代码

1/ 在pom.xml里添加依赖

<!-- mbg mybatis-generator-core-->
    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.3.5</version>
    </dependency>

2/ mbg.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>
        <context id="DB2Tables" targetRuntime="MyBatis3">
            <!-- 不生成注释 -->
            <commentGenerator>
                <property name="suppressAllComments" value="true" />
            </commentGenerator>
            <!-- 配置数据库连接 -->
            <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/ssm" userId="root"
                password="root">
            </jdbcConnection>

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

            <!-- 指定javaBean生成的位置 -->
            <javaModelGenerator targetPackage="com.st.bean"
                targetProject=".\src\main\java">
                <property name="enableSubPackages" value="true" />
                <property name="trimStrings" value="true" />
            </javaModelGenerator>

            <!--指定sql映射文件 **Mapper.xml 生成的位置 .\src\main\resources -->
            <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
                <property name="enableSubPackages" value="true" />
            </sqlMapGenerator>

            <!-- 指定dao接口生成的位置,mapper接口 -->
            <javaClientGenerator type="XMLMAPPER"
                targetPackage="com.st.dao" targetProject=".\src\main\java">
                <property name="enableSubPackages" value="true" />
            </javaClientGenerator>

            <!-- table指定每个表的生成策略 -->
            <table tableName="user" domainObjectName="User"></table>
            
        </context>
    </generatorConfiguration>

3/ mbg运行文件

package com.st.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 MBGTest {

    public static void main(String[] args) throws Exception {

        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        //指定 逆向工程配置文件(mbg.xml配置文件放在项目根目录下)
        File configFile = new File("mbg.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);
    }

}

 

posted @ 2017-12-09 01:23  面向对象爱好者社区  阅读(358)  评论(0编辑  收藏  举报