第一步:修改dao和mapper
UserMapper.java
public interface UserMapper {
int deleteByPrimaryKey(Long id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Long id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
User selectUser(@Param("userName") String userName);
User updatePwd(@Param("userPwd") String newPwd,
@Param("userName") String userName);
}
UserMapper.xml
<?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.ray.dao.UserMapper">
<resultMap id="BaseResultMap" type="com.ray.entity.User">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="user_name" jdbcType="VARCHAR" property="userName" />
<result column="user_email" jdbcType="VARCHAR" property="userEmail" />
<result column="user_pwd" jdbcType="VARCHAR" property="userPwd" />
</resultMap>
<sql id="Base_Column_List">
id, user_name, user_email, user_pwd
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=BIGINT}
</select>
<!-- 查询用户 -->
<select id="selectUser" parameterType="com.ray.entity.User" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user where user_name = #{userName}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
delete from user
where id = #{id,jdbcType=BIGINT}
</delete>
<!-- 新增用户 -->
<insert id="insert" parameterType="com.ray.entity.User">
insert into user (id, user_name, user_email,
user_pwd)
values (#{id,jdbcType=BIGINT}, #{userName,jdbcType=VARCHAR}, #{userEmail,jdbcType=VARCHAR},
#{userPwd,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.ray.entity.User">
insert into user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="userName != null">
user_name,
</if>
<if test="userEmail != null">
user_email,
</if>
<if test="userPwd != null">
user_pwd,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=BIGINT},
</if>
<if test="userName != null">
#{userName,jdbcType=VARCHAR},
</if>
<if test="userEmail != null">
#{userEmail,jdbcType=VARCHAR},
</if>
<if test="userPwd != null">
#{userPwd,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.ray.entity.User">
update user
<set>
<if test="userName != null">
user_name = #{userName,jdbcType=VARCHAR},
</if>
<if test="userEmail != null">
user_email = #{userEmail,jdbcType=VARCHAR},
</if>
<if test="userPwd != null">
user_pwd = #{userPwd,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
<update id="updateByPrimaryKey" parameterType="com.ray.entity.User">
update user
set user_name = #{userName,jdbcType=VARCHAR},
user_email = #{userEmail,jdbcType=VARCHAR},
user_pwd = #{userPwd,jdbcType=VARCHAR}
where id = #{id,jdbcType=BIGINT}
</update>
<!-- 修改密码 -->
<update id="updatePwd" parameterType="com.ray.entity.User">
update user
set user_pwd = #{userPwd}
where user_name = #{userName}
</update>
</mapper>
第二步:创建service
├── service
│ ├── UserService.java
│ └── impl
│ └── UserServiceImpl.java
UserService.java
/**
* @author Ray
* @date 2018/5/23 0023
*/
public interface UserService {
void save(User user);
User checkLogin(String userName, String userPwd);
boolean checkRegister(String userName, String userPwd, String email);
boolean updatePwd(String newPwd, String username);
}
UserServiceImpl.java
package com.ray.service.impl;
import com.ray.dao.UserMapper;
import com.ray.entity.User;
import com.ray.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
/**
* @author Ray
* @date 2018/5/23 0023
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
/**
* 新增用户
* @param user
*/
@Override
public void save(User user) {
userMapper.insert(user);
}
/**
* 登录检查
* @param userName
* @param userPwd
* @return
*/
@Override
public User checkLogin(String userName, String userPwd) {
User user = userMapper.selectUser(userName);
if(user != null && user.getUserPwd().equals(userPwd)){
return user;
}
return null;
}
/**
* 注册检查
* @param userName
* @param userPwd
* @param email
* @return
*/
@Override
public boolean checkRegister(String userName, String userPwd, String email) {
User user = userMapper.selectUser(userName);
if(user != null){
return false;
}else{
User insertUser = new User();
insertUser.setUserName(userName);
insertUser.setUserPwd(userPwd);
insertUser.setUserEmail(email);
save(insertUser);
return true;
}
}
/**
* 更改密码
* @param newPwd
* @param username
* @return
*/
@Override
public boolean updatePwd(String newPwd, String username) {
User user = userMapper.updatePwd(newPwd, username);
if(user != null){
return true;
}
return false;
}
}
第三步:创建controller
└── controller
└── UserController.java
UserController.java
package com.ray.controller;
import com.ray.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* @author Ray
* @date 2018/5/23 0023
*/
@Controller
public class UserController {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Resource
private UserService userService;
/**
* 用户注册
* @RequestMapping 请求路径
* @ResponseBody 返回JSON数据
*/
@RequestMapping("insertUser")
@ResponseBody
public boolean insertUser(HttpServletRequest request, HttpServletResponse response){
String username = request.getParameter("username");
String password = request.getParameter("password");
String email = request.getParameter("email");
boolean result = userService.checkRegister(username,password,email);
if(result){
return true;
}
return false;
}
}