springboot结合mybatis

网友投稿 193 2023-01-09

springboot结合mybatis

mybatis-plus简介:

Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。这是官方给的定义,关于mybatis-plus的更多介绍及特性,可以参考mybatis-plus官网。那么它是怎么增强的呢?其实就是它已经封装好了一些crud方法,我们不需要再写xml了,直接调用这些方法就行,就类似于JPA。

项目模板

1、项目概览

项目结构

创建项目时,父项目用springboot,子项目用maven,父项目统一管理,子项目分模块

springboot父项目(子项目用maven建)

- common模块

- web模块

- .......

父项目pom.xml:统一管理版本

1.8

3.4.2

3.0.0

2.3

8.0.25

2.3

3.4.1

3.0.2

com.baomidou

mybatis-plus-boot-starter

${mp.version}

com.baomidou

mybatis-plus-generator

${mybatis-plus-generator.version}

org.apache.velocity

velocity-engine-core

${velocity.version}

io.springfox

springfox-swagger2

${swagger.version}

io.springfox

springfox-swagger-ui

${swagger.version}

mysql

mysql-connector-java

${mysql-connect-java.version}

com.github.xiaoymin

knife4j-spring-boot-starter

${knife4j.version}

org.springframework.boot

spring-boot-maven-plugin

true

子项目pom.xml:公共模块(common)提取出来

org.springframework.boot

spring-boot-starter-web

com.baomidou

mybatis-plus-boot-starter

com.baomidou

mybatis-plus-generator

org.apache.velocity

velocity-engine-core

&uxOuKvMklt;dependency>

io.springfox

springfox-swagger2

io.springfox

springfox-swagger-ui

mysql

mysql-connector-java

org.projectlombok

lombok

com.github.xiaoymin

knife4j-spring-boot-starter

org.springframework.boot

spring-boot-maven-plugin

true

子项目web模块pom.xml(直接引用common模块)

cn.jie

admin-base-common

0.0.1-SNAPSHOT

代码生成器

public class CodeGenerator {

/**

*

* 读取控制台内容

*

*/

public static String scanner(String tip) {

Scanner scanner = new Scanner(System.in);

StringBuilder help = new StringBuilder();

help.append("请输入" + tip + ":");

System.out.println(help.toString());

if (scanner.hasNext()) {

String ipt = scanner.next();

if (StringUtils.isNotBlank(ipt)) {

return ipt;

}

}

throw new MybatisPlusException("请输入正确的" + tip + "!");

}

public static void main(String[] args) {

// 代码生成器

AutoGenerator mpg = new AutoGenerator();

// 全局配置

GlobalConfig gc = new GlobalConfig();

String projectPath = scanner("项目路径");

//项目生成路径

gc.setOutputDir(projectPath + "/src/main/java");

gc.setAuthor("sky");

//打开资源管理器

gc.setOpen(false);

//开启swagger

gc.setSwagger2(true);

//覆盖文件

gc.setFileOverride(false);

gc.setServiceName("%sService");

//主键自增

gc.setIdType(IdType.AUTO);

//java.util.date

gc.setDateType(DateType.ONLY_DATE);

mpg.setGlobalConfig(gc);

// 数据源配置

DataSourceConfig dsc = new DataSourceConfig();

dsc.setUrl("jdbc:mysql://localhost:3306/adminweb?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8");

// dsc.setSchemaName("public");

dsc.setDriverName("com.mysql.cj.jdbc.Driver");

dsc.setUsername("root");

dsc.setPassword("root"http://);

dsc.setDbType(DbType.MYSQL);

mpg.setDataSource(dsc);

// 包配置

PackageConfig pc = new PackageConfig();

pc.setModuleName(scanner("模块名"));

pc.setParent("cn.jie");

pc.setEntity("entity");

pc.setMapper("mapper");

pc.setController("controller");

pc.setService("service");

pc.setServiceImpl("service.impl");

mpg.setPackageInfo(pc);

// 策略配置

StrategyConfig strategy = new StrategyConfig();

//表

strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));

//驼峰命名

strategy.setNaming(NamingStrategy.underline_to_camel);

strategy.setColumnNaming(NamingStrategy.underline_to_camel);

//lombok

strategy.setEntityLombokModel(true);

//restful

strategy.setRestControllerStyle(true);

strategy.setControllerMappingHyphenStyle(true);

mpg.setStrategy(strategy);

mpg.execute();

}

}

springboot配置

spring:

application:

name: admin-base-web

datasource:

type:

driver-class-name: com.mysql.cj.jdbc.Driver

url: jdbc:mysql://localhost:3306/adminweb?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8

username: root

password: root

jackson:

date-format: yyyy-MM-dd HH:mm:ss

time-zone: GMT+8

server:

port: 8081

mybatis-plus:

global-config:

db-config:

logic-delete-field: deleted # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2)

logic-delete-value: 1 # 逻辑已删除值(默认为 1)

logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)

mapper-locations: classpath*:/mapper/*.xml

2、美化swagger-ui

swagger注意要用3.0.0版本

配置完swagger2config

@Configuration

@EnableSwagger2

@EnableKnife4j

@Import(BeanValidatorPluginsConfiguration.class)

public class Swagger2Config {

@Bean(value = "defaultApi2")

public Docket defaultApi2() {

Docket docket=new Docket(DocumentationType.SWAGGER_2)

.apiInfo(new ApiInfoBuilder()

//.title("swagger-bootstrap-ui-demo RESTful APIs")

.description("物资管理系统API文档")

.termsOfServiceUrl("https://cnblogs.com/thatbluesky/")

.contact(new Contact("我的博客","https://cnblogs.com/thatbluesky/","1879186403@qq.com"))

.version("1.0")

.build())

//分组名称

.groupName("1.0版本")

.select()

//这里指定Controller扫描包路径

.apis(RequestHandlerSelectors.basePackage("cn.jie.system.controller"))

.paths(PathSelectors.any())

.build();

return docket;

}

}

访问:http://localhost:8081/doc.html

以上就是springboot结合mybatis-plus快速生成项目模板的方法的详细内容,更多关于springboot mybatis-plus项目模板 的资料请关注我们其它相关文章!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java三个类加载器及它们的相互关系
下一篇:京东圆通快递物流查询单号(京东快递包裹单号查询)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~