SpringBoot项目的搭建

开发工具:

  • MYSQL 数据库
  • Tomcat 应用服务器
  • Git 版本管理
  • IntelliJ IDEA 开发IDE
  • Navicat for MySQL 数据库客户端
  • PDMan 建模工具
  • Postman 接口调试工具

开发环境

  • jdk10+
  • Mysql5.5+
  • Redis
  • Spring boot 大于2.2.x
  • Maven
  • knife4j
  • tk.Mybatis
  • Druid

本博客以项目中最常见的CURD为主,其它功能后续添加

1.1 创建项目

1.png

1.2 选择版本

2.png

1.3导入依赖


        
            org.springframework.boot
            spring-boot-starter-data-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-data-mongodb
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.4
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
         
            com.alibaba
            druid
            1.0.9
        
        
            mysql
            mysql-connector-java
        
        
            tk.mybatis
            mapper
            4.0.3
        
        
            tk.mybatis
            mapper-spring-boot-starter
            2.0.3
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            junit
            junit
            test
        
        
            com.github.xiaoymin
            knife4j-spring-boot-starter
            2.0.2
        
        
            com.google.guava
            guava
            29.0-jre
        
    

2.1 创建SpringBoot启动类

@SpringBootApplication
@MapperScan("com.wjr.mapper")
public class qaApplication {
    public static void main(String[] args) {
        SpringApplication.run(qaApplication.class, args);
    }
}

2.2 创建实体类(以其中一个对象Sysconf为例)

package com.wjr.pojo;

import javax.persistence.Column;

/**
 * @author wen
 */
public class Sysconf {
    private String id;
    @Column(name = "`value`")
    private String value;
    @Column(name = "`key`")
    private String key;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }
}

2.3 创建Mapper接口

如果只是实现简单的CURD,tk.mybatis为我们提供了快捷的方式

public interface SysconfMapper extends Mapper {
}

2.4 创建Service接口

public interface SysconfService {
    List selectAll();
    List select(Sysconf sysconf);
    Integer insert(Sysconf sysconf);
    Integer delete(Sysconf sysconf);
    Integer update(Sysconf sysconf, Example sysconfExample);
    Integer updateByPrimaryKeySelective(Sysconf sysconf);
}

2.5 创建Service的实现类

@Service
public class SysconfServiceImpl implements SysconfService {

    //注入mapper
    @Autowired
    SysconfMapper sysconfMapper;

    /**
     * 查询全部
     * @return
     */
    @Override
    public List selectAll () {
        return sysconfMapper.selectAll();
    }

    /**
     * 按条件查询
     * @param sysconf
     * @return
     */
    @Override
    public List select(Sysconf sysconf) {
        return sysconfMapper.select(sysconf);
    }

    /**
     * 添加
     * @param sysconf
     * @return
     */
    @Override
    public Integer insert(Sysconf sysconf) {
        return sysconfMapper.insertSelective(sysconf);
    }

    /**
     * 删除
     * @param sysconf
     * @return
     */
    @Override
    public Integer delete(Sysconf sysconf) {
        return sysconfMapper.delete(sysconf);
    }

    /**
     * 按条件修改
     * @param sysconf
     * @param sysconfExample
     * @return
     */
    @Override
    public Integer update(Sysconf sysconf, Example sysconfExample) {
        return sysconfMapper.updateByPrimaryKey(sysconf);
    }

    /**
     * 按主键修改
     * @param sysconf
     * @return
     */
    @Override
    public Integer updateByPrimaryKeySelective(Sysconf sysconf) {
        return sysconfMapper.updateByPrimaryKey(sysconf);
    }
}

2.6 创建测试类

需要测试的数据根据实际情况自行编写,删除方法根据逻辑按需求执行。

@RunWith(SpringRunner.class)
@SpringBootTest
public class SysconfServiceTest {

    @Autowired
    SysconfService sysconfService;
    @Autowired
    SysconfMapper sysconfMapper;

    @Test
    public void selectAll() {
        List list = sysconfService.selectAll();
        System.out.println(list);
    }

    @Test
    public void select() {
        Sysconf sysconf = new Sysconf();
        sysconf.setKey("5");
        sysconf.setValue("2");
        List list = sysconfService.select(sysconf);
        System.out.println(list);
    }

    @Test
    public void insert() {
        Sysconf sysconf = new Sysconf();
        sysconf.setId("1");
        sysconf.setKey("3.0");
        sysconf.setValue("version");
        int count = sysconfService.insert(sysconf);
        System.out.println(count);
    }

