mybatis- generator代码生成配置及达梦的代码生成问题

本文介绍了如何配置Mybatis Generator进行代码生成,包括在pom.xml中的设置,修改generatorConfig.xml中的数据库连接信息和目标包名。针对达梦数据库,总结了代码生成时只有insert和insertSelective方法的问题,原因可能为数据库模式不正确或表缺少主键。

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

1、pom 配置

 <!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
 <!--mybatis-generator所需依赖-->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
        </dependency>
	<!-- 编译打包配置(mybatis generator配置以及将本地jar包打入目标项目jar包配置)-->
 <build>
        <plugins>
            <!--mybatis-generator运行-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <!--待会要创建的generator配置文件,但是绝对不需要!!!有了它反而会报错!-->
                    <!--<configurationFile>generatorConfig.xml</configurationFile>-->
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.2</version>
                    </dependency>
                </dependencies>
            </plugin>
            <!--maven 打jar包-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                    <!--打包本地包-->
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
        </plugins>
    </build>

2、generatorConfig.xml文件配置(放在resource文件夹下)

2.1、仅需修改下面mybatis generator文件的几处地址即可使用

a、改为自己本地的驱动包

<!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
    <classPathEntry location="D:\DmJdbcDriver18.jar"/>

b、改数据库名与密码为自己的数据库名与密码

<!--数据库驱动类,数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="dm.jdbc.driver.DmDriver"
                        connectionURL="jdbc:dm://localhost:5236?serverTimezone=UTC&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf-8"
                        userId="数据库用户名"
                        password="数据库密码">
            <!--MySQL不支持 schema或者catalog-->
            <property name="nullCatalogMeansCurrent" value="true"/>
        </jdbcConnection>

c、将targetPackage中的值改为自己的包名

<!-- 生成模型(pojo)的包名和位置-->
        <javaModelGenerator targetPackage="com.cetc.entity.po" targetProject="src/main/java">
       
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成映射文件的包名和位置(*.mapper文件)-->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成mapper位置(DAO层)-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.cetc.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

d、tableName值改为自己的数据库里表名

 <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
        <table tableName="SYS_RESOURCE"
               domainObjectName="SysResourceDO"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table tableName="TABLE2"
               domainObjectName="Table2"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>

2.2、下文件为全文件

<?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="D:\DmJdbcDriver18.jar"/>
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--mybatis generator 实现达梦的代码生成时,jdbc信息要写表对应的用户的userId与pwd,不能写SYSDBA-->
        <!--数据库驱动类,数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="dm.jdbc.driver.DmDriver"
                        connectionURL="jdbc:dm://localhost:5236?serverTimezone=UTC&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf-8"
                        userId="数据库用户名"
                        password="数据库密码">
            <!--MySQL不支持 schema或者catalog-->
            <property name="nullCatalogMeansCurrent" value="true"/>
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成模型(pojo)的包名和位置-->
        <javaModelGenerator targetPackage="com.cetc.entity.po" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成映射文件的包名和位置(*.mapper文件)-->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成mapper位置(DAO层)-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.cetc.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
        <table tableName="SYS_RESOURCE"
               domainObjectName="SysResourceDO"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table tableName="TABLE2"
               domainObjectName="Table2"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>

3、点击IDEA右侧的 plugins中的mybatis-generator即可等待生成代码

点击IDEA右侧的 plugins中的mybatis-generator即可等待生成代码

4、mybatis-generator 达梦代码生成问题总结

4.1、生成的方法只有insert与inserSelective

a、jdbcConnection中数据库名不是该表原本所属的模式或者用户

如数据库中名为TEST模式下有个表TABLE1,要对生成该表的CRUD代码,但是mybatsiGenerator.xml中 userId写着SYSDBA,密码也写着SYSDBA或者类似的其他用户名而不是 TEST以及对应的密码

 <jdbcConnection driverClass="dm.jdbc.driver.DmDriver"
                        connectionURL="jdbc:dm://localhost:5236?serverTimezone=UTC&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf-8"
                        userId="SYSDBA"
                        password="SYSDBA">
            <!--MySQL不支持 schema或者catalog-->
            <property name="nullCatalogMeansCurrent" value="true"/>
        </jdbcConnection>

b、要生成的CRUD对应的表没有主键(嗯。。挺好玩的…)

——————暂时先如此。。。TO BE CONTINUED

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值