操作工具:eclipse
准备工作:在resources目录下增加generatorConfig.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>
<!-- 制度MySQL驱动包的路径,别放在中文路径xia -->
<classPathEntry
location="E:\dormitory\dormitory_maven\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar" />
<context id="context1">
<commentGenerator>
<!--是否取消注销 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/parkingdb?characterEncoding=utf-8"
userId="root" password="123456">
</jdbcConnection>
<!-- 所生成的实体类的位置默认资源包src -->
<javaModelGenerator
targetPackage="com.st.eleventh.model" targetProject="src/main/java">
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.st.eleventh.user.model, true:com.st.eleventh.user.model.[schemaName] -->
<property name="enableSubPackages" value="false" />
<!-- 是否针对string类型的字段在set的时候进行trim调用 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--所生成的sqlMapper的映射文件位置,默认资源包src -->
<sqlMapGenerator
targetPackage="com.st.eleventh.dao.mapper.parking"
targetProject="src/main/resources">
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.st.eleventh.user.model, true:com.st.eleventh.user.model.[schemaName] -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!--配置到接口生成信息 -->
<javaClientGenerator
targetPackage="com.st.eleventh.dao" type="XMLMAPPER"
targetProject="src/main/java">
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.st.eleventh.user.model, true:com.st.eleventh.user.model.[schemaName] -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 配置表信息 -->
<table schema="parkingdb" tableName="t_cars"
domainObjectName="ParkingCars" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true" />
</table>
<table schema="parkingdb" tableName="t_ctms"
domainObjectName="Customer" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true" />
</table>
<table schema="parkingdb" tableName="t_disobey_record"
domainObjectName="PayRecord" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true" />
</table>
<table schema="parkingdb" tableName="t_group"
domainObjectName="ParkGroup" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true" />
</table>
<table schema="parkingdb" tableName="t_monthly_report"
domainObjectName="MthReport" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true" />
</table>
<table schema="parkingdb" tableName="t_parecord"
domainObjectName="ParkRecord" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true" />
</table>
<table schema="parkingdb" tableName="t_parking"
domainObjectName="Parking" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true" />
</table>
<table schema="parkingdb" tableName="t_space"
domainObjectName="Space" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true" />
</table>
<table schema="parkingdb" tableName="t_worker"
domainObjectName="Worker" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true" />
</table>
</context>
</generatorConfiguration>
2.在pom.xml增加相应数据库依赖,和在build中加入mybatis插件。
<!-- 添加mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- 添加mybatis/spring整合包依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!-- 添加mysql驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-driver.version}</version>
</dependency>
<build>
<plugins>
<!--mybatis逆向工程 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
3.右键点击项目名--》Run As-->Maven build-->在Goals中添加mybatis-generator:generate,点击apply然后run。运行完后刷新项目,就有自动生成的代码了。
注:如果刷新后没有自动生成代码,或者run时console控制台报错,请检查:
1.generatorConfig.xml中你mysql驱动包的路径是否正确
2.generatorConfig.xml中你数据库连接信息,数据库名是否正确
3.pom.xml中是否加入了要用到的依赖包,加入了可以右键点击项目名-->Maven-->Update Project进行更新下。
步骤图:
我的座右铭:不会,我可以学;落后,我可以追赶;跌倒,我可以站起来;我一定行。