文章目录
效果图:
1 sql部分:
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`nickname` varchar(20) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
`gender` varchar(2) DEFAULT NULL,
`regtime` varchar(30) DEFAULT NULL,
`birthday` varchar(30) DEFAULT NULL,
`sign` text,
`tel` varchar(11) DEFAULT NULL,
`state` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
2 根据数据表生成实体类:
public class UserInfo implements Serializable {
private Integer id;//用户id
private String name;//登录号
private String nickname;//昵称
private int state;//用户状态1为正常,0为禁用
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public void setRegtime(String regtime) {
this.regtime = regtime;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
private String password;//密码
private String gender;//性别 0为无,1为男,2为女
private String regtime;//注册时间
private String birthday;//生日
private String sign;//个人签名
private String tel;//电话
// private List<Role> roles;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getRegtime() {
return regtime;
}
public void setRegTime(String regTime) {
this.regtime = regTime;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", nickname='" + nickname + '\'' +
", state=" + state +
", password='" + password + '\'' +
", gender='" + gender + '\'' +
", regtime='" + regtime + '\'' +
", birthday='" + birthday + '\'' +
", sign='" + sign + '\'' +
", tel='" + tel + '\'' +
'}';
}
}
3 dao接口和mapper文件
@Repository
public interface UserDao {
List<UserInfo> findUsers();
int addUser(UserInfo user);
UserInfo findUserByName(String name);
int delUserByName(String name);
int editUser(UserInfo user);
int delUsers(List<String> list);
int setUserState(UserInfo userInfo);
}
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.redocloud.ssm.dao.UserDao">
<select id="findUsers" resultType="com.redocloud.ssm.entity.UserInfo">
select * from user
</select>
<insert id="addUser" parameterType="com.redocloud.ssm.entity.UserInfo">
insert into user(name,password,gender,regtime,birthday,nickname,tel,sign,state)
values(#{name},#{password},#{gender},#{regtime},#{birthday},#{nickname},#{tel},#{sign},#{state})
</insert>
<select id="findUserByName" parameterType="string" resultType="com.redocloud.ssm.entity.UserInfo">
select * from user where name = #{name}
</select>
<delete id="delUserByName" parameterType="string">
delete from user where name=#{name}
</delete>
<delete id="delUsers">
delete from user where name in
<foreach collection="list" item="name" open="(" close=")" separator=",">
#{name}
</foreach>
</delete>
<update id="editUser" parameterType="com.redocloud.ssm.entity.UserInfo">
update user set gender=#{gender},regtime=#{regtime},birthday=#{birthday},nickname=#{nickname},tel=#{tel},sign=#{sign} where name=#{name}
</update>
<update id="setUserState" parameterType="com.redocloud.ssm.entity.UserInfo">
update user set state = #{state} where name =#{name}
</update>
</mapper>
4 service和实现类:
/**
* ClassName : UserService
* package : com.redocloud.ssm.service
* 功能描述:
*
* @Date : 2020/5/4 0004 12:25
* @Author : one world
*/
public interface UserService {
List<UserInfo> findUsers();
int addUser(UserInfo user);
UserInfo findUserByName(String name);
int delUserByname(String name);
int editUser(UserInfo user);
int delUsers(List<String> list);
int setUserState(UserInfo userInfo);
}
@Service("userService")
/**
* ClassName : UserServiceImpl
* package : com.redocloud.ssm.service.impl
* 功能描述:
*
* @Date : 2020/5/4 0004 12:26
* @Author : one world
*/
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<UserInfo> findUsers() {
return userDao.findUsers();
}
@Override
public int addUser(UserInfo user) {
if(userDao.addUser(user)>0){
return 1;
}
return 0;
}
@Override
public UserInfo findUserByName(String name) {
return userDao.findUserByName(name);
}
@Override
public int delUserByname(String name) {
return userDao.delUserByName(name);
}
@Override
public int editUser(UserInfo user) {
return userDao.editUser(user);
}
@Override
public int delUsers(List<String> list) {
return userDao.delUsers(list);
}
@Override
public int setUserState(UserInfo userInfo) {
return userDao.setUserState(userInfo);
}
}
5 控制器:
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/addUser")
@ResponseBody
public String addUser(@RequestParam("name") String name,@RequestParam("password") String password){
UserInfo user = new UserInfo();
user.setGender("男");
user.setNickname(name);
System.out.println("添加用户");
user.setSign("");
user.setName(name);
user.setState(1);
user.setTel("");
String msg = "";
user.setBirthday("1990-01-01");
user.setPassword(MD5Utils.string2MD5(password));
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
user.setRegTime(simpleDateFormat.format(new Date()));
int rows = userService.addUser(user);
System.out.println("行数"+rows);
if(rows>0) {
System.out.println("注册成功");
msg = "login";
}else {
msg = "reg";
}
return JSON.toJSONString(msg);
}
// @RequestMapping("/login")
// @ResponseBody
// public String login(@RequestParam("name") String name, @RequestParam("password") String password, HttpSession session){
// //用户返回ajax dataType对应的信息
// String msg = "";
// UserInfo user = userService.findUserByName(name);
// System.out.println("用户"+user+"请求登录");
// if(user!=null) {
// if(MD5Utils.passwordIsTrue(password, user.getPassword())){
// System.out.println("登录成功!");
// session.setAttribute("name",user.getName());
// session.setAttribute("gender",user.getGender());
// session.setAttribute("sign",user.getSign());
// session.setAttribute("birthday",user.getBirthday());
// session.setAttribute("regtime",user.getRegtime());
// String token = TokenUtils.token(name);
// session.setAttribute("token",token);
//
// msg = "success";
// }else{
// msg = "error";
// }
// }else{
// msg = "error";
// }
// return JSON.toJSONString(msg);
// }
@ResponseBody
@RequestMapping("/login")
public ModelAndView login(HttpServletRequest request, HttpServletResponse response){
HttpSession session = request.getSession();
ModelAndView mv = new ModelAndView();
String name = request.getParameter("username");
System.out.println("用户名为:"+name);
String password = request.getParameter("password");
UserInfo user = userService.findUserByName(name);
System.out.println("用户信息:"+user);
if(user!=null&&MD5Utils.passwordIsTrue(password,user.getPassword())&&user.getState()!=0){
System.out.println("登录成功");
mv.addObject("username",user.getNickname());
mv.setViewName("index");
}else{
System.out.println("登录失败");
mv.setViewName("login");
}
return mv;
}
/**
* 批量删除用户
*/
@RequestMapping("/delUsers")
@ResponseBody
public String delUsers(@RequestParam("userNames") String ids){
List<String> list = new ArrayList<>();
String[] strs = ids.split(",");
for(String str:strs){
list.add(str);
}
System.out.println("被删除的用户:"+list);
if(userService.delUsers(list)>0){
return "success";
}
return "error";
}
//编辑自己资料
@RequestMapping("/editMyInfo")
@ResponseBody
public String editMyInfo(@RequestHeader(value = "Authorization") String authorization){
String token = authorization;
System.out.println("editToken:"+token);
String msg="";
if (TokenUtils.verify(token)){
msg = "success";
}else{
msg = "failed";
}
System.out.println(msg);
return msg;
}
@ResponseBody
@RequestMapping("/findUsers")
public Map<String,Object> findUsersPage(@RequestParam("page") Integer pageNo,@RequestParam("limit") Integer pageSize) {
Map<String,Object> map = new HashMap<String,Object>();
PageHelper.startPage(pageNo,pageSize);
List<UserInfo> users = userService.findUsers();
PageInfo<UserInfo> pages = new PageInfo<UserInfo>(users);
map.put("code",0);
map.put("msg","操作成功");
map.put("count",pages.getTotal());
map.put("data",pages.getList());
System.out.println("分页");
System.out.println(JSON.toJSON(map));
return map;
}
@ResponseBody
@RequestMapping("/del")
public String del(@RequestParam("name") String name){
String msg = "";
int rows = userService.delUserByname(name);
if(rows>0){
msg = "ok";
}else{
msg="no";
}
return JSON.toJSONString(msg);
}
//根据用户名设置用户状态
@ResponseBody
@RequestMapping(value = "/setUserState",produces = "text/html;charset=UTF-8")
public String setUserState(@RequestParam("name") String name,@RequestParam("state") int state){
System.out.println("状态设置");
UserInfo user = new UserInfo();
user.setName(name);
if(state==1){
state=0;
}else{
state=1;
}
user.setState(state);
System.out.println(name+state);
int rows = userService.setUserState(user);
if(rows>0){
return JSON.toJSONString("success");
}
return JSON.toJSONString("error");
}
@ResponseBody
@RequestMapping("/editUser")
public String editUser(@RequestBody String json) throws UnsupportedEncodingException {
String data = new String(json.getBytes("iso8859-1"),"UTF-8");
String msg = "";
data = java.net.URLDecoder.decode(json,"UTF-8");
System.out.println("解码后"+data);
data = data.substring(0,data.length()-1);
System.out.println("去除后"+data);
UserInfo user = JSONObject.parseObject(data , UserInfo.class);// jsonStr 是String类型。
if(user.getSign().equals("")||user.getSign()==""||user.getSign()==null){
user.setSign("");
}
if (user.getTel().equals("") || user.getTel() == ""||user.getTel()==null) {
user.setTel("");
}
System.out.println(user);
if(userService.editUser(user)>0){
msg = "ok";
}else{
msg="修改失败";
}
String info = JSON.toJSONString(msg);
System.out.println(info);
return info;
}
}
6 工具类:
public class MD5Utils {
/***
* MD5加码 生成32位md5码
*/
public static String string2MD5(String inStr) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
e.printStackTrace();
return "";
}
char[] charArray = inStr.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++)
byteArray[i] = (byte) charArray[i];
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
/**
* 加密解密算法 执行一次加密,两次解密
*/
public static String convertMD5(String inStr) {
char[] a = inStr.toCharArray();
for (int i = 0; i < a.length; i++) {
a[i] = (char) (a[i] ^ 't');
}
String s = new String(a);
return s;
}
/**
* 判断输入的密码和数据库中保存的MD5密码是否一致
* @param inputPassword 输入的密码
* @param md5DB 数据库保存的密码
* @return
*/
public static boolean passwordIsTrue(String inputPassword,String md5DB) {
String md5 = string2MD5(inputPassword);
return md5DB.equals(md5);
}
}
7 HTML页面使用的是HTML+layui内容较多,请看源码
https://download.csdn.net/download/qq_42739776/12456475