jee、spring、spring mvc、mybatis 学习(七)

    xiaoxiao2026-08-15  6

    mybatis-generator 使用说明

    转载请申明出处:http://blog.csdn.net/qq5132834/article/details/52222626

    http://www.cnblogs.com/linjiqin/p/3407047.html

    第六节开始涉及数据库了,但只是最简单的JDBC数据库直连。从这节开始讲【mybatis】了。不过在此之前先要介绍一个mybatis的必用工具【mybatis-generator】,这个工具的主要目的是将数据库表字段转成对应的【xxx.java】类、【xxxExample.java】类、【xxxMapper.xml】文件。工具的下载地址:http://download.csdn.net/download/qq5132834/5977673   。

    1、下载上述链接内容,解压到D盘根目录上,可以看到一个xml文件【generator.xml】,内容如下:

    <?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:\mybatis-generator-core-1.3.2\lib\mysq-connector-java-5.1.8-bin.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?characterEncoding=utf8" userId="root" password="root"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <javaModelGenerator targetPackage="com.soft.model" targetProject="D:\mybatis-generator-core-1.3.2\src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="sqlmap" targetProject="D:\mybatis-generator-core-1.3.2\src"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.soft.mapping" targetProject="D:\mybatis-generator-core-1.3.2\src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table tableName="login" domainObjectName="Login" > </table> </context> </generatorConfiguration> 注意其中三处数据:

    驱动包

    <classPathEntry location="D:\mybatis-generator-core-1.3.2\lib\mysq-connector-java-5.1.8-bin.jar" /> 数据库连接 <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?characterEncoding=utf8" userId="root" password="root"> </jdbcConnection>

    数据表

    <table tableName="login" domainObjectName="Login" >

    使用说明,在DOS命令行中运行:

    这个可以将数据库中的表直接转变成类的工具,只需要将包解压放到D盘的根目录下即可使用,然后设置generator.xml中的数据库连接方法。 1、驱动包 <classPathEntry location="D:\mybatis-generator-core-1.3.2\lib\mysq-connector-java-5.1.8-bin.jar" /> 2、数据库连接包 <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?characterEncoding=utf8" userId="root" password="root"> </jdbcConnection> 3、设置Table table的其他属性 enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" 4、在dos中运行 java -jar D:\mybatis-generator-core-1.3.2\lib\mybatis-generator-core-1.3.2.jar -configfile D:\mybatis-generator-core-1.3.2\generator.xml -overwrite

    在【mysql】的【test】数据库中新建【login】表。然后在【dos】中运行【mybatis-generator】工具。会产生对应java类和xml文件。

    1、产生xml资源文件:产生的路径:D:\mybatis-generator-core-1.3.2\src\sqlmap,注意xml文件路径:src\sqlmap

    2、产生mapping类,产生的路径是:D:\mybatis-generator-core-1.3.2\src\com\soft\mapping,注意类路径:src\com\soft\mapping

    3、产生model类,产生的路径是:D:\mybatis-generator-core-1.3.2\src\com\soft\model,注意是:src\com\soft\model

    4、综上产生的类以及资源路径,在【ZZZ】项目的根类【src】目录下新建三个包:

    4.1、【com.soft.model】

    4.2、【com.soft.mapping 】

    4.3、【sqlmap】

    然后将【mybatis-generator】工具产生类和xml资源复制对应的包中便可。

    转载请注明原文地址: https://ju.6miu.com/read-1311198.html
    最新回复(0)