1、生成數據庫數據
2、配置pom文件
這個plugin文件里有配置項和依賴以及版本號
修改configurationFile路徑為項目里存在的generatorConfig.xml文件,因為后續的配置都在這個文件中進行。
<plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.7</version><configuration><configurationFile>E:\Java\java初階學習\JavaCode\java-learning\others\MybatisPlusTest\src\main\resources\generator\generatorConfig.xml</configurationFile><overwrite>true</overwrite><verbose>true</verbose></configuration><dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency></dependencies></plugin>
3、配置generatorConfig.xml文件
這里面有幾個地方需要自己進行修改。
- 驅動包的路徑,要配置成自己電腦里的連接數據庫的驅動包
- 連接配置,需要自己輸入數據庫的id和密碼
- 實體類生成的位置:需要在指定的目錄下創包
- mapper.xml生成的位置:需要在指定的目錄下創包
- DAO生成的位置:需要在指定的目錄下創包
- 配置數據庫中的表,table_name是數據庫中表的名字,domainObjectName是對應的java中對象的名字(需修改或新建)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration><!-- 驅動包路徑,location中路徑替換成??本地路徑 --><classPathEntry location="C:\environment\mysql-connector-java-5.1.49"/><context id="DB2Tables" targetRuntime="MyBatis3"><!-- 禁??動?成的注釋 --><commentGenerator><property name="suppressAllComments" value="true"/><property name="suppressDate" value="true"/></commentGenerator><!-- 連接配置 --><jdbcConnection driverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://127.0.0.1:3306/forum_db?characterEncoding=utf8&useSSL=false"userId="root"password=""></jdbcConnection><javaTypeResolver><!-- ?數統?轉為BigDecimal --><property name="forceBigDecimals" value="false"/></javaTypeResolver><!-- 實體類?成位置 --><javaModelGenerator targetPackage="com.example.mybatisplustest.model"targetProject="src/main/java"><property name="enableSubPackages" value="true"/><property name="trimStrings" value="true"/></javaModelGenerator><!-- mapper.xml?成位置 --><sqlMapGenerator targetPackage="mapper"targetProject="src/main/resources"><property name="enableSubPackages" value="true"/></sqlMapGenerator><!-- DAO類?成位置 --><javaClientGenerator type="XMLMAPPER"targetPackage="com.example.mybatisplustest.dao" targetProject="src/main/java"><property name="enableSubPackages" value="true"/></javaClientGenerator><!-- 配置?成表與實例, 只需要修改表名tableName, 與對應類名domainObjectName 即可--><table tableName="t_article" domainObjectName="Article"enableSelectByExample="false"enableDeleteByExample="false" enableDeleteByPrimaryKey="false"enableCountByExample="false"enableUpdateByExample="false"><!-- 類的屬性?數據庫中的真實字段名做為屬性名, 不指定這個屬性會?動轉換為駝峰命名規則--><property name="useActualColumnNames" value="true"/></table><table tableName="t_article_reply" domainObjectName="ArticleReply"enableSelectByExample="false"enableDeleteByExample="false" enableDeleteByPrimaryKey="false"enableCountByExample="false"enableUpdateByExample="false"><property name="useActualColumnNames" value="true"/></table><table tableName="t_board" domainObjectName="Board"enableSelectByExample="false" enableDeleteByExample="false"enableDeleteByPrimaryKey="false" enableCountByExample="false"enableUpdateByExample="false"><property name="useActualColumnNames" value="true"/></table><table tableName="t_message" domainObjectName="Message"enableSelectByExample="false"enableDeleteByExample="false" enableDeleteByPrimaryKey="false"enableCountByExample="false"enableUpdateByExample="false"><property name="useActualColumnNames" value="true"/></table><table tableName="t_user" domainObjectName="User"enableSelectByExample="false" enableDeleteByExample="false"enableDeleteByPrimaryKey="false" enableCountByExample="false"enableUpdateByExample="false"><property name="useActualColumnNames" value="true"/></table></context>
</generatorConfiguration>
4、收尾工作
- 在每一個xml文件中的插入語句的后面加入useGeneratedKeys="true" keyProperty="id" >
- 在dao文件中的每個Mapper語句上方,加上@Mapper注解
- 在model中加@Data,刪除get set
- 配置掃描路徑,在config包下,新建一個MybatisConfig類,用于指定掃描路徑
@Configuration
@MapperScan("com/example/mybatisplustest/dao")
public class MybatisConfig{}
5.yml文件中配置掃描路徑
mybatis:mapper-locations: classpath:mapper/**/*.xml