java修改⽤户密码全代码
java实现修改密码
前段时间⼀直在写东西,本来不想把⽤户这块的东西放出来,想了想,还是弄⼀个修改密码功能实现⼀下,⾸先介绍⼀下我这个项⽬,他有session等验证,所以搞起来得和正式的项⽬⼀样得实现,废话不多说,直接开始,先是搞⼀个请求去查都有哪些账号
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""/TR/html4/loose.dtd">
<html>
<!--前端实在是太差劲了,有很多地⽅不会写,但是还是将就着实现吧,以后慢慢优化,模态框是⽹上搜的,实在是不怎么懂前端-->
<script type="text/javascript"
src="<%=ContextPath()%>/jquery/jquery-3.1.1.min.js"></script>
<script
src="/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet"
href="/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>⽤户管理</title>
</head>
<body>
<div id="div_user"></div>
<!--模态框(Modal)-->
<div class="modal fade" id="myModal"
tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button"class="close" data-dismiss="modal"
aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">修改密码</h4>
</div>
<div class="modal-body">
<input type="text" id="username"class="username">账号<input高以翔个人资料
type="password" id="y_password"class="y_password">原密码<input
type="password" id="x_password"class="x_password">新密码<input郜林女友
type="password" id="qr_password"class="qr_password">确认新密码
</div>
<div class="modal-footer">
<a id="pswErrorMsg"></a>
<button type="button"class="btn btn-default" data-dismiss="modal">关闭
</button>
<button type="button"class="btn btn-primary" onclick="submit()">
提交更改</button>
</div>
</div>
<!--/.modal-content -->
</div>
<!--/.modal -->
</div>
</body>
<script type="text/javascript">
分子动理论$(document).ready(function(){
user();
})
<!--页⾯加载以后会执⾏这个函数去后台查都有哪些账号-->
function user(){
$.ajax({
"url":"getuser.do",
"type":"get",
"dataType":"json",
success :function(data){
console.log(data);
var div_user = ElementById('div_user');
data.map(function(item){
var p_div = ateElement("div");
var p = ateElement("p");
var button = ateElement("button");
p.id ="p_user";
p.innerHTML = item.username;
button.id ='triggerBtn'
// button.value='123';
$("#triggerBtn").text("修改密码");
button.setAttribute('class','btn btn-primary btn-lg');
button.setAttribute('data-toggle','modal');
button.setAttribute('data-target','#myModal');
p_div.appendChild(p);
p_div.appendChild(button);
div_user.appendChild(p_div);
})
}
})
}
</script>
<!--模态框的⽂本检查-->
<script type="text/javascript">
function submit(){
var msg = ElementById('pswErrorMsg');
var username =$("#username").val();
var y_password =$("#y_password").val();
var x_password =$("#x_password").val();
var qr_password =$("#qr_password").val();
console.log(y_password + x_password + qr_password)
var RegExp1 =/(?!^(\d+|[a-z]+|[A-Z]+|[-_.:,!@#%&*?]+)$)^[\w-.:,!@#%&*?]*/;
var RegExp2 =/[A-Za-z0-9.\-_:,!@#%&*()]*/;
var RegExp3 =/[A-Za-z0-9.\-_:,!@#%&*()]{6,32}/;
if(y_password ==""|| x_password ==""|| qr_password ==""){
msg.innerHTML ="密码不能为空!";
return true;
}
(x_password)!= x_password){
msg.innerHTML ="密码仅⽀持数字、⼤写字母、⼩写字母和指定特殊字符!"; return true;
}
if(!st(x_password)){
msg.innerHTML ="密码数字、⼤写字母、⼩写字母和指定特殊字符⾄少包含2种!"; return true;
}
if(!(x_password)){
msg.innerHTML ="密码长度应为6~32个字符!";
return true;
}
if(x_password !== qr_password){
msg.innerHTML ="修改的密码不⼀致";
return true;
}
var url ="changepwd.do";
var data ={
"username":username,
"y_password": y_password,
"x_password": x_password,
"qr_password": qr_password,
};
$.ajax({
"url": url,
"type":"get",
"dataType":"json",
data : data,
success :function(data){
console.log(data)
if(data==0){
msg.innerHTML ="未知错误";
return true;
}else if(data==1){
msg.innerHTML ="没有到该⽤户的相关信息";
return false;
}else if(data==2){
msg.innerHTML ="密码错误!请检查密码";
return false;
}else if(data==3){
msg.innerHTML ="您⽆权修改此⽤户密码";
return true;
}else if(data==4){
msg.innerHTML ="修改成功";
return true;
}
孟非女儿}
})
}
</script>
</html>
这就是查到的数据返回到前端后展⽰的,我不懂前段端,所以只能这个样⼦了
实体类,get set 和toString⾃⼰⽣成⼀下,我这⾥为了节约空间就只写那些参数了
public class User {
private Integer id;
private String username;//账号
private String user_password;//密码
private Integer identity;//权限
//get  set  toString 略多省工厂停产
}
查询都有哪些⽤户controller
//查询有哪些账号Controller
@RequestMapping("getuser")
@ResponseBody
public JSONArray getUser(){
List<User> res =null;
JSONArray json =null;
res = userService.findUser();
json = JSONArray.fromObject(res);
return json;
}
接下来是dao层代码,
public interface UserDao {
/
/ ⽤户登录
public User verifyUser(User user);
//库⾥⾯查询有哪些账号
List<User>findUser();
// 修改密码时查询⼀下密码对错
User managUser(String userName);
// 更新密码
int uodataPassword(@Param("username") String username, @Param("x_password") String x_password); }
再就是service和他的实现类的代码
public interface UserService {
public User userLogin(User user);
// 查询有哪些⽤户
List<User>findUser();
// 修改密码查询账号对⽐密码
User managUser(String userName);
// 更新密码
int uodataPassword(@Param("username") String username, @Param("x_password") String x_password); }
实现类代码
@Service
public class UserServiceimpl implements UserService {
@Autowired
private UserDao userDao;
// 验证登录
public User userLogin(User user){
return userDao.verifyUser(user);
}
public List<User>findUser(){
return userDao.findUser();
}
public User managUser(String userName){
return userDao.managUser(userName);
}
public int uodataPassword(String username, String x_password){
return userDao.uodataPassword(username, x_password);
}
}
sql代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC"-////DTD Mapper 3.0//EN""/dtd/mybatis-3-mapper.dtd"> <mapper namespace="webapidemo.wzy.dao.UserDao.UserDao">
<select id="verifyUser"
resultType="webapidemo.wzy.Entity.MyUser.User"
parameterType="webapidemo.wzy.Entity.MyUser.User">
SELECT*FROM myuser where username = #{username};
</select>
<select id="findUser"
resultType="webapidemo.wzy.Entity.MyUser.User">
清明节手抄报资料<!--identity=2可写可不写,我主要是不想让使⽤的⼈看到我的超管账号-->
select username from myuser where identity=2;
</select>
<!--修改密码时候查询是否有这个⽤户,与登录重复了-->
<select id="managUser"
resultType="webapidemo.wzy.Entity.MyUser.User" parameterType="map">
SELECT*FROM myuser where username = #{username};
</select>
<!--修改密码-->
<update id="uodataPassword"
parameterType="webapidemo.wzy.Entity.MyUser.User">
update myuser set user_password = #{x_password}
where
username=#{username}
</update>
</mapper>
处理检查修改密码的controller