SpringBoot项目搭建

springboot官方文档:官方文档

1. Maven Spring Boot搭建
首先在pom文件中添加两个maven依赖

<!-- 在maven工程中加入SpringBoot 内核支持 
(标准搭建应将springboot的核心支付放入到parent标签中,
而不应该放在dependencies标签中,这样保证后续的springboot分支maven包,
都基于parent标签中的核心来延展)-->

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.0.2.RELEASE</version>
  <type>pom</type>
</dependency>
<!--上述配置需要修改优化-->

<!-- 在maven工程中加入SpringBoot web工程的支持 -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>2.0.2.RELEASE</version>
</dependency>

修改后的pom基础配置

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
</parent>

<dependencies>
	<!-- 在maven工程中加入SpringBoot web工程的支持 -->
	<dependency>
	  <groupId>org.springframework.boot</groupId>
	  <artifactId>spring-boot-starter-web</artifactId>
	  <version>2.0.2.RELEASE</version>
	</dependency>
</dependencies>

2.创建SpringBoot项目入口

/**
 1. 该类需要放在根包下,@SpringBootApplication 中会自动扫描该包下的所有spring注解配置,如若不放在
 2. 根包下,会导致扫码扫码不了。解决方案是手动配置一下扫描包路径,但是显然没必要。根据行业习惯,都是放
 3. 在根包下面的。
*/
@SpringBootApplication 
public class MySpringbootApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(MySpringbootApplication.class, args);
    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MySpringbootApplication.class);
    }
}
  1. 编写yml配置文件
server:
 port: 8080 # tomcat启动端口号
 tomcat:
   uri-encoding: utf-8
  1. 整合mybatis
    在pom.xml文件中添加maven包的支持
   <dependency>
     <groupId>org.apache.tomcat.embed</groupId>
     <artifactId>tomcat-embed-jasper</artifactId>
     <version>9.0.8</version>
   </dependency>
   <dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
     <version>1.3.2</version>
   </dependency>
   <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid-spring-boot-starter</artifactId>
     <version>1.1.9</version>
   </dependency>
   <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>5.1.46</version>
   </dependency>

在pom.xml配置文件中添加plugin

<plugin>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 <version>2.0.2.RELEASE</version>
</plugin>

在yml文件中加入连接池配置

#定义数据库连接池
spring:
	datasource:
		driverClassName: com.mysql.jdbc.Driver #驱动
		url: jdbc:mysql://localhost:3306/链接 #链接数据库地址
		username: 用户名 #连接用户名
		password: 密码 #连接密码
mybatis: 
	mapper-locations: classpath:mapper/*.xml #定义所有的mapper.xml文件路径
	configuration: 
		log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #显示sql
		return-instance-for-empty-row: true #如果这里的value为false(默认值),mybatis在没有查询到内容的时候返回null
  1. 加入系统日志记录
logging: 
	level: 
		org:
			springframework: 
				web: ERROR
		com:
			yiibai: DEBUG
	pattern:
		console: "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"  # Logging pattern for the console
		file: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n" # Logging pattern for file
	file: E:/logs/mylog.log # 日志存储地址
  1. 加入swagger
    引入依赖:
	   <!-- swagger2-->
       <dependency>
           <groupId>io.springfox</groupId>
           <artifactId>springfox-swagger2</artifactId>
           <version>${swagger.version}</version>
       </dependency>
       <!-- swagger2-UI-->
       <dependency>
           <groupId>io.springfox</groupId>
           <artifactId>springfox-swagger-ui</artifactId>
           <version>${swagger.version}</version>
       </dependency>
       <dependency>
           <groupId>com.github.xiaoymin</groupId>
           <artifactId>swagger-bootstrap-ui</artifactId>
           <version>${swagger.bootstrap.ui}</version>
       </dependency>

创建配置类:

package com.tally.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2020/11/17
 * Time: 11:15
 * Description:
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {
    @Bean
    public Docket customDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(true)//使用swagger
                .securitySchemes(security())
                .select()
                //加了ApiOperation注解的类,才生成接口文档
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * name:开发者姓名
     * url:开发者网址
     * email:开发者邮箱
     *
     * @return
     */
    private ApiInfo apiInfo() {
        Contact contact = new Contact("Usopp.tsui", "https://blog.youkuaiyun.com/weixin_45136521?spm=1010.2135.3001.5113", "保密");
        return new ApiInfoBuilder()
                .title("EasyTally——API接口文档")//标题
                .description("EasyTally接口文档")//文档接口的描述
                .termsOfServiceUrl("http://www.easytally.com")
                .contact(contact)
                .version("1.1.0")//版本号
                .build();
    }

    /**
     * 请求携带参数
     *
     * @return
     */
    private List<ApiKey> security() {
        return newArrayList(
                new ApiKey("token", "token", "header"),
                new ApiKey("user_id", "user_id", "header")
        );
    }
}

