mybatis自动生成代码

来源:互联网 发布:华为查看端口 编辑:程序博客网 时间:2024/06/07 20:07

mybatis是一种半自动的ORM框架,灵活的配置让我们能够更好的和数据库进行交互,但是如果手写大量的配置信息很容易出错,并且很难被发现,不过还好,mybatis的映射文件可以通过工具自动生成,这样可以省去很多时间,完事了将代码直接copy进项目即可。

以下是我使用mybatis生成代码的过程

首先是下载几个生成代码的jar包和配置文件,具体如下所示:


资源免费下载地址:http://download.csdn.net/detail/java_mdzy/9911618


具体配置文件

<?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:\generator\mysql-connector-java-5.1.34.jar" /> <!-- <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />--><context id="DB2Tables" targetRuntime="MyBatis3"><commentGenerator><property name="suppressAllComments" value="true" /></commentGenerator><!-- 数据库链接URL、用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/databaseName?characterEncoding=utf8" userId="root" password="root"> <!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">--></jdbcConnection><javaTypeResolver><property name="forceBigDecimals" value="false" /></javaTypeResolver><!-- 生成模型的包名和位置 --><javaModelGenerator targetPackage="medo.model" targetProject="D:\generator\src"><property name="enableSubPackages" value="true" /><property name="trimStrings" value="true" /></javaModelGenerator><!-- 生成的映射文件包名和位置 --><sqlMapGenerator targetPackage="medo.mapping" targetProject="D:\generator\src"><property name="enableSubPackages" value="true" /></sqlMapGenerator><!-- 生成DAO的包名和位置 --><javaClientGenerator type="XMLMAPPER" targetPackage="medo.dao" targetProject="D:\generator\src"><property name="enableSubPackages" value="true" /></javaClientGenerator><!-- 要生成那些表(更改tableName和domainObjectName就可以) --><table tableName="tb_pro_info" domainObjectName="ProjectInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /><!-- <table tableName="course_info" domainObjectName="CourseInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /><table tableName="course_user_info" domainObjectName="CourseUserInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> --></context></generatorConfiguration>


然后按住win+r--->cmd进入命令提示符界面,进入jar包文件夹目录,运行命令java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite,即可生成代码


原创粉丝点击