SSM+maven+sqlserver+mybatis-generator逆向生成类

本文介绍如何利用mybatis-generator插件自动生成Spring+SpringMVC+MyBatis项目的持久层代码,包括POJO对象、Mapper接口及映射文件。

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

当数据库表单较多的时候,手动去添加类容易出错,而且会浪费很多时间。现在介绍一种可以自动生成类的方法。。。我的项目使用的是Spring+springmvc+mybatis这个框架,开发工具是eclips,数据库sqlserver。要自动生成持久层,首先要在pom.xml里面配置mybatis-generator的插件,配置数据库的驱动包,具体代码如下

<?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="C:\Users\lyj\Desktop\engineeringApp\app\lib\jtds-1.3.1.jar"/>
<!-- <classPathEntry location="C:\Users\lyj\.m2\repository\mysql\mysql-connector-java\5.1.35\mysql-connector-java-5.1.35.jar"/>  -->
    <context id="sqlserverTables" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>  
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> 
         <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin> 
        <commentGenerator>
            <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 -->
            <!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true -->
            <property name="suppressDate" value="true" />
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="false" />
        </commentGenerator>
        <!--数据库链接URL,用户名、密码 -->
       <!--  <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://192.168.7.36:3306/t_user" userId="root" password="123">
            </jdbcConnection> -->
            <jdbcConnection driverClass="net.sourceforge.jtds.jdbc.Driver"
            connectionURL="jdbc:jtds:sqlserver://192.168.1.9:1433;databaseName="test" userId="root" password="123456">
         </jdbcConnection>
        <javaTypeResolver>
            <!-- This property is used to specify whether MyBatis Generator should 
                force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, -->
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成模型的包名和位置 -->
        <javaModelGenerator targetPackage="com.lyj.pojo"
            targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置 -->
        <sqlMapGenerator targetPackage="com.lyj.mapping"
            targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.lyj.dao" implementationPackage="com.lyj.dao.impl"  targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 要生成哪些表 -->
        <table tableName="bd_bm" domainObjectName="MyBook"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="true" />
            <generatedKey column="Id" sqlStatement="SqlServer" type="post" identity="true"/>



            </table>
    </context>
</generatorConfiguration>

这里注释掉的部分是连接mysql数据库的。因为maven的仓库里面没有jtds-1.3.1.jar这个包,所有我将它下载到本地并且用

<classPathEntry location="C:\Users\lyj\Desktop\engineeringApp\app\lib\jtds-1.3.1.jar"/>

这个语句标注了jtds.jar的位置,这样,就能加载到sqlserver数据库的驱动了。其他的具体看一看注释,应该都可以明白。注意的是最后要生成的表的名字一定要写对。然后选中工程,右键,选择run,选择maven build 这里写图片描述

在如图所示的位置。Goals里面输入mybatis-generator:generate 然后点击run。如果显示
这里写图片描述

显示build success 就是项目构建成功了。然后刷新(按F5),就可以看到生成的pojo对象,mapper接口,mapping映射文件。可以根据需要选择是否添加注释。。
哦,还有一点,如果在生成持久层的时候,com.lyj.pojo这个包存在,就会直接在包下生成类,如果不存在,就会先新建一个com.lyj.pojo这个包,然后在包下生成类。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值