springboot和mybatis的整合—黑马上课学习笔记

web开发的基础

  运用的是BS架构:这种架构是一种浏览器/服务器模式
  客户端只需要浏览器,而数据存储和逻辑都在服务端

HTTP协议:

 是一种超文本传输协议,规定了浏览器和服务器之间的规则

Web服务器:

负责解析HTTP协议,解析请求数据,并发送响应数据

Tomcat:

springboot内置了Tomcat

SpringBootWeb+Mybatis入门

springboot和mybatis的整合—黑马上课学习笔记_第1张图片

步骤:
1.创建springboot工程,在创建时选择web、lombak、mybatis、mysql等依赖
2.在properties中配置四要素(username、password、url、Driver(mysql驱动))+日志+启动驼峰命名
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.150.101:3306/base_framework
#开启驼峰命名
mybatis.configuration.map-underscore-to-camel-case=true
#日志
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
3.首先创建domain包,在创建实体类dish,不过要确保数据库有dish的数据
create database base_framework default charset utf8mb4;
use base_framework;
drop table if exists dish;
CREATE TABLE `dish` (
  `id` int  PRIMARY KEY AUTO_INCREMENT COMMENT 'ID',
  `name` varchar(20) DEFAULT NULL COMMENT '名称',
  `category_id` int DEFAULT NULL COMMENT '分类ID',
  `price` double(10,2) DEFAULT NULL COMMENT '价格',
  `image` varchar(100) DEFAULT NULL COMMENT '图片名称',
  `description` varchar(200) DEFAULT NULL COMMENT '描述',
  `status` tinyint NOT NULL COMMENT '状态, 1:启售, 0:停售',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间'
)  COMMENT='菜品表';

INSERT INTO `dish` VALUES (1,'辣子鸡',1,78.00,'f966a38e-0780-40be-bb52-5699d13cb3d9.jpg','来自鲜嫩美味的小鸡,值得一尝',1,'2022-05-27 09:38:43','2022-05-27 09:38:43');
INSERT INTO `dish` VALUES (2,'毛氏红烧肉',1,68.00,'0a3b3288-3446-4420-bbff-f263d0c02d8e.jpg','毛氏红烧肉毛氏红烧肉,确定不来一份?',1,'2022-05-27 09:40:19','2022-05-27 09:40:19');
INSERT INTO `dish` VALUES (3,'组庵鱼翅',1,48.00,'740c79ce-af29-41b8-b78d-5f49c96e38c4.jpg','组庵鱼翅,看图足以表明好吃程度',1,'2022-05-27 09:41:19','2022-05-27 09:41:19');
INSERT INTO `dish` VALUES (4,'霸王别姬',1,128.00,'057dd338-e487-4bbc-a74c-0384c44a9ca3.jpg','还有什么比霸王别姬更美味的呢?',1,'2022-05-27 09:43:08','2022-05-27 09:43:08');
INSERT INTO `dish` VALUES (5,'全家福',1,118.00,'a53a4e6a-3b83-4044-87f9-9d49b30a8fdc.jpg','别光吃肉啦,来份全家福吧,让你长寿又美味',1,'2022-05-27 09:44:08','2022-05-27 09:44:08');
INSERT INTO `dish` VALUES (6,'邵阳猪血丸子',1,138.00,'2a50628e-7758-4c51-9fbb-d37c61cdacad.jpg','看,美味不?来嘛来嘛,这才是最爱吖',1,'2022-05-27 09:45:12','2022-05-27 09:45:12');
INSERT INTO `dish` VALUES (7,'口味蛇',2,168.00,'0f4bd884-dc9c-4cf9-b59e-7d5958fec3dd.jpg','爬行界的扛把子,东兴-口味蛇,让你欲罢不能',1,'2022-05-27 09:46:23','2022-05-27 09:46:23');
INSERT INTO `dish` VALUES (8,'辣子鸡丁',2,88.00,'ef2b73f2-75d1-4d3a-beea-22da0e1421bd.jpg','辣子鸡丁,辣子鸡丁,永远的魂',1,'2022-05-27 09:49:16','2022-05-27 09:49:16');
INSERT INTO `dish` VALUES (9,'麻辣兔头',2,198.00,'2a2e9d66-b41d-4645-87bd-95f2cfeed218.jpg','麻辣兔头的详细制作,麻辣鲜香,色泽红润,回味悠长',1,'2022-05-27 09:52:24','2022-05-27 09:52:24');
INSERT INTO `dish` VALUES (10,'蒜泥白肉',2,98.00,'d2f61d70-ac85-4529-9b74-6d9a2255c6d7.jpg','多么的有食欲啊',1,'2022-05-27 09:54:30','2022-05-27 09:54:30');
INSERT INTO `dish` VALUES (11,'鱼香肉丝',2,38.00,'8dcfda14-5712-4d28-82f7-ae905b3c2308.jpg','鱼香肉丝简直就是我们童年回忆的一道经典菜,上学的时候点个鱼香肉丝盖饭坐在宿舍床上看着肥皂剧,绝了!现在完美复刻一下上学的时候感觉',1,'2022-05-27 09:55:13','2022-05-27 09:55:13');
INSERT INTO `dish` VALUES (12,'麻辣水煮鱼',2,148.00,'1fdbfbf3-1d86-4b29-a3fc-46345852f2f8.jpg','鱼片是买的切好的鱼片,放几个虾,增加味道',1,'2022-05-27 09:58:15','2022-05-27 09:58:15');
INSERT INTO `dish` VALUES (13,'鱼香炒鸡蛋',2,20.00,'0f252364-a561-4e8d-8065-9a6797a6b1d3.jpg','鱼香菜也是川味的特色。里面没有鱼却鱼香味',1,'2022-05-27 09:59:06','2022-05-27 09:59:06');
INSERT INTO `dish` VALUES (14,'脆皮烧鹅',3,128.00,'e476f679-5c15-436b-87fa-8c4e9644bf33.jpeg','“广东烤鸭美而香,却胜烧鹅说古冈(今新会),燕瘦环肥各佳妙,君休偏重便宜坊”,可见烧鹅与烧鸭在粤菜之中已早负盛名。作为广州最普遍和最受欢迎的烧烤肉食,以它的“色泽金红,皮脆肉嫩,味香可口”的特色,在省城各大街小巷的烧卤店随处可见。',1,'2022-05-27 10:20:27','2022-05-27 10:20:27');
INSERT INTO `dish` VALUES (15,'白切鸡',3,66.00,'9ec6fc2d-50d2-422e-b954-de87dcd04198.jpeg','白切鸡是一道色香味俱全的特色传统名肴,又叫白斩鸡,是粤菜系鸡肴中的一种,始于清代的民间。白切鸡通常选用细骨农家鸡与沙姜、蒜茸等食材,慢火煮浸白切鸡皮爽肉滑,清淡鲜美。著名的泮溪酒家白切鸡,曾获商业部优质产品金鼎奖。湛江白切鸡更是驰名粤港澳。粤菜厨坛中,鸡的菜式有200余款之多,而最为人常食不厌的正是白切鸡,深受食家青睐。',1,'2022-05-27 10:21:48','2022-05-27 10:21:48');

