在pom文件中添加generator插件和代码生成的配置文件
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!-- 加载mybatis用于生成代码的配置文件,这个文件要在这个路径下 -->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
generatorConfig.xm:如下
generatorConfig自动生成配置文件,按照注释修改成自己本地的参数即可
<?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>
<properties resource="application.properties"></properties>
<!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包的地址 要改-->
<classPathEntry location="D:\Mysql\mysql-connector-java-5.0.8-bin.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 要改-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="${spring.datasource.url}" userId="${spring.datasource.username}" password="${spring.datasource.password}">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成实体类的包名和位置 要改-->
<javaModelGenerator targetPackage="com.ahav.meeting_details.pojo" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成sql映射文件的包名和位置 要改-->
<sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成mapper接口的包名和位置 要改-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.ahav.meeting_details.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名 要改-->
<table tableName="equipment_tab" domainObjectName="EquipmentTab" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="meeting_details_tab" domainObjectName="MeetingDetailsTab" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="meetingroom_tab" domainObjectName="MeetingroomTab" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
配置文件的修改
#指定xml映射路径,classpath代表在resources包下
mybatis.mapper-locations=classpath:mapping/*.xml
#给这个包下的类起别名
mybatis.type-aliases-package=com.ahav.reserve.pojo,com.ahav.system.entity,com.ahav.email.pojo
运行generator根据数据库表自动加载,实体类,mapper接口,mapper.xml文件,步骤如下
命令:mybatis-generator:generate -e
运行插件!
此时mapper接口,pojo,mapper映射文件已经生成!
对mapper接口添加该注解,@Mapper表示这个接口由mybatis实现,@Repositort表示将这个mapper交给spring管理。