启动后访问localhost:port/doc.html即可

  1. 修改为mybatis-plus

修改依赖

        <!--整合mybatis-plus-->
<!--        <dependency>-->
<!--            <groupId>org.mybatis.spring.boot</groupId>-->
<!--            <artifactId>mybatis-spring-boot-starter</artifactId>-->
<!--            <version>1.3.2</version>-->
<!--        </dependency>-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatisplus-spring-boot-starter</artifactId>
            <version>${mybatisplus.spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>${mybatisplus.version}</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-support</artifactId>
            <version>${mybatis.plus.support}</version>
        </dependency>

修改日志配置文件

mybatis-plus:
 mapper-locations: classpath:mapper/*.xml #定义所有的mapper.xml文件路径
 configuration:
   log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #显示sql
   return-instance-for-empty-row: true

使用示例:
实体创建:

package com.tally.modules.base.db;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
* Created with IntelliJ IDEA.
* Author: Usopp.tsui
* Date: 2020/11/17
* Time: 14:00
* Description:
*/
@Data
@TableName("t_user_info")
@ApiModel(value = "TUserInfo", description = "数据库-用户表")
public class TUserInfoEntity {
  @TableId("user_id")
  @ApiModelProperty(value = "用户主键", dataType = "String", notes = "用户主键")
  private String userId;

  @TableField("user_name")
  @ApiModelProperty(value = "用户名", dataType = "String", notes = "用户名")
  private String userName;

  @TableField("mobile")
  @ApiModelProperty(value = "电话号码", dataType = "String", notes = "电话号码")
  private String mobile;

  @TableField("passwd")
  @ApiModelProperty(value = "用户密码", dataType = "String", notes = "用户密码")
  private String passwd;

  @TableField("salt")
  @ApiModelProperty(value = "盐", dataType = "String", notes = "盐")
  private String salt;

}

mapper:

package com.tally.modules.base.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tally.modules.base.db.TUserInfoEntity;
import org.apache.ibatis.annotations.Mapper;

/**
* Created with IntelliJ IDEA.
* Author: Usopp.tsui
* Date: 2020/11/17
* Time: 14:05
* Description:
*/
@Mapper
public interface TUserInfoMapper extends BaseMapper<TUserInfoEntity> {
}

service:

package com.tally.modules.base.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.tally.modules.base.db.TUserInfoEntity;

/**
* Created with IntelliJ IDEA.
* Author: Usopp.tsui
* Date: 2020/11/17
* Time: 14:07
* Description:
*/
public interface ITUserInfoService extends IService<TUserInfoEntity> {
}

serviceImpl:

package com.tally.modules.base.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tally.modules.base.db.TUserInfoEntity;
import com.tally.modules.base.mapper.TUserInfoMapper;
import com.tally.modules.base.service.ITUserInfoService;
import org.springframework.stereotype.Service;

/**
* Created with IntelliJ IDEA.
* Author: Usopp.tsui
* Date: 2020/11/17
* Time: 14:07
* Description:
*/
@Service("ITUserInfoService")
public class TUserInfoServiceImpl extends ServiceImpl<TUserInfoMapper, TUserInfoEntity> implements ITUserInfoService {
}

* 至此mybatis-plus即可正常使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值