1、用generator生成好User
2、定義接口
com.二級包名.controller.portal
下創建UserController java類
package com. mymmall. controller. portal;
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 {
@RequestMapping ( value= "login.do" , method = RequestMethod. POST)
@ResponseBody
public Object login ( String username, String password, HttpSession session) {
return null;
}
}
3、實現登錄邏輯
com.mymmall.service.impl下創建UserServiceImpl java類
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
private UserMapper userMapper;
@Override
public ServerResponse< User> login ( String username, String password) {
int resultCount = userMapper. checkUsername ( username) ;
if ( resultCount == 0 ) {
return ServerResponse. createByErrorMessage ( "用戶名不存在" ) ;
}
User user = userMapper. selectLogin ( username, password) ;
if ( user == null) {
return ServerResponse. createByErrorMessage ( "用戶不存在" ) ;
}
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) ;
int checkUsername ( String username) ;
User selectLogin ( @Param ( "username" ) String username, @Param ( "password" ) String password) ;
}
mappers/UserMapper.xml
加上下面兩個sql
id:對應的函數名
resultType:返回的數據類型
parameterType:使用的參數的數據類型
#{} 可以防sql注入,並傳入變量
< select id = " checkUsername" resultType = " int" parameterType = " string" >
select count(1) from mmall_user
where username = #{username}
</ select>
resultMap="BaseResultMap"
:返回的類型爲generator定義好的User類型
<include refid="Base_Column_List" />
:generator定義好的所有此表的字段
< select id = " selectLogin" resultMap = " BaseResultMap" parameterType = " map" >
select < include refid = " Base_Column_List" /> from mmall_user
where username = ${username} and
password = #{password}
</ select>
5、實現接口
com.二級包名.controller.portal下創建UserController java類
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;
@RequestMapping ( value= "login.do" , method = RequestMethod. POST)
@ResponseBody
public ServerResponse< User> login ( String username, String password, HttpSession session) {
ServerResponse< User> response = iUserService. login ( username, password) ;
if ( response. isSuccess ( ) ) {
session. setAttribute ( “currentUser”, response. getData ( ) ) ;
}
return response;
}
}