当前位置:   article > 正文

Springboot+Jpa+Ajax完成异步用户添加删除修改功能(超详细)_springboot jpa ajax

springboot jpa ajax

#说实话,我前端真的很一般,所以做这个要用js,css,我算是跌跌撞撞过来的...不过总算完成了

 

#实体类:

Client:

  1. package com.example.main.classs;
  2. import javax.persistence.Column;
  3. import javax.persistence.Entity;
  4. import javax.persistence.GeneratedValue;
  5. import javax.persistence.GenerationType;
  6. import javax.persistence.Id;
  7. import javax.persistence.Table;
  8. @Entity
  9. @Table(name="client")
  10. public class Client {
  11. @GeneratedValue(strategy=GenerationType.TABLE)
  12. @Id
  13. private int id; //id
  14. private String role="ROLE_CLIENT";
  15. public String getRole() {
  16. return role;
  17. }
  18. public void setRole(String role) {
  19. this.role = role;
  20. }
  21. public int getId() {
  22. return id;
  23. }
  24. public void setId(int id) {
  25. this.id = id;
  26. }
  27. private String username;
  28. public String getUsername() {
  29. return username;
  30. }
  31. public void setUsername(String username) {
  32. this.username = username;
  33. }
  34. public String getPassword() {
  35. return password;
  36. }
  37. public void setPassword(String password) {
  38. this.password = password;
  39. }
  40. private String password;
  41. @Column(nullable=true)
  42. private String name; //客户名字
  43. @Column(nullable=true)
  44. private int age; //客户年龄
  45. public String getName() {
  46. return name;
  47. }
  48. public void setName(String name) {
  49. this.name = name;
  50. }
  51. public int getAge() {
  52. return age;
  53. }
  54. public void setAge(int age) {
  55. this.age = age;
  56. }
  57. public String getAddress() {
  58. return address;
  59. }
  60. public void setAddress(String address) {
  61. this.address = address;
  62. }
  63. public String getPhone_number() {
  64. return phone_number;
  65. }
  66. public void setPhone_number(String phone_number) {
  67. this.phone_number = phone_number;
  68. }
  69. public String getSex() {
  70. return sex;
  71. }
  72. public void setSex(String sex) {
  73. this.sex = sex;
  74. }
  75. @Override
  76. public String toString() {
  77. return "Client [name=" + name + ", age=" + age + ", address=" + address + ", phone_number=" + phone_number
  78. + ", sex=" + sex + "]";
  79. }
  80. @Column(nullable=true)
  81. private String address; //住址
  82. private String phone_number; //手机号码
  83. private String sex; //年龄
  84. public boolean isEmpty() {
  85. if(this.address!=null && this.name!=null& this.phone_number!=null && this.sex!=null) return false;
  86. return true;
  87. }
  88. }

#Dao层:

  1. package com.example.main.dao;
  2. import javax.transaction.Transactional;
  3. import org.springframework.data.jpa.repository.Modifying;
  4. import org.springframework.data.jpa.repository.Query;
  5. import org.springframework.data.repository.Repository;
  6. import com.example.main.classs.Client;
  7. public interface ajaxRepository extends Repository<Client, Integer> {
  8. @Transactional
  9. @Modifying
  10. @Query(value="delete from Client where id =?1",nativeQuery = true)
  11. void aadelete(int id);
  12. @Transactional
  13. @Modifying
  14. @Query(value="update client set address=?5,name=?2,password=?3,sex=?4,age=?7,phone_number=?6 where id=?1",nativeQuery = true)
  15. void ajaxedit(int id,String name,String password,String sex,String address,String phonenumber,int age);
  16. }

用的是Jpa,所以只需要创建一个接口类,实现Repository类就可以了。

我写了两个函数,aadelte(int id):这个函数是根据id删除用户,前端点击按钮,按钮的value值是id值,然后再从前端传过来。

ajaxedit(int id,String name,String password,String sex,String address,String phonenumber,int age):传回来要修改的id值,及要修改的数据。

