Springboot+mybatis+postgre尝试

一、环境

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;

创建结果:
Springboot+mybatis+postgre尝试_第1张图片

2、resources目录下创建目录mybatis-genarator,新建文件generatorConfig.xml,文件内容:




<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>
        
        <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>
        
        <javaClientGenerator type="XMLMAPPER" targetPackage="cays.postgre.dao" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true"/>
        javaClientGenerator>
        
        <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项目的依赖


<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.2.2.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>caysgroupId>
    <artifactId>postgreartifactId>
    <version>1version>
    <name>postgrename>
    <description>Demo project for Postgre Testdescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.1.1version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintagegroupId>
                    <artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>
        
        <dependency>
            <groupId>org.postgresqlgroupId>
            <artifactId>postgresqlartifactId>
            <version>42.2.8version>
        dependency>
        
        <dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-maven-pluginartifactId>
            <version>1.4.0version>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.62version>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
            
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.6version>
                <configuration>
                    <configurationFile>src/main/resources/mybatis-generator/generatorConfig.xmlconfigurationFile>
                    <verbose>trueverbose>
                    <overwrite>trueoverwrite>
                configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifactsid>
                        <goals>
                            <goal>generategoal>
                        goals>
                        <phase>generate-sourcesphase>
                    execution>
                executions>
                <dependencies>
                    <dependency>
                        <groupId>org.postgresqlgroupId>
                        <artifactId>postgresqlartifactId>
                        <version>42.2.8version>
                    dependency>
                dependencies>
            plugin>
        plugins>
    build>

project>

4、点击idea右侧的Maven–>postgre–>Plugins–>mybatis-generator–>双击mybatis-generator:generator,生成文件:
Springboot+mybatis+postgre尝试_第2张图片

你可能感兴趣的:(Sql)