搭建SpringBoot框架常引入的依赖

父依赖:

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <!--  可选修改:之前的一些案例按照此版本搭建      -->
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

Web支持(Spring Mvc):

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

Mybatis依赖:

 <!--  spring boot 整合mybatis的核心依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
        <!--        数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

分页插件依赖(不属于mybatisplus):

 <!--pageHelper分页插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>

连接数据库的yaml文件:

# yaml/yml  也是spring的配置文件  properties可以实现的  这个也可以


#后缀名:.yaml/yml
#语法: key 和 value 通过冒号+空格 来间隔
#   父级属性通过 冒号 +换行+ 缩进来识别
# 优势:简洁 + 父级属性共用
# server.port=8081
# server.xxx=xxx
#server:
#  port: 8081
#  address: xxxxx

spring:
  thymeleaf:
    cache: false # 关闭缓存,默认开启
  datasource:
    url: jdbc:mysql://localhost:3306/shixunv3.0?useUnicode=true&characterEncoding=utf-8&serverTimezone=CTT
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
    #mybatis:
    #  #对应mapper映射xml文件所在路径
    #  mapper-locations: classpath:mapping/*.xml
    #  #对应实体类路径
    #  type-aliases-package: com.example.demo.entity
    #  configuration:
    #    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #输出sql到控制台
    # 下划线 自动 转 驼峰的开关  =>  在mybatis 里面默认关闭  ;  mybatis-plus 里面开启的
#    map-underscore-to-camel-case: false
mybatis:
  mapper-locations: classpath:mapper/*.xml  #对应mapper映射xml文件所在路径
  type-aliases-package: com.xxxx.entity  #对应实体类路径
  # 全局配置id自增  =>
  global-config:
    db-config:
      id-type: auto
#分页配置
pagehelper:
  helperDialect: mysql # 数据库方言
  reasonable: true # 修改默认值

mybatis-plus依赖:

  <!--        引入Mybatis plus 依赖  增强mybatis-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>

添加mybatisplus依赖后会简化数据库操作,需要更改yaml文件

连接数据库的yaml文件:

# yaml/yml  也是spring的配置文件  properties可以实现的  这个也可以


#后缀名:.yaml/yml
#语法: key 和 value 通过冒号+空格 来间隔
#   父级属性通过 冒号 +换行+ 缩进来识别
# 优势:简洁 + 父级属性共用
# server.port=8081
# server.xxx=xxx
#server:
#  port: 8081
#  address: xxxxx

spring:
  thymeleaf:
    cache: false # 关闭缓存,默认开启
  datasource:
    url: jdbc:mysql://localhost:3306/shixunv3.0?useUnicode=true&characterEncoding=utf-8&serverTimezone=CTT
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
    #mybatis:
    #  #对应mapper映射xml文件所在路径
    #  mapper-locations: classpath:mapping/*.xml
    #  #对应实体类路径
    #  type-aliases-package: com.example.demo.entity
    #  configuration:
    #    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #输出sql到控制台
    # 下划线 自动 转 驼峰的开关  =>  在mybatis 里面默认关闭  ;  mybatis-plus 里面开启的
#    map-underscore-to-camel-case: false
# mybatis-plus配置
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    # 下划线 自动 转 驼峰的开关  =>  在mybatis 里面默认关闭  ;  mybatis-plus 里面开启的
  #    map-underscore-to-camel-case: true
  typeAliasesPackage: com.example.javen.entity
  mapperLocations: classpath:mapping/*.xml
  # 全局配置id自增  =>
  global-config:
    db-config:
      id-type: auto

mubatis-plus的分页插件:

// 启动类修改
@MapperScan("com.xxxx.mapper")//扫描mapper包下的接口
@Configuration
public class MybatisPlusConfig {
    // 最新版
    @Bean  // <bean id=""/>  也是声明是Spring 的一个Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

 

lombok依赖,简化实体类,可以少些代码 

 <!-- 引入lombok,简化pojo类-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

接口文档的依赖:

         <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

可以将各种类型数据转化为json的依赖:

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.73</version>
        </dependency>

面向切面AOP依赖:

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

对通用结果封装成json格式的类:

/**
 * 通用的返回结果封装
 */
@Data
public class WebResultJson {
    public final static int OK=1;
    public final static String OK_MSG="操作成功!";
    public final static int FAIL=0;
    public final static String FAIL_MSG="操作失败!";

    private int code;
    private String msg;
    private Object data;

    public WebResultJson(int code,String msg,Object data){
        this.code=code;
        this.msg=msg;
        this.data=data;
    }
    public static WebResultJson ok(){
        return ok(OK_MSG,null);
    }
    public static WebResultJson ok(String msg) {
        return new WebResultJson(OK,msg,null);
    }
    public static WebResultJson ok(Object data) {
        return new WebResultJson(OK,OK_MSG,data);
    }
    public static WebResultJson ok(String msg, Object data) {
        return new WebResultJson(OK,msg,data);
    }


    public static WebResultJson fail(){
        return fail(FAIL_MSG,null);
    }
    public static WebResultJson fail(String msg) {
        return new WebResultJson(FAIL,msg,null);
    }
    public static WebResultJson fail(Object data) {
        return new WebResultJson(FAIL,FAIL_MSG,data);
    }
    public static WebResultJson fail(String msg, Object data) {
        return new WebResultJson(FAIL,msg,data);
    }
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sshm_666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值