Spring Boot - 使用Spring Boot构建SM项目

重点内容前言:第一次使用Spring Boot开发项目,之前时从Spring+Spring Mvc+MyBatis过渡过来的,先来个小Demo。

一、首先我们需要先创建一个基于SpringBoot的项目(我这里使用的是IDEA)
1、首先打开IDEA,new project
2、点击Spring Initializr,然后点击Next
这里写图![这里写图片描述
3、创建项目
这里写图片描述
这里写图片描述
这里写图片描述

二、搭建SM架构
1、找到pom文件
2、在pom文件中添加依赖jar包

 <!-- 创建SpringMvc、MyBatis项目所需要添加的依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>

三、基于此架构进行一些test功能编写
流程:
前端Restful Get请求到服务端,根据id查询Student数据。

项目包结构:
这里写图片描述
1、首先需要在pom文件中添加操作数据库的依赖

<!-- 数据库链接依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
         <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
         <!-- 面向切面编程所需要添加的依赖,在这里主要是用来进行事务的控制 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

2、配置数据库连接信息
找到application.properties,然后将数据库连接信息添加进去
这里写图片描述

spring.datasource.url=jdbc:mysql://localhost:3306/springboottest
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
mybatis.type-aliases-package=com.example.models

3、然后再进行Controller、Dao层代码的编写
Controller层

/**
 * Created by crg on 2018/7/17 10:15
 */
@RestController
public class StudentController {

    //注入Dao层,如果无法注入请检查SpringBoot启动类是否和Controller、Dao在一级目录。
    @Autowired
    private StudentMapper studentMapper;

    @RequestMapping(value = "/student/{id}" , method = RequestMethod.GET)//Restful接口,指定接口请求为Post
    //注意PathVariable中的值必须和value中请求参数的value一致
    public Student findStudentById(@PathVariable("id") Integer sid){

        //根据id查询学生数据
        if (sid != null){
            Student student = studentMapper.querryStudentById(sid);

            return student;

        }else {

            return null;
        }
    }
}

Dao层

/**
 * Created by crg on 2018/7/17 10:25
 */
@Repository
public interface StudentMapper {

    /**
     * 根据id查询学生数据
     */
    @Select("SELECT * FROM student WHERE sid = #{id}")
    Student querryStudentById(Integer id);
}

Pojo

/**
 * Created by crg on 2018/7/17 10:12
 */
public class Student {

    private Integer sid;
    private String sname;
    private String sage;

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSage() {
        return sage;
    }

    public void setSage(String sage) {
        this.sage = sage;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                ", sage='" + sage + '\'' +
                '}';
    }
}

4、在SpringBoot启动类中配置一些包扫描以及事务控制

@SpringBootApplication
//事务控制注解
@EnableTransactionManagement
//Dao层包扫描
@MapperScan("com.guttv.dao")
//Controller层包扫描
@ComponentScan("com.guttv.controller")
public class SpringbootdemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootdemoApplication.class, args);
    }
}

四、启动
直接在SpringBoot启动类中右键启动,或者在行号那一列点击三角启动按钮。

五、访问
发送Get请求测试,我的访问地址为:http://localhost:8080/student/2

猜你喜欢

转载自blog.csdn.net/m0_37602117/article/details/81087428