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

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



<generatorConfiguration>
<classPathEntry location="C:\Users\lyj\Desktop\engineeringApp\app\lib\jtds-1.3.1.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>
            
            
            <property name="suppressDate" value="true" />
            
            <property name="suppressAllComments" value="false" />
        commentGenerator>
        
       
            <jdbcConnection driverClass="net.sourceforge.jtds.jdbc.Driver"
            connectionURL="jdbc:jtds:sqlserver://192.168.1.9:1433;databaseName="test" userId="root" password="123456">
         jdbcConnection>
        <javaTypeResolver>
            
            <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>
        
        <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这个包,所有我将它下载到本地并且用

\Users\lyj\Desktop\engineeringApp\app\lib\jtds-1.3.1.jar"/>

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

在如图所示的位置。Goals里面输入mybatis-generator:generate 然后点击run。如果显示
SSM+maven+sqlserver+mybatis-generator逆向生成类_第2张图片

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

你可能感兴趣的:(SSM+maven+sqlserver+mybatis-generator逆向生成类)