SpringBoot是一个运行在后端的程序,若干客户端会不断请求springboot 然后springboot会根据请求和数据库进行交互。
这里我填写了用户名和密码 以及数据库名称 记得勾选默认架构
点击连接测试 需要下载驱动 点击下载
点击表格数据 更改数据 回车 点击上面绿色箭头 上传数据至数据库
可以看到后台数据库已经更新
仓库地址https://mvnrepository.com/
Spring Boot Starter JDBC
复制以下内容,将依赖粘贴到pom.xml中
点击右侧Maven重新加载
这里的kob是数据库名称
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
package com.kob.backedn2.pojo;// 数据库一张表对应一个类 @data 将常用的get set方法 添加进去
//@NoArgsConstructor 无参构造函数
//@AllArgsConstructor 有参构造函数
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {private Integer id;private String username;private String password;
}
package com.kob.backedn2.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kob.backedn2.pojo.User;
import org.apache.ibatis.annotations.Mapper;// 将增删改查的操作映射成sql语句
@Mapper
public interface UserMapper extends BaseMapper {}
package com.kob.backedn2.controller.user;
import com.kob.backedn2.mapper.UserMapper;
import com.kob.backedn2.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
public class UserController {@AutowiredUserMapper userMapper;// 注解开发 获取所有的用户@GetMapping("/user/all/")public List getAll(){return userMapper.selectList(null);}}
查询指定用户的信息
package com.kob.backedn2.controller.user;import com.kob.backedn2.mapper.UserMapper;
import com.kob.backedn2.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
public class UserController {@AutowiredUserMapper userMapper;
// 注解开发 获取所有的用户@GetMapping("/user/all/")public List getAll(){return userMapper.selectList(null);}// 查询指定id的信息 userid 使用{} 进行包围@GetMapping("/user/{userId}")public User getuser(@PathVariable int userId){return userMapper.selectById(userId);}
}
package com.kob.backedn2.controller.user;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kob.backedn2.mapper.UserMapper;
import com.kob.backedn2.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;@RestController
public class UserController {@AutowiredUserMapper userMapper;
// 注解开发 获取所有的用户@GetMapping("/user/all/")public List getAll(){return userMapper.selectList(null);}
// 查询指定id的信息 userid 使用{} 进行包围@GetMapping("/user/{userId}/")public List getuser(@PathVariable int userId){QueryWrapper queryWrapper = new QueryWrapper<>();queryWrapper.ge("id",2).le("id",3);return userMapper.selectList(queryWrapper);}
}
后端controller层代码 解析浏览器地址栏输入的信息 然后userMapper将数据写入数据库
package com.kob.backedn2.controller.user;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kob.backedn2.mapper.UserMapper;
import com.kob.backedn2.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
public class UserController {@AutowiredUserMapper userMapper;
// 注解开发 获取所有的用户@GetMapping("/user/all/")public List getAll(){return userMapper.selectList(null);}
// 查询指定id的信息 userid 使用{} 进行包围@GetMapping("/user/{userId}/")public User getuser(@PathVariable int userId){QueryWrapper 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){User user = new User(userId,username,password);userMapper.insert(user);return "Add User Successfully";}
}
// 删除用户 判断urL格式 然后调用该函数@GetMapping("/user/delete/{userId}/")public String deleteUser(@PathVariable int userId){userMapper.deleteById(userId);// 根据Id删除用户return "Delete User Successfully";}
org.springframework.boot spring-boot-starter-security 3.0.0
密码会重新生成,输入登录,退出之后各种api就不能正常使用 登陆进去之后可以使用
username:user
password:控制台会输出 一串密码
package com.kob.backedn2.service.impl;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kob.backedn2.mapper.UserMapper;
import com.kob.backedn2.pojo.User;
import com.kob.backedn2.service.impl.utils.UserDetailsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;@Service
public class UserDetailsServiceImpl implements UserDetailsService {@Autowiredprivate UserMapper userMapper;@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {QueryWrapper queryWrapper = new QueryWrapper<>();queryWrapper.eq("username", username);User user = userMapper.selectOne(queryWrapper);if (user == null) {throw new RuntimeException("用户不存在");}return new UserDetailsImpl(user);}
}
package com.kob.backedn2.service.impl.utils;import com.kob.backedn2.pojo.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;import java.util.Collection;@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserDetailsImpl implements UserDetails {private User user;@Overridepublic Collection extends GrantedAuthority> getAuthorities() {return null;}@Overridepublic String getPassword() {return user.getPassword();}@Overridepublic String getUsername() {return user.getUsername();}@Overridepublic boolean isAccountNonExpired() {return true;}@Overridepublic boolean isAccountNonLocked() {return true;}@Overridepublic boolean isCredentialsNonExpired() {return true;}@Overridepublic boolean isEnabled() {return true;}
}
package com.kob.backedn2.config;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 {@Beanpublic PasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}
}