初识MyBatis-Generator
详细请见: http://www.mybatis.org/generator/quickstart.html
使用mybatis-generator-core-x.x.x.jar加上配置文件来生成
1. 建立配置文件D:\temp\test.xml (详细配置见官网)
说明:connectionURL中jdbc:sqlserver://192.168.xx.xx:端口;databaseName=WicherTest
用的sqlserver, 192.168.xx.xx:端口表示sqlserver服务端所在的ip及侦听的端口
WicherTest为数据库名,记得 databaseName关键字与前面的分隔符是分号(; ),自己有次写错了,结果。。。
//*******************************************************************
<?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节点没有任何属性-->
<generatorConfiguration>
<!--数据库驱动-->
<classPathEntry location="C:/Users/wicher/.m2/repository/com/microsoft/sqlserver/mssql-jdbc/6.2.2.jre8/mssql-jdbc-6.2.2.jre8.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
connectionURL="jdbc:sqlserver://192.168.xx.xx:端口;databaseName=WicherTest"
userId="wicher"
password="wicherpasswd">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model类存放位置-->
<javaModelGenerator targetPackage="model" targetProject="D:\WorkTest\wicherspringmfcfirst\src\main\java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="mapping" targetProject="D:\WorkTest\wicherspringmfcfirst\src\main\java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="dao" targetProject="D:\WorkTest\wicherspringmfcfirst\src\main\java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名-->
<table tableName="user" domainObjectName="userModel"
enableCountByExample = "false"
enableUpdateByExample = "false"
enableDeleteByExample = "false"
enableSelectByExample = "false"
selectByExampleQueryId = "false">
</table>
</context>
</generatorConfiguration>
//*******************************************************************
2. 下载mybatis-generator-core-1.3.6.jar包 (通过maven下载的,使用的版本为 1.3.6)
路径为: C:\Users\wicher\.m2\repository\org\mybatis\generator\mybatis-generator-core\1.3.6
maven的pom.xml添加了
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
</dependency>
3. 执行DOS命令
java -jar C:\Users\wicher\.m2\repository\org\mybatis\generator\mybatis-generator-core\1.3.6\mybatis-generator-core-1.3.6.jar -configfile D:\temp\test.xml -overwrite
生成的文件就在 D:\WorkTest\wicherspringmfcfirst\src\main\java下,要留意执行结果