安装mysql
讲义里有详细的安装流程:
讲义地址
记得添加环境变量,是为了在任何目录下都能运行mysql;
这里讲下如果之前想删除mysql的,需要重新安装的请注意:
1.停止mysql服务
2.删除软件
3.删除数据库数据存放位置,如果是默认的地址,就是 C:\ProgramData\MySQL
一般mysql如果版本是8.0以上的,不删也是可以的,一台主机可以安装多个mysql;
连接数据库,创建kob数据库
首先,先用管理员权限打开cmd(输入cmd后 按Ctrl+Shift+Enter
);
IDEA图形化操作数据库
初次下载等个大概10分钟即可,记得要等它下载好了再往下走
如果出现了以下类似提示,那得注意中英文符号以及语法方面
配置SpringBoot在pom.xml文件中添加依赖
在pom.xml
文件中添加依赖:
Spring Boot Starter JDBC
Project Lombok
帮我们简化代码帮我自动写一些构造函数
MySQL Connector/J
mybatis-plus-boot-starter
MyBatis Plus 封装了许多sql语句
mybatis-plus-generator
spring-boot-starter-security
最后面那个先别装
jjwt-api
在application.properties中添加数据库配置
SpringBoot如果想访问数据库的话需要做一些配置
需要通过用户和密码访问
spring.datasource.username=root
spring.datasource.password=123456//这里之前设置什么密码,就填那个密码
spring.datasource.url=jdbc:mysql://localhost:3306/kob?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
改写下根路径下内容
indexController.java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class indexController {
@RequestMapping("/")
public String index(){
return "pk/index.html";
}
}
POJO
@Data //简化了get set tostring
@NoArgsConstructor//无参构造
@AllArgsConstructor//有参构造
public class User {
private Integer id;
private String username;
private String password;
}
DAO
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
Controller
UserController.java
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kob.backend.mapper.UserMapper;
import com.kob.backend.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
UserMapper userMapper;
@GetMapping("/user/all/")
public List<User>getAll(){
return userMapper.selectList(null);
}
@GetMapping("/user/{userId}/")
public User getuser(@PathVariable int userId){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id",userId);
return userMapper.selectOne(queryWrapper);
}
@GetMapping("/user/add/{userId}/{username}/{password}/")
public String addUser(@PathVariable int userId,@PathVariable String username,@PathVariable String password){
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String encodedPassword = passwordEncoder.encode(password);
User user = new User(userId,username,encodedPassword);
userMapper.insert(user);
return "Add User Successfully";
}
@GetMapping("/user/delete/{userId}/")
public String deleteUser(@PathVariable int userId){
userMapper.deleteById(userId);
return "Delete User Successfully";
}
}
密码加密
实现config.SecurityConfig
类,用来实现用户密码的加密存储
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
最后还有个小问题关于pom.xml
这个报错了,但却能运行hhh
原因是因为版本号没有和父项目保持一致。
得看下面的图中版本号是否一样,不一样改过来就行,实在不行,添加版本号
附注:
1.所有的依赖,我都是安装最新的版本,运行起来没问题
2.密码加密后,记得把数据库里的密码改成加密形式的
3.记得保存下代码git