#Service层:

  1. package com.example.main.service;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Service;
  4. import org.springframework.util.DigestUtils;
  5. import com.example.main.dao.ajaxRepository;
  6. @Service
  7. public class ajaxservice {
  8. @Autowired
  9. private ajaxRepository ajaxrepository;
  10. public String ajaxedit(String name,String address,String age,String phone_number,String sex,String id,String password) {
  11. int idd=Integer.parseInt(id);
  12. int agee=Integer.parseInt(age);
  13. String temp=DigestUtils.md5DigestAsHex(password.getBytes());
  14. ajaxrepository.ajaxedit(idd, name, temp, sex, address, phone_number, agee);
  15. return "success";
  16. }
  17. }

呃..service层偷懒了,按理说,应该有删除,修改和添加。但是删除的话我是直接在controller调了dao层。。大家别学我。

然后添加的话,我使用了注册用户的那个接口。只有修改是重新写了一个service。

 

#Controller层

  1. package com.example.main.controller;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.ui.Model;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.PostMapping;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestParam;
  11. import org.springframework.web.bind.annotation.ResponseBody;
  12. import com.example.main.dao.UserRepository;
  13. import com.example.main.dao.ajaxRepository;
  14. import com.example.main.service.Loginservice;
  15. import com.example.main.service.ajaxservice;
  16. @Controller
  17. @RequestMapping("/")
  18. public class indexController {
  19. private static Logger logger=LoggerFactory.getLogger(indexController.class);
  20. @Autowired
  21. private ajaxservice Ajaxservice;
  22. @Autowired
  23. private Loginservice loginservice;
  24. @Autowired
  25. private ajaxRepository ajax;
  26. @Autowired
  27. private UserRepository userRepository;
  28. @RequestMapping("/index")
  29. public String geiindex() {
  30. return "jiemian/index";
  31. }
  32. @RequestMapping("/index2")
  33. public String edituser(Model model) {
  34. model.addAttribute("info", userRepository.findAll());
  35. return "jiemian/index2";
  36. }
  37. @RequestMapping("/index3")
  38. public String deleteuser(Model model) {
  39. model.addAttribute("info", userRepository.findAll());
  40. return "jiemian/index3";
  41. }
  42. @RequestMapping("/ceshi") //测试
  43. public String ceshi(Model model) {
  44. model.addAttribute("info",userRepository.findAll() );
  45. return "jiemian/ceshi";
  46. }
  47. @GetMapping("/ajax") //删除
  48. @ResponseBody
  49. public String ajax(@RequestParam("username") String id) {
  50. System.out.println("调用了ajax:"+id);
  51. int temp=Integer.parseInt(id);
  52. ajax.aadelete(temp);
  53. return "heihei";
  54. }
  55. @PostMapping("/ajax1") //测试
  56. @ResponseBody
  57. public String ajax1(@RequestParam("username") String id) {
  58. System.out.println("调用了ajax1"+id);
  59. return "ajxa1";
  60. }
  61. @PostMapping("/ajaxadd") //添加
  62. @ResponseBody
  63. public String ajaxadd(@RequestParam("name") String name,@RequestParam("username") String username,
  64. @RequestParam("password") String password,@RequestParam("age") String age,@RequestParam("sex") String sex,
  65. @RequestParam("address") String address,@RequestParam("phonenumber") String phonenumber) {
  66. loginservice.add(name, address, age, phonenumber, sex,username,password);
  67. return "success";
  68. }
  69. @PostMapping("/ajaxedit") //修改
  70. @ResponseBody
  71. public String ajaxedit(@RequestParam("name") String name,@RequestParam("id") String id,
  72. @RequestParam("password") String password,@RequestParam("age") String age,@RequestParam("sex") String sex,
  73. @RequestParam("address") String address,@RequestParam("phonenumber") String phonenumber) {
  74. logger.info("id:"+id);
  75. Ajaxservice.ajaxedit(name, address, age, phonenumber, sex, id, password);
  76. return "success";
  77. }
  78. }

 

每个controller我都写了注释,应该不难理解。我这里没写判断的,实际中最好写上判断,也就是数据库是否真的操作成功。

 

