springboot+mybatis学习笔记(一)_构建springboot项目

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。SpringBoot项目有替代springMVC项目的趋势,学习SpringBoot与SpringCloud项目不得不提上日程,也许你把springMVC项目玩的溜溜的,轻而易举的就能掌握SpringBoot项目,但也不能因为这个放缓接触SpringBoot的脚步。每每被问起有没有接触过SpringBoot与SpringCloud时,心中都有一些心虚,但接触之后,感觉并没什么可特别难理解的,思想跟springMVC相近,只是用法更加简洁。现在将学习笔记贴出,以备以后的预习巩固。

第一步:构建SpringBoot项目

这里我选择先搭建一个基于SpringBoot的web项目,再向里面集成Mybatis,更好的理解两者之间的集成,而没有一次将所有的框架集成进来。

构建SpringBoot项目的官方网址:https://start.spring.io/

构建SpringBoot

在这个页面中,可以根据自己需要,选择是构建gradle项目还是maven项目;可以选择SpringBoot的版本。如果以后熟悉了这个结构,可以点最下面的switch to the full version 进行完整的框架选择。这里我构建gradle项目,版本选择2.1.2。输入组名和项目名称后点击最下面的构建工程就可以完成项目的构建了。

第二步:导入SpringBoot项目

下面是我点击Generate Project后下载下来的项目

SpringBoot项目

下面我们将项目导入到IDEA中:

将项目解压到一个目录下,目录中最好没有中文,防止可能出现的不必要的麻烦。点击目录:File->Open...,在弹出的页面中找到解压好的项目,点击确定,我这里勾选了自动导入依赖包和使用本地的gradle版本生产项目,具体图示如下:

导入成功后就是如下的目录结构:

启动下项目,看是否可以正常运行:

在启动类上右击,选择run或者debug项目,即可运行项目

查看控制台,出现springBoot启动特有图案,说明项目可以正常启动,但由于项目目前是空框架,所以启动后自动退出了:

第三步:简单的mvc结构

我们搭建好了SpringBoot项目,下一步我们就要开始开发我们的web项目:

因为要构建web项目,所以引入了一个spring-boot-starter-web包,用的是implementation而不是compile,如果你使用的也是gradle,这两个导入方式的区别可以了解下。整合了web开发的框架:

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	implementation('org.springframework.boot:spring-boot-starter-web')
}

TestController.java 

package com.example.SpringBootMybatisDemo.test.controller;

import com.example.SpringBootMybatisDemo.test.entity.Student;
import com.example.SpringBootMybatisDemo.test.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/test")
public class TestController {
    @Autowired
    TestService service;

    @ResponseBody
    @RequestMapping("/getStudents")
    public List<Student> getStus(){
        return service.getStudents();
    };
}

因为我要返回json格式的数据,这里加了一个@ResponseBody标签,可以将返回结果自动封装成json,如果不加这个注解,默认返回的是个页面,将报404错误。 

TestService.java 

package com.example.SpringBootMybatisDemo.test.service;

import com.example.SpringBootMybatisDemo.test.entity.Student;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class TestService {
    public List<Student> getStudents() {
        List<Student> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Student stu=new Student();
            stu.setName("王"+i);
            stu.setNum(i);
            stu.setSex(i%2==0?"男":"女");
            list.add(stu);
        }
        return list;
    }
}

Student.java 

package com.example.SpringBootMybatisDemo.test.entity;

public class Student {
    private int num;
    private String name;
    private String sex;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

运行SpringBoot项目,这样一个简单的web项目就构建完成了:

至此,这个基于springBoot的web项目就搭建完成了,源码放在github:https://github.com/827164881/SpringBootMybatis,源码会根据教程持续更新,下一篇文档打算写写SpringBoot集成MyBatis。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值