SSM整合框架的使用

SSM的整合爲:springMVC控制頁面跳轉、spring控制bean的注入、mybatis控制數據庫。通過搭建這三種框架從而形成SSM框架,框架的整合搭建可以更加方便開發人員和系統的便捷移動和高可維護性等好處。本文介紹從0搭建SSM框架並完成簡單增刪改查功能操作數據庫的過程。

目錄

一:需要配置的文件

二:增刪改查操作

三:綜合知識點

 

一:需要配置的文件

SSM框架的使用,是基於springMVC、spring+mybatis的基礎上進行整合,說白了就是在spring+mybatis的基礎上加入了springMVC,是他們兩個整合之後運行在MVC架構中。相關配置文件則相應的就包含了spring、springMVC、mybatis的配置文件。現在開始從0搭建SSM的運行環境。

1. springMVC的配置:

該配置主要提供了請求和響應的配置,瀏覽器客戶端發出請求,服務器攔截後交給spring來處理,然後交給對應的controller來完成處理,返回完成響應。首先在web.xml中設置springMVC的攔截servlet用來攔截需要過濾的請求,然後創建springMVC.xml配置文件告訴服務器攔截之後如何處理等信息,文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SSM</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 配置springMVC -->
  <servlet>
  	<servlet-name>spring</servlet-name>  
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springMVC.xml</param-value>   //設置springMVC配置文件的路徑
  	</init-param>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>spring</servlet-name>
  	<url-pattern>*.do</url-pattern>    //攔截所有以.do結尾的請求
  </servlet-mapping>
</web-app>

src下springMVC.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
     
	<context:annotation-config/>
	
	<!-- 自動掃描controller包 -->
	<context:component-scan base-package="com.controller">
		<context:include-filter type="annotation" 
			expression="org.springframework.stereotype.Controller"/>	
	</context:component-scan>
	
	<!-- 開啓註解驅動,使訪問路徑和註解內容匹配 -->
	 <mvc:annotation-driven >
       <mvc:message-converters register-defaults="true">
          <bean class="org.springframework.http.converter.StringHttpMessageConverter">
             <property name="supportedMediaTypes" value="text/plain;charset=UTF-8" />
          </bean>
       </mvc:message-converters>   
    </mvc:annotation-driven>
	
	<!-- 訪問靜態資源 -->
	<mvc:default-servlet-handler/>
        
        <!-- 視圖定位 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    	<property name="prefix" value="/jsp/hero/" />
    	<property name="suffix" value=".jsp" />
    </bean>            
</beans>

2. 配置spring,在web.xml中設置一個監聽器,它會隨着tomcat的啓動進行掃描項目下的jar包從而加載spring配置文件。創建spring的配置文件applicationContext.xml文件,用來注入mybatis的配置信息和註冊各種業務所需要的bean。這裏按照SM整合的方式配置spring,配置一個映射器的整合配置方式。文件如下:

web.xml中添加監聽器:

 <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

src下創建applicationContext文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
     
     <context:annotation-config />        //開啓自動掃描功能
     
     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
     	<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
     	<property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8" />
     	<property name="username" value="root" />
     	<property name="password" value="root" />
     </bean>
     
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatis-config.xml" />            
     </bean>
     
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     	<property name="basePackage" value="com.dao" />
     </bean>     
     <import resource="spring/spring-*.xml"/>    //掃描spring下的所有spring子配置文件
     
</beans>

3. src下配置mybatis的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
	<typeAliases>
		<package name="com.pojo"/>
	</typeAliases>

	<mappers>
		<mapper resource="mybatis/Hero.xml"/>
	</mappers>
</configuration>

4. 項目結構:

本例的項目結構爲:pojo層、mapper層(dao接口層)、sql-xxx.xml(dao實現層)、service接口層、serviceImpl層、controller層(採用註解方式跳轉controller)。

 

二:增刪改查操作

基於SSM框架下,完成對數據庫的操作。請求流程爲:

