目录
1.框架搭建
2.前端搭建
3.后端编写
需求:完成简单的连表条件查询以及添加即可
1.创建springboot项目
2.相关依赖
org.springframework.boot spring-boot-starter-web mysql mysql-connector-java runtime org.projectlombok lombok true com.baomidou mybatis-plus-boot-starter 3.5.1 org.springframework.boot spring-boot-starter-test test 3.配置文件
#数据源信息 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/db_saas_fbms?serverTimezone=Asia/Shanghai&characterEncoding=UTF8 spring.datasource.username=root spring.datasource.password=123456789 #指定映射文件的路径--链表操作 mybatis-plus.mapper-locations=classpath:/mapper/*.xml #sql日志 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
4.数据库:
/* Navicat Premium Data Transfer Source Server : wqg1 Source Server Type : MySQL Source Server Version : 50731 Source Host : localhost:3306 Source Schema : db_saas_fbms Target Server Type : MySQL Target Server Version : 50731 File Encoding : 65001 Date: 25/08/2023 10:20:37 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for ums_agent -- ---------------------------- DROP TABLE IF EXISTS `ums_agent`; CREATE TABLE `ums_agent` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号', `username` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '用户名', `levelid` int(10) NOT NULL COMMENT '等级编号(外键关联代理商等级表)', `nickname` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '昵称', `phonenum` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '电话', `status` int(10) NULL DEFAULT NULL COMMENT '账号状态(1:正常,2:禁用)', `create_time` date NULL DEFAULT NULL COMMENT '创建时间', `icon` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '头像路径', `growth` int(10) NULL DEFAULT NULL COMMENT '成长值', `integration` int(10) NULL DEFAULT NULL COMMENT '积分', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of ums_agent -- ---------------------------- INSERT INTO `ums_agent` VALUES (1, 'admin01', 1, '代理01', '15346342611', 1, '2023-08-25', 'www.baidu.com', 100, 10); INSERT INTO `ums_agent` VALUES (2, 'admin02', 2, '代理02', '15346342622', 1, '2023-08-25', 'www.baidu.com', 100, 10); -- ---------------------------- -- Table structure for ums_agent_level -- ---------------------------- DROP TABLE IF EXISTS `ums_agent_level`; CREATE TABLE `ums_agent_level` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号', `name` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '等级名称', `note` varchar(200) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '描述说明', `growth_point` int(10) NULL DEFAULT NULL COMMENT '等级成长值临界点', `priviledge_birthday` int(10) NULL DEFAULT NULL COMMENT '是否有生日特权', `addtime` date NULL DEFAULT NULL COMMENT '添加时间', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of ums_agent_level -- ---------------------------- INSERT INTO `ums_agent_level` VALUES (1, '白银', '等级1', 1, NULL, '2023-08-25'); INSERT INTO `ums_agent_level` VALUES (2, '黄金', '等级2', 1, NULL, '2023-08-25'); INSERT INTO `ums_agent_level` VALUES (3, '铂金', '等级3', 1, NULL, '2023-08-25'); INSERT INTO `ums_agent_level` VALUES (4, '王者', '等级4', 1, NULL, '2023-08-25'); SET FOREIGN_KEY_CHECKS = 1;
5.实体类
@Data @TableName(value ="ums_agent") public class Agent implements Serializable { /** * 编号 */ @TableId(type = IdType.AUTO) private Integer id; /** * 用户名 */ private String username; /** * 等级编号(外键关联代理商等级表) */ private Integer levelid; /** * 昵称 */ private String nickname; /** * 电话 */ private String phonenum; /** * 账号状态(1:正常,2:禁用) */ private Integer status; /** * 创建时间 */ private Date createTime ; /** * 头像路径 */ private String icon ; /** * 成长值 */ private Integer growth; /** * 积分 */ private Integer integration; @TableField(exist = false) private AgentLevel agentLevel; }
@Data @TableName(value ="ums_agent_level") public class AgentLevel implements Serializable { /** * 编号 */ @TableId(type = IdType.AUTO) private Integer id; /** * 等级名称 */ private String name; /** * 描述说明 */ private String note; /** * 等级成长值临界点 */ private Integer growthPoint; /** * 是否有生日特权 */ private Integer priviledgeBirthday; /** * 添加时间 */ private Date addtime; }
Title 查询 添加 正常 禁用 正常 禁用
1.设置统一返回类型
@NoArgsConstructor @AllArgsConstructor @Data public class Result
{ //表示状态码 private Integer code; //消息提示 private String msg; //响应的数据内容 private Object data; } 2.controller层
@RestController @RequestMapping("/agent") public class AgentController { @Autowired private AgentService agentService; /** * 查询所有 * @param input * @return */ @PostMapping("/query") public Result
> agentList(@RequestParam String input) { List
agentList = agentService.agentList(input); return new Result<>(200,"成功",agentList); } /** * 添加 * @param agent * @return */ @PostMapping("/insert") public Result insert(@RequestBody Agent agent){ agent.setIcon("www.baidu.com"); Date date = new Date(); agent.setCreateTime(date); boolean save = agentService.save(agent); return new Result<>(200,"成功",null); } } @RestController @RequestMapping("/agentLevel") public class AgentLevelController { @Autowired private AgentLevelService agentLevelService; /** * 查询等级 * @return */ @GetMapping("/query") public Result select(){ return agentLevelService.select(); } }
3.service层
public interface AgentService extends IService
{ /** * 查询所有 * @param input * @return */ List agentList(String input); } public interface AgentLevelService { /** * 查询等级 * @return */ Result select(); }
5.serviceImpl层
@Service public class AgentServiceImpl extends ServiceImpl
implements AgentService { @Autowired private AgentDao agentDao; /** * 查询所有 * @param input * @return */ @Override public List agentList(String input) { return agentDao.agentList(input); } } @Service public class AgentLevelServiceImpl implements AgentLevelService { @Autowired private AgentLevelDao agentLevelDao; /** * 查询等级 * @return */ @Override public Result select() { List
agentLevels = agentLevelDao.selectList(null); return new Result<>(200,"查询成功",agentLevels); } } 6.dao层
@Mapper public interface AgentDao extends BaseMapper
{ /** * 查询所有 * @param input * @return */ List agentList(String input); } @Mapper public interface AgentLevelDao extends BaseMapper< AgentLevel> { }
7.
完成