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);
}
}