javaWeb ssm框架 学习笔记(2)2019.05.22

    xiaoxiao2022-07-07  200

    在框架中实现简单的用户注册 在完成User实体类的情况下 首先在dao中 做好定义

    import java.util.List; public interface SysUserDao { //用户注册 //1查询账户是否存在 public User selectAccount(String account); //2在确定账户不存在后添加账户 void register(@Param(value = "account")String account,@Param(value = "password")String password); //完整的注册 public Integer registerUser(@Param(value = "account")String account,@Param(value = "password")String password); }

    然后在service中定义UserService中需要的接口

    import java.util.List; public interface UserService { //用户注册 public Integer registerUser(String account,String password); }

    再然后在service的impl的userServiceImpl中实现

    package com.dc.ssm.service.impl; import com.dc.ssm.dao.SysUserDao; import com.dc.ssm.po.User; import com.dc.ssm.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.xml.crypto.Data; import java.util.Date; import java.util.List; @Service public class userServiceImpl implements UserService { @Autowired private SysUserDao userDao; @Override public Integer registerUser(String account, String password) { User user=userDao.selectAccount(account); int s=0; if (user!=null){ s=1; }else { userDao.register(account,password); s=0; } return s; } }

    其中使用userDao.selectAccount(account)与userDao.register(account,password)分别完成对于数据库的中账号的查询与账户的添加 其中selectAccount,register对应mapper中id

    <?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.dc.ssm.dao.SysUserDao"> <!--id为接口名--> <!--查找--> <select id="selectAccount" parameterType="string" resultType="com.dc.ssm.po.User"> select *from t_sys_user where account=#{account} </select> <!--添加--> <insert id="register" > insert into t_sys_user(account,password) value(#{account},#{password}) </insert> </mapper>

    最后在控制台controller中的UserController

    package com.dc.ssm.controller; import com.dc.base.pojo.BaseModel; import com.dc.ssm.po.User; import com.dc.ssm.service.UserService; import com.wordnik.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import javax.ws.rs.HttpMethod; import java.util.List; @Controller public class UserController { @Autowired UserService userService; @ApiOperation(value = "账户注册" ,notes = "用户注册",httpMethod = HttpMethod.POST) @RequestMapping("/registerUser") @ResponseBody public BaseModel registerUser(BaseModel baseModel,@RequestParam String account,@RequestParam String password){ int s=userService.registerUser(account,password); if (s==0){ baseModel.setResultCode(0); baseModel.setMessage("注册成功"); }else { baseModel.setResultCode(500); baseModel.setMessage("账户名已存在"); } return baseModel; } }

    http://localhost:8991/swagger/index.html#!//registerUser 测试地址

    最新回复(0)