Home>

First, use $http to query mysql data

angular.module ("app", [])
.controller ("myctrl", function ($scope, $http) {
 $http.get ("http://127.0.0.1:80/user/getusers")
 .success (function (resp) {
  console.log (resp);
 })
 .error ()
 //jquery
 /*$.get("url",function (data) {
 });* /
})

Corresponding background java code:

public void getusers () {
  list<user>users=user.dao.find ("select * from t_user");
renderjson (users);
}

Second, $http to add, delete, and modify data

(1) $http send 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/foundation.min.css" rel="external nofollow">
 <style type="text/css">
  html, body {font-size:14px;}
 </style>
</head>
<body ng-app="app">
 <div ng-controller="myctrl">
  <input type="text" ng-model="id">
  <input type="text" ng-model="name">
  <button onclick="adduser ()">add</button>
  <button onclick="deluser ()">delete</button>
 </div>
</body>
<script src="js/angular.min.js"></script>
<script src="app.js"></script>
</html>
angular.module ("app", [])
 .controller ("myctrl", function ($scope, $http) {
  $scope.id="";
  $scope.name="";
  $scope.adduser=function () {
   $http.post ("http://127.0.0.1:80/user/adduser", {id:$scope.id, name:$scope.name})
    .success (function (resp) {
     if (resp.success) {
      alert ("Added successfully");
     }
    })
  }
  $scope.deluser=function () {
   $htp.post ("http://127.0.0.1:80/user/deluser", {id:$scope.id})
   .success (function () {
    if (resp.success) {
     alert ("Successfully deleted");
    }
   })
  }
 })

Background java code:

public void adduser () {
  string id=getpara ("id");
  string name=getpara ("name");
  user user=new user ();
  boolean isok=false;
  if (id!=null&&id.equals ("")) {
   isok=user.set ("id", id) .set ("name", name) .update ();
  } else {
   isok=user.set ("name", name) .save ();
  }
  renderjson ("seccess", isok);
}
public void deluser () {
  string id=getpara ("id");
  boolean isok=user.dao.delebyid (id);
  renderjson ("seccess", isok);
}

to sum up

  • Previous Four methods for Oracle to connect to a remote database
  • Next Tutorial of using ng-if, ng-show/ng-hide and ng-switch in AngularJS