SpringBoot教程

2 篇文章 0 订阅
1 篇文章 0 订阅
本文是一篇关于SpringBoot的快速入门教程,包括创建项目、编写Controller、运行测试及整合JUnit和MyBatis的步骤。SpringBoot简化了SpringMVC的配置,提供自动装配功能。同时展示了如何与MyBatis集成,实现数据查询。
摘要由CSDN通过智能技术生成

SpringBoot教程

SpringBoot快速入门,想知道更详细或者SpringBoot与更多主流框架结合还需自己去探索

入门案例

1、创建SpringBoot项目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
接下来所有next,等待依赖下载完毕,把多余的文件先删除
在这里插入图片描述

2、编写Contrtoller

package com.xxxit.Controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController     //@RestController 代替了@Controller和@ResponseBody
@RequestMapping("users")    //路由前缀
public class UserController {

    @GetMapping("/{id}")    //路径参数
    public String selectUser(@PathVariable Integer id){
        System.out.println("获得id:"+id);
        return "{'id':"+id+"}";
    }

}

3、运行main函数并测试

在这里插入图片描述

4、与SpringMVC对比

① SpringMVC手动导入依赖 SpringBoot选择依赖
② SpringMVC要写SpringConfig,ServletConfig等配置文件 SpringBoot不用

整合

SpringBoot整合JUnit

1、新建一个SpringBoot项目
2、编写service层
package com.xxxit.service;

public interface UserService {

    public String select();

}

package com.xxxit.service.Impl;

import com.xxxit.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Override
    public String select() {
        System.out.println("SpringBoot整合JUnit");
        return "SpringBoot整合JUnit";
    }
}
3、编写测试类
package com.xxxit;

import com.xxxit.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest     // 新建SpringBoot项目自带的测试类
class Springbootdemo01ApplicationTests {

    @Autowired      // 新建业务对象,自动装配
    private UserService userService;

    @Test
    void contextLoads() {
        userService.select();       // 测试业务对象方法
    }

}
4、运行测试类

在这里插入图片描述

SpringBoot整合MyBatis

在这里插入图片描述

1、创建一个SpringBoot项目

选择MyBatis Framework和MySQL Driver
在这里插入图片描述
删除多余的文件
在这里插入图片描述

2、编写application.yml
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/db1
    username: root
    password: root

#    yaml文件:后必须有一个空格
#  三种配置文件都存在(优先级)
#  properties>yml>yaml
3、编写实体类
package com.xxxit.domain;

public class User {

    private Integer id;
    private String username;
    private String password;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
4、编写dao层
package com.xxxit.dao;

import com.xxxit.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper     //告诉SpringBoot使用mapper代理开发
public interface UserMapper {

    @Select("select * from tb_user where id = #{id};")
    public User selectById(Integer id);

}
5、编写测试类
package com.xxxit;

import com.xxxit.dao.UserDao;
import com.xxxit.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringbootMybatisDemoApplicationTests {

    @Autowired
    private UserDao userDao;

    @Test
    void contextLoads() {
        User user = userDao.selectById(3);
        System.out.println(user);
    }

}

6、测试输出结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值