逆向工程 (Java+Oracle框架--mybatis)

本文介绍了如何运用mybatis-generator进行Java逆向工程操作,通过配置generatorConfig.xml,可以自动化生成POJO、Mapper接口及Mapper.xml文件,大大提高了开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

逆向工程

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

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="true" />
        </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="E:\码歌\码歌Oracle\day25\002_code\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="E:\码歌\码歌Oracle\day25\002_code\mybatis_generator\src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!--生成接口类存放位置(Dao|Mapper) -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.xxxx.mappers"
                             targetProject="E:\码歌\码歌Oracle\day25\002_code\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>
package test;


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;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值