【问题标题】:how to insert JSON array into MySQL using mvc spring如何使用 mvc spring 将 JSON 数组插入 MySQL
【发布时间】:2015-11-10 21:12:11
【问题描述】:

嗨,我是春天的新手,正在开发一个 POST 模块。 如何在数据库中插入 JSON 数组。 你能告诉我如何解决这个问题吗?

我还有一个例子可以向你展示这一点。 链接:-http://hello-angularjs.appspot.com/angularjs-http-service-ajax-post-json-data-code-example

这里是控制器代码

@RequestMapping(value = "/create1", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
BillingModel addbillingmodel(@RequestBody BillingModel billingmodel) {
    try { 


        billingmodel.getItemid();
        billingmodel.getQuantity();

        dataServices.addEntity(billingmodel); 


        return billingmodel;
    } catch (Exception e) {
         e.printStackTrace();
        return billingmodel;
    }

   }
  }

这是我的带有 JSON 的 html 页面。

<!DOCTYPE html>
<html data-ng-app="serviceModule">
<head>
<meta charset="ISO-8859-1">
<title>AngularJS POST Spring MVC</title>
<script    
src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js">   
</script>
<script type="text/javascript">

var serviceModule = angular.module('serviceModule', []);

serviceModule.controller("AngularJSPostController", function($scope, $http) {

        $scope.variable = "AngularJS POST Spring MVC Example:"; 
        var dataObj = {
                "itemid" : "11",
                "quantity" : "22",


        };      

        var response = 
   $http.post('/CRUDWebAppMavenized/billing_bakery/create1', dataObj);
        response.success(function(data, status, headers, config) {
            $scope.responseData = data;
        });
        response.error(function(data, status, headers, config) {
            alert( "Exception details: " + JSON.stringify({data: data}));
        });

    });

 </script>
 </head>
 <body data-ng-controller="AngularJSPostController"> 




  <div>
    <h4>{{variable}}</h4>
    <b>You had sent below data through post:</b>
    <p>Response:  {{responseData}}</p>      
  </div>

  </body>
</html>

我必须连续处理多个数据。

【问题讨论】:

    标签: mysql json angularjs spring spring-mvc


    【解决方案1】:

    1.在您的 BillingModel 类中创建以下方法 -

         import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
         import com.fasterxml.jackson.core.JsonParseException;
         import com.fasterxml.jackson.core.JsonProcessingException;
         import com.fasterxml.jackson.databind.JsonMappingException;
         import com.fasterxml.jackson.databind.ObjectMapper;
         public String toJson() throws JsonProcessingException {
           ObjectMapper mapper = new ObjectMapper();
           String json = mapper.writeValueAsString(this);
           return json;
        }
    

    2.你应该有 blob 类型的列来存储 JSON

    3.当你调用 addEntity 时,在你的 DAO 类中,做这样的事情 -

    final MapSqlParameterSource sqlParams = new MapSqlParameterSource()
    .addValue("item_json",billingModel.toJson().getBytes("UTF-8"));
    

    您也可以参考。 Converting Java objects to JSON with Jackson

    【讨论】:

    • 2. 你应该有 blob 类型的列来存储 JSON ???实际上我想在表列中插入 JSON 数据作为字符串值,那么为什么要使用 blob?
    • String json 只是一个 String ,如果你想存储在 VARCHAR 中你也可以这样做。在 mysql 中有 text 类型的列来存储长字符串数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 2022-08-14
    • 2016-12-21
    • 2011-08-11
    • 1970-01-01
    • 2017-10-29
    相关资源
    最近更新 更多