发布时间:2025-06-24 19:35:20 作者:北方职教升学中心 阅读量:072
namespace属性指定了这个映射器的唯一命名空间,通常与对应的Java接口的全限定名一致 --> <select id="findAllUsers" resultType="com.example.login.pojo.User"> <!-- SQL查询语句,用于获取所有用户信息 --> select * from users </select></mapper><!-- 映射器(Mapper)的结束 -->
2.3 service层
service层用于处理从mapper拿到的数据:
package com.example.login.service;import com.example.login.pojo.User;import java.util.List;//接口层public interface UsersService { //查找所有用户 List<User> findAllUsers();}
impl:
package com.example.login.service.impl;import com.example.login.dao.UserMapper;import com.example.login.pojo.User;import com.example.login.service.UsersService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;//实现类@Servicepublic class UsersServiceImpl implements UsersService { @Autowired UserMapper userMapper; @Override public List<User> findAllUsers(){ return userMapper.findAllUsers(); }}
2.4 controller层
package com.example.login.controller;import com.example.login.pojo.User;import com.example.login.service.UsersService;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import java.util.List;@Slf4j@Controllerpublic class UsersController { @Autowired UsersService usersService; @PostMapping("userslist") public ResponseEntity<String> userslist(@RequestBody User user) { //搜索前端传过来的用户名来搜索数据库的用户 User loginUser = usersService.findByName(user.getName()); if(loginUser == null){ return ResponseEntity.ok("用户名不存在"); } if(user.getPassword().equals(loginUser.getPassword())){ return ResponseEntity.ok("success"); } return ResponseEntity.ok("密码错误"); } @PostMapping("adduser") public ResponseEntity<String> adduser(@RequestBody User user) { // 处理请求数据 System.out.println(user.getName()); usersService.addUser(user); // 返回响应 return ResponseEntity.ok("success"); }}
至此,我们的后端架构基本构建完成,项目目录结构图如下:
我们打开数据库,运行LoginApplication主文件:
数据库如图所示:
3. 接口测试
我们利用postman进行接口测试:
GET方法,输入localhost:8080/springboot/usersList
或者打开浏览器,输入localhost:8080/springboot/usersList
接口测试通过,拿到Json数据。
一、Harmonyos登录演示
3. 注册功能
Harmonyos登录演示
注册功能与登录功能类似,我就不具体的描述了,如果有不懂的小伙伴,可以私信小编哦。