#然后就是Ajax部分

#添加用户的ajax的js代码:

  1. <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  2. <script>
  3. function sumbit(){
  4. var name=document.getElementById("name");
  5. var username=document.getElementById("username");
  6. var password=document.getElementById("password");
  7. var age=document.getElementById("age");
  8. var address=document.getElementById("address");
  9. var sex=document.getElementById("sex");
  10. var phonenumber=document.getElementById("phonenumber");
  11. $.ajax({
  12. type:"POST",
  13. url:"/test/ajaxadd",
  14. data:{name:name.value,username:username.value,password:password.value,age:age.value,address:address.value,sex:sex.value,phonenumber:phonenumber.value},
  15. dataType:'text',
  16. success:function(data){
  17. alert("添加:"+data);
  18. name.value="";
  19. username.value="";
  20. password.value="";
  21. age.value="";
  22. address.value="";
  23. sex.value="";
  24. phonenumber.value="";
  25. }
  26. });
  27. }
  28. </script>

html代码:

  1. <table class="table table-bordered text-nowrap mb-0">
  2. <thead>
  3. <tr>
  4. <th>Client Name</th>
  5. <th>Client Username</th>
  6. <th>Client age</th>
  7. <th>Client sex</th>
  8. <th>Client Address</th>
  9. <th>Client Phone number</th>
  10. <th>Client Password</th>
  11. </tr>
  12. </thead>
  13. <tbody>
  14. <tr>
  15. <td><input type="text" id="name"/></td>
  16. <td><input type="text" id="username"/></td>
  17. <td><input type="text" id="age"/></td>
  18. <td><input type="text" id="sex"/></td>
  19. <td><input type="text" id="address"/></td>
  20. <td><input type="text" id="phonenumber"/></td>
  21. <td><input type="password" id="password"/></td>
  22. <td ><button type="button" class="btn btn-action btn-primary" id="buttonid" onclick="sumbit()">添加</button></td>
  23. </tr>
  24. </tbody>
  25. </table>

 

修改部分的ajax的js代码:

  1. <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  2. <script>
  3. function show(){
  4. var oDiv=document.getElementById("shuru");
  5. document.getElementById("edit").disabled = true;
  6. oDiv.style.display="block";
  7. }
  8. function sumbit(id){
  9. var name=document.getElementById("name");
  10. var password=document.getElementById("password");
  11. var age=document.getElementById("age");
  12. var address=document.getElementById("address");
  13. var sex=document.getElementById("sex");
  14. var phonenumber=document.getElementById("phonenumber");
  15. $.ajax({
  16. type:"POST",
  17. dataType:'text',
  18. url:"/test/ajaxedit",
  19. data:{id:id.value,name:name.value,password:password.value,age:age.value,address:address.value,sex:sex.value,phonenumber:phonenumber.value},
  20. success:function(status){
  21. alert("修改:"+status);
  22. name.value="";
  23. password.value="";
  24. age.value="";
  25. address.value="";
  26. sex.value="";
  27. phonenumber.value="";
  28. document.getElementById("shuru").style.display="none";
  29. document.getElementById("edit").disabled = false;
  30. window.location.reload();
  31. }
  32. });
  33. }
  34. </script>

