Springboot+ajax the way to transfer JSON arrays and a single piece of data The following is a demo that transmits Ajax to a background single and parse multiple data:
The structure diagram is as follows:
The following is the relevant code:
Pom.xml:
<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 Http://maven.apache . org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupid>com.example</ groupid> <artifactId>springboot-ssm</artifactId> <version>0.0.1-SNAPSHOT</version> < ;p ackaging>jar</packaging> <name>springboot-ssm</name> <description>demo Project for SPR ing boot</description> <parent> <groupId>org.springframework.boot</groupId> <a Rtifactid>spring-boot-starter-parent</artifactid> <version>1.5.9.RELEASE</version> < Relativepath/> <!--lookup parent from repository to </parent> <properties> <project . build.sourceencoding>utf-8</project.bUild.sourceencoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <!--The following two items are required if not configured, parsing Themleaft will be problematic--<thymeleaf.vers Ion>3.0.2.release</thymeleaf.version> <thymeleaf-layout-dialect.version>2.0.5</ thymeleaf-layout-dialect.version> </properties> <dependencies> <dependency> & Lt;groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid > </dependency> <dependency> <GROUPID>ORG.SPRINGFRAMEWORK.BOOT</GROUPID&G T <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </depen Dency> <!--MyBatis and mysql--> <dependency> <groupid>org.mybatis.spring.boot< ;/groupid> <artifactid>mybatis-spring-boot-starter</artifactid> <version>1.2.0</version> </ dependency> <dependency> <groupId>mysql</groupId> <artifactid>mysq L-connector-java</artifactid> </dependency> <!--druid Dependent--<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <vers Ion>1.0.25</version> </dependency> <!--add static and Templates dependency--<DEPENDENCY&G T <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-web</artifacti d> </dependency> <dependency> <groupid>org.springframework.boot</groupid& Gt <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> < ;artifactid>spring-boot-starter-thymeleaf</artifactid> </dependency> <!--analysis json--> <dependency> <groupId>net.sf.json-lib</groupId> <artifactid>json-lib</ Artifactid> <version>2.4</version> <classifier>jdk15</classifier> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-maven-plugin</ artifactid> </plugin> </plugins> </build></project>
Studentcontroller:
Package Com.home.controller;import Com.home.entity.student;import Com.home.service.studentservice;import Net.sf.json.jsonarray;import Net.sf.json.jsonobject;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.controller;import Org.springframework.ui.model;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestparam;import Org.springframework.web.bind.annotation.responsebody;import java.util.List; @Controllerpublic class Studentcontroller {@Autowired private studentservice studentservice; @RequestMapping ("/list") @ResponseBody public list<student> list () {list<student> studentlist = Studentservice.getallstu (); return studentlist; } @RequestMapping ("/tablelist") public String Tablelist (model model) {list<student> studentlist = Stud Entservice.getallstu (); Model.addattribute ("Studentlist", studentlist); REturn "Jsonarray"; } @RequestMapping ("/gotoadd") public String Gotoadd () {return ' add '; } @RequestMapping ("/add") public String Add (Student stu) {Studentservice.insert (STU); return "result"; } @RequestMapping ("/gotoaddjson") public String Gotoaddjson () {return ' JSON '; } @RequestMapping ("/jsonarrayadd") @ResponseBody public string Jsonaadd (@RequestParam ("IDs") string IDs) { System.out.println (IDS); Jsonarray Jsonarray = jsonarray.fromobject (IDs); for (int i = 0; i < jsonarray.size (); i++) {Jsonobject jsonobject = Jsonarray.getjsonobject (i); SYSTEM.OUT.PRINTLN ("The JSON array passed over the parameters are:" + "the" + i + "bar:" + "\ n" + jsonobject.get ("id")); } return "JSON array added successfully"; The//json array is passed @RequestMapping ("/jsonadd") @ResponseBody public string Jsonarrayadd (@RequestParam ("IDs") string IDS) {Jsonobject jsonobject = jsonobject.fromobject (IDs); System.out.prinTLN ("jsonobject==>" + jsonobject); Student stu = (Student) Jsonobject.tobean (Jsonobject, Student.class); System.out.println ("stu==>" + stu); Return "Succeeded!"; }}
Student:
package com.home.entity;import java.io.Serializable;public class Student implements Serializable { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; }}
Studentmapper:
package com.home.mapper;import com.home.entity.Student;import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapperpublic interface StudentMapper { List<Student> getAllStu(); int deleteByPrimaryKey(Integer id); int insert(Student record); int insertSelective(Student record); Student selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(Student record); int updateByPrimaryKey(Student record);}
Studentservice:
package com.home.service;import com.home.entity.Student;import java.util.List;public interface StudentService { List<Student> getAllStu(); void insert(Student stu);}
Studentserviceimpl:
package com.home.service.impl;import com.home.entity.Student;import com.home.mapper.StudentMapper;import com.home.service.StudentService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import javax.annotation.Resource;import java.util.List;@Servicepublic class StudentServiceImpl implements StudentService { @Resource private StudentMapper studentMapper; @Override public List<Student> getAllStu() { return studentMapper.getAllStu(); } @Override public void insert(Student stu) { studentMapper.insert(stu); }}
Studentmapper.xml:
<?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.home.mapper.StudentMapper" > <resultmap id= "baseresultmap" type= " Com.home.entity.Student "> <id column=" id "property=" id "jdbctype=" INTEGER "/> <result column=" n Ame "property=" name "jdbctype=" VARCHAR "/> <result column=" age "property=" Age "jdbctype=" INTEGER "/> &L t;/resultmap> <sql id= "base_column_list" > ID, Name, age </sql> <select id= "Selectbyprimarykey "Resultmap=" Baseresultmap "parametertype=" Java.lang.Integer "> select <include refid=" Base_column_lis T "/> from student where id = #{id,jdbctype=integer} </select> <delete id=" deletebyprimary Key "parametertype=" Java.lang.Integer "> Delete from student where id = #{id,jdbctype=integer} </delete> <insert id= "Insert" parametertype= "com.home.entity.Student" > INSERT into Student (ID, name, age) VALUES (#{id, Jdbctype=integer}, #{name,jdbctype=varchar}, #{age,jdbctype=integer}) </insert> <insert id= "Insertselec tive "parametertype=" com.home.entity.Student "> INSERT INTO Student <trim prefix=" ("suffix=") "suffix Overrides= "," > <if test= "id! = NULL" > ID, </if> <if te st= "Name! = NULL" > name, </if> <if test= "Age! = NULL" > Age, </if> </trim> <trim prefix= "VALUES (" suffix= ")" suffixoverrides= "," > <IF test= "id! = NULL" > #{id,jdbctype=integer}, </if> <if te st= "Name! = NULL" > #{name,jdbctype=varchar}, </if> <if test= "Age! = NULL "> #{Age,jdbctype=integer}, </if> </trim> </insert> <update id= "Updatebyprimarykey Selective "parametertype=" com.home.entity.Student "> Update Student <set > <if test=" Name! = NULL "> name = #{name,jdbctype=varchar}, </if> <if test=" Age! = N Ull "> Age = #{age,jdbctype=integer}, </if> </set> where id = #{id, Jdbctype=integer} </update> <update id= "Updatebyprimarykey" parametertype= "Com.home.entity.Student" > Update student Set name = #{name,jdbctype=varchar}, age = #{age,jdbctype=integer} where id = #{id,jdbctype=int EGER} </update> <select id= "Getallstu" resulttype= "com.home.entity.Student" > select * from Student </select></mapper>
Sqlmapperconfig.xml:
<?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> <!-- <settings> 开启二级缓存 <setting name="cacheEnabled" value="true" /> </settings> --> <!-- 只需要定义个别名,这个应该有 --> <!-- springboot集成pageHelper(Java配置未注入的时候可以用下面配置) --> <!-- <plugins> com.github.pagehelper为PageHelper类所在包名 <plugin interceptor="com.github.pagehelper.PageInterceptor"> 使用下面的方式配置参数,后面会有所有的参数介绍 <property name="helperDialect" value="mysql" /> </plugin> </plugins> --></configuration>
Json.html:
<! DOCTYPE html>
Jsonarray.html:
<! DOCTYPE html>
Application.properties:
#server.port=80logging.level.org.springframework=DEBUG#springboot mybatis#jiazai mybatis peizhiwenjianmybatis.mapper-locations = classpath:mapper/*Mapper.xmlmybatis.config-location = classpath:mybatis/sqlMapperConfig.xml#mybatis.type-aliases-package = com.demo.bean#shujuyuanspring.datasource.driver-class-name= com.mysql.jdbc.Driverspring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8spring.datasource.username = *******spring.datasource.password = *******spring.thymeleaf.prefix=classpath:/templates/
After running the selection of two items, you can get the ID value of the row selected, for bulk operation:Springboot+ajax methods for transmitting JSON arrays and single data