springboot整合MyBatis-Plus和postgresql,实现postgresql选择Module查询数据

前言:mybatis-plus官网:https://mp.baomidou.com/

自己最近在学习使用mybatis-plus结合springboot快速搭建持久化服务,但是mybatis-plus(以下简称mp)连接mysql还好,连接postgresql,特别是指定postgresql指定数据库下的指定module的时候,总是报错。就是我只能连接到默认public的模式上。为了解决这个问题我真的是煞费苦心。

在springboot中引入我们需要的依赖以及相应版本

  
        1.8
        0.0.1-SNAPSHOT
        3.0.5
        2.0
        2.7.0
        3.9
        1.3.1
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            org.springframework.boot
            spring-boot-devtools
            true
        

        
        
            org.projectlombok
            lombok
        
        
            junit
            junit
            test
        

        
        
            com.baomidou
            mybatis-plus-boot-starter
            ${mybatis-plus.version}
        

        
        
            org.apache.velocity
            velocity-engine-core
            ${velocity.version}
        

        
        
            io.springfox
            springfox-swagger2
            ${swagger.version}
        
        
            io.springfox
            springfox-swagger-ui
            ${swagger.version}
        

        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        

        
        
            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        

        
            javax.servlet
            javax.servlet-api
            3.0.1
            provided
        

        
        
            org.apache.poi
            poi
            3.9
        

        
        
            org.apache.poi
            poi-ooxml
            3.9
        

        
        
            joda-time
            joda-time
            2.10.1
        

        
            org.postgresql
            postgresql
            runtime
        


    

引入模块后我们还需要在application.properties中配置必要的数据库属性,否者代码生成器会启动失败。

# 服务端口
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

在上面的配置文件中配置上自己的属性后,就可以启动自己的代码生成器了,代码生成器如下

@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();
    }

我的代码生成器是写在test中的一个方法。

画重点了:

在我的代码生成器中有这么一段代码

// 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);

springboot整合MyBatis-Plus和postgresql,实现postgresql选择Module查询数据_第1张图片

大家只需要将

pc.setModuleName("jiankongservice"); //模块名
pc.setParent("com.atguigu.jiankong");这两行代码与自己项目中的路径一致就行,也可以按照我的路径命名那个,或者按照自己的需求更改。

 // 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);

数据源配置中有这么一行,dsc.setSchemaName("test");,是我翻看文档才发现的,在连接postgresql时,可以指定这个值为自己module的值,这样我们就可以指定module利用强大的mp生成我们需要的文件了。然后制定自己的数据库地址还有密码。

然后就可以执行test方法。在我们的路径下就会生成相应的entity,mapper,controller,service等

还有一点我要提一下,就是我们要在配置文件中指定我们的module,如果使用的是public,可以不指定。

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

就是配置文件中的这行代码

我的postgresql是9.6的,所以使用的是currentSchema,低版本的好像是其他的路径

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

配置文件中修改完后,我们就可以使用mybatis-plus写自己的逻辑代码了。逻辑代码不在演示。

 

你可能感兴趣的:(数据库)