第六章登陆与注册 本章主要内容如下
登陆注册相关
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.hospital.patient.wx.api.db.dao.UserDao"> <!-- 添加患者账户 --> <insert id="insertPatient" parameterType="com.example.hospital.patient.wx.api.db.pojo.UserEntity"> UPSERT INTO HOSPITAL.PATIENT_USER ( id, open_id, nickname, photo, sex, status, create_time ) VALUES ( NEXT VALUE FOR HOSPITAL.PATIENT_USER_SEQUENCE, #{openId}, #{nickname}, #{photo}, #{sex}, #{status}, CURRENT_TIMESTAMP ) </insert> <!-- 检查用户是否已注册 --> <select id="searchAlreadyRegistered" parameterType="String" resultType="Integer"> SELECT id FROM HOSPITAL.PATIENT_USER WHERE open_id = #{openId} LIMIT 1 </select> <!-- 患者登录 --> <select id="patientLogin" parameterType="map" resultType="com.example.hospital.patient.wx.api.db.pojo.UserEntity"> SELECT id, open_id, nickname, photo, sex, status, create_time FROM HOSPITAL.PATIENT_USER WHERE open_id = #{openId} AND password = #{password} </select> </mapper>
以下是在com.example.hospital.patient.wx.api.db.dao.UserDao
接口中声明相关DAO方法的代码:
public interface UserDao { /** * 插入用户实体对象到数据库中 * @param entity 要插入的用户实体 * @return 受影响的行数 */ @Transactional int insert(UserEntity entity); /** * 根据openId查询是否已经注册过 * @param openId 用户的唯一标识openId * @return 如果已经注册返回对应的用户ID,如果未注册返回null */ Integer searchAlreadyRegistered(String openId); }
服务层代吗
@Service public class UserServiceImpl implements UserService { @Value("${wechat.app - id}") private String appId; @Value("${wechat.app - secret}") private String appSecret; @Resource private UserDao userDao; @Resource private UserInfoCardDao userInfoCardDao; @Override public HashMap<String, Object> loginOrRegister(String code, String nickname, String photo, String sex) { // 用临时授权兑换openId String openId = this.getOpenId(code); HashMap<String, Object> map = new HashMap<>(); // 是否为已注册用户 Integer id = userDao.searchAlreadyRegistered(openId); if (id == null) { UserEntity entity = new UserEntity(); entity.setOpenId(openId); entity.setNickname(nickname); entity.setPhoto(photo); entity.setSex(sex); userDao.insert(entity); map.put("success", true); map.put("message", "注册成功"); map.put("userId", entity.getId()); } else { map.put("success", true); map.put("message", "登录成功"); map.put("userId", id); } return map; } private String getOpenId(String code) { // 这里应该是调用微信相关接口,根据code获取openId的逻辑 // 例如使用http请求调用微信的API,这里只是模拟返回一个openId return "mockOpenId"; } }
以下是截图
然后服务层,和实现
控制层
效果二
标签:openId,String,map,entity,全栈,springboot,put,id,复习 From: https://www.cnblogs.com/ZzwWan/p/18666073