Java小白的数据库爱情(九)mybatis 逆向工程

本文介绍了mybatis-generator,这是一款用于Java的自动代码生成工具,能够帮助开发者快速生成pojo、mapper及xml文件。通过配置generatorConfig.xml,可以轻松实现数据库模型的自动化创建。
摘要由CSDN通过智能技术生成

Java小白的数据库爱情(九)mybatis 逆向工程

mybatis-generator是一款mybatis自动代码生成工具,可以 通过配置,快速生成pojo,mapper和xml文件.

官方网址:http://mybatis.org/generator/configreference/xmlconfig.html

generatorConfig.xml配置 需添加到资源包下 src下

<?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="Tables" targetRuntime="MyBatis3">
        <!-- 生成的Java文件的编码 -->
        <property name="javaFileEncoding" value="UTF-8" />
        <!-- JavaBean 实现 序列化 接口 -->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
        <commentGenerator>
            <property name="suppressDate" value="false" />
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="false" />
        </commentGenerator>
        <!--数据库链接地址账号密码 -->
        <jdbcConnection
                driverClass="oracle.jdbc.driver.OracleDriver"
                connectionURL="jdbc:oracle:thin:@localhost:1521:xe"
                userId="SCOTT"
                password="TIGER">
        </jdbcConnection>
        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true 时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!--生成 Model 类存放位置 -->
        <javaModelGenerator targetPackage="com.xxxx.pojo"
                            targetProject="D:\yinweijava\java43\workspace\mybatis_generator\src">
            <!--构造器-->
            <property name="constructorBased" value="true"/>
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--生成映射文件存放位置 -->
        <sqlMapGenerator targetPackage="com.xxxx.mappers"
                         targetProject="D:\yinweijava\java43\workspace\mybatis_generator\src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!--生成接口类存放位置(Dao|Mapper) -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.xxxx.mappers" targetProject="D:\yinweijava\java43\workspace\mybatis_generator\src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 参考数据库中需要生成的对应表信息 -->
        <table tableName="dept" domainObjectName="Dept"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
            <columnOverride column="deptno" javaType="Integer"/>
        </table>
        <table tableName="emp" domainObjectName="Emp"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
        <table tableName="t_user" domainObjectName="User"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

生成需要的文件

public class Util {

    public static void main(String[] args) throws Exception {
        List<String> warnings = new ArrayList<>();
        boolean overwrite = true;
        try {
            //指定 逆向工程配置文件
            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);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值