工具主要使用了 spring boot(后台)+thymeleaf(模板引擎)+bootstrap(前端框架),使用的都是轻量级框架,
做这个工具的目的主要是为了方便自己使用,也会部署在公司内网服务上,
另外也希望通过记录的方式加深自己的记忆和对项目的理解。
第一篇先介绍设备管理
建好项目结构弄 pom.xml , 加入 thymeleaf,加入数据库方面的依赖,所需依赖很少
<?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.tool</groupId>
<artifactId>plaform</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>plaform</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!--以下两项需要如果不配置,解析themleaft 会有问题-->
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.5</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- 热部署(代码有更改之后,重新启动应用) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在 src /main/resource 建立 application.properties 文件
server.port=9002
spring.mvc.static-path-pattern=/static/**
mongodb.thirdparty.uri=mongodb://test:123456@121.201.8.120:27014
mongodb.thirdparty.dbName=tt
<!DOCTYPE HTML>
<!-- thymeleaf 导入 -->
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>测试平台</title>
<link rel="stylesheet" href="../static/css/bootstrap.min.css"/>
<link rel="stylesheet" href="../static/css/mystyle.css"/>
</head>
<body style="margin-left: 50px">
<div class="navdiv">
<nav class="navbar navbar-default navbar-fixed-top head-top">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand btn-lg" href="#"><span class="glyphicon glyphicon-home"></span>首页</a>
</div>
<ul class="nav navbar-nav navbar-right">
<li class="active"><a href="#"><span class="glyphicon glyphicon-lock"></span>设备列表</a></li>
<li><a><span class="glyphicon glyphicon-th-list"></span>测试平台</a></li>
<li><a><span class="glyphicon glyphicon-asterisk"></span>Mock平台</a></li>
<li><a><span class="glyphicon glyphicon-road"></span>接口测试平台</a></li>
<li><a><span class="glyphicon glyphicon-cog"></span>注册</a></li>
</ul>
</div>
</nav>
</div>
<div class="search ">
<form class="form-inline" th:action="@{/search}" method="post" th:object="${testPlaform}">
<div class="form-group">
<label class="sr-only" for="exampleInputAmount">Iphone8</label>
<div class="input-group">
<div class="input-group-addon">手机型号</div>
<input type="text" class="form-control" id="exampleInputAmount" placeholder="" th:field="*{mobile}">
</div>
</div>
<button type="submit" class="btn btn-primary">查询</button>
</form>
</div>
<div class="data table-responsive">
<table class="table table-bordered table table-hover">
<thead>
<tr>
<th class="col-1">
编号
</th>
<th class="col-2">
厂家
</th>
<th class="col-2">
型号
</th>
<th class="col-2">
UUID
</th>
<th class="col-2">
保管人
</th>
<th class="col-3">
备注
</th>
</tr>
</thead>
<tbody>
<!--dddd-->
<tr th:each="mobile: ${mobileInfoList}">
<td>1</td>
<td th:text="${mobile.plaform}">mobile</td>
<td th:text="${mobile.mobile}">iphone 7</td>
<td th:text="${mobile.uuid}">TYKHJKHJK</td>
<td th:text="${mobile.owner}">xiaoming</td>
<td th:text="${mobile.remark}">未借出</td>
</tr>
</tbody>
</table>
</div>
<script src="../static/js/jquery.min.js.min.js"></script>
<script src="../static/js/bootstrap.min.js"></script>
</body>
</html>
package com.tool.plaform.dao;
import com.mongodb.BasicDBObject;
import com.mongodb.client.MongoCursor;
import com.tool.plaform.domain.TestPlaform;
import com.tool.plaform.utils.MongoDbUtils;
import org.bson.Document;
import java.util.ArrayList;
import java.util.List;
public class TestMobileDao {
public List<TestPlaform> findAllTestMobile(){
BasicDBObject query=new BasicDBObject();
MongoCursor<Document> cursor=MongoDbUtils.find("TestMobile",query);
List<TestPlaform> list=parseTestPlaform(cursor);
return list;
}
public List<TestPlaform> findTestMobileByPlaform(String plaform){
BasicDBObject query=new BasicDBObject();
query.append("型号",plaform);
MongoCursor<Document> cursor=MongoDbUtils.find("TestMobile",query);
System.out.println(cursor);
List<TestPlaform> list=parseTestPlaform(cursor);
return list;
}
public List<TestPlaform> parseTestPlaform(MongoCursor<Document> cursor){
List<TestPlaform> list=new ArrayList<>();
try {
while (cursor!=null && cursor.hasNext()) {
Document doc = cursor.next();
if(doc.containsKey("_id")){
TestPlaform aa=setTestMobileObj(doc);
list.add(aa);
}
}
} catch (Exception e) {
} finally {
if (cursor!=null)
cursor.close();
}
return list;
}
public TestPlaform setTestMobileObj(Document doc ){
TestPlaform testPlaform=new TestPlaform();
String mobile=doc.getString("型号");
String plaform=doc.getString("厂家");
String uuid=doc.getString("uuid");
String owner=doc.getString("保管人");
String remark=doc.getString("备注");
testPlaform.setMobile(mobile);
testPlaform.setOwner(owner);
testPlaform.setPlaform(plaform);
testPlaform.setRemark(remark);
testPlaform.setUuid(uuid);
return testPlaform;
}
}
package com.tool.plaform.controller;
import com.tool.plaform.dao.TestMobileDao;
import com.tool.plaform.domain.TestPlaform;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
@Controller
public class PlaformController {
TestMobileDao dao = new TestMobileDao();
@RequestMapping("/")
public String index(Model model) {
List<TestPlaform> testPlaforms = dao.findAllTestMobile();
model.addAttribute("mobileInfoList", testPlaforms);
return "index";
}
@RequestMapping(value = "/search", method = RequestMethod.POST)
public String searchByPlaform(TestPlaform testPlaform, Model model) {
List<TestPlaform> testPlaforms = dao.findTestMobileByPlaform(testPlaform.getPlaform());
model.addAttribute("mobileInfoList", testPlaforms);
return "index";
}
@ModelAttribute
TestPlaform setTestPlaform() {
return new TestPlaform();
}
}
发布测试访问 http://localhost:9002/,大功告成
接口和 mock 测试的前端页面打算使用 testerhome 上几个大神已经写好的页面,后台自己从新写一遍,
下一篇介绍具体过程