springboot3+vue项目实践-黑马
springboot3+vue3项目实践-黑马
编辑时间:2024/12/30
来源:b站黑马
基础篇
导学课程
前置条件:
后端:javaSE JAVAWeb、SSM框架
前端:html、css、JavaScript
工具:JDK17+、IDEA2021+ 、maven3.5+、vscode
springboot概述
概述
springboot是spring提供的一个子项目,用于快速构建spring应用程序。
spring Framework核心功能:
spring Data 数据获取
spring AMQP 消息传递
spring security 认证连接
spring Cloud 服务治理
spring特性
起步依赖:本质就是一个Maven坐标、整合了完成一个功能需要的所有坐标
自动配置:遵循约定大约配置的原则,在boot程序启动后,一些bean对象会自动注入到ioc容器,不需要手动声明,简化开发
其他特性:内部的tomcat、Jetty(无需部署WAR文件)、外部化配置、不需要XML配置(properties/yml)
springboot入门
创建maven工程
导入spring-boot-stater-web起步依赖编写controller
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
System.out.println("Hello World~");
return "Hello world~";
}
}
提供启动类
//启动类
@SpringBootApplication
扫描itheima下的包(扫描的范围)
//@ComponentScan(basePackages = "com.itheima")
public class SpringbootQuickstartApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootQuickstartApplication.class, args);
}
}
springboot工程建设
手动创建SpringBoot工程
创建maven工程
引入依赖
提供启动类
springboot配置文件
properties配置文件
yaml 配置文件
目的:
实现代码:
跳转路径改变:
开发中一般使用yml文件
yml配置信息书写和获取
1.配置信息书写
值钱边必须有空格,作为分隔符
使用空格作为缩进表示层级关系,相同层级左侧对齐
2.配置信息获取
@Value(“${键名}”)
@ConfigurationProperties(prefix=“前缀”)
springboot整合mybatis
1.创建表
create database if not exists mybatis;
use mybatis;
create table user(
id int unsigned primary key auto_increment comment 'ID',
name varchar(100) comment '姓名',
age tinyint unsigned comment '年龄',
gender tinyint unsigned comment '性别, 1:男, 2:女',
phone varchar(11) comment '手机号'
) comment '用户表';
insert into user(id, name, age, gender, phone) VALUES (null,'白眉鹰王',55,'1','18800000000');
insert into user(id, name, age, gender, phone) VALUES (null,'金毛狮王',45,'1','18800000001');
insert into user(id, name, age, gender, phone) VALUES (null,'青翼蝠王',38,'1','18800000002');
insert into user(id, name, age, gender, phone) VALUES (null,'紫衫龙王',42,'2','18800000003');
insert into user(id, name, age, gender, phone) VALUES (null,'光明左使',37,'1','18800000004');
insert into user(id, name, age, gender, phone) VALUES (null,'光明右使',48,'1','18800000005');
2.创建工程 springbootquickstart
3.导入依赖
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
4.application.yml配置文件
#数据源
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis
username: root
password: 123456
mybatis:
configuration:
map-underscore-to-camel-case: true #开启驼峰命名和下划线命名的转换
5.创建pojo包,创建User.java
package com.itheima.springbootquickstart.pojo;
public class User {
private Integer id;
private String name;
private Short age;
private Short gender;
private String phone;
public User() {
}
public User(Integer id, String name, Short age, Short gender, String phone) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.phone = phone;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Short getAge() {
return age;
}
public void setAge(Short age) {
this.age = age;
}
public Short getGender() {
return gender;
}
public void setGender(Short gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender=" + gender +
", phone='" + phone + '\'' +
'}';
}
}
6.创建mapper包,创建Usermapper.java
package com.itheima.springbootquickstart.mapper;
import com.itheima.springbootquickstart.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface UserMapper {
@Select("select * from user where id= #{id}")
public User findById(Integer id);
}
7.创建Service包,创建UserService.java
package com.itheima.springbootquickstart.service;
import com.itheima.springbootquickstart.pojo.User;
public interface UserService {
public User findById(Integer id);
}
8.创建impl包,创建接口的实现类UserServiceImpl.java
package com.itheima.springbootquickstart.service.impl;
import org.springframework.stereotype.Service;
import com.itheima.springbootquickstart.pojo.User;
import com.itheima.springbootquickstart.service.UserService;
import com.itheima.springbootquickstart.mapper.UserMapper;
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User findById(Integer id){
return userMapper.findById(id);
}
}
9.创建Controller包,创建UserController.java
package com.itheima.springbootquickstart.controller;
import com.itheima.springbootquickstart.pojo.User;
import com.itheima.springbootquickstart.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@RestController
@Validated
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findById")
public User findById(Integer id){
return userService.findById(id);
}
}
效果演示