package com.hp.service.impl;
import com.hp.entity.User;
import com.hp.mapper.UserMapper;
import com.hp.service.IUserServicce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements IUserServicce {
@Autowired
private UserMapper userMapper;
@Override
public User login(User user){
User u = userMapper.selectUser(user);
return u;
}
@Override
public void register(User user) {
userMapper.inserUser(user);
}
package com.hp.controller;
import com.hp.entity.User;
import com.hp.service.IUserServicce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/userController")
public class UserController {
@Autowired
private IUserServicce userServicce;
@RequestMapping(value = {"/login"},method = {RequestMethod.POST})
public String login(@RequestParam("U_NAME") String U_NAME, @RequestParam("U_PS") String U_PS, Model model){
User user = new User();
user.setU_NAME(U_NAME);
user.setU_PS(U_PS);
if (userServicce.login(user) != null){
model.addAttribute("U_NAME",U_NAME);
return "success";
}
else {
model.addAttribute("error","账号或密码错误");
return "unsuccess";
}
}
}**
<?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.hp.mapper.UserMapper">
<select id="selectUser" parameterType="com.hp.entity.User" resultType="com.hp.entity.User">
select * from t_user
<where>
<if test="U_NAME != null and U_NAME !=''">
and U_NAME = #{U_NAME}
</if>
<if test="U_PS != null and U_PS !=''">
and U_PS = #{U_PS}
</if>
</where>
</select>
<insert id="insertUser" parameterType="com.hp.entity.User">
insert into t_user(U_NAME,U_PS,U_PHONE)
values (#{U_NAME},#{U_PS},#{U_PHONE})
</insert>
</mapper>