前言:mybatis-plus官网:https://mp.baomidou.com/
自己最近在学习使用mybatis-plus结合springboot快速搭建持久化服务,但是mybatis-plus(以下简称mp)连接mysql还好,连接postgresql,特别是指定postgresql指定数据库下的指定module的时候,总是报错。就是我只能连接到默认public的模式上。为了解决这个问题我真的是煞费苦心。
在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>
引入模块后我们还需要在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);
大家只需要将
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写自己的逻辑代码了。逻辑代码不在演示。