postgresql之mybatis.generator的使用
在PostgreSQL中使用MyBatis Generator (MBG) 生成代码时,你需要配置一个generatorConfig.xml
文件,并且可能还需要相应的Maven或Gradle配置。以下是一个简单的例子:
- 添加MyBatis Generator依赖到你的项目中。
对于Maven项目,在pom.xml
中添加:
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>
对于Gradle项目,在build.gradle
中添加:
dependencies {
implementation 'org.mybatis.generator:mybatis-generator-core:1.4.0'
}
- 创建
generatorConfig.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-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="PostgreSQLTables" targetRuntime="MyBatis3">
<jdbcConnection driverClass="org.postgresql.Driver"
connectionURL="jdbc:postgresql://localhost:5432/yourdatabase"
userId="yourUsername"
password="yourPassword">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<javaModelGenerator targetPackage="com.yourpackage.model" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.yourpackage.mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.yourpackage.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="your_table_name">
<property name="useActualColumnNames" value="true"/>
<generatedKey column="id" sqlStatement="SELECT CURRVAL(pg_get_serial_sequence('your_table_name', 'id'))"/>
</table>
</context>
</generatorConfiguration>
- 创建一个Java类来运行MBG:
import org.mybatis.generator.api.MyBatisGenerator;
import org
评论已关闭