编写mybatis generator配置
1、在resources目录下编写配置
内容如下,根据自己需要改动包名、密码等信息:
<?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>
<context id="sss" targetRuntime="MyBatis3" defaultModelType="flat">
<!-- 自动识别数据库关键字,默认false,如果设置为true,根据SqlReservedWords中定义的关键字列表;
一般保留默认值,遇到数据库关键字(Java关键字),使用columnOverride覆盖
-->
<!--<property name="autoDelimitKeywords" value="true"/>-->
<!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; -->
<!--<property name="beginningDelimiter" value="'"/>
<property name="endingDelimiter" value="'"/>-->
<!-- 生成的Java文件的编码 -->
<!--<property name="javaFileEncoding" value="UTF-8"/>-->
<!-- 格式化java代码 -->
<!--<property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>-->
<!-- 格式化XML代码 -->
<!--<property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>-->
<!--连接数据库-->
<jdbcConnection driverClass="org.postgresql.Driver"
connectionURL="jdbc:postgresql://localhost:5432/ceshiku"
userId="postgres"
password="你的密码">
</jdbcConnection>
<!--生成实体类,targetPackage是包名,targetProject是生成的包在工程中的哪个文件夹下-->
<javaModelGenerator targetPackage="com.zzmj.pojo" targetProject="src/main/java/">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--<javaModelGenerator targetPackage="实体类包名" targetProject="/xx/src/main/java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>-->
<!--生成.xml文件-->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources/">
</sqlMapGenerator>
<!--生成dao层接口-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.zzmj.dao" targetProject="src/main/java/">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!--数据库表名配置(几张表就几个table标签),tablename是数据库的表名,domainObjectName是生成实体类的类名-->
<table tableName="sys_area" domainObjectName="SysAreaa" delimitIdentifiers="true" delimitAllColumns="true">
<property name="useActualColumnNames" value="false"/>
</table>
<!--
<table schema="xx" tableName="表名" domainObjectName="类名" enableCountByExample="true"
enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true" ></table>
-->
</context>
</generatorConfiguration>
导入和依赖
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!--mybatis generator--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> <version>3.3</version> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <!--配置文件的位置--> <configurationFile>src/main/resources/mybatis-generator-config.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.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.2.18</version> </dependency> </dependencies> </plugin> </plugins> </build>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.18</version>
</dependency>
代码生成
点击 mybatis-generator.generate
等待生成成功
生成的结果如下,如果没看到文件,可以重启idea