提交用例
This commit is contained in:
parent
85f4d1a591
commit
4484b8d2af
58
spring-boot/spring-boot-jsp/pom.xml
Normal file
58
spring-boot/spring-boot-jsp/pom.xml
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
<?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>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.1.1.RELEASE</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
<groupId>com.heibaiying</groupId>
|
||||||
|
<artifactId>spring-boot-jsp</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>spring-boot-jsp</name>
|
||||||
|
<description>JSP project for Spring Boot</description>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<!--整合 jsp 依赖包-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.tomcat.embed</groupId>
|
||||||
|
<artifactId>tomcat-embed-jasper</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>jstl</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.heibaiying.springboot;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SpringBootJspApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SpringBootJspApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.heibaiying.springboot.bean;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class Programmer {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
private float salary;
|
||||||
|
|
||||||
|
private LocalDate birthday;
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.heibaiying.springboot.controller;
|
||||||
|
|
||||||
|
import com.heibaiying.springboot.bean.Programmer;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping("index")
|
||||||
|
public class JspController {
|
||||||
|
|
||||||
|
@RequestMapping
|
||||||
|
public String jsp(Model model){
|
||||||
|
Programmer programmer = new Programmer("heibai", 21, 1298.31f, LocalDate.now());
|
||||||
|
model.addAttribute("programmer",programmer);
|
||||||
|
return "show";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
spring:
|
||||||
|
mvc:
|
||||||
|
view:
|
||||||
|
prefix: /WEB-INF/jsp/
|
||||||
|
suffix: .jsp
|
@ -0,0 +1,13 @@
|
|||||||
|
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>programmer</title>
|
||||||
|
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/show.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<ul>
|
||||||
|
<li>姓名: ${programmer.name}</li>
|
||||||
|
<li>年龄: ${programmer.age}</li>
|
||||||
|
</ul>
|
||||||
|
</body>
|
||||||
|
</html>
|
4
spring-boot/spring-boot-jsp/src/main/webapp/css/show.css
Normal file
4
spring-boot/spring-boot-jsp/src/main/webapp/css/show.css
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
li{
|
||||||
|
font-size: 30px;
|
||||||
|
color: blueviolet;
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.heibaiying.springboot;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class SpringBootJspApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
48
spring-boot/spring-boot-mongodb/pom.xml
Normal file
48
spring-boot/spring-boot-mongodb/pom.xml
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
<?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>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.1.1.RELEASE</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
<groupId>com.heibaiying</groupId>
|
||||||
|
<artifactId>spring-boot-mongodb</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>spring-boot-mongodb</name>
|
||||||
|
<description>mongodb demo project for Spring Boot</description>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.heibaiying.springboot;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SpringBootMongodbApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SpringBootMongodbApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.heibaiying.springboot.bean;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.springframework.data.annotation.Id;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class Programmer {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
private float salary;
|
||||||
|
|
||||||
|
private Date birthday;
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.heibaiying.springboot.repository;
|
||||||
|
|
||||||
|
import com.heibaiying.springboot.bean.Programmer;
|
||||||
|
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
* @description : jpa 方式查询 对于mongo而言 更推荐这种查询方式 比起原生的语法更加简洁
|
||||||
|
*/
|
||||||
|
public interface ProgrammerRepository extends MongoRepository<Programmer, String> {
|
||||||
|
|
||||||
|
void deleteAllByName(String name);
|
||||||
|
|
||||||
|
Programmer findAllByName(String names);
|
||||||
|
|
||||||
|
Programmer findByNameAndAge(String name, int age);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
spring:
|
||||||
|
data:
|
||||||
|
mongodb:
|
||||||
|
database: spring
|
||||||
|
uri: mongodb://192.168.0.108:27017
|
@ -0,0 +1,72 @@
|
|||||||
|
package com.heibaiying.springboot;
|
||||||
|
|
||||||
|
import com.heibaiying.springboot.bean.Programmer;
|
||||||
|
import com.heibaiying.springboot.repository.ProgrammerRepository;
|
||||||
|
import com.mongodb.client.result.DeleteResult;
|
||||||
|
import com.mongodb.client.result.UpdateResult;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||||
|
import org.springframework.data.mongodb.core.query.Criteria;
|
||||||
|
import org.springframework.data.mongodb.core.query.Query;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.springframework.data.mongodb.core.query.Criteria.where;
|
||||||
|
import static org.springframework.data.mongodb.core.query.Query.query;
|
||||||
|
import static org.springframework.data.mongodb.core.query.Update.update;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class MongoJPATests {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ProgrammerRepository repository;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void insert() {
|
||||||
|
// 单条插入
|
||||||
|
repository.save(new Programmer("python", 23, 21832.34f, new Date()));
|
||||||
|
// 批量插入
|
||||||
|
List<Programmer> programmers = new ArrayList<Programmer>();
|
||||||
|
programmers.add(new Programmer("java", 21, 52200.21f, new Date()));
|
||||||
|
programmers.add(new Programmer("Go", 34, 500.21f, new Date()));
|
||||||
|
repository.saveAll(programmers);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 条件查询
|
||||||
|
@Test
|
||||||
|
public void select() {
|
||||||
|
Programmer java = repository.findByNameAndAge("java", 21);
|
||||||
|
Assert.assertEquals(java.getSalary(), 52200.21f, 0.01);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 更新数据
|
||||||
|
@Test
|
||||||
|
public void MUpdate() {
|
||||||
|
repository.save(new Programmer("Go", 8, 500.21f, new Date()));
|
||||||
|
Programmer go = repository.findAllByName("Go");
|
||||||
|
Assert.assertEquals(go.getAge(), 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除指定数据
|
||||||
|
@Test
|
||||||
|
public void delete() {
|
||||||
|
repository.deleteAllByName("python");
|
||||||
|
Optional<Programmer> python = repository.findById("python");
|
||||||
|
Assert.assertFalse(python.isPresent());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,68 @@
|
|||||||
|
package com.heibaiying.springboot;
|
||||||
|
|
||||||
|
import com.heibaiying.springboot.bean.Programmer;
|
||||||
|
import com.mongodb.client.result.DeleteResult;
|
||||||
|
import com.mongodb.client.result.UpdateResult;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||||
|
import org.springframework.data.mongodb.core.query.Criteria;
|
||||||
|
import org.springframework.data.mongodb.core.query.Query;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.springframework.data.mongodb.core.query.Criteria.where;
|
||||||
|
import static org.springframework.data.mongodb.core.query.Query.query;
|
||||||
|
import static org.springframework.data.mongodb.core.query.Update.update;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class MongoOriginalTests {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MongoTemplate mongoTemplate;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void insert() {
|
||||||
|
// 单条插入
|
||||||
|
mongoTemplate.insert(new Programmer("xiaoming", 12, 5000.21f, new Date()));
|
||||||
|
List<Programmer> programmers = new ArrayList<Programmer>();
|
||||||
|
// 批量插入
|
||||||
|
programmers.add(new Programmer("xiaohong", 21, 52200.21f, new Date()));
|
||||||
|
programmers.add(new Programmer("xiaolan", 34, 500.21f, new Date()));
|
||||||
|
mongoTemplate.insert(programmers, Programmer.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 条件查询
|
||||||
|
@Test
|
||||||
|
public void select() {
|
||||||
|
Criteria criteria = new Criteria();
|
||||||
|
criteria.andOperator(where("name").is("xiaohong"), where("age").is(21));
|
||||||
|
Query query = new Query(criteria);
|
||||||
|
Programmer one = mongoTemplate.findOne(query, Programmer.class);
|
||||||
|
System.out.println(one);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 更新数据
|
||||||
|
@Test
|
||||||
|
public void MUpdate() {
|
||||||
|
UpdateResult updateResult = mongoTemplate.updateMulti(query(where("name").is("xiaoming")), update("age", 35), Programmer.class);
|
||||||
|
System.out.println("更新记录数:" + updateResult.getModifiedCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除指定数据
|
||||||
|
@Test
|
||||||
|
public void delete() {
|
||||||
|
DeleteResult result = mongoTemplate.remove(query(where("name").is("xiaolan")), Programmer.class);
|
||||||
|
System.out.println("影响记录数:" + result.getDeletedCount());
|
||||||
|
System.out.println("是否成功:" + result.wasAcknowledged());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
55
spring-boot/spring-boot-redis/pom.xml
Normal file
55
spring-boot/spring-boot-redis/pom.xml
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
<?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>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.1.1.RELEASE</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
<groupId>com.heibaiying</groupId>
|
||||||
|
<artifactId>spring-boot-redis</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>spring-boot-redis</name>
|
||||||
|
<description>Redis project for Spring Boot</description>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<!--redis starter-->
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<!--jackson 序列化包 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>2.9.8</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.heibaiying.springbootredis;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SpringBootRedisApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SpringBootRedisApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.heibaiying.springbootredis.bean;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@EqualsAndHashCode
|
||||||
|
public class Programmer implements Serializable {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
private float salary;
|
||||||
|
|
||||||
|
private Date birthday;
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
package com.heibaiying.springbootredis.config;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||||
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||||
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
* @description : 自定义序列化器
|
||||||
|
* 不定义的话默认采用的是serializer.JdkSerializationRedisSerializer.serialize()序列化为二进制字节码 存储在数据库中不直观
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class RedisConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
||||||
|
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
|
||||||
|
redisTemplate.setConnectionFactory(redisConnectionFactory);
|
||||||
|
|
||||||
|
// 使用Jackson2JsonRedisSerialize 需要导入依赖 com.fasterxml.jackson.core jackson-databind
|
||||||
|
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
|
||||||
|
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
// 第一个参数表示: 表示所有访问者都受到影响 包括 字段, getter / isGetter,setter,creator
|
||||||
|
// 第二个参数表示: 所有类型的访问修饰符都是可接受的,不论是公有还有私有表变量都会被序列化
|
||||||
|
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||||
|
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
||||||
|
|
||||||
|
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
|
||||||
|
|
||||||
|
// 设置key,value 序列化规则
|
||||||
|
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
|
||||||
|
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||||
|
redisTemplate.afterPropertiesSet();
|
||||||
|
return redisTemplate;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,70 @@
|
|||||||
|
package com.heibaiying.springbootredis.redis;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.*;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
* @description : redis 基本操作
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class RedisObjectOperation {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisTemplate<Object, Object> objectRedisTemplate;
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 操作对象
|
||||||
|
*/
|
||||||
|
public void ObjectSet(Object key, Object value) {
|
||||||
|
ValueOperations<Object, Object> valueOperations = objectRedisTemplate.opsForValue();
|
||||||
|
valueOperations.set(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 操作元素为对象列表
|
||||||
|
*/
|
||||||
|
public void ListSet(Object key, List<Object> values) {
|
||||||
|
ListOperations<Object, Object> listOperations = objectRedisTemplate.opsForList();
|
||||||
|
values.forEach(value -> listOperations.leftPush(key, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 操作元素为对象集合
|
||||||
|
*/
|
||||||
|
public void SetSet(Object key, Set<Object> values) {
|
||||||
|
SetOperations<Object, Object> setOperations = objectRedisTemplate.opsForSet();
|
||||||
|
values.forEach(value -> setOperations.add(key, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 获取对象
|
||||||
|
*/
|
||||||
|
public Object ObjectGet(Object key) {
|
||||||
|
ValueOperations<Object, Object> valueOperations = objectRedisTemplate.opsForValue();
|
||||||
|
return valueOperations.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 列表弹出元素
|
||||||
|
*/
|
||||||
|
public Object ListLeftPop(Object key) {
|
||||||
|
ListOperations<Object, Object> listOperations = objectRedisTemplate.opsForList();
|
||||||
|
return listOperations.leftPop(key, 2, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 集合弹出元素
|
||||||
|
*/
|
||||||
|
public Object SetPop(Object key) {
|
||||||
|
SetOperations<Object, Object> setOperations = objectRedisTemplate.opsForSet();
|
||||||
|
return setOperations.pop(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,70 @@
|
|||||||
|
package com.heibaiying.springbootredis.redis;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.*;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
* @description : redis 基本操作
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class RedisOperation {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private StringRedisTemplate redisTemplate;
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 操作普通字符串
|
||||||
|
*/
|
||||||
|
public void StringSet(String key, String value) {
|
||||||
|
ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
|
||||||
|
valueOperations.set(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 操作列表
|
||||||
|
*/
|
||||||
|
public void ListSet(String key, List<String> values) {
|
||||||
|
ListOperations<String, String> listOperations = redisTemplate.opsForList();
|
||||||
|
values.forEach(value -> listOperations.leftPush(key, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 操作集合
|
||||||
|
*/
|
||||||
|
public void SetSet(String key, Set<String> values) {
|
||||||
|
SetOperations<String, String> setOperations = redisTemplate.opsForSet();
|
||||||
|
values.forEach(value -> setOperations.add(key, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 获取字符串
|
||||||
|
*/
|
||||||
|
public String StringGet(String key) {
|
||||||
|
ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
|
||||||
|
return valueOperations.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 列表弹出元素
|
||||||
|
*/
|
||||||
|
public String ListLeftPop(String key) {
|
||||||
|
ListOperations<String, String> listOperations = redisTemplate.opsForList();
|
||||||
|
return listOperations.leftPop(key, 2, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 集合弹出元素
|
||||||
|
*/
|
||||||
|
public String SetPop(String key) {
|
||||||
|
SetOperations<String, String> setOperations = redisTemplate.opsForSet();
|
||||||
|
return setOperations.pop(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
spring:
|
||||||
|
redis:
|
||||||
|
host: 127.0.0.1
|
||||||
|
port: 6379
|
||||||
|
# 默认采用的也是 0 号数据库 redis官方在4.0之后版本就不推荐采用单节点多数据库(db1-db15)的方式存储数据,如果有需要应该采用集群方式构建
|
||||||
|
database: 0
|
||||||
|
|
||||||
|
# 如果是集群节点 采用如下配置指定节点
|
||||||
|
#spring.redis.cluster.nodes
|
@ -0,0 +1,57 @@
|
|||||||
|
package com.heibaiying.springbootredis;
|
||||||
|
|
||||||
|
import com.heibaiying.springbootredis.bean.Programmer;
|
||||||
|
import com.heibaiying.springbootredis.redis.RedisObjectOperation;
|
||||||
|
import com.heibaiying.springbootredis.redis.RedisOperation;
|
||||||
|
import org.assertj.core.util.Sets;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class RedisObjectTests {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisObjectOperation redisOperation;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void StringOperation() {
|
||||||
|
Programmer programmer = new Programmer("小明", 12, 3334.3f, new Date());
|
||||||
|
redisOperation.ObjectSet("programmer", programmer);
|
||||||
|
Programmer objectGet = (Programmer) redisOperation.ObjectGet("programmer");
|
||||||
|
Assert.assertEquals(objectGet, programmer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ListOperation() {
|
||||||
|
Programmer programmer01 = new Programmer("小明", 12, 3334.3f, new Date());
|
||||||
|
Programmer programmer02 = new Programmer("小红", 12, 3334.3f, new Date());
|
||||||
|
redisOperation.ListSet("programmerList", Arrays.asList(programmer01, programmer02));
|
||||||
|
Programmer programmer = (Programmer) redisOperation.ListLeftPop("programmerList");
|
||||||
|
Assert.assertEquals(programmer.getName(), "小红");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 需要注意的是Redis的集合(set)不仅不允许有重复元素,并且集合中的元素是无序的,
|
||||||
|
* 不能通过索引下标获取元素。哪怕你在java中传入的集合是有序的newLinkedHashSet,但是实际在Redis存储的还是无序的集合
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void SetOperation() {
|
||||||
|
Programmer programmer01 = new Programmer("小明", 12, 3334.3f, new Date());
|
||||||
|
Programmer programmer02 = new Programmer("小爱", 12, 3334.3f, new Date());
|
||||||
|
Programmer programmer03 = new Programmer("小红", 12, 3334.3f, new Date());
|
||||||
|
redisOperation.SetSet("programmerSet", Sets.newLinkedHashSet(programmer01, programmer02, programmer03));
|
||||||
|
Programmer programmer = (Programmer) redisOperation.SetPop("programmerSet");
|
||||||
|
Assert.assertNotNull(programmer);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,47 @@
|
|||||||
|
package com.heibaiying.springbootredis;
|
||||||
|
|
||||||
|
import com.heibaiying.springbootredis.redis.RedisOperation;
|
||||||
|
import org.assertj.core.util.Sets;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class RedisTests {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisOperation redisOperation;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void StringOperation() {
|
||||||
|
redisOperation.StringSet("hello", "redis");
|
||||||
|
String s = redisOperation.StringGet("hello");
|
||||||
|
Assert.assertEquals(s, "redis");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ListOperation() {
|
||||||
|
redisOperation.ListSet("skill", Arrays.asList("java", "oracle", "vue"));
|
||||||
|
String s = redisOperation.ListLeftPop("skill");
|
||||||
|
Assert.assertEquals(s, "vue");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 需要注意的是Redis的集合(set)不仅不允许有重复元素,并且集合中的元素是无序的,
|
||||||
|
* 不能通过索引下标获取元素。哪怕你在java中传入的集合是有序的newLinkedHashSet,但是实际在Redis存储的还是无序的集合
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void SetOperation() {
|
||||||
|
redisOperation.SetSet("skillSet", Sets.newLinkedHashSet("java", "oracle", "vue"));
|
||||||
|
String s = redisOperation.SetPop("skillSet");
|
||||||
|
Assert.assertNotNull(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
63
spring-boot/spring-boot-tomcat/pom.xml
Normal file
63
spring-boot/spring-boot-tomcat/pom.xml
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
<?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>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.1.1.RELEASE</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
<groupId>com.heibaiying</groupId>
|
||||||
|
<artifactId>spring-boot-tomcat</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>spring-boot-tomcat</name>
|
||||||
|
<description>boot-tomcat project for Spring Boot</description>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
<!--排除依赖 使用外部tomcat容器启动-->
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<!--使用外置容器时候SpringBootServletInitializer 依赖此包 -->
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>servlet-api</artifactId>
|
||||||
|
<version>2.5</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.heibaiying.springboottomcat;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||||
|
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 如果用外置tomcat,启动报错java.lang.NoClassDefFoundError: javax/el/ELManager
|
||||||
|
* 是因为tomcat 7.0 el-api包中没有ELManager类 , 切换tomcat 为8.0 以上版本即可
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SpringBootTomcatApplication extends SpringBootServletInitializer {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
|
||||||
|
//传入SpringBoot应用的主程序
|
||||||
|
return application.sources(SpringBootTomcatApplication.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SpringBootTomcatApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.heibaiying.springboottomcat.bean;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class Programmer {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
private float salary;
|
||||||
|
|
||||||
|
private LocalDate birthday;
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.heibaiying.springboottomcat.controller;
|
||||||
|
;
|
||||||
|
import com.heibaiying.springboottomcat.bean.Programmer;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping("index")
|
||||||
|
public class JspController {
|
||||||
|
|
||||||
|
@RequestMapping
|
||||||
|
public String jsp(Model model){
|
||||||
|
Programmer programmer = new Programmer("heibai", 21, 1298.31f, LocalDate.now());
|
||||||
|
model.addAttribute("programmer",programmer);
|
||||||
|
return "show";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
spring:
|
||||||
|
mvc:
|
||||||
|
view:
|
||||||
|
prefix: /WEB-INF/jsp/
|
||||||
|
suffix: .jsp
|
@ -0,0 +1,13 @@
|
|||||||
|
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>programmer</title>
|
||||||
|
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/show.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<ul>
|
||||||
|
<li>姓名: ${programmer.name}</li>
|
||||||
|
<li>年龄: ${programmer.age}</li>
|
||||||
|
</ul>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -0,0 +1,4 @@
|
|||||||
|
li{
|
||||||
|
font-size: 30px;
|
||||||
|
color: blueviolet;
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.heibaiying.springboottomcat;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class SpringBootTomcatApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
56
spring-boot/spring-boot-websocket/pom.xml
Normal file
56
spring-boot/spring-boot-websocket/pom.xml
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
<?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>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.1.1.RELEASE</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
<groupId>com.heibaiying</groupId>
|
||||||
|
<artifactId>spring-boot-websocket</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>spring-boot-websocket</name>
|
||||||
|
<description>Websocket project for Spring Boot</description>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-freemarker</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.heibaiying.springboot;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SpringBootWebsocketApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SpringBootWebsocketApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.heibaiying.springboot.constant;
|
||||||
|
|
||||||
|
import javax.websocket.Session;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : 罗祥
|
||||||
|
* @description :
|
||||||
|
* @date :create in 2018/12/27
|
||||||
|
*/
|
||||||
|
public interface Constant {
|
||||||
|
|
||||||
|
String USER_NAME="username";
|
||||||
|
|
||||||
|
Map<String, Session> nameAndSession = new ConcurrentHashMap<>();
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.heibaiying.springboot.controller;
|
||||||
|
|
||||||
|
import com.heibaiying.springboot.constant.Constant;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpSession;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : 罗祥
|
||||||
|
* @description : 简单登录
|
||||||
|
* @date :create in 2018/12/27
|
||||||
|
*/
|
||||||
|
@Controller
|
||||||
|
public class LoginController {
|
||||||
|
|
||||||
|
@PostMapping("login")
|
||||||
|
public String login(String username, HttpSession session) {
|
||||||
|
session.setAttribute(Constant.USER_NAME, username);
|
||||||
|
return "chat";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public String index() {
|
||||||
|
return "index";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
package com.heibaiying.springboot.websocket;
|
||||||
|
|
||||||
|
import com.heibaiying.springboot.constant.Constant;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.websocket.OnClose;
|
||||||
|
import javax.websocket.OnMessage;
|
||||||
|
import javax.websocket.OnOpen;
|
||||||
|
import javax.websocket.Session;
|
||||||
|
import javax.websocket.server.PathParam;
|
||||||
|
import javax.websocket.server.ServerEndpoint;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URLDecoder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
*/
|
||||||
|
|
||||||
|
@ServerEndpoint(value = "/socket/{username}")
|
||||||
|
@Component
|
||||||
|
public class ChatSocket {
|
||||||
|
|
||||||
|
|
||||||
|
@OnOpen //建立连接时候触发
|
||||||
|
public void onOpen(Session session, @PathParam("username") String username) {
|
||||||
|
// 这个方法是线程不安全的
|
||||||
|
Constant.nameAndSession.putIfAbsent(username, session);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@OnClose //关闭连接时候触发
|
||||||
|
public void onClose(Session session, @PathParam("username") String username) {
|
||||||
|
Constant.nameAndSession.remove(username);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnMessage //处理消息
|
||||||
|
public void onMessage(Session session, String message, @PathParam("username") String username) throws UnsupportedEncodingException {
|
||||||
|
// 防止中文乱码
|
||||||
|
String msg = URLDecoder.decode(message, "utf-8");
|
||||||
|
// 简单模拟群发消息
|
||||||
|
Constant.nameAndSession.forEach((s, webSocketSession)
|
||||||
|
-> {
|
||||||
|
try {
|
||||||
|
webSocketSession.getBasicRemote().sendText(username + " : " + msg);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.heibaiying.springboot.websocket;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class WebSocketConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ServerEndpointExporter serverEndpointExporter() {
|
||||||
|
return new ServerEndpointExporter();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<title>${Session["username"]}您好!欢迎进入群聊大厅!</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h5>${Session["username"]}您好!欢迎进入群聊大厅!</h5>
|
||||||
|
<input id="message" type="text">
|
||||||
|
<button id="btn">发送消息</button>
|
||||||
|
<div id="show">
|
||||||
|
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
let btn = document.getElementById("btn");
|
||||||
|
let message = document.getElementById("message");
|
||||||
|
let show = document.getElementById("show");
|
||||||
|
let ws = new WebSocket("ws://localhost:8080/socket/${Session["username"]}");
|
||||||
|
ws.onmessage = function (evt) {
|
||||||
|
let node = document.createElement("div");
|
||||||
|
node.innerHTML = "<h5>" + evt.data + "</h5>";
|
||||||
|
show.appendChild(node);
|
||||||
|
};
|
||||||
|
btn.addEventListener("click", function () {
|
||||||
|
let data = message.value;
|
||||||
|
console.log(data);
|
||||||
|
if (data) {
|
||||||
|
ws.send(encodeURI(data));
|
||||||
|
} else {
|
||||||
|
alert("请输入消息后发送");
|
||||||
|
}
|
||||||
|
message.value = "";
|
||||||
|
});
|
||||||
|
// 关闭页面时候关闭ws
|
||||||
|
window.addEventListener("beforeunload", function (event) {
|
||||||
|
ws.close();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -0,0 +1,12 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<title>Title</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<form action="/login" method="post">
|
||||||
|
<input name="username" type="text">
|
||||||
|
<button id="btn">输入临时用户名后登录!</button>
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.heibaiying.springboot;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class SpringBootWebsocketApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -23,6 +23,7 @@ public class CustomerHandler extends TextWebSocketHandler {
|
|||||||
@Override
|
@Override
|
||||||
public void afterConnectionEstablished(WebSocketSession session) {
|
public void afterConnectionEstablished(WebSocketSession session) {
|
||||||
String username = getNameFromSession(session);
|
String username = getNameFromSession(session);
|
||||||
|
//这个方法是线程不安全的
|
||||||
nameAndSession.putIfAbsent(username, session);
|
nameAndSession.putIfAbsent(username, session);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user