html代码:

  1. <div class="card-body">
  2. <div class="table-responsive">
  3. <table class="table table-bordered text-nowrap mb-0">
  4. <thead>
  5. <tr>
  6. <th>Id</th>
  7. <th>Client Name</th>
  8. <th>Client Username</th>
  9. <th>Client age</th>
  10. <th>Client sex</th>
  11. <th>Client Address</th>
  12. <th>Client Phone number</th>
  13. <th><div id="shu" style='display:none;'>输入:<input type="text"></div> </th>
  14. </tr>
  15. </thead>
  16. <tbody>
  17. <tr th:each ="i:${info}">
  18. <td th:text = "${i.id}"></td>
  19. <td th:text = "${i.name}"></td>
  20. <td th:text = "${i.username}"></td>
  21. <td th:text = "${i.age}"></td>
  22. <td th:text = "${i.sex}"></td>
  23. <td th:text = "${i.address}"></td>
  24. <td th:text = "${i.phone_number}"></td>
  25. <td ><button type="button" class="btn btn-action btn-primary" id="edit" onclick="show()" th:value="${i.id}">编辑</button>
  26. <button type="button" class="btn btn-action btn-primary" id="sumbit" onclick="sumbit(this)" th:value="${i.id}">确定</button></td>
  27. </tr>
  28. </tbody>
  29. </table>
  30. <div id="shuru" style='display:none;'>
  31. <table class="table table-bordered text-nowrap mb-0">
  32. <tr>
  33. <td><input type="text" id="name" placeholder="姓名" ></td>
  34. <td><input type="text" id="age" placeholder="年龄" ></td>
  35. <td><input type="text" id="address" placeholder="地址" ></td>
  36. <td><input type="text" id="phonenumber" placeholder="手机号" ></td>
  37. <td><input type="text" id="sex" placeholder="性别" ></td>
  38. <td><input type="password" id="password" placeholder="密码 " ></td>
  39. </tr>
  40. </table>
  41. </div>
  42. </div>
  43. </div>

这个要说一下,编辑那一行先是隐藏的,只有点击编辑按钮才会显示,点击完编辑按钮以后,编辑按钮自动变灰色不可用。点击确定之后修改生效,编辑那一行自动隐藏,编辑按钮重新可用。页面刷新。   要做到这个效果我可真是百度了巨久。。

 

删除的ajax的js代码:

  1. <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  2. <script>
  3. function loadXMLDoc(field)
  4. {
  5. var xmlhttp;
  6. if (window.XMLHttpRequest)
  7. {
  8. // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
  9. xmlhttp=new XMLHttpRequest();
  10. }
  11. else
  12. {
  13. // IE6, IE5 浏览器执行代码
  14. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  15. }
  16. xmlhttp.onreadystatechange=function()
  17. {
  18. if (xmlhttp.readyState==4 && xmlhttp.status==200)
  19. {
  20. alert("删除成功");
  21. window.location.reload();
  22. }
  23. }
  24. var url="/test/ajax?username=";
  25. url=encodeURI(url);
  26. xmlhttp.open("GET",url+encodeURIComponent(field.value),true);
  27. xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  28. xmlhttp.send();
  29. }
  30. </script>

呃。。之前的两个是用JQuery写的,这个不是,就将就看吧,正好如果看不懂JQuery还能看这个,毕竟之前的我就是看不懂JQuery。。

Html代码:

  1. <div class="row">
  2. <div class="col-12 col-sm-12">
  3. <div class="card">
  4. <div class="card-header">
  5. <h4>Product Summary</h4>
  6. </div>
  7. <div class="card-body">
  8. <div class="table-responsive">
  9. <table class="table table-bordered text-nowrap mb-0">
  10. <thead>
  11. <tr>
  12. <th>Id</th>
  13. <th>Client Name</th>
  14. <th>Client Username</th>
  15. <th>Client age</th>
  16. <th>Client sex</th>
  17. <th>Client Address</th>
  18. <th>Client Phone number</th>
  19. </tr>
  20. </thead>
  21. <tbody>
  22. <tr th:each ="i:${info}">
  23. <td th:text = "${i.id}"></td>
  24. <td th:text = "${i.name}"></td>
  25. <td th:text = "${i.username}"></td>
  26. <td th:text = "${i.age}"></td>
  27. <td th:text = "${i.sex}"></td>
  28. <td th:text = "${i.address}"></td>
  29. <td th:text = "${i.phone_number}"></td>
  30. <td ><button type="button" class="btn btn-action btn-primary" id="buttonid" onclick="loadXMLDoc(this)" th:value="${i.id}">删除</button></td>
  31. </tr>
  32. </tbody>
  33. </table>
  34. </div>
  35. </div>
  36. </div>
  37. </div>
  38. </div>

 

效果图:登录:

界面图:

右上角显示登录用户名,当然,左边也有。

修改用户:

源码下载传送门

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/652183
推荐阅读
相关标签
  

闽ICP备14008679号