This article mainly introduces the relevant content of the built-in service $http to add, delete and modify the database. It is shared for your reference and learning. Let’s take a look at the detailed introduction below:
1. Use $http to query MySQL data
('app',[]) .controller('MyCtrl',function ($scope,$http) { $('http://127.0.0.1:80/user/getUsers') .success(function (resp) { (resp); }) .error() //jQuery /*$.get('url',function (data) { });*/ })
Corresponding background Java code:
public void getUsers(){ List<User> users = ("select * from t_user"); renderJson(Users); }
2. $http implements the addition, deletion and modification of data
(1)$http sends request with parameters
(2) Add, delete and modify MySQL data
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>AngularJS $http</title> <link rel="stylesheet" href="css/" rel="external nofollow" > <style type="text/css"> html,body{font-size:14px;} </style> </head> <body style="padding:10px;" ng-app="app"> <div ng-controller="MyCtrl"> <input type="text" ng-model="id"> <input type="text" ng-model="name"> <button class="button" onclick="addUser()">Add to</button> <button class="button" onclick="delUser()">delete</button> </div> </body> <script src="js/"></script> <script src=""></script> </html>
('app', []) .controller('MyCtrl', function ($scope, $http) { $=" "; $=" "; $ = function () { $('http://127.0.0.1:80/user/addUser',{id:$, name:$}) .success(function (resp) { if(){ alert("Added successfully"); } }) } $ = function () { $('http://127.0.0.1:80/user/delUser',{id:$}) .success(function () { if(){ alert('Delete successfully'); } }) } })
Background Java code:
public void addUser(){ String id = getPara("id"); String name = getPara("name"); User user = new User(); boolean isok = false; if(id != null && ("")){ isok = ("id",id).set("name",name).update(); }else{ isok = ("name",name).save(); } renderJson("seccess",isok); } public void delUser(){ String id = getPara("id"); boolean isok = (id); renderJson("seccess",isok); }
Summarize
The above is the entire content of this article. I hope that the content of this article will be of some help to everyone’s learning or use. If you have any questions, you can leave a message to communicate. Thank you for your support.