1.所需要的jar mybatis-generator-core-1.3.1-sources.jar,配置文件 generator.xml,和对应的数据库驱动包。
2.下面我就以mysql数据库为类。
3.创建好我的数据库名字为:mybatis,创建表为
CREATE TABLE tuser(
id INT PRIMARY KEY,
NAME VARCHAR(10),
PASSWORD VARCHAR(100)
)
4.新建一个src文件
5.在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:数据库驱动包的位置--> <classPathEntry location="E:\mybatis-tool\mysql-connector-java-5.1.7-bin.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3"><commentGenerator> <property name="suppressAllComments" value="true" /></commentGenerator>
<!--数据连接的配置--><jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/mybatis" userId="root" password="root"> </jdbcConnection>
<javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver><!-- targetProject:项目生成的位置--> <javaModelGenerator targetPackage="com.zhlk.model" targetProject="E:\mybatis-tool\src\"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator>
<sqlMapGenerator targetPackage="com.zhlk.mappers" targetProject="E:\mybatis-tool\src\"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.zhlk.dao" targetProject="E:\mybatis-tool\src\"> <property name="enableSubPackages" value="true" /> </javaClientGenerator><!-- tableName:表名字domainObjectName:类名 -->
<table tableName="tuser" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table>
</context> </generatorConfiguration>
6.cmd下切换到mybatis-generator-core-1.3.1.jar所在的路径
运行命令:java -jar E:\mybatis-tool\mybatis-generator-core-1.3.1.jar -configfile E:\mybatis-tool\generator.xml -overwrite
7.运行成功如下: