1.使用逆向生成工具生成dao,xml,pojo
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
</dependencies>
<configuration>
<configurationFile>generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
工具类
<?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>
<!--连接数据库jdbc驱动包的位置-->
<!--E:\MavenRepository\mysql\mysql-connector-java\8.0.26-->
<classPathEntry location="E:\MavenRepository\mysql\mysql-connector-java\8.0.26\mysql-connector-java-8.0.26.jar"/>
<!--配置table表信息内容, targetRuntime指定采用Mybatis3的版本-->
<context id="tables" targetRuntime="Mybatis3">
<!--<commentGenerator>
<!– 去除自动生成的注释 –>
<property name="suppressAllComments" value="true" />
</commentGenerator>-->
<!-- 是否生成注释 去除自动生成的注释-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 数据库连接配置 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/springboot?serverTimezone=Asia/Shanghai"
userId="root" password="root" />
<!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--配置生成的实体包
targetPackage:生成的实体包位置,默认存放在src目录下
targetProject:目标工程名
-->
<!--生成pojo类, targetPackage指定pojo类的包名, targetProject指定生成的pojo放在哪个工程下-->
<javaModelGenerator targetPackage="com.example.pojo" targetProject="src/main/java" />
<!--生成Mybatis的Mapper.xml文件,targetPackage指定mapper.xml的包名-->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources" />
<!--生成Mapper接口类文件-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 配置表
schema:不用填写
tableName: 表名
enableCountByExample、enableSelectByExample、enableDeleteByExample、enableUpdateByExample、selectByExampleQueryId:
去除自动生成的例子
-->
<!--tableName:数据库的表名
domainObjectName: 实体类名-->
<table schema=""
tableName="t_student"
domainObjectName="Student"
enableCountByExample="false"
enableSelectByExample="false"
enableDeleteByExample="false"
enableUpdateByExample="false"
selectByExampleQueryId="false" >
</table>
</context>
</generatorConfiguration>
2.生成的mapper位置不同
- mapper接口和xml文件在src/java目录下
<1> @mapper(写在mapper接口类中)或者@mapperScan(在Application启动类)注解
<2> pom文件:
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>*/*.xml</include>
</includes>
</resource>
</resources>
- 接口在src/java下, xml在resource下
<1> 在yml文件:
mybatis-mapper-location: classpath:mapper/*.xml