接口例子——登录
1、用 mybatis-generator 逆向生成
2、定义接口
controller
层下创建java类
,称为UserController
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
@Controller //表明这是控制的作用
@RequestMapping("/user/") // 定义接口前缀
public class UserController {
/**
* 用户登录接口
* @param username 用户名
* @param password 密码
* @param session
* @return
*/
@RequestMapping(value = "login.do", method = RequestMethod.POST) // 接口名,和使用的方法
@ResponseBody // 使用json格式
public Object login(String username, String password, HttpSession session){
return null;
}
}
3、实现登录逻辑
com.mymmall.service.impl
下创建java类
,称为UserServiceImpl
package com.mymmall.service.impl;
import com.mymmall.common.ServerResponse;
import com.mymmall.dao.UserMapper;
import com.mymmall.pojo.User;
import com.mymmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("iUserService")
public class UserServiceImpl implements IUserService {
@Autowired
// 引用dao的mysql接口
private UserMapper userMapper;
@Override
public ServerResponse<User> login(String username, String password) {
// checkUsername 在dao中实现
int resultCount = userMapper.checkUsername(username);
if(resultCount == 0){
return ServerResponse.createByErrorMessage("用户名不存在");
}
// selectLogin 在dao中实现
User user = userMapper.selectLogin(username, password);
if(user == null){
return ServerResponse.createByErrorMessage("用户不存在");
}
// 把密码设置为空,并把user返回出去给接口使用
user.setPassword(org.apache.commons.lang3.StringUtils.EMPTY);
return ServerResponse.createBySuccess("登录成功", user);
}
}
com.mymmall.service
下创建接口类
,称为IUserService
package com.mymmall.service;
import com.mymmall.common.ServerResponse;
import com.mymmall.pojo.User;
public interface IUserService {
ServerResponse<User> login(String username, String password);
}
4、与mysql的交互
- com.mymmall.dao.
UserMapper
package com.mymmall.dao;
import com.mymmall.pojo.User;
import org.apache.ibatis.annotations.Param;
public interface UserMapper {
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
// 定义 checkUsername 函数用于查询是否有这个用户
int checkUsername(String username);
// 定义 selectLogin 函数用于查询用户输入的这个用户和密码是否正确
// 多个参数时,要用 @Param("一般与参数一样")
User selectLogin(@Param("username") String username,@Param("password") String password);
}
mappers/
UserMapper.xml
添加<select id="checkUsername" resultType="int" parameterType="string"> select count(1) from mmall_user where username = #{username} </select>
<select id="selectLogin" resultMap="BaseResultMap" parameterType="map"> select <include refid="Base_Column_List" /> from mmall_user where username = ${username} and password = #{password} </select>
- id:对应的函数名
- resultType:返回的数据类型
- parameterType:使用的参数的数据类型
- #{} 可以防sql注入,并传入变量
resultMap="BaseResultMap"
:返回的类型为generator定义好的User类型<include refid="Base_Column_List" />
:generator定义好的所有此表的字段
5、实现接口
- com.二级包名.controller.portal下创建
java类
,称为UserController
package com.mymmall.controller.portal;
import com.mymmall.common.ServerResponse;
import com.mymmall.pojo.User;
import com.mymmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
@Controller //表明这是控制的作用
@RequestMapping("/user/") // 重用接口前缀
public class UserController {
@Autowired
private IUserService iUserService;
/**
* 用户登录接口
* @param username 用户名
* @param password 密码
* @param session
* @return
*/
@RequestMapping(value= "login.do", method = RequestMethod.POST) // 接口名,和使用的方法
@ResponseBody // 使用json格式
public ServerResponse<User> login(String username, String password, HttpSession session){
ServerResponse<User> response = iUserService.login(username, password);
if(response.isSuccess()){
// 如果成功,则把用户放进session中
session.setAttribute(“currentUser”, response.getData());
}
return response;
}
}