springboot integrates MyBatis-Plus and postgresql to realize postgresql select Module to query data

Foreword: mybatis-plus official website: https://mp.baomidou.com/

I have recently been learning to use mybatis-plus combined with springboot to quickly build a persistent service, but mybatis-plus (hereinafter referred to as mp) is good for connecting to mysql and connecting to postgresql, especially when specifying the specified module under the specified postgresql database, it always reports an error. That is, I can only connect to the default public mode. I really took great pains to solve this problem.

Introduce the dependencies and corresponding versions we need in springboot

  <properties>
        <java.version>1.8</java.version>
        <guli.version>0.0.1-SNAPSHOT</guli.version>
        <mybatis-plus.version>3.0.5</mybatis-plus.version>
        <velocity.version>2.0</velocity.version>
        <swagger.version>2.7.0</swagger.version>
        <poi.version>3.9</poi.version>
        <commons-fileupload.version>1.3.1</commons-fileupload.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--开发者工具-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <!--lombok用来简化实体类:需要安装lombok插件-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <!--mybatis-plus 持久层-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis-plus.version}</version>
        </dependency>

        <!-- velocity 模板引擎, Mybatis Plus 代码生成器需要 -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>${velocity.version}</version>
        </dependency>

        <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!--添加tomcat-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>

        <!--xls-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>

        <!--xlsx-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>

        <!--日期格式化工具-->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.10.1</version>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>


    </dependencies>

After introducing the module, we need to configure the necessary database properties in application.properties, otherwise the code generator will fail to start.

# 服务端口
server.port=8012
# 服务名
spring.application.name=xxx
# 环境设置:dev、test、prod
spring.profiles.active=dev

//数据库的地址以及端口号
spring.datasource.url=jdbc:postgresql://your ip:5432/okapi2?currentSchema=test
spring.datasource.username=folio_admin
spring.datasource.password=your password
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.hbm2ddl.auto=update

spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

#mybatis日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#返回json的全局时间格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
#spring.jackson.time-zone=GMT+8

#配置mapper xml文件的路径
mybatis-plus.mapper-locations=classpath:com/atguigu/jiankong/jiankongservice/mapper/xml/*.xml

After configuring your own properties in the above configuration file, you can start your own code generator, the code generator is as follows

@Test
    public void run() {


        // 1、创建代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 2、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");

        gc.setAuthor("testjava");
        gc.setOpen(false); //生成后是否打开资源管理器
        gc.setFileOverride(false); //重新生成时文件是否覆盖
        gc.setServiceName("%sService");	//去掉Service接口的首字母I
        gc.setIdType(IdType.ID_WORKER_STR); //主键策略
        gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
        gc.setSwagger2(true);//开启Swagger2模式

        mpg.setGlobalConfig(gc);

        // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:postgresql://39.106.33.252:5432/okapi2");
        dsc.setDriverName("org.postgresql.Driver");
        dsc.setSchemaName("test");
        dsc.setUsername("folio_admin");
        dsc.setPassword("calis123");
        dsc.setDbType(DbType.POSTGRE_SQL);
        mpg.setDataSource(dsc);

        // 4、包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("jiankongservice"); //模块名
        pc.setParent("com.atguigu.jiankong");

        pc.setController("controller");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("rr");

        strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
        strategy.setTablePrefix(pc.getModuleName() + "_"); //生成实体时去掉表前缀
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作
        strategy.setRestControllerStyle(true); //restful api风格控制器
        strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符

        mpg.setStrategy(strategy);

        // 6、执行
        mpg.execute();
    }

My code generator is a method written in test.

Picture emphasis:

There is such a piece of code in my code generator

// 4、包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("jiankongservice"); //模块名
        pc.setParent("com.atguigu.jiankong");

        pc.setController("controller");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

You only need to

pc.setModuleName("jiankongservice"); //Module name
pc.setParent("com.atguigu.jiankong"); These two lines of code are the same as the path in your project, or you can name it according to my path, or follow Change your own needs.

 // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:postgresql://39.106.33.252:5432/okapi2");
        dsc.setDriverName("org.postgresql.Driver");
        dsc.setSchemaName("test");
        dsc.setUsername("folio_admin");
        dsc.setPassword("calis123");
        dsc.setDbType(DbType.POSTGRE_SQL);
        mpg.setDataSource(dsc);

There is such a line in the data source configuration, dsc.setSchemaName("test");, which I found after looking through the document. When connecting to postgresql, you can specify this value as the value of your own module, so that we can specify the module to take advantage of the power The mp generates the files we need. Then develop your own database address and password.

Then you can execute the test method. The corresponding entity, mapper, controller , service, etc. will be generated under our path

Another point I want to mention is that we have to specify our module in the configuration file. If you are using public, you can leave it alone.

spring.datasource.url=jdbc:postgresql://your ip:5432/okapi2?currentSchema=test

Is this line of code in the configuration file

My postgresql is 9.6, so the currentSchema is used, and the lower version seems to be other paths

postgresql-> 9.3 及以前的版本指定方式
spring.datasource.url=jdbc:postgresql://localhost:5432/postgresql?searchpath=newschema
postgresql-> 9.4 及以后的版本指定方式
spring.datasource.url=jdbc:postgresql://localhost:5432/postgresql?currentSchema=newschema

After modifying the configuration file, we can use mybatis-plus to write our own logic code. The logic code is not demonstrated.

 

Guess you like

Origin blog.csdn.net/wjg1314521/article/details/105130060