网站制作器软件下载,在线制图网,长春880元网站建设,做网站字号多大我写了一个 maven 项目的 Demo#xff0c;用来演示 JAVA8 如何读取 JSON 文件树形结构#xff0c;并将这种树形结构保存到 MySQL 中。
json文件 city.json
{name: 山东省,sub: [{name: 青岛市,sub用来演示 JAVA8 如何读取 JSON 文件树形结构并将这种树形结构保存到 MySQL 中。
json文件 city.json
{name: 山东省,sub: [{name: 青岛市,sub: [{name: 市南区},{name: 市北区},{name: 城阳区},{name: 李沧区},]},{name: 济南市,sub: [{name: 市中区},{name: 历下区},{name: 天桥区}]},{name: 淄博市,sub: [{name: 张店区},{name: 临淄区},]},{name: 枣庄市},]
}
MySQL 数据库的 city 表结构
CREATE TABLE city (id int(10) NOT NULL AUTO_INCREMENT COMMENT 主键,parent_id int(10) NOT NULL COMMENT 父级ID,name varchar(50) NOT NULL COMMENT 名称,del_flag tinyint(1) NOT NULL COMMENT 0未删除1已删除,PRIMARY KEY (id)
) ENGINEInnoDB AUTO_INCREMENT15 DEFAULT CHARSETutf8mb4
pom文件
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdzhangchao/groupIdartifactIdJava8Json/artifactIdversion1.0-SNAPSHOT/versionpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingproject.reporting.outputEncodingUTF-8/project.reporting.outputEncodingjava.version1.8/java.versionmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependencies!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --dependencygroupIdcom.google.code.gson/groupIdartifactIdgson/artifactIdversion2.10.1/version /dependency!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j --dependencygroupIdcom.mysql/groupIdartifactIdmysql-connector-j/artifactIdversion8.3.0/version/dependency!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --dependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversion3.5.15/version/dependency/dependencies
/projectmybatis 配置文件
?xml version1.0 encodingUTF-8 ?
!DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd
configurationenvironments defaultdevelopmentenvironment iddevelopmenttransactionManager typeJDBC/dataSource typePOOLEDproperty namedriver valuecom.mysql.cj.jdbc.Driver/property nameurl valuejdbc:mysql://127.0.0.1:3306/test/property nameusername valueroot/property namepassword value123456//dataSource/environment/environmentsmappersmapper resourcemapper/CityMapper.xml//mappers
/configuration
读取配置文件的类 DBFactory.java
package zhangchao.common.db;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.IOException;
import java.io.InputStream;public class DBFactory {private static SqlSessionFactory sqlSessionFactory null;static {String resource mybatis-config.xml;InputStream inputStream null;try {inputStream Resources.getResourceAsStream(resource);} catch (IOException e) {e.printStackTrace();}sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);}public static SqlSessionFactory getInstance(){return sqlSessionFactory;}}
mybatis 的 mapper XML 文件 CityMapper.xml
?xml version1.0 encodingUTF-8?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacezhangchao.addjsontree.CityMapperresultMap idrm typezhangchao.addjsontree.Cityid propertyid columnc_id/result propertyparentId columnparent_id/result propertyname columnname/result propertydelFlag columndel_flag//resultMapinsert idinsert useGeneratedKeystrue keyPropertyidINSERT INTO test.city (parent_id,name,del_flag)VALUES(#{parentId},#{name},#{delFlag})/insert
/mapper和数据库表对应的实体类 City.java
package zhangchao.addjsontree;public class City {private Integer id;private Integer parentId;private String name;private Integer delFlag;public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public Integer getParentId() {return parentId;}public void setParentId(Integer parentId) {this.parentId parentId;}public String getName() {return name;}public void setName(String name) {this.name name;}public Integer getDelFlag() {return delFlag;}public void setDelFlag(Integer delFlag) {this.delFlag delFlag;}
}
和 JSON 对应的 DTO 文件 CityDto.java
package zhangchao.addjsontree;import java.util.List;public class CityDto {private Integer id;private Integer parentId;private String name;private ListCityDto sub;Overridepublic String toString() {final StringBuffer sb new StringBuffer(CityDto{);sb.append(id).append(id);sb.append(, parentId).append(parentId);sb.append(, name).append(name).append(\);sb.append(, sub).append(sub);sb.append(});return sb.toString();}public ListCityDto getSub() {return sub;}public void setSub(ListCityDto sub) {this.sub sub;}public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public Integer getParentId() {return parentId;}public void setParentId(Integer parentId) {this.parentId parentId;}public String getName() {return name;}public void setName(String name) {this.name name;}}
CityMapper.java
package zhangchao.addjsontree;public interface CityMapper{int insert(City city);
}
整个程序的主方法演示了具体插入数据库的算法。
先调用 getJSon() 方法从 JSON 文件中获取 JSON 字符串然后通过 GSON 转换成 CityDto 对象。
然后使用了树的宽度优先算法遍历树形结构currentLevel 列表保存当前层节点nextLevel 列表保存下一层节点。
每次循环把当前层节点插入数据库并且在插入数据库后获取数据库 ID这里把表主键设置成整数自增。对子节点做是否为空的检查并且把 NULL 子节点删除掉。把数据库 ID 保存到子节点的 parentId 成员变量中。
把非空子节点加入到 nextLevel 列表中然后让 currentLevel 引用指向 nextLevel。
package zhangchao.addjsontree;import com.google.gson.Gson;
import org.apache.ibatis.session.SqlSession;
import zhangchao.common.db.DBFactory;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;/*** author zhangchao */
public class TestAddJsonTree {private static String getJSon(){File f new File(E:\\ws\\zc\\Java8Json\\src\\main\\resources\\JsonFile\\city.json);StringBuilder sb new StringBuilder();FileInputStream fis null;BufferedReader br null;try {fis new FileInputStream(f);br new BufferedReader(new InputStreamReader(fis, UTF-8));String str br.readLine();while(str ! null) {sb.append(str);str br.readLine();}} catch (FileNotFoundException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (fis ! null) {fis.close();}if (br ! null) {br.close();}} catch (IOException e) {e.printStackTrace();}}String json sb.toString();return json;}private static void dtoToDomain(CityDto dto, City domain) {domain.setName(dto.getName());domain.setParentId(dto.getParentId());domain.setDelFlag(0);}public static void main(String[] args) {String json getJSon();Gson gson new Gson();CityDto cityDto gson.fromJson(json, CityDto.class);if (cityDto null) {return;}cityDto.setParentId(-1);ListCityDto currentLevel new ArrayList();currentLevel.add(cityDto);SqlSession sqlSession DBFactory.getInstance().openSession(false);try {CityMapper cityMapper sqlSession.getMapper(CityMapper.class);while (currentLevel ! null !currentLevel.isEmpty()) {ListCityDto nextLevel new ArrayList();for (CityDto dto : currentLevel) {City domain new City();dtoToDomain(dto, domain);cityMapper.insert(domain);ListCityDto sub dto.getSub();if (sub ! null !sub.isEmpty()) {for (IteratorCityDto iterator sub.iterator(); iterator.hasNext();) {CityDto item iterator.next();if (item null) {iterator.remove();} else {item.setParentId(domain.getId());}}nextLevel.addAll(sub);}}currentLevel nextLevel;}sqlSession.commit();} catch (Exception e) {e.printStackTrace();sqlSession.rollback();} finally {sqlSession.close();}}
}
最后插入数据库的效果如下