SpringBoot
Spring Boot 中文文档 (springdoc.cn)
整合MyBatis
基础-概述-MySQL安装及启动_哔哩哔哩_bilibili
MySQL
下载
使用系统登录是不需要密码的使用enter
即可
要给MySQL添加登录密码,可以按照以下步骤操作:
- 连接到MySQL服务器:
使用终端或命令行工具连接到您的MySQL服务器。您可以使用以下命令连接到MySQL,如果没有密码保护,直接按下Enter键即可访问。mysql -u root
- 更改root用户密码:
连接到MySQL服务器后,可以使用以下命令更改root用户的密码:ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
将
new_password
替换为您要设置的新密码。
- 刷新权限:
更改密码后,使用以下命令刷新权限以使更改生效:FLUSH PRIVILEGES;
- 退出MySQL:
退出MySQL服务器连接,可以使用以下命令:quit;
- 使用新密码登录:
现在您可以使用新密码连接到MySQL服务器:mysql -u root -p
系统将提示输入密码,输入您刚刚设置的新密码。
通过执行以上步骤,您可以为MySQL添加登录密码并确保数据库安全。记得妥善保管好您的密码。
终端客户端连接MySQL
需要将..\MySQL\MySQL_server\bin
添加到系统的path
环境变量中。在命令行使用
mysql -u root -p
-u
后是用户名
- 打开
database
窗口
- 在
Idea
中配置MySQL
数据库连接
打开控制台,添加SQL语句全选后点击左上角的运行。
引入mybatis
的起步依赖和驱动依赖(记得刷新Maven
)
再application.yml
中添加如下配置:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ # 点击数据库查看属性,可以找到这个url
username: root
password: 123456
在Spring Boot中的 MVC(Model-View-Controller)架构,将应用程序的不同功能模块进行分离和组织。
Controller
:Controller层是应用程序的控制层,负责接收用户请求,调用对应的Service处理业务逻辑,并最终返回给用户响应结果。通常一个Controller类会包含多个处理请求的方法,每个方法对应一个具体的请求路径和请求方式。Mapper
(或Dao):Mapper通常用于定义数据库操作的接口,通过注解或XML配置与数据库的映射关系。在MyBatis等持久层框架中,Mapper负责定义数据库的CRUD操作,实现数据的持久化和查询。Service
:Service层主要负责处理业务逻辑,将Controller层传递过来的请求参数进行处理,并调用Mapper层进行数据库操作,最终返回处理结果给Controller层。Service层可以对业务逻辑进行封装、复用和事务管理。
Project.pojo.user
package com.itheima.springboot_quickstart.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 + '\'' +
'}';
}
}
创建基本的数据类
Project.controller.UserController
package com.itheima.springboot_quickstart.controller;
import com.itheima.springboot_quickstart.pojo.User;
import com.itheima.springboot_quickstart.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findById")
public User findById(Integer id) {
return userService.findById(id);
}
}
接受用户请求并将业务逻辑传递给service
Project.service.impl.UserServiceimpl
package com.itheima.springboot_quickstart.service.impl;
import com.itheima.springboot_quickstart.mapper.UserMapper;
import com.itheima.springboot_quickstart.pojo.User;
import com.itheima.springboot_quickstart.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceimpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User findById(Integer id) {
return userMapper.findById(id);
}
}
调用Mapper实现查询数据的业务逻辑,
Project.mapper.UserMapper
package com.itheima.springboot_quickstart.mapper;
import com.itheima.springboot_quickstart.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);
}
在Spring Boot中,@Select注解通常用于MyBatis框架的Mapper接口方法上,用于指定SQL查询语句。具体来说,@Select(“select * from user where id = #{id}”)表示在执行这个Mapper接口方法时,将执行指定的SQL查询语句,查询user表中id字段等于参数id的记录。
其中:
- @Select是MyBatis注解,用于标记查询操作。
- “select * from user where id = #{id}”是SQL查询语句,其中的#{id}是一个占位符,表示要通过参数id传入具体的值进行替换。
- 当Mapper接口方法被调用时,MyBatis框架会将传入的参数值替换掉占位符#{id},然后执行这个SQL查询语句,并将查询结果返回给调用方。
通过@Select注解,可以在Mapper接口方法上直接定义SQL查询语句,简化了与数据库交互的代码编写过程,同时实现了SQL语句和Java代码的分离。这样的方式可以提高代码的可读性和维护性。