SpringBoot入门案例 (idea联网版)
选择Spring Initializr,选择jdk版本,其他都不用动,选择当前模块使用的技术集
选择Spring web
package controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//Rest模式
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping
public String getById(){
System.out.println("spingboot is runing..");
return "springboot is running";
}
}
运行成功
到目前为止一个最简单的boot程序已经完成
这两个文件就组成了整体的结构
springboot对比spring的优点:就只需要写一个控制类。
最重要的一点是springboot需要联网操作
小结
SpringBoot创建 (idea不能联网,官网创建)
搜索spring官网
拉到最下面,点击Spring Initializr
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-z11IiGRT-1641018122084)(https://cdn.jsdelivr.net/gh/1wenjinjie/picture/img/202112251244884.png)]
springboot创建 (阿里云版)
总结:国内访问spring官网可能回很慢,可以使用阿里云的网站,速度回变快
SpringBoot简介
小结:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tnHSRY7x-1641018122089)(https://cdn.jsdelivr.net/gh/1wenjinjie/picture/img/202112271900273.png)]
@SpringBootApplication
public class SpringBoot0101QuickstartApplication {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(SpringBoot0101QuickstartApplication.class, args);
BookController bean=ctx.getBean(BookController.class);
System.out.println("bean====>"+bean);
}
}
变更依赖包
小结:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EG7YOguZ-1641018122091)(https://cdn.jsdelivr.net/gh/1wenjinjie/picture/img/202112271949920.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8rCTOEfo-1641018122091)(https://cdn.jsdelivr.net/gh/1wenjinjie/picture/img/202112271954490.png)]
小结
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cGAxbRvJ-1641018122092)(https://cdn.jsdelivr.net/gh/1wenjinjie/picture/img/202112272012263.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rIjNll0g-1641018122092)(https://cdn.jsdelivr.net/gh/1wenjinjie/picture/img/202112272014305.png)]
server.port=80
server:
port:81
server:
port:82
1 .yml(主流)
2 .yaml
package com.itheima.springboot_01_02quickstart;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
//1.定义数据模型封装yaml文件对应的数据
//2.定义spring管控的bean
@Component
//3.指定加载的数据
@ConfigurationProperties(prefix = "datasource")
public class MyDataSource {
private String driver;
private String url;
private String username;
private String password;
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
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;
}
@Override
public String toString() {
return "MyDataSource{" +
"driver='" + driver + '\'' +
", url='" + url + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
// datasource:
// driver: com.mysql.jdbc.Driver
// url: jdbc:mysql://localhost/springboot_db
// username: root
// password: root666
小结:
小结:
小结:
package com.itheima.domain;
public class Book {
private Integer id;
private String type;
private String name;
private String description;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", type='" + type + '\'' +
", name='" + name + '\'' +
", description='" + description + '\'' +
'}';
}
}
package com.itheima.dao;
import com.itheima.domain.Book;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface BookDao {
@Select("select * from tbl_book where id= #{id}")
public Book getById(Integer id);
}
package com.itheima.springboot_05_mybitis;
import com.itheima.dao.BookDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot05MybitisApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void contextLoads() {
System.out.println(bookDao.getById(1));
}
}
小结:
先创建普通的项目(需要勾选1个mysql driver)
然后去maven官网找
#2.??????
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
username: root
password: 123456
#设置Mp相关的配置
mybatis-plus:
global-config:
db-config:
table-prefix: tbl_
小结:
创建项目,和之前一样勾选两个
导入依赖包坐标
小结:
总体:
Lombok,一个Java类库,提供了一组注解,简化POJO实体类开发
如何使用
package com.itheima.springboot_08_ssmp.dao;
import com.itheima.dao.BookDao;
import com.itheima.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class BookDaoTest {
@Autowired
private BookDao bookDao;
@Test
void testGetId(){
System.out.println(bookDao.selectById(5));
}
@Test
void testSave(){
Book book = new Book();
book.setType("测试数据123");
book.setName("测试数据123");
book.setDescrption("测试数据123");
bookDao.insert(book);
}
@Test
void testUpdate(){
Book book = new Book();
book.setId(9);
book.setType("测试数据123aaa");
book.setName("测试数据123");
book.setDescrption("测试数据123");
bookDao.updateById(book);
}
@Test
void testDelete(){
bookDao.deleteById(10);
}
@Test
void testGetAll(){
System.out.println( bookDao.selectList(null));
}
@Test
void testGetPage(){
}
@Test
void testGetBy(){}
}
小结:
@Test
void testGetPage(){
IPage page = new Page(2,5);
bookDao.selectPage(page,null);
}
package com.itheima.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration //配置类
public class MPconfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); //分页拦截器
return interceptor;
}
}
使用QueryWrapper对象封装查询条件,推荐使用LambdaQueryWrapper对象,所有查询操作封装方法调用
所有查询操作封装成方法调用
查询条件支持动态条件拼装
@Test
void testGetBy(){
QueryWrapper<Book> qw = new QueryWrapper<Book>();
qw.like("name","spring");
bookDao.selectList(qw);
}
@Test //使用Lambda表达式减少写错
void testGetBy2(){
String name = "spring";
LambdaQueryWrapper<Book> lqw=new LambdaQueryWrapper<Book>();
lqw.like(name!=null,Book::getName,name);
bookDao.selectList(lqw);
}
业务层开发
package com.itheima.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.domain.Book;
import java.util.List;
public interface BookService {
Boolean save(Book book);
Boolean update(Book book);
Boolean delete(Integer id);
Book getById(Integer id);
List<Book> getAll();
IPage<Book> getPage(int currentPage,int pageSize);
}
package com.itheima.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dao.BookDao;
import com.itheima.domain.Book;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service //业务层对应的bean
public class BookServiceImpl implements BookService {
//转调数据层,进行注入
@Autowired
private BookDao bookDao;
@Override
public Boolean save(Book book) {
//将业务状态设置为操作是否完成
return bookDao.insert(book)>0;
}
@Override
public Boolean update(Book book) {
return bookDao.updateById(book)>0;
}
@Override
public Boolean delete(Integer id) {
return bookDao.deleteById(id)>0;
}
@Override
public Book getById(Integer id) {
return bookDao.selectById(id);
}
@Override
public List<Book> getAll() {
return bookDao.selectList(null);
}
@Override
public IPage<Book> getPage(int currentPage, int pageSize) {
IPage page = new Page(currentPage,pageSize);
bookDao.selectPage(page,null);
return page;
}
}
package com.itheima.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class BookServiceTestCase {
@Autowired
private BookService bookService;
@Test
void testGetById(){ //业务层需要打印出来
System.out.println(bookService.getById(4));
}
@Test
void testSave(){
Book book = new Book();
book.setType("测试数据122223");
book.setName("测试数据123");
book.setDescrption("测试数据123");
bookService.save(book);
}
@Test
void testUpdate(){
Book book = new Book();
book.setId(9);
book.setType("测试123aaa");
book.setName("测试数据123");
book.setDescrption("测试数据123");
bookService.update(book);
}
@Test
void testDelete(){
bookService.delete(10);
}
@Test
void testGetAll(){
bookService.getAll();
}
@Test
void testGetPage(){
IPage<Book> page = bookService.getPage(2, 5);
}
}
小结:
package com.itheima.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class BookServiceTest {
@Autowired
private IBookService bookService;
@Test
void testGetById(){ //业务层需要打印出来
System.out.println(bookService.getById(4));
}
@Test
void testSave(){
Book book = new Book();
book.setType("测试数据122223");
book.setName("测试数据123");
book.setDescrption("测试数据123");
bookService.save(book);
}
@Test
void testUpdate(){
Book book = new Book();
book.setId(9);
book.setType("测试123aaa");
book.setName("测试数据123");
book.setDescrption("测试数据123");
bookService.updateById(book);
}
@Test
void testDelete(){
bookService.removeById(12);
}
@Test
void testGetAll(){
bookService.list();
}
@Test
void testGetPage(){
IPage<Book> page = new Page<>(2,5);
bookService.page(page);
}
}
小结:
表现层开发
@Autowired
private IBookService bookService;
@GetMapping
public List<Book> getAll(){
return bookService.list();
}
@PostMapping
public Boolean save(@RequestBody Book book){
return bookService.save(book);
}
@PutMapping
public Boolean update(@RequestBody Book book){
return bookService.modifyBook(book);
}
@DeleteMapping("{id}")
public Boolean delete(@PathVariable Integer id){
return bookService.delete(id);
}
@GetMapping("{id}")
public Book getById(@PathVariable Integer id){
return bookService.getById(id);
}
@GetMapping("{currentPage}/{pageSize}")
public IPage<Book> getPage(@PathVariable int currentPage,@PathVariable int pageSize){
return bookService.getPage(currentPage, pageSize);
}
小结:
@Autowired
private IBookService bookService;
@GetMapping
public R getAll(){
return new R(true,bookService.list());
}
@PostMapping
public R save(@RequestBody Book book){
return new R(bookService.save(book));
}
@PutMapping
public R update(@RequestBody Book book){
return new R(bookService.modifyBook(book));
}
@DeleteMapping("{id}")
public R delete(@PathVariable Integer id){
return new R(bookService.delete(id));
}
@GetMapping("{id}")
public R getById(@PathVariable Integer id){
return new R(true,bookService.getById(id));
}
@GetMapping("{currentPage}/{pageSize}")
public R getPage(@PathVariable int currentPage,@PathVariable int pageSize){
return new R(true,bookService.getPage(currentPage, pageSize));
}
小结:
前后端协议联调
小结:
小结:
小结:
小结:
加载分页数据
分页页码切换
小结:
小结: