sprongboot学习(2)–构建树结构,springboot2整合mybatis、mysql和FastJson
本篇文章的目的主要是学习springboot技术(1)利用Springboot实现树状结构(2)使用阿里的FastJson实现Json风格的数据显示(3)解决springboot2中FastJson功能失效的问题。
1、数据库
可以拷贝下面的sql语句在mysql数据库中执行
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0;
-- Table structure for sys_dept
DROP TABLE IF EXISTS sys_dept; CREATE TABLE sys_dept ( dept_id bigint(20) NOT NULL AUTO_INCREMENT, parent_id bigint(20) NULL DEFAULT NULL COMMENT '上级部门ID,一级部门为0', name varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '部门名称', order_num int(11) NULL DEFAULT NULL COMMENT '排序', del_flag tinyint(4) NULL DEFAULT 0 COMMENT '是否删除 -1:已删除 0:正常', PRIMARY KEY (dept_id) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '部门管理' ROW_FORMAT = Dynamic;
-- Records of sys_dept
INSERT INTO sys_dept VALUES (1, 0, '总集团', 0, 0); INSERT INTO sys_dept VALUES (2, 1, '北京分公司', 1, 0); INSERT INTO sys_dept VALUES (3, 1, '上海分公司', 2, 0); INSERT INTO sys_dept VALUES (4, 3, '技术部', 0, 0); INSERT INTO sys_dept VALUES (5, 3, '销售部', 1, 0); INSERT INTO sys_dept VALUES (6, 2, '其他部', 0, 0); INSERT INTO sys_dept VALUES (7, 4, '技术三级菜单', 0, 0); INSERT INTO sys_dept VALUES (8, 5, '销售三级菜单', 0, 0);
SET FOREIGN_KEY_CHECKS = 1;
2、项目结构
3、代码说明(解决springboot2中FastJson不起作用的问题)
(1)pom.xml加入fastjson依赖
>
>com.alibaba >
>fastjson >
>1.2.15 >
>
(2)继承WebMvcConfigurerAdapter ,重写WebMvcConfigurerAdapter方法 解决springboot2中FastJson不起作用的问题。
@Configuration
public class WebMvcConf extends WebMvcConfigurerAdapter {
/**
* 使用fastJson的方式一 需要 extends WebMvcConfigurationSupport
* 1.需要先定义一个convert转换消息的对象
* 2.添加fastJson的配置信息,比如:是否要格式化返回的json数据
* 3.在convert中添加配置信息
* 4.将convert添加到converts当中
* @param converters
* 这种方式才能解决乱码问题,下面的方式二我试了一下,不得行
* 原文链接:https://blog.csdn.net/qq_33371766/article/details/82220605
*/
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// for (HttpMessageConverter> converter : converters) {
// if (converter instanceof MappingJackson2HttpMessageConverter){
// converters.remove(converter);
// }
// }
Iterator<HttpMessageConverter<?>> iterator = converters.iterator();
while(iterator.hasNext()){
HttpMessageConverter<?> converter = iterator.next();
if(converter instanceof MappingJackson2HttpMessageConverter){
iterator.remove();
}
}
super.configureMessageConverters(converters);
//1.需要先定义一个convert转换消息对象
FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
//2.添加fastJson的配置信息,比如:是否要格式化返回的json数据
FastJsonConfig fastConfig=new FastJsonConfig();
fastConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//处理中文乱码问题(不然出现中文乱码)
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
//3.在convert中添加配置信息
fastConverter.setFastJsonConfig(fastConfig);
//4.将convert添加到converts当中
converters.add(fastConverter);
}
}
4、其他代码说明
(1)pom.xml
<?xml version="1.0" encoding="UTF-8"?>
://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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
>4.0.0 >
>
>org.springframework.boot >
>spring-boot-starter-parent >
>2.3.0.RELEASE >
> <!-- lookup parent from repository -->
com.xinyu.manage
treebuild
0.0.1-SNAPSHOT
treebuild
Demo project for Spring Boot
>
>1.8 >
>
>
>
>org.springframework.boot >
>spring-boot-starter-web >
>
>
>org.mybatis.spring.boot >
>mybatis-spring-boot-starter >
>2.1.2 >
>
>
>mysql >
>mysql-connector-java >
>runtime >
>
>
>com.alibaba >
>fastjson >
>1.2.15 >
>
>
>org.springframework.boot >
>spring-boot-starter-test >
>test >
>
>
>org.junit.vintage >
>junit-vintage-engine >
>
>
>
>
>
>
>
>org.springframework.boot >
>spring-boot-maven-plugin >
>
>
>
>
(2)application.properties
# 端口号8080
server.port=8080
# spring连接mysql配置
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/tree?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# mybatis
#指定POJO扫描包来让mybatis自动扫描到自定义POJO
mybatis.type-aliases-package=com.xinyu.manage.entity
mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapperLocations=classpath:mybatis/mapper/*.xml
(3)entity
import com.alibaba.fastjson.JSON;
import java.io.Serializable;
import java.util.List;
/**
* 部门Tree结构
*/
//@TableName("sys_dept")
public class DeptEntity implements Serializable {
//部门id
//@TableId(value="dept_id",type= IdType.AUTO)
private Integer deptId;
//父Id
private Integer parentId;
//部门名称
private String name;
//排序编号
private String orderNum;
//删除标识
private String delFlag;
// 子节点
private List<DeptEntity> treeNode;
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
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 String getOrderNum() {
return orderNum;
}
public void setOrderNum(String orderNum) {
this.orderNum = orderNum;
}
public String getDelFlag() {
return delFlag;
}
public void setDelFlag(String delFlag) {
this.delFlag = delFlag;
}
public List<DeptEntity> getTreeNode() {
return treeNode;
}
public void setTreeNode(List<DeptEntity> treeNode) {
this.treeNode = treeNode;
}
@Override
public String toString() {
return JSON.toJSONString(this);
}
}
(4)service
public interface TreeService {
List<DeptEntity> getNextNodeTree(Integer deptId);
DeptEntity getNodeTree();
}
(5)serviceImp
@Service
public class TreeServiceImp implements TreeService{
@Autowired
private UserDao userDao;
public List<DeptEntity> getNextNodeTree(Integer deptId){
return userDao.getNextNodeTree(deptId);
}
public DeptEntity getNodeTree(){
return userDao.getNodeTree();
}
}
(6)
UserDao
@Mapper
public interface UserDao {
DeptEntity getNodeTree();
List<DeptEntity> getNextNodeTree(int deptId);
}
mybatis-config.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">
>
>
>
>
>
NodeMapper.xml:路径resources/mybatis/mapper/NodeMapper.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">
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
(7)TreeController
@RestController
public class TreeController {
@Autowired
private TreeService treeService;
@RequestMapping("/tree/{deptId}")
public List<DeptEntity> tree(@PathVariable int deptId){
return treeService.getNextNodeTree(deptId);
}
@RequestMapping("/treeShow")
public List<DeptEntity> treeShow(@RequestParam("id") int id){
return treeService.getNextNodeTree(id);
}
@RequestMapping("nodeTree")
public DeptEntity nodeTree(){
return treeService.getNodeTree();
}
}
(8)http://localhost:8080/treeShow?id=1
页面数据显示以Json风格显示
github项目链接:https://github.com/hnwolfs220/treebuild
树状结构的项目参考: https://blog.csdn.net/qq_38164123/article/details/94358131