list, Predicate predicate) {
86 | list.stream().filter((l) -> (predicate.test(l))).forEach((l) -> {
87 | System.out.println(l);
88 | });
89 | }
90 |
91 | }
92 |
93 |
94 | /**
95 | * 你可以使用 下面语法实现Lambda:
96 | *
97 | * (params) -> expression
98 | * (params) -> statement
99 | * (params) -> { statements }
100 | *
101 | * 带参数的例子可以在:baseController.java中找到
102 | */
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/lambda/TestMap.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.lambda;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 |
6 | public class TestMap {
7 | public static void main(String[] args) {
8 | Map map = new HashMap<>();
9 | map.put("java", "script");
10 | }
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/listener/RequestListenerDemo.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.listener;
2 |
3 | import javax.servlet.ServletRequestEvent;
4 | import javax.servlet.ServletRequestListener;
5 | import javax.servlet.annotation.WebListener;
6 |
7 | @WebListener
8 | public class RequestListenerDemo implements ServletRequestListener{
9 |
10 | @Override
11 | public void requestDestroyed(ServletRequestEvent sre) {
12 | System.err.println("request destroy");
13 | }
14 |
15 | @Override
16 | public void requestInitialized(ServletRequestEvent sre) {
17 | System.err.println("request init");
18 |
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/listener/ServletContextListenerDemo.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.listener;
2 |
3 | import javax.servlet.ServletContextEvent;
4 | import javax.servlet.ServletContextListener;
5 | import javax.servlet.annotation.WebListener;
6 |
7 | @WebListener
8 | public class ServletContextListenerDemo implements ServletContextListener {
9 |
10 | @Override
11 | public void contextInitialized(ServletContextEvent sce) {
12 | System.err.println("-1.ServletContextListener init");
13 | }
14 |
15 | @Override
16 | public void contextDestroyed(ServletContextEvent sce) {
17 | System.err.println("ServletContextListener destroy");
18 |
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/listener/SessionListenerDemo.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.listener;
2 |
3 | import javax.servlet.annotation.WebListener;
4 | import javax.servlet.http.HttpSessionEvent;
5 | import javax.servlet.http.HttpSessionListener;
6 |
7 | @WebListener
8 | public class SessionListenerDemo implements HttpSessionListener {
9 |
10 | @Override
11 | public void sessionCreated(HttpSessionEvent se) {
12 | System.err.println("3.session listener..");
13 | }
14 |
15 | @Override
16 | public void sessionDestroyed(HttpSessionEvent se) {
17 | System.err.println("session 被销毁");
18 |
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/pagination/PaginationFormatting.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.pagination;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 |
6 | import org.springframework.data.domain.Page;
7 | import org.springframework.data.domain.Pageable;
8 |
9 | import com.example.demo.bean.Persons;
10 | import com.example.demo.repository.PersonsRepository;
11 | import com.example.demo.utils.SpringUtil;
12 |
13 |
14 | interface Types {
15 |
16 | public Page query();
17 |
18 | public Integer getCount();
19 |
20 | public Integer getPageNumber();
21 |
22 | public Long getTotal();
23 |
24 | public Object getContent();
25 | }
26 |
27 | class BasePaginationInfo {
28 |
29 | public Pageable pageable;
30 |
31 | public PersonsRepository instance = SpringUtil.getBean(PersonsRepository.class);
32 |
33 | public String sex, email;
34 |
35 | public BasePaginationInfo(String sexName, String emailName, Pageable pageable) {
36 |
37 | this.pageable = pageable;
38 |
39 | this.sex = sexName;
40 |
41 | this.email = emailName;
42 | }
43 | }
44 |
45 | class AllType extends BasePaginationInfo implements Types {
46 |
47 |
48 | public AllType(String sexName, String emailName, Pageable pageable) { //String sexName, String emailName,
49 |
50 | super(sexName, emailName, pageable);
51 |
52 | }
53 |
54 | public Page query() {
55 |
56 | return this.instance.findAll(
57 |
58 | this.pageable
59 |
60 | );
61 | }
62 |
63 | public Integer getCount() {
64 | return this.query().getSize();
65 | }
66 |
67 | public Integer getPageNumber() {
68 |
69 | return this.query().getNumber();
70 |
71 | }
72 |
73 | public Long getTotal() {
74 | return this.query().getTotalElements();
75 | }
76 |
77 | public Object getContent() {
78 | return this.query().getContent();
79 | }
80 | }
81 |
82 | class SexEmailType extends BasePaginationInfo implements Types {
83 |
84 | public SexEmailType(String sexName, String emailName, Pageable pageable) {
85 |
86 | super(sexName, emailName, pageable);
87 |
88 | }
89 |
90 | public Page query() {
91 |
92 | return this.instance.findBySexAndEmailContains(
93 |
94 | this.sex,
95 |
96 | this.email,
97 |
98 | this.pageable
99 | );
100 | }
101 |
102 | public Integer getCount() {
103 | return this.query().getSize();
104 | }
105 |
106 | public Integer getPageNumber() {
107 |
108 | return this.query().getNumber();
109 |
110 | }
111 |
112 | public Long getTotal() {
113 | return this.query().getTotalElements();
114 | }
115 |
116 | public Object getContent() {
117 | return this.query().getContent();
118 | }
119 |
120 |
121 | }
122 |
123 | class SexType extends BasePaginationInfo implements Types {
124 |
125 | public SexType(String sexName, String emailName, Pageable pageable) { //String sexName, String emailName,
126 |
127 | super(sexName, emailName, pageable);
128 | }
129 |
130 | public Page query() {
131 |
132 | return this.instance.findBySex(
133 |
134 | this.sex,
135 |
136 | this.pageable
137 | );
138 | }
139 |
140 | public Integer getCount() {
141 | return this.query().getSize();
142 | }
143 |
144 | public Integer getPageNumber() {
145 |
146 | return this.query().getNumber();
147 |
148 | }
149 |
150 | public Long getTotal() {
151 | return this.query().getTotalElements();
152 | }
153 |
154 | public Object getContent() {
155 | return this.query().getContent();
156 | }
157 | }
158 |
159 |
160 | public class PaginationFormatting {
161 |
162 | private PaginationMultiTypeValuesHelper multiValue = new PaginationMultiTypeValuesHelper();
163 |
164 | private Map results = new HashMap<>();
165 |
166 | public Map filterQuery(String sex, String email, Pageable pageable) {
167 |
168 | Types typeInstance;
169 |
170 | if (sex.length() == 0 && email.length() == 0) {
171 |
172 | typeInstance = new AllType(sex, email, pageable);
173 |
174 | } else if (sex.length() > 0 && email.length() > 0) {
175 |
176 | typeInstance = new SexEmailType(sex, email, pageable);
177 |
178 | } else {
179 | typeInstance = new SexType(sex, email, pageable);
180 | }
181 |
182 | this.multiValue.setCount(typeInstance.getCount());
183 |
184 | this.multiValue.setPage(typeInstance.getPageNumber() + 1);
185 |
186 | this.multiValue.setResults(typeInstance.getContent());
187 |
188 | this.multiValue.setTotal(typeInstance.getTotal());
189 |
190 | this.results.put("data", this.multiValue);
191 |
192 | return results;
193 | }
194 |
195 | }
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/pagination/PaginationMultiTypeValuesHelper.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.pagination;
2 |
3 | public class PaginationMultiTypeValuesHelper {
4 |
5 | private Integer count, page;
6 |
7 | private Object results;
8 |
9 | private Long total;
10 |
11 | public void setCount(Integer name) {
12 | this.count = name;
13 | }
14 |
15 | public Integer getCount() {
16 | return count;
17 | }
18 |
19 | public Integer getPage() {
20 | return page;
21 | }
22 |
23 | public void setPage(Integer page) {
24 | this.page = page;
25 | }
26 |
27 | public Object getResults() {
28 | return results;
29 | }
30 |
31 | public void setResults(Object results) {
32 | this.results = results;
33 | }
34 |
35 | public Long getTotal() {
36 | return total;
37 | }
38 |
39 | public void setTotal(Long total) {
40 | this.total = total;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/repository/PersonsRepository.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.repository;
2 |
3 | import java.util.List;
4 |
5 | import org.springframework.data.domain.Page;
6 | import org.springframework.data.domain.Pageable;
7 | import org.springframework.data.jpa.repository.JpaRepository;
8 | import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
9 | import org.springframework.data.jpa.repository.Query;
10 | import org.springframework.stereotype.Repository;
11 |
12 | import com.example.demo.bean.Persons;
13 |
14 | @Repository
15 | public interface PersonsRepository extends JpaRepository, JpaSpecificationExecutor {
16 |
17 | @Query(value = "select DISTINCT sex from Persons p")
18 | List findSex();
19 |
20 | Page findAll(Pageable pageable);
21 |
22 | Page findBySexAndEmailContains(String sexName, String emailName, Pageable pageable);
23 |
24 | Page findBySex(String sexName, Pageable pageable);
25 |
26 | Persons findById(Long id);
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/repository/StudentRepository.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.repository;
2 |
3 | import org.springframework.data.jpa.repository.JpaRepository;
4 | import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
5 | import org.springframework.stereotype.Repository;
6 |
7 | import com.example.demo.bean.Student;
8 |
9 | @Repository
10 | public interface StudentRepository extends JpaRepository, JpaSpecificationExecutor {
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/repository/TreeNodeRepository.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.repository;
2 |
3 | import org.springframework.data.jpa.repository.JpaRepository;
4 | import org.springframework.stereotype.Repository;
5 |
6 | import com.example.demo.bean.TreeNode;
7 |
8 | @Repository
9 | public interface TreeNodeRepository extends JpaRepository{
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/repository/UserRepository.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.repository;
2 |
3 | import org.springframework.data.domain.Page;
4 | import org.springframework.data.domain.Pageable;
5 | import org.springframework.data.jpa.repository.JpaRepository;
6 | import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
7 | import org.springframework.stereotype.Repository;
8 |
9 | import com.example.demo.bean.User;
10 |
11 | @Repository
12 | public interface UserRepository extends JpaRepository, JpaSpecificationExecutor {
13 |
14 | Page findAll(Pageable pageable);
15 |
16 | User findById(String id);
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/utils/FileUploadUtil.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.utils;
2 |
3 | import java.io.File;
4 |
5 | import javax.servlet.http.HttpServletRequest;
6 |
7 | import org.springframework.web.multipart.MultipartFile;
8 |
9 | public class FileUploadUtil {
10 | public static Boolean uploadFile(HttpServletRequest request, MultipartFile file) {
11 | System.out.println("开始");
12 | //String path = request.getSession().getServletContext().getRealPath("upload");
13 | String path = "C:\\Users\\shenzm\\Desktop\\upload";
14 | String fileName = file.getOriginalFilename();
15 | System.out.println(path);
16 | File targetFile = new File(path, fileName);
17 | if (!targetFile.exists()) {
18 | targetFile.mkdirs();
19 | }
20 | // 保存
21 | try {
22 | file.transferTo(targetFile);
23 | return true;
24 | } catch (Exception e) {
25 | e.printStackTrace();
26 | return false;
27 | }
28 |
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/utils/HanyuPinyinHelper.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.utils;
2 |
3 | import net.sourceforge.pinyin4j.PinyinHelper;
4 | import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
5 | import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
6 | import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
7 | import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
8 | import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
9 |
10 | public class HanyuPinyinHelper {
11 |
12 | /**
13 | * 将文字转为汉语拼音
14 | * @param chineselanguage 要转成拼音的中文
15 | */
16 | public String toHanyuPinyin(String ChineseLanguage){
17 | char[] cl_chars = ChineseLanguage.trim().toCharArray();
18 | String hanyupinyin = "";
19 | HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
20 | defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部小写
21 | defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
22 | defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V) ;
23 | try {
24 | for (int i=0; i T getBean(Class clazz) {
29 | return getApplicationContext().getBean(clazz);
30 | }
31 |
32 | public static T getBean(String name, Class clazz) {
33 | return getApplicationContext().getBean(name, clazz);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | # dev env
2 | server.port=8000
3 |
4 | logging.level.root=info
5 |
6 | paginatio.max-per-page=6
7 |
8 | logging.file=classpath:/log-info.log
9 |
10 |
11 | #spring.mvc.view.prefix=/
12 | #spring.mvc.view.suffix=.html
13 |
14 |
15 | # datasource
16 | spring.jpa.generate-ddl=true
17 | spring.jpa.hibernate.ddl-auto=update
18 | spring.datasource.continueOnError=true
19 | spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy
20 | spring.jpa.database=mysql
21 | spring.jpa.show-sql=true
22 |
23 | spring.datasource.url=jdbc:mysql://10.1.51.96:3306/test?useUnicode=true&characterEncoding=UTF-8&connectTimeout=60000&socketTimeout=60000&autoReconnect=true&autoReconnectForPools=true&failOverReadOnly=false
24 | spring.datasource.username=root
25 | spring.datasource.password=root
26 | spring.datasource.driverClassName=com.mysql.jdbc.Driver
27 | spring.datasource.sqlScriptEncoding=utf-8
28 |
29 | # HikariCP settings
30 | spring.datasource.hikari.connection-timeout=60000
31 | spring.datasource.hikari.maximum-pool-size=15
32 | spring.datasource.hikari.max-lifetime=1800000
33 | spring.datasource.hikari.idle-timeout=600000
34 | spring.datasource.hikari.read-only=false
35 |
36 | #dataSoruce config
37 | spring.datasource.max-active=100
38 | spring.datasource.max-idle=8
39 | spring.datasource.min-idle=8
40 | spring.datasource.initial-size=30
41 | spring.datasource.validation-query=select 1
42 | spring.datasource.test-on-borrow=true
43 |
44 |
45 |
--------------------------------------------------------------------------------
/src/main/resources/static/chart-backup.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | D3制作力导向关系图
6 |
7 |
8 |
9 |
10 |
11 |
12 |
152 |
153 |
--------------------------------------------------------------------------------
/src/main/resources/static/chart.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | D3制作力导向关系图
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/src/main/resources/static/chart.js:
--------------------------------------------------------------------------------
1 | /**
2 | * nodes :
3 | * name:节点的title,
4 | * group:围绕中心的层数,一个组合同一个颜色
5 | * index:区分的标记
6 | *
7 | * links:
8 | * source:源
9 | * target:链接的目的地
10 | */
11 | $(document).ready(function () {
12 | var renderId ="#main_2";
13 |
14 | var jsonStr = {
15 | "nodes": [
16 | {"name": "中国", "group": 1, "index": 0},
17 | {"name": "内蒙古", "group": 2, "index": 1},
18 | {"name": "猫咪", "group": 3, "index": 2},
19 | {"name": "四川", "group": 2, "index": 3},
20 | {"name": "棕熊", "group": 2, "index": 4},
21 | {"name": "臭豆腐", "group": 3, "index": 5},
22 | {"name": "小猪猪", "group": 2, "index": 6},
23 | {"name": "湖南", "group": 2, "index": 7},
24 | {"name": "大熊猫", "group": 3, "index": 8},
25 | {"name": "北京", "group": 2, "index": 9},
26 | {"name": "雾霾", "group": 3, "index": 10}
27 | ],
28 | "links": [
29 | {"source": 0, "target": 1},
30 | {"source": 1, "target": 2},
31 | {"source": 0, "target": 3},
32 | {"source": 3, "target": 8},
33 | {"source": 0, "target": 4},
34 | {"source": 0, "target": 6},
35 | {"source": 0, "target": 7},
36 | {"source": 7, "target": 5},
37 | {"source": 0, "target": 9},
38 | {"source": 9, "target": 10}
39 | ]
40 | };
41 |
42 | drawChart(renderId,jsonStr);/*在$(“#main_2”)作用域内画图*/
43 |
44 | });
45 |
46 | /**
47 | *
48 | * @param renderId 渲染的ID
49 | * @param graph 数据
50 | * @returns
51 | */
52 | function drawChart(renderId, graph) {
53 | var index = 0;
54 | var width = 660, height = 580;
55 | var color = ["#FF8000", "#9393FF", "#0080FF"];
56 | var force = d3.layout.force() /*layout将json格式转化为力学图可用的格式*/
57 | .linkDistance(20) /*指定结点连接线的距离,默认为20*/
58 | .charge(-300) /*顶点的电荷数。该参数决定是排斥还是吸引,数值越小越互相排斥*/
59 | .size([width, height]); /*作用域*/
60 | var svg;
61 | var dd = $('');
62 | $(renderId).append(dd);
63 | /*D3采用SVG来更加生动展现数据,此处设置svg的基本样式*/
64 | svg = d3.select(".d3strench").append("svg")
65 | .attr("width", width)
66 | .attr("height", height)
67 | .attr('border', 'red')
68 | .style({
69 | 'margin': '0 auto',
70 | 'display': 'block'
71 | });
72 | var nodes = graph.nodes.slice(), /*nodes() 里传入顶点的数组*/ links = [], bilinks = [];
73 |
74 |
75 | /*将数据组装成source-->target的形式*/
76 | graph.links.forEach(function (link) {
77 | var s = nodes[link.source],
78 | t = nodes[link.target],
79 | i = {}; // 中间节点
80 | nodes.push(i);
81 | links.push({source: s, target: i}, {source: i, target: t});
82 | bilinks.push([s, i, t]);
83 | });
84 | force.nodes(nodes).links(links).start();
85 |
86 | /*svg的path标签被称为”可以组成任何形状的形状”,所以此处用path标签来绘制线条*/
87 | var link = svg.selectAll(".link")//线条
88 | .data(bilinks)
89 | .enter().append("path")
90 | .attr("class", "link")
91 | .attr("stroke", function (d) {
92 | return color[d[0].group - 1];
93 | }).attr("stroke-width", 1)
94 |
95 | /*接下来是数据的渲染*/
96 | var node = svg.selectAll(".node")
97 | .data(graph.nodes)
98 | .enter().append("g")
99 | .attr("class", "node")
100 | .attr("group", function (d) {
101 | return d.group;
102 | }).call(force.drag);
103 |
104 | node.append("circle")
105 | .attr("r", 8)
106 | .style("fill", function (d) {
107 | return color[d.group - 1];
108 | })
109 | .style("stroke", function (d) {
110 | return color[d.group - 1];
111 | }).style("stroke-width", "8") //圆外面的轮廓线
112 | .style("stroke-opacity", "0.6"); //圆外面的轮廓线的透明度
113 |
114 | node.filter(function (d) {
115 | return d.group !== 0;
116 | }).append("text")
117 | .attr("font-family", "微软雅黑")
118 | .attr("text-anchor", "middle")
119 | .attr("dy", function () { //dy表示文字的偏移量
120 | return "0em";
121 | })
122 | .attr('x', function (d) {
123 | d3.select(this).append('tspan')//添加文字
124 | .text(function () {
125 | return d.name;
126 | });
127 | d3.selectAll(".node[group='3'] text")//设置圆圈的样式以及半径
128 | .selectAll("tspan")
129 | .attr("fill", "#000");
130 | d3.selectAll(".node[group='1'] circle")
131 | .attr('r', 40)
132 | .style('cursor', 'pointer');
133 | d3.selectAll(".node[group='2'] circle")
134 | .attr('r', 25);
135 | d3.selectAll(".node[group='3'] circle")
136 | .attr('r', 30);
137 | });
138 |
139 |
140 | //为每个节点设置title(类似于html标签的title属性)
141 | node.append("title").text(function (d) {return d.name;});
142 |
143 | /*拖拽事件*/
144 | force.on("tick", function () {
145 | link.attr("d", function (d) {//设置线条的偏移以及路径
146 | var dx = d[2].x - d[0].x, dy = d[2].y - d[0].y, dr = Math.sqrt(dx * dx + dy * dy);
147 | /*下面表示位置的菜蔬中, M(表示画笔落下的位置), A(画椭圆)是大写的,表示绝对位置。当使用相对位置时,要小写*/
148 | return "M" + d[0].x + "," + d[0].y + "A" + dr + "," + dr + " 0 0,0 " + d[2].x + "," + d[2].y;
149 | }).attr("fill", "transparent");
150 | node.attr("transform", function (d) {//circle节点的偏移量
151 | return "translate(" + d.x + "," + d.y + ")";
152 | });
153 | });
154 | force.stop();
155 | force.start();
156 | }
--------------------------------------------------------------------------------
/src/main/resources/static/force.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 力导向图
5 |
6 |
7 |
11 |
12 |
13 |
97 |
98 |
99 |
100 |
--------------------------------------------------------------------------------
/src/test/java/com/example/demo/ApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.example.demo;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class ApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------