    @Test
    public void delete() {
        Sysconf sysconf = new Sysconf();
        sysconf.setId("1");
        int count = sysconfService.delete(sysconf);
        System.out.println(count);
    }

    @Test
    public void update() {
        Sysconf sysconf = new Sysconf();
        sysconf.setId("3");
        Example example = new Example(Sysconf.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("key", "6");

        int count = sysconfService.update(sysconf,example);
        System.out.println(count);
    }
    @Test
    public void updateByPrimaryKeySelective() {
        Sysconf sysconf = new Sysconf();

        sysconf.setKey("5");
        sysconf.setValue("1");
        int count = sysconfMapper.updateByPrimaryKey(sysconf);
        System.out.println(count);
    }
}

2.7 运行测试类

每一个方法运行一次,查看是否出现问题。


3.png

2.8 创建Controller返回结构

public class HttpResult implements Serializable{

    @ApiModelProperty(value = "状态码,200标识正常/500为异常,其他可以自己定义")
    private int code = 200;
    @ApiModelProperty(value = "消息,当code为200时可以忽略消息,当code为500等异常时,可查看该消息查找原因")![5.png](https://upload-images.jianshu.io/upload_images/23444454-0184092df4f1a13e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

    private String msg;
    private Object data;
    
    public static HttpResult error() {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常,请联系管理员");
    }
    
    public static HttpResult error(String msg) {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
    }
    
    public static HttpResult error(int code, String msg) {
        HttpResult r = new HttpResult();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    public static HttpResult ok(String msg) {
        HttpResult r = new HttpResult();
        r.setMsg(msg);
        return r;
    }
    
    public static HttpResult ok(Object data) {
        HttpResult r = new HttpResult();
        r.setData(data);
        return r;
    }
    
    public static HttpResult ok() {
        return new HttpResult();
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
    
}

2.9创建Controller层

@RestController
@RequestMapping("sysconf")
@Api(tags = "Sysconf Controller 测试功能接口")
public class SysconfController {
    @Autowired
    SysconfService sysconfService;
    @GetMapping("selectAll")
    public HttpResult selectAll(){
        List list = sysconfService.selectAll();
        System.out.println(list);
        return HttpResult.ok(list);
    }
    @GetMapping("select")
    public HttpResult select(Sysconf sysconf){
        System.out.println(sysconf);
        List list = sysconfService.select(sysconf);
        System.out.println(list);
        return HttpResult.ok(list);
    }
    @PostMapping("update")
    public HttpResult update(Sysconf sysconf){
        Example example = new Example(Answer.class);
        Integer count = sysconfService.update(sysconf,example);
        System.out.println(count);
        return HttpResult.ok(count);
    }
    @PostMapping("updateByPrimaryKeySelective")
    public HttpResult updateByPrimaryKeySelective(Sysconf sysconf){
        Example example = new Example(Answer.class);
        Integer count = sysconfService.updateByPrimaryKeySelective(sysconf);
        System.out.println(count);
        return HttpResult.ok(count);
    }
    @PostMapping("delete")
    public HttpResult delete(Sysconf sysconf){
        Integer count = sysconfService.delete(sysconf);
        System.out.println(count);
        return HttpResult.ok(count);
    }
    @PostMapping("insert")
    public HttpResult insert(Sysconf sysconf){
        Integer count = sysconfService.insert(sysconf);
        System.out.println(count);
        return HttpResult.ok(count);
    }
}

2.10 创建SwaggerConfig

@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)      // 选择swagger2版本
                .apiInfo(apiInfo())         //定义api文档汇总信息
                .select()
                .apis(RequestHandlerSelectors
                        .basePackage("com.wjr.controller"))  // 指定生成api文档的包
                .paths(PathSelectors.any())     // 指定所有路径
                .build()
                ;
    }

    /**
     * 构建文档api信息
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("文档标题")     // 文档标题
                .contact(new Contact("name", "url", "mail"))   //联系人信息
                .description("描述")      //描述
                .version("0.1")     //文档版本号
                .termsOfServiceUrl("http://localhost:8080")     //网站地址
                .build();
    }
}

3.1 使用PostMan测试接口

L}%AIB[A`LQRISM]_T_R76Y.png
6.png

3.2 通过Swagger查看接口文档

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

7.png

你可能感兴趣的:(SpringBoot项目的搭建)