一、环境
1、操作系统:CentOS7。
2、Postgresql版本:12.0。
安装包下载地址:https://ftp.postgresql.org/pub/source/v12.0/postgresql-12.0.tar.gz
安装参考:https://www.cnblogs.com/halberd-lee/p/11699694.html
二、创建项目
1、postgre数据库的postgres下创建数据库cays,cays下新建schema:school,company。
school下建表student:
-- auto-generated definition
create table student
(
sno varchar(100) not null
constraint student_pk
primary key,
sname varchar(100),
sage integer default 0,
score double precision default 0,
start_time timestamp,
t_json json
);
comment on table student is '学生表';
alter table student
owner to postgres;
company下创建表emp:
-- auto-generated definition
create table emp
(
empno varchar(100) not null
constraint emp_pk
primary key,
ename varchar(100),
age integer default 0,
salary double precision default 0,
start_time timestamp,
end_time timestamp,
t_json json
);
comment on table emp is '雇员表';
alter table emp
owner to postgres;
创建结果:
2、resources目录下创建目录mybatis-genarator,新建文件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="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 连接数据库,地址账号密码-->
<jdbcConnection driverClass="org.postgresql.Driver" connectionURL="jdbc:postgresql://192.168.163.129:5432/cays"
userId="postgres" password="123456" />
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model实体类存放位置-->
<javaModelGenerator targetPackage="cays.postgre.entity" targetProject="./src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="cays.postgre.dao" targetProject="./src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名,指定同一个数据库下的不同schema下的不同的表-->
<table tableName="emp" domainObjectName="Emp" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="student" domainObjectName="Student" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
3、SpringBoot项目的依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cays</groupId>
<artifactId>postgre</artifactId>
<version>1</version>
<name>postgre</name>
<description>Demo project for Postgre Test</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot的Mybatis支持-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 连接Postgre驱动包 -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.8</version>
</dependency>
<!-- Mybatis的generator支持 快速生成通用CRUD-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
</dependency>
<!-- Fastjson做JSON格式转换 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- Mybatis生成mapper文件,dao类,pojo类 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.8</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
4、点击idea右侧的Maven–>postgre–>Plugins–>mybatis-generator–>双击mybatis-generator:generator,生成文件: