Mybatis-逆向工程:自动生成代码

Mybatis Generator:

  简称MBG,是一个专门为Mybatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件、接口、以及bean类。支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储过程等复杂sql的定义不支持自动生成;

官方文档地址:http://www.mybatis.org/generator

官方工程地质:https://github.com/mybatis/generator/releases

一、配置

mbg.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?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="E:\Workspaces\MyEclipse10\CodeGenerator\WebRoot\WEB-INF\lib\jtds-1.2.jar" />
 
    <!-- id随便起没什么意义<br>     targetRuntime : MyBatis3Simple -- 生成简单版的CRUD 脚本sql<br>                        MyBatis3 -- 生成带动态sql脚本<br>     -->
    <context id="contextOne" targetRuntime="MyBatis3" defaultModelType="conditional">
 
        <!-- 控制生成注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
            <property name="suppressDate" value="true" />
        </commentGenerator>
 
        <!-- ORACLE 的连接串-->
        <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
            connectionURL="jdbc:oracle:thin:@192.168.1.24:1521:ORCL"
            userId="root"
            password="123">
        </jdbcConnection>
 
 
        <!-- MSSQL2008 -->
        <!--<jdbcConnection driverClass="net.sourceforge.jtds.jdbc.Driver"-->
                        <!--connectionURL="jdbc:jtds:sqlserver://124.95.165.217/ZhengFuBan" userId="sa"-->
                        <!--password="sql2008!@#">-->
        <!--</jdbcConnection>-->
 
        <javaTypeResolver type="com.code.generator.Convert" />
 
        <!-- 实体类pojo的生成策略  (targetPackage="com.framework.code.domain"这里是生成的包名,要和程序实际包名一致)
        targetProject:目标工程
        -->
        <javaModelGenerator targetPackage="com.framework.code.domain" targetProject=".\src">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
 
        <!-- mapper.xml (targetPackage="com.framework.code.domain"这里是生成的包名,要和程序实际包名一致)
           targetProject:目标工程
        -->
        <!--sqlMapGenerator: sql映射的生成策略-->
        <sqlMapGenerator targetPackage="com.framework.code.mapper" targetProject=".\conf" >
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
 
        <!-- javaClientGenerator: mapper接口所在位置 (targetPackage="com.framework.code.domain"这里是生成的包名,要和程序实际包名一致) -->
        <javaClientGenerator targetPackage="com.framework.code.mapper" targetProject=".\src" type="XMLMAPPER" >
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
 
        <!--  指定要逆向分析哪些数据库表名,根据表要创建javaBean -->
        <table tableName="tab_dept" domainObjectName="Department"></table>
        <table tableName="tab_employee" domainObjectName="Employee"></table>
    </context>
</generatorConfiguration>

 

  运行XML:

1
2
3
4
5
6
7
8
9
10
public void testMbg(){
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        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 @   IT6889  阅读(142)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示