客戶端發起請求--web.xml攔截請求給springMVC--springMVC根據配置文件交給相關控制器處理--spring中註冊service這個bean,該bean中注入heroDao這個被自動創建的bean(不懂請參考spring+mybatis整合)--控制器中自動裝配service這個bean--通過該bean調用方法操作數據庫--返回modelandview對象完成跳轉或者返回string對象完成跳轉。

1. 查詢heroLiet:

㈠創建hero實體類,並在數據庫中有hero表,且存在記錄。

㈡創建HeroMapper接口,以及sql-hero.xml文件:


public interface HeroMapper {

	public List<Hero> getHerList();
	
	public int addHero(Hero hero);
	
	public int getTotal();
	
	public List<Hero> getHerList(Page page);
	
	public void delHero (int id);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
<mapper namespace="com.dao.HeroMapper">
	<select id="getHerList" resultType="Hero" parameterType="Page">
		select * from hero_1
		<if test="start!=null and count!=null">
			limit #{start},#{count}
		</if>
	</select>
		
	<insert id="addHero" parameterType="Hero">
		insert into hero_1
		(name) values
		(#{name})
	</insert>
	
	<insert id="delHero" parameterType="_int">
		delete from hero_1
		where id=#{id}
	</insert>
	
	<select id="getTotal" resultType="_int">
		select count(*) from hero_1
	</select>			
</mapper>

㈢創建HeroService接口和HeroServiceImpl實現類:


public interface IHeroService {
	public List<Hero> getHeroList();	
	public int addHero(Hero hero);
	public int getTotal();	
	public List<Hero> getHeroList(Page page);	
	public void delHero(int id);	
	public void addTwo();
}

public class HeroServiceImpl implements IHeroService {	
	private HeroMapper heroDao;
	public List<Hero> getHeroList() {
		return heroDao.getHerList();
	}	
	public int addHero(Hero hero) {
		return heroDao.addHero(hero);
	}
	public int getTotal() {
		return heroDao.getTotal();
	}
	public List<Hero> getHeroList(Page page) {
		return heroDao.getHerList(page);
	}	
	public void delHero(int id) {
		heroDao.delHero(id);
	}		
    //heroDao的get/set方法	
}

㈣:spring文件夾下創建spring-hero.xml文件,配置hero需要使用的bean:其中,heroDao這個bean是被spring-mybatis自動創建的。然後注入到heroService的heroDao屬性中。需要在heroService接口中給出heroDao屬性的get/set方法。

<beans
     <bean id="heroController" class="com.controller.HeroController">
     	<property name="iHeroService" ref="heroService" />
     </bean>
     
     <bean id="heroService" class="com.service.impl.HeroServiceImpl">
     	<property name="heroDao" ref="heroDao" />
     </bean>               
</beans>

㈤在controller包下創建HeroController類,作爲控制器。此時基本架構已經創建完成,則現在可以創建一個請求地址,然後在controller控制器中完成業務邏輯,返回jsp頁面,這裏以請求heroList.do這個地址爲例,返回heroList.jsp並遍歷顯示該集合爲例。在controller中:

@Controller
@RequestMapping("/hero")              //上級請求地址,類似於struts的namespace
public class HeroController {

	@Resource
	private IHeroService iHeroService;    //get/set方法
	private List<Hero> heroList;          //get/set方法

	@RequestMapping("/showHero.do")     //請求地址
	public ModelAndView getHeroList() {
		ModelAndView mv = new ModelAndView("showHero");
		heroList = new ArrayList<Hero>();
		heroList = iHeroService.getHeroList();
		mv.addObject("heroList", heroList);
		return mv;
	}

}

㈥在jsp/hero下創建showHero.jsp,使用c:forEach標籤完成遍歷輸出。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>英雄列表</title>
</head>
<body>
<table align="center" border="1">
	<tr>
		<td>id</td>
		<td>name</td>
	</tr>
	<c:forEach items="${heroList}" var="h" varStatus="vs">
	<tr>
		<td>${h.id}</td>
		<td>${h.name}</td>
	</tr>
	</c:forEach>
</table>
</body>
</html>

㈦訪問localhost:8080/SSM/hero/showHero.do,即可顯示heroList數據。請求數據--跳轉controller--跳轉到映射的方法--執行方法--跳轉至JSP完成響應。

至此,一個完整的SSM框架就搭建完成了。

2. 分頁實現

實現分頁利用了SQL的limit查詢功能,並且創建了page類,用來存儲開始頁,末頁,顯示頁數,當前頁數等屬性信息。廢話不說,直接上代碼:

page實體類:

public class Page {	
	private int start;	
	private int count;	
	private int last;	
	private int total;	
	private int pages;	
	private int nowPage;	
    //此方法是:傳入total值,計算最後一頁的start值。
	public void changeLast(int total) {
		if(total%count > 0) {
			last = (total/count) * count;
			/*return last;*/
		}else {
			last = (total/count - 1) *count;
			/*return last;*/
		}
	}
	//此方法是顯示總頁數的值
	public void changePages(int total) {
		if(total%count > 0) {
			pages = (total/count)+1;
		}else {
			pages = total/count;
		}
	}
	//此方法是計算當前頁數的值
	public void nowPage(int total) {
		nowPage = start/count+1;
	}
}

 HeroController中的方法如下:

	@RequestMapping("/showHeroByLimit.do")
	public ModelAndView getHeroListByLimit(Page page,HttpServletRequest req) {
		String id = req.getParameter("id"); //此操作是在添加成功後,重定向至該方法是傳遞一個添加成功的id值,並將這個id傳遞至jsp中,判斷是否添加成功使用。
		ModelAndView mv = new ModelAndView();
		int total = 0;
		total = iHeroService.getTotal(); //獲取總條數
		
		if(page.getCount() == 0) {   //判斷如果直接訪問該請求,則page對象爲空的,判斷爲空給page屬性一個初始值。即默認顯示第一頁。
			page = new Page();
			page.setStart(0);
			page.setCount(5);
		}
		page.changeLast(total);  //計算最後一頁的start值
		page.changePages(total); //計算總頁數

		if(page.getStart() < 0) {    //此判斷是控制如果已經在第一頁了,仍然點上一頁還是會顯示第一頁,最後一頁點擊下一與同理。
			page.setStart(0);
		}else if(page.getStart() >= total) {
			page.setStart(page.getLast());
		}
		page.nowPage(total);  //計算當前頁數。
		
		heroList = new ArrayList<Hero>();
		heroList = iHeroService.getHeroList(page);  //分頁查詢
		mv.addObject("heroList", heroList);
		mv.addObject("page", page);
		mv.addObject("id", id);
		mv.setViewName("showHeroByLimit");  //跳轉
		return mv;
	}

jsp頁面中: 點擊上一頁、下一頁、首頁、末頁時,都會傳遞相關的參數到後臺中,則回臺根據傳遞的參數分頁查詢。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>英雄分頁列表</title>
</head>
<body>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<table align="center" border="1">
	<tr>
		<td>id</td>
		<td>name</td>
		<td>操作</td>
	</tr>
	<c:forEach items="${heroList}" var="h" varStatus="vs">
	<tr>
		<td>${h.id}</td>
		<td>${h.name}</td>
		<td>
			<button class="e" id="${h.id}">修改</button>
			<button class="d" id="${h.id}">刪除</button>
		</td>
	</tr>
	</c:forEach>
	<tr>
		<td colspan="3">
			<a href="?start=0&count=${page.count}">首頁</a>
			<a href="?start=${page.start-page.count}&count=${page.count}">上一頁</a>
			<a href="?start=${page.start+page.count}&count=${page.count}">下一頁</a>
			<a href="?start=${page.last}&count=${page.count}">末頁</a>
			<font>總頁數${page.pages}</font>
			<font>當前頁數${page.nowPage}</font>
		</td>
	</tr>
	
</table>
<div style="display:none" align="center" id="showAdd">
<form>
	英雄名稱<input type="text" name="name" id="name">
	<button onclick="add()">添加</button>
	<div style="display:none" align="center" id="msg"></div>
</form>
</div>
<script type="text/javascript" src="../js/hero/hero.js?t=06"></script>
</body>
</html>

3. 新增添加方法,和通過Ajax刪除英雄

當點擊添加英雄按鈕後,顯示下面的編輯框,如果再次點擊添加英雄按鈕,則此編輯框消失。在編輯框中設置了爲空驗證,如果不爲空點擊添加之後,提交數據至後臺,添加到數據庫,並返回該展示頁面,提示:添加成功,顯示3秒後消失。JSP頁面如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>英雄分頁列表</title>
</head>
<body>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<div align="center">
	<!-- 點擊按鈕,觸發JS中的showAdd方法,效果就是在切換隱藏和顯示下面的form編輯框 -->
	<button onclick="showAdd()">添加英雄</button>
	 <!-- 用來判斷是否添加成功,因爲添加成功的話後臺會傳遞一個id值過來 -->
	<input type="hidden" value="${id}" id="addSucc">
	<!-- 用來顯示添加成功或者失敗的提示信息 -->
	<font id="divSucc" style="display: block"></font>
</div>
<table align="center" border="1">
	<tr>
		<td>id</td>
		<td>name</td>
		<td>操作</td>
	</tr>
	<c:forEach items="${heroList}" var="h" varStatus="vs">
	<tr>
		<td>${h.id}</td>
		<td>${h.name}</td>
		<td>
			<button class="e" id="${h.id}">修改</button>
			<!-- 點擊刪除按鈕後,JS中有class爲d的觸發方法,會執行ajax請求,完成刪除,請看JS的觸發方法 -->
			<button class="d" id="${h.id}">刪除</button>
		</td>
	</tr>
	</c:forEach>
	<tr>
		<td colspan="3">
			<a href="?start=0&count=${page.count}">首頁</a>
			<a href="?start=${page.start-page.count}&count=${page.count}">上一頁</a>
			<a href="?start=${page.start+page.count}&count=${page.count}">下一頁</a>
			<a href="?start=${page.last}&count=${page.count}">末頁</a>
			<font>總頁數${page.pages}</font>
			<font>當前頁數${page.nowPage}</font>
		</td>
	</tr>
	
</table>
<div style="display:none" align="center" id="showAdd">
<form>
	英雄名稱<input type="text" name="name" id="name">
	<!-- 點擊添加之後,進行表單爲空驗證,驗證通過後提交數據到後臺,完成數據插入 -->
	<button onclick="add()">添加</button>
	<!-- 提示輸入爲空驗證消息 -->
	<div style="display:none" align="center" id="msg"></div>
</form>
</div>
<script type="text/javascript" src="../js/hero/hero.js?t=06"></script>
</body>
</html>

JS頁面:

//預加載函數
$(function () {
	if($("#addSucc").val() == null || $("#addSucc").val() == "") {
		$("#divSucc").hide();
	}else {
		$("#divSucc").attr("color","blue");
		$("#divSucc").html("英雄池添加成功!");
		$("#divSucc").fadeIn(1000); // 延遲一秒顯示
		$("#divSucc").fadeOut(3000);//延遲三秒隱藏
	}
    //刪除按鈕的觸發方法,確認框如果點擊是則執行ajax刪除。
	$("button.d").click(function (){
		var d = confirm("確定要刪除該英雄麼?");
		if(d) {
			var id = $(this).attr("id");
			var url = "delHero.do";
			$.ajax({
				url:url,
				type:"get",
				data:{
					id:id
				},
				dataType:"text",
				success:function(result) {
					if(result == 'OK') {
						alert("刪除成功!");
						window.location.reload();
					}else {
						alert("刪除失敗!")
					}
				}
			});
		}
	});
	
	
	
});

//顯示與隱藏切換
function showAdd() {
	$("#showAdd").toggle();	
	
}

//點擊添加之後的表單驗證和提交後臺。
function add() {
	var form = window.document.forms[0];
	if($("#name").val() == null || $("#name").val() == "") {
		$("#msg").show();
		$("#msg").html("輸入的英雄名爲空,請檢查!");
		$("form").submit(function() { 
			return false;
		});
	}else {
		form.action = "addHero.do";
		form.submit();
	}
}

4. 着重介紹下ajax刪除請求

①首先點擊刪除按鈕後,會觸發這個方法,如果點擊是,我們在jsp中將遍歷的hero的id值存儲在該按鈕的id屬性中,則通過attr讀取id屬性的值可以輕易的獲取到該條數據的id值,將該id值傳值後臺完成刪除,當然是通過ajax的方式完成的。ajax就需要傳遞數據到後臺,後臺方法返回一個結果。

$("button.d").click(function (){
        var d = confirm("確定要刪除該英雄麼?");
        if(d) {
            var id = $(this).attr("id");
            var url = "delHero.do";
            $.ajax({
                url:url,
                type:"get",
                data:{
                    id:id
                },
                dataType:"text",
                success:function(result) {
                    if(result == 'OK') {
                        alert("刪除成功!");
                        window.location.reload();
                    }else {
                        alert("刪除失敗!")
                    }
                }
            });
        }
    });

②ajax請求的後臺方法:一般在web服務器中,ajax請求返回值讀取的話是讀取寫入到responseBody中的數據。我們在struts中請求ajax時,可以通過獲取httpservletResponse對象,通過getWrite.write()方法,寫入到response響應體中,來提供給ajax作爲返回值。則再SSM框架中可以直接使用@ResponseBody註解,就可以直接通過返回字符串的方式將該字符串輸出到responseBody中了。

	@ResponseBody   //使用該註解,可以直接將返回值返回至HTML.response.body中,
	@RequestMapping("/delHero.do")
	public String delHero(HttpServletResponse res,HttpServletRequest req) throws IOException {
		id = Integer.parseInt(req.getParameter("id"));
		iHeroService.delHero(id);
		System.out.println(id);
		return "OK";
	}
	

三:綜合知識點

在springMVC中,controller類上面的@requestMapping("hero")與方法中的地址@requestMapping("list"),形成組合地址完成訪問,也就是/hero/list。

@responseBody 與@requestBody的使用以及區別。具體可以參考這位博主的博文:https://blog.csdn.net/ff906317011/article/details/78552426

@Responsebody 註解表示該方法的返回的結果直接寫入 HTTP 響應正文(ResponseBody)中,一般在ajax異步獲取數據時使用,通常是在使用 @RequestMapping 後,返回值通常解析爲跳轉路徑,加上 @Responsebody 後返回結果不會被解析爲跳轉路徑,而是直接寫入HTTP 響應正文中。 

有關JSON數據中,字符和對象之間的轉換,也就是{name:"zhangsan"}與{"name":"zhangsan"}這兩種方式之間的轉換:

①在JS中,將json對象轉換爲字符即var json = {name:"zhang"}轉換爲{"name":"zhang"} 
var jsonStr = JSON.stringify(json)

反過來轉換就是:var json = JSON.parse(jsonStr)

②在java後臺中Json對象與字符串之間的轉換:
從string a = {name:"zhang"}轉換爲Json json={"name":"zhang"}
Json json = new Json();
json.puy("json",JSONObject.toJSON(a)); //此時就有了一個json
json.toJSONString()就轉換爲了{"name","zhang"}了。

反過來轉換就是JSON.parseObject

使用{"name":"zhang"}這種格式的JSON數據更容易匹配值對象中。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章