创建数据库数据后,创建实体类dish

import lombok.Data;
import java.time.LocalDateTime;
@Data
public class Dish {
    private Integer id;
    private String name;//菜品名称
    private Integer categoryId;    //菜品分类id
    private double price;    //菜品价格
    private String image;    //图片
    private String description;    //描述信息
    private Integer status;    //0 停售 1 起售
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
}
4.创建mapper包,并且创建DishMapper类;并在类上面添加Mapper,将接口对象添加到IOC容器中
DishMapper接口中创建`List listAll();`
package com.itheima.mapper;

import com.itheima.domain.Dish;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
@Mapper
public interface DishMapper {
    @Select("select * from dish;")
    List<Dish> listAll();
}
5.创建Service包,并在Service包下创建DishService接口,创建listAll方法
package com.itheima.service;

import com.itheima.domain.Dish;

import java.util.List;

public interface DishService {

    List<Dish> listAll();
}
6.创建在Service包下创建Impl包,在Impl包下创建DishServiceimpl类,实现DishService接口,注入DishMapper
package com.itheima.service.impl;

import com.itheima.domain.Dish;
import com.itheima.mapper.DishMapper;
import com.itheima.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Override
    public List<Dish> listAll() {
        return dishMapper.listAll();
    }
}
7.创建Controller包,在该包下创建DishController类,在DishController中需要拥有三个内容:
在类上面添加RestController
1.Service业务处理,以及注解Autowired
2.接收请求的参数
3.响应结果
package com.itheima.controller;

import com.itheima.domain.Dish;
import com.itheima.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DishController {
    @Autowired
    private DishService dishService;

    @RequestMapping("/dish/listAll")
    public List<Dish> listAll(){

        return dishService.listAll();
    }
}

8.测试
启动测试类,打开浏览器输入:

http://localhost:8080//dish/listAl

学习笔记:
图例演示:
springboot和mybatis的整合—黑马上课学习笔记_第2张图片
springboot和mybatis的整合—黑马上课学习笔记_第3张图片
springboot和mybatis的整合—黑马上课学习笔记_第4张图片
springboot和mybatis的整合—黑马上课学习笔记_第5张图片
springboot和mybatis的整合—黑马上课学习笔记_第6张图片
springboot和mybatis的整合—黑马上课学习笔记_第7张图片
springboot和mybatis的整合—黑马上课学习笔记_第8张图片
springboot和mybatis的整合—黑马上课学习笔记_第9张图片

你可能感兴趣的:(mybatis,spring,boot,java)