mybatis 自动生成代码
配置 generator.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-3-generator-config_1_0.dtd" > <generatorConfiguration> <!-- 本地数据库驱动程序jar包的全路径 --> <classPathEntry location="C:\Users\\tl19638\\.m2\\repository\\mysql\\mysql-connector-java\\8.0.12\\mysql-connector-java-8.0.12.jar"/> <context id="context" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="false"/> <property name="suppressDate" value="true"/> </commentGenerator> <!-- 数据库的相关配置 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/springmvc?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true" userId="root" password="123456"/> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 实体类生成的位置 --> <javaModelGenerator targetPackage="com.xc.mybatis.entity" targetProject=".\src\main\java"> <property name="enableSubPackages" value="false"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- *Mapper.xml 文件的位置 sqlMapGenerator--> <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- Mapper 接口文件的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.xc.mybatis.mapper" targetProject=".\src\main\java"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- 相关表的配置 --> <table tableName="tb_area"/> </context> </generatorConfiguration>
需要改一些内容:
- 本地数据库驱动程序jar包的全路径(必须要改)。
- 数据库的相关配置(必须要改)
- 相关表的配置(必须要改)
- 实体类生成存放的位置。
- MapperXML 生成文件存放的位置。
- Mapper 接口存放的位置。
配置 pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.homejim.mybatis</groupId> <artifactId>mybatis-generator</artifactId> <version>1.0-SNAPSHOT</version> <!--在原基础上添加 这些就好了--> <build> <finalName>mybatis-generator</finalName> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> <configuration> <!--此处需要注意, 文件与上面的文件匹配--> <configurationFile>src/main/resources/generator.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> </dependencies> </plugin> </plugins> </build> <!--到此结束--> </project>
生成
Maven Projects->项目->Plugins->mybatis-generator
官方的文档:
http://www.mybatis.org/generator/configreference/xmlconfig.html
参考:
https://www.cnblogs.com/homejim/p/9782403.html#%E9%85%8D%E7%BD%AE-generator.xml