findAll() {
28 | return userRepository.findAll();
29 | }
30 |
31 | @Override
32 | public User insertByUser(User user) {
33 | LOGGER.info("新增用户:" + user.toString());
34 | return userRepository.save(user);
35 | }
36 |
37 | @Override
38 | public User update(User user) {
39 | LOGGER.info("更新用户:" + user.toString());
40 | return userRepository.save(user);
41 | }
42 |
43 | @Override
44 | public User delete(Long id) {
45 | User user = userRepository.findById(id).get();
46 | userRepository.delete(user);
47 |
48 | LOGGER.info("删除用户:" + user.toString());
49 | return user;
50 | }
51 |
52 | @Override
53 | public User findById(Long id) {
54 | LOGGER.info("获取用户 ID :" + id);
55 | return userRepository.findById(id).get();
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/chapter-2-spring-boot-quick-start/src/main/java/spring/boot/core/web/UserController.java:
--------------------------------------------------------------------------------
1 | package spring.boot.core.web;
2 |
3 | import org.springframework.beans.factory.annotation.Autowired;
4 | import org.springframework.stereotype.Controller;
5 | import org.springframework.ui.ModelMap;
6 | import org.springframework.web.bind.annotation.ModelAttribute;
7 | import org.springframework.web.bind.annotation.PathVariable;
8 | import org.springframework.web.bind.annotation.RequestMapping;
9 | import org.springframework.web.bind.annotation.RequestMethod;
10 | import spring.boot.core.domain.User;
11 | import spring.boot.core.service.UserService;
12 |
13 | /**
14 | * 用户控制层
15 | *
16 | * Created by bysocket on 24/07/2017.
17 | */
18 | @Controller
19 | @RequestMapping(value = "/users") // 通过这里配置使下面的映射都在 /users
20 | public class UserController {
21 |
22 | @Autowired
23 | UserService userService; // 用户服务层
24 |
25 | /**
26 | * 获取用户列表
27 | * 处理 "/users" 的 GET 请求,用来获取用户列表
28 | * 通过 @RequestParam 传递参数,进一步实现条件查询或者分页查询
29 | */
30 | @RequestMapping(method = RequestMethod.GET)
31 | public String getUserList(ModelMap map) {
32 | map.addAttribute("userList", userService.findAll());
33 | return "userList";
34 | }
35 |
36 | /**
37 | * 显示创建用户表单
38 | *
39 | */
40 | @RequestMapping(value = "/create", method = RequestMethod.GET)
41 | public String createUserForm(ModelMap map) {
42 | map.addAttribute("user", new User());
43 | map.addAttribute("action", "create");
44 | return "userForm";
45 | }
46 |
47 | /**
48 | * 创建用户
49 | * 处理 "/users" 的 POST 请求,用来获取用户列表
50 | * 通过 @ModelAttribute 绑定参数,也通过 @RequestParam 从页面中传递参数
51 | */
52 | @RequestMapping(value = "/create", method = RequestMethod.POST)
53 | public String postUser(@ModelAttribute User user) {
54 | userService.insertByUser(user);
55 | return "redirect:/users/";
56 | }
57 |
58 | /**
59 | * 显示需要更新用户表单
60 | * 处理 "/users/{id}" 的 GET 请求,通过 URL 中的 id 值获取 User 信息
61 | * URL 中的 id ,通过 @PathVariable 绑定参数
62 | */
63 | @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
64 | public String getUser(@PathVariable Long id, ModelMap map) {
65 | map.addAttribute("user", userService.findById(id));
66 | map.addAttribute("action", "update");
67 | return "userForm";
68 | }
69 |
70 | /**
71 | * 处理 "/users/{id}" 的 PUT 请求,用来更新 User 信息
72 | *
73 | */
74 | @RequestMapping(value = "/update", method = RequestMethod.POST)
75 | public String putUser(@ModelAttribute User user) {
76 | userService.update(user);
77 | return "redirect:/users/";
78 | }
79 |
80 | /**
81 | * 处理 "/users/{id}" 的 GET 请求,用来删除 User 信息
82 | */
83 | @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
84 | public String deleteUser(@PathVariable Long id) {
85 |
86 | userService.delete(id);
87 | return "redirect:/users/";
88 | }
89 |
90 | }
--------------------------------------------------------------------------------
/chapter-2-spring-boot-quick-start/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | ## 是否显示 SQL 语句
2 | spring.jpa.show-sql=true
--------------------------------------------------------------------------------
/chapter-2-spring-boot-quick-start/src/main/resources/static/css/default.css:
--------------------------------------------------------------------------------
1 | /* contentDiv */
2 | .contentDiv {padding:20px 60px;}
--------------------------------------------------------------------------------
/chapter-2-spring-boot-quick-start/src/main/resources/static/images/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpringForAll/springboot-learning-example/6446896ef2f858b7816f9b0c07c7019b42d0cb5c/chapter-2-spring-boot-quick-start/src/main/resources/static/images/favicon.ico
--------------------------------------------------------------------------------
/chapter-2-spring-boot-quick-start/src/main/resources/templates/userForm.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | 用户管理
10 |
11 |
12 |
13 |
14 |
15 |
《 Spring Boot 2.x 核心技术实战》第二章快速入门案例
16 |
17 |
20 |
21 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/chapter-2-spring-boot-quick-start/src/main/resources/templates/userList.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | 用户列表
10 |
11 |
12 |
13 |
14 |
15 |
16 |
《 Spring Boot 2.x 核心技术实战》第二章快速入门案例
17 |
18 |
19 |
22 |
23 |
24 | 用户编号 |
25 | 名称 |
26 | 年龄 |
27 | 出生时间 |
28 | 管理 |
29 |
30 |
31 |
32 |
33 | |
34 | |
35 | |
36 | |
37 | 删除 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/chapter-2-spring-boot-quick-start/src/test/java/spring/boot/core/QuickStartApplicationTests.java:
--------------------------------------------------------------------------------
1 | package spring.boot.core;
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 QuickStartApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/chapter-2-spring-boot-quick-start/src/test/java/spring/boot/core/domain/UserRepositoryTests.java:
--------------------------------------------------------------------------------
1 | package spring.boot.core.domain;
2 |
3 | import org.junit.Assert;
4 | import org.junit.Test;
5 | import org.junit.runner.RunWith;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.boot.test.context.SpringBootTest;
8 | import org.springframework.test.context.junit4.SpringRunner;
9 |
10 | @RunWith(SpringRunner.class)
11 | @SpringBootTest
12 | public class UserRepositoryTests {
13 |
14 | @Autowired
15 | UserRepository userRepository;
16 |
17 | /**
18 | * 单元测试 - 新增用户
19 | */
20 | @Test
21 | public void testSave() {
22 | User user = new User();
23 | user.setName("mumu");
24 | user.setAge(2);
25 | // user.setBirthday(new Date());
26 | user = userRepository.save(user);
27 |
28 | // 验证新增用户
29 | Assert.assertNotNull(user.getId());
30 | }
31 |
32 | /**
33 | * 单元测试 - 删除用户
34 | */
35 | @Test
36 | public void testDelete() {
37 |
38 | // 新增两个用户数据
39 | User mumu = new User();
40 | mumu.setName("mumu");
41 | mumu.setAge(2);
42 | // mumu.setBirthday(new Date());
43 | userRepository.save(mumu);
44 |
45 | User zizi = new User();
46 | zizi.setName("zizi");
47 | zizi.setAge(25);
48 | // zizi.setBirthday(new Date());
49 | userRepository.save(zizi);
50 |
51 | // 验证是否获取的用户列表大小是 2
52 | Assert.assertEquals(2, userRepository.findAll().size());
53 |
54 | // 删除用户
55 | userRepository.delete(mumu);
56 |
57 | // 验证是否获取的用户列表大小是 1
58 | Assert.assertEquals(1, userRepository.findAll().size());
59 | }
60 |
61 | /**
62 | * 单元测试 - 更新用户
63 | */
64 | @Test
65 | public void testUpdate() {
66 | User user = new User();
67 | user.setName("mumu");
68 | user.setAge(2);
69 | // user.setBirthday(new Date());
70 | user = userRepository.save(user);
71 |
72 | user.setName("zizi");
73 | user = userRepository.save(user);
74 |
75 | // 验证新增用户的编号是否为 1
76 | Assert.assertNotNull(user.getId());
77 | Assert.assertEquals("zizi", user.getName());
78 | }
79 |
80 | /**
81 | * 单元测试 - 获取用户列表
82 | */
83 | @Test
84 | public void testFindAll() {
85 | // 新增两个用户数据
86 | User mumu = new User();
87 | mumu.setName("mumu");
88 | mumu.setAge(2);
89 | // mumu.setBirthday(new Date());
90 | userRepository.save(mumu);
91 |
92 | User zizi = new User();
93 | zizi.setName("zizi");
94 | zizi.setAge(25);
95 | // zizi.setBirthday(new Date());
96 | userRepository.save(zizi);
97 |
98 | // 验证是否获取的用户列表大小是 2
99 | Assert.assertEquals(2, userRepository.findAll().size());
100 | }
101 |
102 | /**
103 | * 单元测试 - 获取单个用户
104 | */
105 | @Test
106 | public void testFindById() {
107 | // 新增用户
108 | User mumu = new User();
109 | mumu.setName("mumu");
110 | mumu.setAge(2);
111 | // mumu.setBirthday(new Date());
112 | userRepository.save(mumu);
113 |
114 | // 验证是否获取的用户是否是插入的用户
115 | User expected = userRepository.findById(1L).get();
116 | Assert.assertEquals("mumu", expected.getName());
117 | }
118 |
119 |
120 | }
121 |
--------------------------------------------------------------------------------
/chapter-4-spring-boot-validating-form-input/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | spring.boot.core
7 | chapter-4-spring-boot-validating-form-input
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 | chapter-4-spring-boot-validating-form-input
12 | 第四章表单校验案例
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 2.0.0.M4
18 |
19 |
20 |
21 |
22 | UTF-8
23 | UTF-8
24 | 1.8
25 |
26 |
27 |
28 |
29 |
30 | org.springframework.boot
31 | spring-boot-starter-validation
32 |
33 |
34 |
35 |
36 | org.springframework.boot
37 | spring-boot-starter-web
38 |
39 |
40 |
41 |
42 | org.springframework.boot
43 | spring-boot-starter-test
44 | test
45 |
46 |
47 |
48 |
49 | org.springframework.boot
50 | spring-boot-starter-data-jpa
51 |
52 |
53 |
54 |
55 | com.h2database
56 | h2
57 | runtime
58 |
59 |
60 |
61 |
62 | org.springframework.boot
63 | spring-boot-starter-thymeleaf
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 | org.springframework.boot
73 | spring-boot-maven-plugin
74 | 1.5.1.RELEASE
75 |
76 |
77 |
78 |
79 |
80 |
81 | spring-milestones
82 | Spring Milestones
83 | https://repo.spring.io/libs-milestone
84 |
85 | false
86 |
87 |
88 |
89 |
90 |
91 |
--------------------------------------------------------------------------------
/chapter-4-spring-boot-validating-form-input/src/main/java/spring/boot/core/ValidatingFormInputApplication.java:
--------------------------------------------------------------------------------
1 | package spring.boot.core;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class ValidatingFormInputApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(ValidatingFormInputApplication.class, args);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/chapter-4-spring-boot-validating-form-input/src/main/java/spring/boot/core/domain/User.java:
--------------------------------------------------------------------------------
1 | package spring.boot.core.domain;
2 |
3 | import org.hibernate.validator.constraints.NotEmpty;
4 |
5 | import javax.persistence.Entity;
6 | import javax.persistence.GeneratedValue;
7 | import javax.persistence.Id;
8 | import javax.validation.constraints.Max;
9 | import javax.validation.constraints.Min;
10 | import javax.validation.constraints.NotNull;
11 | import javax.validation.constraints.Size;
12 | import java.io.Serializable;
13 |
14 | /**
15 | * 用户实体类
16 | *
17 | * Created by bysocket on 21/07/2017.
18 | */
19 | @Entity
20 | public class User implements Serializable {
21 |
22 | /**
23 | * 编号
24 | */
25 | @Id
26 | @GeneratedValue
27 | private Long id;
28 |
29 | /**
30 | * 名称
31 | */
32 | @NotEmpty(message = "姓名不能为空")
33 | @Size(min = 2, max = 8, message = "姓名长度必须大于 2 且小于 20 字")
34 | private String name;
35 |
36 | /**
37 | * 年龄
38 | */
39 | @NotNull(message = "年龄不能为空")
40 | @Min(value = 0, message = "年龄大于 0")
41 | @Max(value = 300, message = "年龄不大于 300")
42 | private Integer age;
43 |
44 | /**
45 | * 出生时间
46 | */
47 | @NotEmpty(message = "出生时间不能为空")
48 | private String birthday;
49 |
50 | public Long getId() {
51 | return id;
52 | }
53 |
54 | public void setId(Long id) {
55 | this.id = id;
56 | }
57 |
58 | public String getName() {
59 | return name;
60 | }
61 |
62 | public void setName(String name) {
63 | this.name = name;
64 | }
65 |
66 | public Integer getAge() {
67 | return age;
68 | }
69 |
70 | public void setAge(Integer age) {
71 | this.age = age;
72 | }
73 |
74 | public String getBirthday() {
75 | return birthday;
76 | }
77 |
78 | public void setBirthday(String birthday) {
79 | this.birthday = birthday;
80 | }
81 |
82 | @Override
83 | public String toString() {
84 | return "User{" +
85 | "id=" + id +
86 | ", name='" + name + '\'' +
87 | ", age=" + age +
88 | ", birthday=" + birthday +
89 | '}';
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/chapter-4-spring-boot-validating-form-input/src/main/java/spring/boot/core/domain/UserRepository.java:
--------------------------------------------------------------------------------
1 | package spring.boot.core.domain;
2 |
3 | import org.springframework.data.jpa.repository.JpaRepository;
4 |
5 | /**
6 | * 用户持久层操作接口
7 | *
8 | * Created by bysocket on 21/07/2017.
9 | */
10 | public interface UserRepository extends JpaRepository {
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/chapter-4-spring-boot-validating-form-input/src/main/java/spring/boot/core/service/UserService.java:
--------------------------------------------------------------------------------
1 | package spring.boot.core.service;
2 |
3 |
4 | import spring.boot.core.domain.User;
5 |
6 | import java.util.List;
7 |
8 | /**
9 | * User 业务层接口
10 | *
11 | * Created by bysocket on 24/07/2017.
12 | */
13 | public interface UserService {
14 |
15 | List findAll();
16 |
17 | User insertByUser(User user);
18 |
19 | User update(User user);
20 |
21 | User delete(Long id);
22 |
23 | User findById(Long id);
24 | }
25 |
--------------------------------------------------------------------------------
/chapter-4-spring-boot-validating-form-input/src/main/java/spring/boot/core/service/impl/UserServiceImpl.java:
--------------------------------------------------------------------------------
1 | package spring.boot.core.service.impl;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Service;
7 | import spring.boot.core.domain.User;
8 | import spring.boot.core.domain.UserRepository;
9 | import spring.boot.core.service.UserService;
10 |
11 | import java.util.List;
12 |
13 | /**
14 | * User 业务层实现
15 | *
16 | * Created by bysocket on 24/07/2017.
17 | */
18 | @Service
19 | public class UserServiceImpl implements UserService {
20 |
21 | private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);
22 |
23 | @Autowired
24 | UserRepository userRepository;
25 |
26 | @Override
27 | public List findAll() {
28 | return userRepository.findAll();
29 | }
30 |
31 | @Override
32 | public User insertByUser(User user) {
33 | LOGGER.info("新增用户:" + user.toString());
34 | return userRepository.save(user);
35 | }
36 |
37 | @Override
38 | public User update(User user) {
39 | LOGGER.info("更新用户:" + user.toString());
40 | return userRepository.save(user);
41 | }
42 |
43 | @Override
44 | public User delete(Long id) {
45 | User user = userRepository.findById(id).get();
46 | userRepository.delete(user);
47 |
48 | LOGGER.info("删除用户:" + user.toString());
49 | return user;
50 | }
51 |
52 | @Override
53 | public User findById(Long id) {
54 | LOGGER.info("获取用户 ID :" + id);
55 | return userRepository.findById(id).get();
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/chapter-4-spring-boot-validating-form-input/src/main/java/spring/boot/core/web/UserController.java:
--------------------------------------------------------------------------------
1 | package spring.boot.core.web;
2 |
3 | import org.springframework.beans.factory.annotation.Autowired;
4 | import org.springframework.stereotype.Controller;
5 | import org.springframework.ui.ModelMap;
6 | import org.springframework.validation.BindingResult;
7 | import org.springframework.web.bind.annotation.ModelAttribute;
8 | import org.springframework.web.bind.annotation.PathVariable;
9 | import org.springframework.web.bind.annotation.RequestMapping;
10 | import org.springframework.web.bind.annotation.RequestMethod;
11 | import spring.boot.core.domain.User;
12 | import spring.boot.core.service.UserService;
13 |
14 | import javax.validation.Valid;
15 |
16 | /**
17 | * 用户控制层
18 | *
19 | * Created by bysocket on 24/07/2017.
20 | */
21 | @Controller
22 | @RequestMapping(value = "/users") // 通过这里配置使下面的映射都在 /users
23 | public class UserController {
24 |
25 | @Autowired
26 | UserService userService; // 用户服务层
27 |
28 | /**
29 | * 获取用户列表
30 | * 处理 "/users" 的 GET 请求,用来获取用户列表
31 | * 通过 @RequestParam 传递参数,进一步实现条件查询或者分页查询
32 | */
33 | @RequestMapping(method = RequestMethod.GET)
34 | public String getUserList(ModelMap map) {
35 | map.addAttribute("userList", userService.findAll());
36 | return "userList";
37 | }
38 |
39 | /**
40 | * 显示创建用户表单
41 | *
42 | * @param map
43 | * @return
44 | */
45 | @RequestMapping(value = "/create", method = RequestMethod.GET)
46 | public String createUserForm(ModelMap map) {
47 | map.addAttribute("user", new User());
48 | map.addAttribute("action", "create");
49 | return "userForm";
50 | }
51 |
52 | /**
53 | * 创建用户
54 | * 处理 "/users" 的 POST 请求,用来获取用户列表
55 | * 通过 @ModelAttribute 绑定参数,也通过 @RequestParam 从页面中传递参数
56 | */
57 | @RequestMapping(value = "/create", method = RequestMethod.POST)
58 | public String postUser(ModelMap map,
59 | @ModelAttribute @Valid User user,
60 | BindingResult bindingResult) {
61 |
62 | if (bindingResult.hasErrors()) {
63 | map.addAttribute("action", "create");
64 | return "userForm";
65 | }
66 |
67 | userService.insertByUser(user);
68 |
69 | return "redirect:/users/";
70 | }
71 |
72 |
73 | /**
74 | * 显示需要更新用户表单
75 | * 处理 "/users/{id}" 的 GET 请求,通过 URL 中的 id 值获取 User 信息
76 | * URL 中的 id ,通过 @PathVariable 绑定参数
77 | */
78 | @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
79 | public String getUser(@PathVariable Long id, ModelMap map) {
80 | map.addAttribute("user", userService.findById(id));
81 | map.addAttribute("action", "update");
82 | return "userForm";
83 | }
84 |
85 | /**
86 | * 处理 "/users/{id}" 的 PUT 请求,用来更新 User 信息
87 | *
88 | */
89 | @RequestMapping(value = "/update", method = RequestMethod.POST)
90 | public String putUser(ModelMap map,
91 | @ModelAttribute @Valid User user,
92 | BindingResult bindingResult) {
93 |
94 | if (bindingResult.hasErrors()) {
95 | map.addAttribute("action", "update");
96 | return "userForm";
97 | }
98 |
99 | userService.update(user);
100 | return "redirect:/users/";
101 | }
102 |
103 | /**
104 | * 处理 "/users/{id}" 的 GET 请求,用来删除 User 信息
105 | */
106 | @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
107 | public String deleteUser(@PathVariable Long id) {
108 |
109 | userService.delete(id);
110 | return "redirect:/users/";
111 | }
112 | }
--------------------------------------------------------------------------------
/chapter-4-spring-boot-validating-form-input/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | ## 是否显示 SQL 语句
2 | spring.jpa.show-sql=true
--------------------------------------------------------------------------------
/chapter-4-spring-boot-validating-form-input/src/main/resources/static/css/default.css:
--------------------------------------------------------------------------------
1 | /* contentDiv */
2 | .contentDiv {padding:20px 60px;}
--------------------------------------------------------------------------------
/chapter-4-spring-boot-validating-form-input/src/main/resources/static/images/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpringForAll/springboot-learning-example/6446896ef2f858b7816f9b0c07c7019b42d0cb5c/chapter-4-spring-boot-validating-form-input/src/main/resources/static/images/favicon.ico
--------------------------------------------------------------------------------
/chapter-4-spring-boot-validating-form-input/src/main/resources/templates/userForm.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | 用户管理
10 |
11 |
12 |
13 |
14 |
15 |
《 Spring Boot 2.x 核心技术实战》第二章快速入门案例
16 |
17 |
20 |
21 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/chapter-4-spring-boot-validating-form-input/src/main/resources/templates/userList.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | 用户列表
10 |
11 |
12 |
13 |
14 |
15 |
16 |
《 Spring Boot 2.x 核心技术实战》第二章快速入门案例
17 |
18 |
19 |
22 |
23 |
24 | 用户编号 |
25 | 名称 |
26 | 年龄 |
27 | 出生时间 |
28 | 管理 |
29 |
30 |
31 |
32 |
33 | |
34 | |
35 | |
36 | |
37 | 删除 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/chapter-4-spring-boot-validating-form-input/src/test/java/spring/boot/core/ValidatingFormInputApplicationTests.java:
--------------------------------------------------------------------------------
1 | package spring.boot.core;
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 ValidatingFormInputApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/chapter-5-spring-boot-paging-sorting/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | spring.boot.core
7 | chapter-5-spring-boot-paging-sorting
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 | chapter-5-spring-boot-paging-sorting
12 | 第五章数据分页排序案例
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 2.0.0.M4
18 |
19 |
20 |
21 |
22 | UTF-8
23 | UTF-8
24 | 1.8
25 |
26 |
27 |
28 |
29 |
30 |
31 | org.springframework.boot
32 | spring-boot-starter-web
33 |
34 |
35 |
36 |
37 | org.springframework.boot
38 | spring-boot-starter-test
39 | test
40 |
41 |
42 |
43 |
44 | org.springframework.boot
45 | spring-boot-starter-data-jpa
46 |
47 |
48 |
49 |
50 | com.h2database
51 | h2
52 | runtime
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 | org.springframework.boot
62 | spring-boot-maven-plugin
63 | 1.5.1.RELEASE
64 |
65 |
66 |
67 |
68 |
69 |
70 | spring-milestones
71 | Spring Milestones
72 | https://repo.spring.io/libs-milestone
73 |
74 | false
75 |
76 |
77 |
78 |
79 |
80 |
--------------------------------------------------------------------------------
/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/PagingSortingApplication.java:
--------------------------------------------------------------------------------
1 | package spring.boot.core;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * 应用启动程序
8 | *
9 | * Created by bysocket on 18/09/2017.
10 | */
11 | @SpringBootApplication
12 | public class PagingSortingApplication {
13 |
14 | public static void main(String[] args) {
15 | SpringApplication.run(PagingSortingApplication.class, args);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/domain/User.java:
--------------------------------------------------------------------------------
1 | package spring.boot.core.domain;
2 |
3 | import javax.persistence.Entity;
4 | import javax.persistence.GeneratedValue;
5 | import javax.persistence.Id;
6 | import java.io.Serializable;
7 |
8 | /**
9 | * 用户实体类
10 | *
11 | * Created by bysocket on 18/09/2017.
12 | */
13 | @Entity
14 | public class User implements Serializable {
15 |
16 | /**
17 | * 编号
18 | */
19 | @Id
20 | @GeneratedValue
21 | private Long id;
22 |
23 | /**
24 | * 名称
25 | */
26 | private String name;
27 |
28 | /**
29 | * 年龄
30 | */
31 | private Integer age;
32 |
33 | /**
34 | * 出生时间
35 | */
36 | private String birthday;
37 |
38 | public Long getId() {
39 | return id;
40 | }
41 |
42 | public void setId(Long id) {
43 | this.id = id;
44 | }
45 |
46 | public String getName() {
47 | return name;
48 | }
49 |
50 | public void setName(String name) {
51 | this.name = name;
52 | }
53 |
54 | public Integer getAge() {
55 | return age;
56 | }
57 |
58 | public void setAge(Integer age) {
59 | this.age = age;
60 | }
61 |
62 | public String getBirthday() {
63 | return birthday;
64 | }
65 |
66 | public void setBirthday(String birthday) {
67 | this.birthday = birthday;
68 | }
69 |
70 | @Override
71 | public String toString() {
72 | return "User{" +
73 | "id=" + id +
74 | ", name='" + name + '\'' +
75 | ", age=" + age +
76 | ", birthday=" + birthday +
77 | '}';
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/domain/UserRepository.java:
--------------------------------------------------------------------------------
1 | package spring.boot.core.domain;
2 |
3 | import org.springframework.data.repository.PagingAndSortingRepository;
4 |
5 | /**
6 | * 用户持久层操作接口
7 | *
8 | * Created by bysocket on 18/09/2017.
9 | */
10 | public interface UserRepository extends PagingAndSortingRepository {
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/service/UserService.java:
--------------------------------------------------------------------------------
1 | package spring.boot.core.service;
2 |
3 |
4 | import org.springframework.data.domain.Page;
5 | import org.springframework.data.domain.Pageable;
6 | import spring.boot.core.domain.User;
7 |
8 | /**
9 | * User 业务层接口
10 | *
11 | * Created by bysocket on 18/09/2017.
12 | */
13 | public interface UserService {
14 |
15 | /**
16 | * 获取用户分页列表
17 | *
18 | * @param pageable
19 | * @return
20 | */
21 | Page findByPage(Pageable pageable);
22 |
23 | /**
24 | * 新增用户
25 | *
26 | * @param user
27 | * @return
28 | */
29 | User insertByUser(User user);
30 | }
31 |
--------------------------------------------------------------------------------
/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/service/impl/UserServiceImpl.java:
--------------------------------------------------------------------------------
1 | package spring.boot.core.service.impl;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.data.domain.Page;
7 | import org.springframework.data.domain.Pageable;
8 | import org.springframework.stereotype.Service;
9 | import spring.boot.core.domain.User;
10 | import spring.boot.core.domain.UserRepository;
11 | import spring.boot.core.service.UserService;
12 |
13 | /**
14 | * User 业务层实现
15 | *
16 | * Created by bysocket on 18/09/2017.
17 | */
18 | @Service
19 | public class UserServiceImpl implements UserService {
20 |
21 | private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);
22 |
23 | @Autowired
24 | UserRepository userRepository;
25 |
26 | @Override
27 | public Page findByPage(Pageable pageable) {
28 | LOGGER.info(" \n 分页查询用户:"
29 | + " PageNumber = " + pageable.getPageNumber()
30 | + " PageSize = " + pageable.getPageSize());
31 | return userRepository.findAll(pageable);
32 | }
33 |
34 | @Override
35 | public User insertByUser(User user) {
36 | LOGGER.info("新增用户:" + user.toString());
37 | return userRepository.save(user);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/web/UserController.java:
--------------------------------------------------------------------------------
1 | package spring.boot.core.web;
2 |
3 | import org.springframework.beans.factory.annotation.Autowired;
4 | import org.springframework.data.domain.Page;
5 | import org.springframework.data.domain.Pageable;
6 | import org.springframework.web.bind.annotation.*;
7 | import spring.boot.core.domain.User;
8 | import spring.boot.core.service.UserService;
9 |
10 | /**
11 | * 用户控制层
12 | *
13 | * Created by bysocket on 18/09/2017.
14 | */
15 | @RestController
16 | @RequestMapping(value = "/users") // 通过这里配置使下面的映射都在 /users
17 | public class UserController {
18 |
19 | @Autowired
20 | UserService userService; // 用户服务层
21 |
22 | /**
23 | * 获取用户分页列表
24 | * 处理 "/users" 的 GET 请求,用来获取用户分页列表
25 | * 通过 @RequestParam 传递参数,进一步实现条件查询或者分页查询
26 | *
27 | * Pageable 支持的分页参数如下
28 | * page - 当前页 从 0 开始
29 | * size - 每页大小 默认值在 application.properties 配置
30 | */
31 | @RequestMapping(method = RequestMethod.GET)
32 | public Page getUserPage(Pageable pageable) {
33 | return userService.findByPage(pageable);
34 | }
35 |
36 | /**
37 | * 创建用户
38 | * 处理 "/users" 的 POST 请求,用来获取用户列表
39 | * 通过 @RequestBody 绑定实体类参数
40 | */
41 | @RequestMapping(value = "/create", method = RequestMethod.POST)
42 | public User postUser(@RequestBody User user) {
43 | return userService.insertByUser(user);
44 | }
45 |
46 | }
--------------------------------------------------------------------------------
/chapter-5-spring-boot-paging-sorting/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | ## 是否显示 SQL 语句
2 | spring.jpa.show-sql=true
3 |
4 | ## DATA WEB 相关配置 {@link SpringDataWebProperties}
5 | ## 分页大小 默认为 20
6 | spring.data.web.pageable.default-page-size=3
7 | ## 当前页参数名 默认为 page
8 | spring.data.web.pageable.page-parameter=pageNumber
9 | ## 当前页参数名 默认为 size
10 | spring.data.web.pageable.size-parameter=pageSize
11 | ## 字段排序参数名 默认为 sort
12 | spring.data.web.sort.sort-parameter=orderBy
--------------------------------------------------------------------------------
/chapter-5-spring-boot-paging-sorting/src/test/java/spring/boot/core/PagingSortingApplicationTests.java:
--------------------------------------------------------------------------------
1 | package spring.boot.core;
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 PagingSortingApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/doc/qrcode.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpringForAll/springboot-learning-example/6446896ef2f858b7816f9b0c07c7019b42d0cb5c/doc/qrcode.jpg
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | springboot :: Examples
5 |
6 | springboot
7 | springboot
8 | 1.0-SNAPSHOT
9 | pom
10 |
11 |
12 |
13 |
14 | springboot-helloworld
15 |
16 | springboot-properties
17 |
18 | springboot-configuration
19 |
20 |
21 |
22 | springboot-restful
23 |
24 | springboot-freemarker
25 |
26 | springboot-validation-over-json
27 |
28 |
29 |
30 | springboot-mybatis
31 |
32 | springboot-mybatis-annotation
33 |
34 | springboot-mybatis-mutil-datasource
35 |
36 |
37 |
38 | springboot-mybatis-redis
39 |
40 | springboot-mybatis-redis-annotation
41 |
42 |
43 |
45 |
46 |
47 |
48 | springboot-dubbo-server
49 | springboot-dubbo-client
50 |
51 | springboot-elasticsearch
52 |
53 |
54 | spring-data-elasticsearch-crud
55 | spring-data-elasticsearch-query
56 |
57 |
58 | chapter-2-spring-boot-quick-start
59 |
60 | chapter-4-spring-boot-validating-form-input
61 |
62 | chapter-5-spring-boot-paging-sorting
63 |
64 |
65 |
66 |
67 |
--------------------------------------------------------------------------------
/spring-data-elasticsearch-crud/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot
7 | spring-data-elasticsearch-crud
8 | 0.0.1-SNAPSHOT
9 | spring-data-elasticsearch-crud :: spring-data-elasticsearch - 基本案例
10 |
11 |
12 |
13 | org.springframework.boot
14 | spring-boot-starter-parent
15 | 1.5.1.RELEASE
16 |
17 |
18 |
19 |
20 |
21 |
22 | org.springframework.boot
23 | spring-boot-starter-data-elasticsearch
24 |
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-web
30 |
31 |
32 |
33 |
34 | junit
35 | junit
36 | 4.12
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/spring-data-elasticsearch-crud/src/main/java/org/spring/springboot/Application.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * Spring Boot 应用启动类
8 | *
9 | * Created by bysocket on 16/4/26.
10 | */
11 | // Spring Boot 应用的标识
12 | @SpringBootApplication
13 | public class Application {
14 |
15 | public static void main(String[] args) {
16 | // 程序启动入口
17 | // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
18 | SpringApplication.run(Application.class,args);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/spring-data-elasticsearch-crud/src/main/java/org/spring/springboot/controller/CityRestController.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.controller;
2 |
3 | import org.spring.springboot.domain.City;
4 | import org.spring.springboot.service.CityService;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 |
7 | import org.springframework.web.bind.annotation.*;
8 |
9 | import java.util.List;
10 |
11 | /**
12 | * 城市 Controller 实现 Restful HTTP 服务
13 | *
14 | * Created by bysocket on 03/05/2017.
15 | */
16 | @RestController
17 | public class CityRestController {
18 |
19 | @Autowired
20 | private CityService cityService;
21 |
22 | /**
23 | * 插入 ES 新城市
24 | *
25 | * @param city
26 | * @return
27 | */
28 | @RequestMapping(value = "/api/city", method = RequestMethod.POST)
29 | public Long createCity(@RequestBody City city) {
30 | return cityService.saveCity(city);
31 | }
32 |
33 | /**
34 | * AND 语句查询
35 | *
36 | * @param description
37 | * @param score
38 | * @return
39 | */
40 | @RequestMapping(value = "/api/city/and/find", method = RequestMethod.GET)
41 | public List findByDescriptionAndScore(@RequestParam(value = "description") String description,
42 | @RequestParam(value = "score") Integer score) {
43 | return cityService.findByDescriptionAndScore(description, score);
44 | }
45 |
46 | /**
47 | * OR 语句查询
48 | *
49 | * @param description
50 | * @param score
51 | * @return
52 | */
53 | @RequestMapping(value = "/api/city/or/find", method = RequestMethod.GET)
54 | public List findByDescriptionOrScore(@RequestParam(value = "description") String description,
55 | @RequestParam(value = "score") Integer score) {
56 | return cityService.findByDescriptionOrScore(description, score);
57 | }
58 |
59 | /**
60 | * 查询城市描述
61 | *
62 | * @param description
63 | * @return
64 | */
65 | @RequestMapping(value = "/api/city/description/find", method = RequestMethod.GET)
66 | public List findByDescription(@RequestParam(value = "description") String description) {
67 | return cityService.findByDescription(description);
68 | }
69 |
70 | /**
71 | * NOT 语句查询
72 | *
73 | * @param description
74 | * @return
75 | */
76 | @RequestMapping(value = "/api/city/description/not/find", method = RequestMethod.GET)
77 | public List findByDescriptionNot(@RequestParam(value = "description") String description) {
78 | return cityService.findByDescriptionNot(description);
79 | }
80 |
81 | /**
82 | * LIKE 语句查询
83 | *
84 | * @param description
85 | * @return
86 | */
87 | @RequestMapping(value = "/api/city/like/find", method = RequestMethod.GET)
88 | public List findByDescriptionLike(@RequestParam(value = "description") String description) {
89 | return cityService.findByDescriptionLike(description);
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/spring-data-elasticsearch-crud/src/main/java/org/spring/springboot/domain/City.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.domain;
2 |
3 | import org.springframework.data.elasticsearch.annotations.Document;
4 |
5 | import java.io.Serializable;
6 |
7 | /**
8 | * 城市实体类
9 | *
10 | * Created by bysocket on 03/05/2017.
11 | */
12 | @Document(indexName = "province", type = "city")
13 | public class City implements Serializable {
14 |
15 | private static final long serialVersionUID = -1L;
16 |
17 | /**
18 | * 城市编号
19 | */
20 | private Long id;
21 |
22 | /**
23 | * 城市名称
24 | */
25 | private String name;
26 |
27 | /**
28 | * 描述
29 | */
30 | private String description;
31 |
32 | /**
33 | * 城市评分
34 | */
35 | private Integer score;
36 |
37 | public Long getId() {
38 | return id;
39 | }
40 |
41 | public void setId(Long id) {
42 | this.id = id;
43 | }
44 |
45 | public String getName() {
46 | return name;
47 | }
48 |
49 | public void setName(String name) {
50 | this.name = name;
51 | }
52 |
53 | public String getDescription() {
54 | return description;
55 | }
56 |
57 | public void setDescription(String description) {
58 | this.description = description;
59 | }
60 |
61 | public Integer getScore() {
62 | return score;
63 | }
64 |
65 | public void setScore(Integer score) {
66 | this.score = score;
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/spring-data-elasticsearch-crud/src/main/java/org/spring/springboot/repository/CityRepository.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.repository;
2 |
3 | import org.spring.springboot.domain.City;
4 | import org.springframework.data.domain.Page;
5 | import org.springframework.data.domain.Pageable;
6 | import org.springframework.data.elasticsearch.annotations.Query;
7 | import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
8 |
9 | import java.util.List;
10 |
11 | /**
12 | * ES 操作类
13 | *
14 | * Created by bysocket on 17/05/2017.
15 | */
16 | public interface CityRepository extends ElasticsearchRepository {
17 | /**
18 | * AND 语句查询
19 | *
20 | * @param description
21 | * @param score
22 | * @return
23 | */
24 | List findByDescriptionAndScore(String description, Integer score);
25 |
26 | /**
27 | * OR 语句查询
28 | *
29 | * @param description
30 | * @param score
31 | * @return
32 | */
33 | List findByDescriptionOrScore(String description, Integer score);
34 |
35 | /**
36 | * 查询城市描述
37 | *
38 | * 等同于下面代码
39 | * @Query("{\"bool\" : {\"must\" : {\"term\" : {\"description\" : \"?0\"}}}}")
40 | * Page findByDescription(String description, Pageable pageable);
41 | *
42 | * @param description
43 | * @param page
44 | * @return
45 | */
46 | Page findByDescription(String description, Pageable page);
47 |
48 | /**
49 | * NOT 语句查询
50 | *
51 | * @param description
52 | * @param page
53 | * @return
54 | */
55 | Page findByDescriptionNot(String description, Pageable page);
56 |
57 | /**
58 | * LIKE 语句查询
59 | *
60 | * @param description
61 | * @param page
62 | * @return
63 | */
64 | Page findByDescriptionLike(String description, Pageable page);
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/spring-data-elasticsearch-crud/src/main/java/org/spring/springboot/service/CityService.java:
--------------------------------------------------------------------------------
1 |
2 | package org.spring.springboot.service;
3 |
4 | import org.spring.springboot.domain.City;
5 |
6 | import java.util.List;
7 |
8 | /**
9 | * 城市 ES 业务接口类
10 | *
11 | */
12 | public interface CityService {
13 |
14 | /**
15 | * 新增 ES 城市信息
16 | *
17 | * @param city
18 | * @return
19 | */
20 | Long saveCity(City city);
21 |
22 | /**
23 | * AND 语句查询
24 | *
25 | * @param description
26 | * @param score
27 | * @return
28 | */
29 | List findByDescriptionAndScore(String description, Integer score);
30 |
31 | /**
32 | * OR 语句查询
33 | *
34 | * @param description
35 | * @param score
36 | * @return
37 | */
38 | List findByDescriptionOrScore(String description, Integer score);
39 |
40 | /**
41 | * 查询城市描述
42 | *
43 | * @param description
44 | * @return
45 | */
46 | List findByDescription(String description);
47 |
48 | /**
49 | * NOT 语句查询
50 | *
51 | * @param description
52 | * @return
53 | */
54 | List findByDescriptionNot(String description);
55 |
56 | /**
57 | * LIKE 语句查询
58 | *
59 | * @param description
60 | * @return
61 | */
62 | List findByDescriptionLike(String description);
63 | }
--------------------------------------------------------------------------------
/spring-data-elasticsearch-crud/src/main/java/org/spring/springboot/service/impl/CityESServiceImpl.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.service.impl;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.spring.springboot.domain.City;
6 | import org.spring.springboot.repository.CityRepository;
7 | import org.spring.springboot.service.CityService;
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.data.domain.Page;
10 | import org.springframework.data.domain.PageRequest;
11 | import org.springframework.data.domain.Pageable;
12 | import org.springframework.stereotype.Service;
13 |
14 | import java.util.List;
15 |
16 | /**
17 | * 城市 ES 业务逻辑实现类
18 | *
19 | * Created by bysocket on 07/02/2017.
20 | */
21 | @Service
22 | public class CityESServiceImpl implements CityService {
23 |
24 | private static final Logger LOGGER = LoggerFactory.getLogger(CityESServiceImpl.class);
25 |
26 | // 分页参数 -> TODO 代码可迁移到具体项目的公共 common 模块
27 | private static final Integer pageNumber = 0;
28 | private static final Integer pageSize = 10;
29 | Pageable pageable = new PageRequest(pageNumber, pageSize);
30 |
31 | // ES 操作类
32 | @Autowired
33 | CityRepository cityRepository;
34 |
35 | public Long saveCity(City city) {
36 | City cityResult = cityRepository.save(city);
37 | return cityResult.getId();
38 | }
39 |
40 | public List findByDescriptionAndScore(String description, Integer score) {
41 | return cityRepository.findByDescriptionAndScore(description, score);
42 | }
43 |
44 | public List findByDescriptionOrScore(String description, Integer score) {
45 | return cityRepository.findByDescriptionOrScore(description, score);
46 | }
47 |
48 | public List findByDescription(String description) {
49 | return cityRepository.findByDescription(description, pageable).getContent();
50 | }
51 |
52 | public List findByDescriptionNot(String description) {
53 | return cityRepository.findByDescriptionNot(description, pageable).getContent();
54 | }
55 |
56 | public List findByDescriptionLike(String description) {
57 | return cityRepository.findByDescriptionLike(description, pageable).getContent();
58 | }
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/spring-data-elasticsearch-crud/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | # ES
2 | spring.data.elasticsearch.repositories.enabled = true
3 | spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300
--------------------------------------------------------------------------------
/spring-data-elasticsearch-query/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot
7 | spring-data-elasticsearch-query
8 | 0.0.1-SNAPSHOT
9 | spring-data-elasticsearch-query :: spring-data-elasticsearch - 实战案例详解
10 |
11 |
12 |
13 | org.springframework.boot
14 | spring-boot-starter-parent
15 | 1.5.1.RELEASE
16 |
17 |
18 |
19 |
20 |
21 |
22 | org.springframework.boot
23 | spring-boot-starter-data-elasticsearch
24 |
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-web
30 |
31 |
32 |
33 |
34 | junit
35 | junit
36 | 4.12
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/spring-data-elasticsearch-query/src/main/java/org/spring/springboot/Application.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * Spring Boot 应用启动类
8 | *
9 | * Created by bysocket on 20/06/2017.
10 | */
11 | // Spring Boot 应用的标识
12 | @SpringBootApplication
13 | public class Application {
14 |
15 | public static void main(String[] args) {
16 | // 程序启动入口
17 | // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
18 | SpringApplication.run(Application.class,args);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/spring-data-elasticsearch-query/src/main/java/org/spring/springboot/controller/CityRestController.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.controller;
2 |
3 | import org.spring.springboot.domain.City;
4 | import org.spring.springboot.service.CityService;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 |
7 | import org.springframework.web.bind.annotation.*;
8 |
9 | import java.util.List;
10 |
11 | /**
12 | * 城市 Controller 实现 Restful HTTP 服务
13 | *
14 | * Created by bysocket on 20/06/2017.
15 | */
16 | @RestController
17 | public class CityRestController {
18 |
19 | @Autowired
20 | private CityService cityService;
21 |
22 | /**
23 | * 插入 ES 新城市
24 | *
25 | * @param city
26 | * @return
27 | */
28 | @RequestMapping(value = "/api/city", method = RequestMethod.POST)
29 | public Long createCity(@RequestBody City city) {
30 | return cityService.saveCity(city);
31 | }
32 |
33 | /**
34 | * 搜索返回分页结果
35 | *
36 | * @param pageNumber 当前页码
37 | * @param pageSize 每页大小
38 | * @param searchContent 搜索内容
39 | * @return
40 | */
41 | @RequestMapping(value = "/api/city/search", method = RequestMethod.GET)
42 | public List searchCity(@RequestParam(value = "pageNumber") Integer pageNumber,
43 | @RequestParam(value = "pageSize", required = false) Integer pageSize,
44 | @RequestParam(value = "searchContent") String searchContent) {
45 | return cityService.searchCity(pageNumber, pageSize,searchContent);
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/spring-data-elasticsearch-query/src/main/java/org/spring/springboot/domain/City.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.domain;
2 |
3 | import org.springframework.data.elasticsearch.annotations.Document;
4 |
5 | import java.io.Serializable;
6 |
7 | /**
8 | * 城市实体类
9 | *
10 | * Created by bysocket on 20/06/2017.
11 | */
12 | @Document(indexName = "province", type = "city")
13 | public class City implements Serializable {
14 |
15 | private static final long serialVersionUID = -1L;
16 |
17 | /**
18 | * 城市编号
19 | */
20 | private Long id;
21 |
22 | /**
23 | * 城市名称
24 | */
25 | private String name;
26 |
27 | /**
28 | * 描述
29 | */
30 | private String description;
31 |
32 | /**
33 | * 城市评分
34 | */
35 | private Integer score;
36 |
37 | public Long getId() {
38 | return id;
39 | }
40 |
41 | public void setId(Long id) {
42 | this.id = id;
43 | }
44 |
45 | public String getName() {
46 | return name;
47 | }
48 |
49 | public void setName(String name) {
50 | this.name = name;
51 | }
52 |
53 | public String getDescription() {
54 | return description;
55 | }
56 |
57 | public void setDescription(String description) {
58 | this.description = description;
59 | }
60 |
61 | public Integer getScore() {
62 | return score;
63 | }
64 |
65 | public void setScore(Integer score) {
66 | this.score = score;
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/spring-data-elasticsearch-query/src/main/java/org/spring/springboot/repository/CityRepository.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.repository;
2 |
3 | import org.spring.springboot.domain.City;
4 | import org.springframework.data.domain.Page;
5 | import org.springframework.data.domain.Pageable;
6 | import org.springframework.data.elasticsearch.annotations.Query;
7 | import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
8 |
9 | import java.util.List;
10 |
11 | /**
12 | * ES 操作类
13 | *
14 | * Created by bysocket on 20/06/2017.
15 | */
16 | public interface CityRepository extends ElasticsearchRepository {
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/spring-data-elasticsearch-query/src/main/java/org/spring/springboot/service/CityService.java:
--------------------------------------------------------------------------------
1 |
2 | package org.spring.springboot.service;
3 |
4 | import org.spring.springboot.domain.City;
5 |
6 | import java.util.List;
7 |
8 | /**
9 | * 城市 ES 业务接口类
10 | *
11 | */
12 | public interface CityService {
13 |
14 | /**
15 | * 新增 ES 城市信息
16 | *
17 | * @param city
18 | * @return
19 | */
20 | Long saveCity(City city);
21 |
22 | /**
23 | * 搜索词搜索,分页返回城市信息
24 | *
25 | * @param pageNumber 当前页码
26 | * @param pageSize 每页大小
27 | * @param searchContent 搜索内容
28 | * @return
29 | */
30 | List searchCity(Integer pageNumber, Integer pageSize, String searchContent);
31 | }
--------------------------------------------------------------------------------
/spring-data-elasticsearch-query/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | # ES
2 | spring.data.elasticsearch.repositories.enabled = true
3 | spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300
--------------------------------------------------------------------------------
/springboot-configuration/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot
7 | springboot-configuration
8 | 0.0.1-SNAPSHOT
9 | springboot-configuration :: 配置 Demo
10 |
11 |
12 |
13 | org.springframework.boot
14 | spring-boot-starter-parent
15 | 1.5.1.RELEASE
16 |
17 |
18 |
19 |
20 |
21 | org.springframework.boot
22 | spring-boot-starter-web
23 |
24 |
25 |
26 |
27 | junit
28 | junit
29 | 4.12
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/springboot-configuration/src/main/java/org/spring/springboot/Application.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * Spring Boot 应用启动类
8 | *
9 | */
10 | // Spring Boot 应用的标识
11 | @SpringBootApplication
12 | public class Application {
13 |
14 | public static void main(String[] args) {
15 | // 程序启动入口
16 | // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
17 | SpringApplication.run(Application.class,args);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/springboot-configuration/src/main/java/org/spring/springboot/config/MessageConfiguration.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.config;
2 |
3 | import org.springframework.context.annotation.Bean;
4 | import org.springframework.context.annotation.Configuration;
5 |
6 | /**
7 | * Created by bysocket on 08/09/2017.
8 | */
9 | @Configuration
10 | public class MessageConfiguration {
11 |
12 | @Bean
13 | public String message() {
14 | return "message configuration";
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/springboot-configuration/src/test/java/org/spring/springboot/config/MessageConfigurationTest.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.config;
2 |
3 | import org.junit.Test;
4 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5 |
6 | import static org.junit.Assert.assertEquals;
7 |
8 | /**
9 | * Spring Boot MessageConfiguration 测试 - {@link MessageConfiguration}
10 | *
11 | */
12 | public class MessageConfigurationTest {
13 |
14 | @Test
15 | public void testGetMessageBean() throws Exception {
16 | AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MessageConfiguration.class);
17 | assertEquals("message configuration", ctx.getBean("message"));
18 | }
19 |
20 | @Test
21 | public void testScanPackages() throws Exception {
22 | AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
23 | ctx.scan("org.spring.springboot");
24 | ctx.refresh();
25 | assertEquals("message configuration", ctx.getBean("message"));
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/springboot-dubbo-client/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot
7 | springboot-dubbo-client
8 | 0.0.1-SNAPSHOT
9 | springboot-dubbo 客户端:: 整合 Dubbo/ZooKeeper 详解 SOA 案例
10 |
11 |
12 |
13 | org.springframework.boot
14 | spring-boot-starter-parent
15 | 1.5.1.RELEASE
16 |
17 |
18 |
19 | 1.0.0
20 |
21 |
22 |
23 |
24 |
25 |
26 | io.dubbo.springboot
27 | spring-boot-starter-dubbo
28 | ${dubbo-spring-boot}
29 |
30 |
31 |
32 |
33 | org.springframework.boot
34 | spring-boot-starter-web
35 |
36 |
37 |
38 |
39 | org.springframework.boot
40 | spring-boot-starter-test
41 | test
42 |
43 |
44 |
45 |
46 | junit
47 | junit
48 | 4.12
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/springboot-dubbo-client/src/main/java/org/spring/springboot/ClientApplication.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot;
2 |
3 | import org.spring.springboot.dubbo.CityDubboConsumerService;
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 | import org.springframework.context.ConfigurableApplicationContext;
7 |
8 | /**
9 | * Spring Boot 应用启动类
10 | *
11 | * Created by bysocket on 16/4/26.
12 | */
13 | // Spring Boot 应用的标识
14 | @SpringBootApplication
15 | public class ClientApplication {
16 |
17 | public static void main(String[] args) {
18 | // 程序启动入口
19 | // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
20 | ConfigurableApplicationContext run = SpringApplication.run(ClientApplication.class, args);
21 | CityDubboConsumerService cityService = run.getBean(CityDubboConsumerService.class);
22 | cityService.printCity();
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/springboot-dubbo-client/src/main/java/org/spring/springboot/domain/City.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.domain;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * 城市实体类
7 | *
8 | * Created by bysocket on 07/02/2017.
9 | */
10 | public class City implements Serializable {
11 |
12 | private static final long serialVersionUID = -1L;
13 |
14 | /**
15 | * 城市编号
16 | */
17 | private Long id;
18 |
19 | /**
20 | * 省份编号
21 | */
22 | private Long provinceId;
23 |
24 | /**
25 | * 城市名称
26 | */
27 | private String cityName;
28 |
29 | /**
30 | * 描述
31 | */
32 | private String description;
33 |
34 | public Long getId() {
35 | return id;
36 | }
37 |
38 | public void setId(Long id) {
39 | this.id = id;
40 | }
41 |
42 | public Long getProvinceId() {
43 | return provinceId;
44 | }
45 |
46 | public void setProvinceId(Long provinceId) {
47 | this.provinceId = provinceId;
48 | }
49 |
50 | public String getCityName() {
51 | return cityName;
52 | }
53 |
54 | public void setCityName(String cityName) {
55 | this.cityName = cityName;
56 | }
57 |
58 | public String getDescription() {
59 | return description;
60 | }
61 |
62 | public void setDescription(String description) {
63 | this.description = description;
64 | }
65 |
66 | @Override
67 | public String toString() {
68 | return "City{" +
69 | "id=" + id +
70 | ", provinceId=" + provinceId +
71 | ", cityName='" + cityName + '\'' +
72 | ", description='" + description + '\'' +
73 | '}';
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/springboot-dubbo-client/src/main/java/org/spring/springboot/dubbo/CityDubboConsumerService.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.dubbo;
2 |
3 | import com.alibaba.dubbo.config.annotation.Reference;
4 | import org.spring.springboot.domain.City;
5 | import org.springframework.stereotype.Component;
6 |
7 | /**
8 | * 城市 Dubbo 服务消费者
9 | *
10 | * Created by bysocket on 28/02/2017.
11 | */
12 | @Component
13 | public class CityDubboConsumerService {
14 |
15 | @Reference(version = "1.0.0")
16 | CityDubboService cityDubboService;
17 |
18 | public void printCity() {
19 | String cityName="温岭";
20 | City city = cityDubboService.findCityByName(cityName);
21 | System.out.println(city.toString());
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/springboot-dubbo-client/src/main/java/org/spring/springboot/dubbo/CityDubboService.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.dubbo;
2 |
3 | import org.spring.springboot.domain.City;
4 |
5 | /**
6 | * 城市业务 Dubbo 服务层
7 | *
8 | * Created by bysocket on 28/02/2017.
9 | */
10 | public interface CityDubboService {
11 |
12 | /**
13 | * 根据城市名称,查询城市信息
14 | * @param cityName
15 | */
16 | City findCityByName(String cityName);
17 | }
18 |
--------------------------------------------------------------------------------
/springboot-dubbo-client/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | ## 避免和 server 工程端口冲突
2 | server.port=8081
3 |
4 | ## Dubbo 服务消费者配置
5 | spring.dubbo.application.name=consumer
6 | spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
7 | spring.dubbo.scan=org.spring.springboot.dubbo
--------------------------------------------------------------------------------
/springboot-dubbo-server/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot
7 | springboot-dubbo-server
8 | 0.0.1-SNAPSHOT
9 | springboot-dubbo 服务端:: 整合 Dubbo/ZooKeeper 详解 SOA 案例
10 |
11 |
12 |
13 | org.springframework.boot
14 | spring-boot-starter-parent
15 | 1.5.1.RELEASE
16 |
17 |
18 |
19 | 1.0.0
20 |
21 |
22 |
23 |
24 |
25 |
26 | io.dubbo.springboot
27 | spring-boot-starter-dubbo
28 | ${dubbo-spring-boot}
29 |
30 |
31 |
32 |
33 | org.springframework.boot
34 | spring-boot-starter-web
35 |
36 |
37 |
38 |
39 | org.springframework.boot
40 | spring-boot-starter-test
41 | test
42 |
43 |
44 |
45 |
46 | junit
47 | junit
48 | 4.12
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/springboot-dubbo-server/src/main/java/org/spring/springboot/ServerApplication.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * Spring Boot 应用启动类
8 | *
9 | * Created by bysocket on 16/4/26.
10 | */
11 | // Spring Boot 应用的标识
12 | @SpringBootApplication
13 | public class ServerApplication {
14 |
15 | public static void main(String[] args) {
16 | // 程序启动入口
17 | // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
18 | SpringApplication.run(ServerApplication.class,args);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/springboot-dubbo-server/src/main/java/org/spring/springboot/domain/City.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.domain;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * 城市实体类
7 | *
8 | * Created by bysocket on 07/02/2017.
9 | */
10 | public class City implements Serializable {
11 |
12 | private static final long serialVersionUID = -1L;
13 |
14 | /**
15 | * 城市编号
16 | */
17 | private Long id;
18 |
19 | /**
20 | * 省份编号
21 | */
22 | private Long provinceId;
23 |
24 | /**
25 | * 城市名称
26 | */
27 | private String cityName;
28 |
29 | /**
30 | * 描述
31 | */
32 | private String description;
33 |
34 | public City() {
35 | }
36 |
37 | public City(Long id, Long provinceId, String cityName, String description) {
38 | this.id = id;
39 | this.provinceId = provinceId;
40 | this.cityName = cityName;
41 | this.description = description;
42 | }
43 |
44 | public Long getId() {
45 | return id;
46 | }
47 |
48 | public void setId(Long id) {
49 | this.id = id;
50 | }
51 |
52 | public Long getProvinceId() {
53 | return provinceId;
54 | }
55 |
56 | public void setProvinceId(Long provinceId) {
57 | this.provinceId = provinceId;
58 | }
59 |
60 | public String getCityName() {
61 | return cityName;
62 | }
63 |
64 | public void setCityName(String cityName) {
65 | this.cityName = cityName;
66 | }
67 |
68 | public String getDescription() {
69 | return description;
70 | }
71 |
72 | public void setDescription(String description) {
73 | this.description = description;
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/springboot-dubbo-server/src/main/java/org/spring/springboot/dubbo/CityDubboService.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.dubbo;
2 |
3 | import org.spring.springboot.domain.City;
4 |
5 | /**
6 | * 城市业务 Dubbo 服务层
7 | *
8 | * Created by bysocket on 28/02/2017.
9 | */
10 | public interface CityDubboService {
11 |
12 | /**
13 | * 根据城市名称,查询城市信息
14 | * @param cityName
15 | */
16 | City findCityByName(String cityName);
17 | }
18 |
--------------------------------------------------------------------------------
/springboot-dubbo-server/src/main/java/org/spring/springboot/dubbo/impl/CityDubboServiceImpl.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.dubbo.impl;
2 |
3 | import com.alibaba.dubbo.config.annotation.Service;
4 | import org.spring.springboot.domain.City;
5 | import org.spring.springboot.dubbo.CityDubboService;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 |
8 | /**
9 | * 城市业务 Dubbo 服务层实现层
10 | *
11 | * Created by bysocket on 28/02/2017.
12 | */
13 | // 注册为 Dubbo 服务
14 | @Service(version = "1.0.0")
15 | public class CityDubboServiceImpl implements CityDubboService {
16 |
17 | public City findCityByName(String cityName) {
18 | return new City(1L,2L,"温岭","是我的故乡");
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/springboot-dubbo-server/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | ## Dubbo 服务提供者配置
2 | spring.dubbo.application.name=provider
3 | spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
4 | spring.dubbo.protocol.name=dubbo
5 | spring.dubbo.protocol.port=20880
6 | spring.dubbo.scan=org.spring.springboot.dubbo
--------------------------------------------------------------------------------
/springboot-elasticsearch/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot
7 | springboot-elasticsearch
8 | 0.0.1-SNAPSHOT
9 | springboot-elasticsearch :: 整合 Elasticsearch
10 |
11 |
12 |
13 | org.springframework.boot
14 | spring-boot-starter-parent
15 | 1.5.1.RELEASE
16 |
17 |
18 |
19 |
20 |
21 |
22 | org.springframework.boot
23 | spring-boot-starter-data-elasticsearch
24 |
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-web
30 |
31 |
32 |
33 |
34 | junit
35 | junit
36 | 4.12
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/springboot-elasticsearch/src/main/java/org/spring/springboot/Application.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * Spring Boot 应用启动类
8 | *
9 | * Created by bysocket on 16/4/26.
10 | */
11 | // Spring Boot 应用的标识
12 | @SpringBootApplication
13 | public class Application {
14 |
15 | public static void main(String[] args) {
16 | // 程序启动入口
17 | // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
18 | SpringApplication.run(Application.class,args);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/springboot-elasticsearch/src/main/java/org/spring/springboot/controller/CityRestController.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.controller;
2 |
3 | import org.spring.springboot.domain.City;
4 | import org.spring.springboot.service.CityService;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.web.bind.annotation.*;
7 |
8 | import java.util.List;
9 |
10 | /**
11 | * 城市 Controller 实现 Restful HTTP 服务
12 | *
13 | * Created by bysocket on 03/05/2017.
14 | */
15 | @RestController
16 | public class CityRestController {
17 |
18 | @Autowired
19 | private CityService cityService;
20 |
21 | @RequestMapping(value = "/api/city", method = RequestMethod.POST)
22 | public Long createCity(@RequestBody City city) {
23 | return cityService.saveCity(city);
24 | }
25 |
26 | @RequestMapping(value = "/api/city/search", method = RequestMethod.GET)
27 | public List searchCity(@RequestParam(value = "pageNumber") Integer pageNumber,
28 | @RequestParam(value = "pageSize", required = false) Integer pageSize,
29 | @RequestParam(value = "searchContent") String searchContent) {
30 | return cityService.searchCity(pageNumber,pageSize,searchContent);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/springboot-elasticsearch/src/main/java/org/spring/springboot/domain/City.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.domain;
2 |
3 | import org.springframework.data.elasticsearch.annotations.Document;
4 |
5 | import java.io.Serializable;
6 |
7 | /**
8 | * 城市实体类
9 | *
10 | * Created by bysocket on 03/05/2017.
11 | */
12 | @Document(indexName = "cityindex", type = "city")
13 | public class City implements Serializable{
14 |
15 | private static final long serialVersionUID = -1L;
16 |
17 | /**
18 | * 城市编号
19 | */
20 | private Long id;
21 |
22 | /**
23 | * 省份编号
24 | */
25 | private Long provinceid;
26 |
27 | /**
28 | * 城市名称
29 | */
30 | private String cityname;
31 |
32 | /**
33 | * 描述
34 | */
35 | private String description;
36 |
37 | public Long getId() {
38 | return id;
39 | }
40 |
41 | public void setId(Long id) {
42 | this.id = id;
43 | }
44 |
45 | public Long getProvinceid() {
46 | return provinceid;
47 | }
48 |
49 | public void setProvinceid(Long provinceid) {
50 | this.provinceid = provinceid;
51 | }
52 |
53 | public String getCityname() {
54 | return cityname;
55 | }
56 |
57 | public void setCityname(String cityname) {
58 | this.cityname = cityname;
59 | }
60 |
61 | public String getDescription() {
62 | return description;
63 | }
64 |
65 | public void setDescription(String description) {
66 | this.description = description;
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/springboot-elasticsearch/src/main/java/org/spring/springboot/repository/CityRepository.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.repository;
2 |
3 | import org.spring.springboot.domain.City;
4 | import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
5 | import org.springframework.stereotype.Repository;
6 |
7 | /**
8 | * Created by bysocket on 17/05/2017.
9 | */
10 | @Repository
11 | public interface CityRepository extends ElasticsearchRepository {
12 |
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/springboot-elasticsearch/src/main/java/org/spring/springboot/service/CityService.java:
--------------------------------------------------------------------------------
1 |
2 | package org.spring.springboot.service;
3 |
4 | import org.spring.springboot.domain.City;
5 | import java.util.List;
6 |
7 | public interface CityService {
8 |
9 | /**
10 | * 新增城市信息
11 | *
12 | * @param city
13 | * @return
14 | */
15 | Long saveCity(City city);
16 |
17 | /**
18 | * 根据关键词,function score query 权重分分页查询
19 | *
20 | * @param pageNumber
21 | * @param pageSize
22 | * @param searchContent
23 | * @return
24 | */
25 | List searchCity(Integer pageNumber, Integer pageSize, String searchContent);
26 | }
--------------------------------------------------------------------------------
/springboot-elasticsearch/src/main/java/org/spring/springboot/service/impl/CityESServiceImpl.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.service.impl;
2 |
3 | import org.elasticsearch.index.query.QueryBuilders;
4 | import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
5 | import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
6 | import org.slf4j.Logger;
7 | import org.slf4j.LoggerFactory;
8 | import org.spring.springboot.domain.City;
9 | import org.spring.springboot.repository.CityRepository;
10 | import org.spring.springboot.service.CityService;
11 | import org.springframework.beans.factory.annotation.Autowired;
12 | import org.springframework.data.domain.Page;
13 | import org.springframework.data.domain.PageRequest;
14 | import org.springframework.data.domain.Pageable;
15 | import org.springframework.data.domain.Sort;
16 | import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
17 | import org.springframework.data.elasticsearch.core.query.SearchQuery;
18 | import org.springframework.stereotype.Service;
19 |
20 | import java.util.List;
21 |
22 | /**
23 | * 城市 ES 业务逻辑实现类
24 | *
25 | * Created by bysocket on 07/02/2017.
26 | */
27 | @Service
28 | public class CityESServiceImpl implements CityService {
29 |
30 | private static final Logger LOGGER = LoggerFactory.getLogger(CityESServiceImpl.class);
31 |
32 | @Autowired
33 | CityRepository cityRepository;
34 |
35 | @Override
36 | public Long saveCity(City city) {
37 |
38 | City cityResult = cityRepository.save(city);
39 | return cityResult.getId();
40 | }
41 |
42 | @Override
43 | public List searchCity(Integer pageNumber,
44 | Integer pageSize,
45 | String searchContent) {
46 | // 分页参数
47 | Pageable pageable = new PageRequest(pageNumber, pageSize);
48 |
49 | // Function Score Query
50 | FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
51 | .add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("cityname", searchContent)),
52 | ScoreFunctionBuilders.weightFactorFunction(1000))
53 | .add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("description", searchContent)),
54 | ScoreFunctionBuilders.weightFactorFunction(100));
55 |
56 | // 创建搜索 DSL 查询
57 | SearchQuery searchQuery = new NativeSearchQueryBuilder()
58 | .withPageable(pageable)
59 | .withQuery(functionScoreQueryBuilder).build();
60 |
61 | LOGGER.info("\n searchCity(): searchContent [" + searchContent + "] \n DSL = \n " + searchQuery.getQuery().toString());
62 |
63 | Page searchPageResults = cityRepository.search(searchQuery);
64 | return searchPageResults.getContent();
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/springboot-elasticsearch/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | # ES
2 | spring.data.elasticsearch.repositories.enabled = true
3 | spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300
--------------------------------------------------------------------------------
/springboot-freemarker/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot
7 | springboot-freemarker
8 | 0.0.1-SNAPSHOT
9 | springboot-freemarker :: 整合 FreeMarker 案例
10 |
11 |
12 |
13 | org.springframework.boot
14 | spring-boot-starter-parent
15 | 1.5.1.RELEASE
16 |
17 |
18 |
19 | 1.2.0
20 | 5.1.39
21 |
22 |
23 |
24 |
25 |
26 | org.springframework.boot
27 | spring-boot-starter-freemarker
28 |
29 |
30 |
31 |
32 | org.springframework.boot
33 | spring-boot-starter-web
34 |
35 |
36 |
37 |
38 | org.springframework.boot
39 | spring-boot-starter-test
40 | test
41 |
42 |
43 |
44 |
45 | org.mybatis.spring.boot
46 | mybatis-spring-boot-starter
47 | ${mybatis-spring-boot}
48 |
49 |
50 |
51 |
52 | mysql
53 | mysql-connector-java
54 | ${mysql-connector}
55 |
56 |
57 |
58 |
59 | junit
60 | junit
61 | 4.12
62 |
63 |
64 |
65 |
--------------------------------------------------------------------------------
/springboot-freemarker/src/main/java/org/spring/springboot/Application.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot;
2 |
3 | import org.mybatis.spring.annotation.MapperScan;
4 | import org.spring.springboot.dao.CityDao;
5 | import org.spring.springboot.domain.City;
6 | import org.springframework.boot.CommandLineRunner;
7 | import org.springframework.boot.SpringApplication;
8 | import org.springframework.boot.autoconfigure.SpringBootApplication;
9 |
10 | /**
11 | * Spring Boot 应用启动类
12 | *
13 | * Created by bysocket on 16/4/26.
14 | */
15 | // Spring Boot 应用的标识
16 | @SpringBootApplication
17 | // mapper 接口类扫描包配置
18 | @MapperScan("org.spring.springboot.dao")
19 | public class Application {
20 |
21 | public static void main(String[] args) {
22 | // 程序启动入口
23 | // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
24 | SpringApplication.run(Application.class,args);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/springboot-freemarker/src/main/java/org/spring/springboot/controller/CityController.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.controller;
2 |
3 | import org.spring.springboot.domain.City;
4 | import org.spring.springboot.service.CityService;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Controller;
7 | import org.springframework.ui.Model;
8 | import org.springframework.web.bind.annotation.*;
9 |
10 | import java.util.List;
11 |
12 | /**
13 | * 城市 Controller 实现 Restful HTTP 服务
14 | *
15 | * Created by bysocket on 07/02/2017.
16 | */
17 | @Controller
18 | public class CityController {
19 |
20 | @Autowired
21 | private CityService cityService;
22 |
23 | @RequestMapping(value = "/api/city/{id}", method = RequestMethod.GET)
24 | public String findOneCity(Model model, @PathVariable("id") Long id) {
25 | model.addAttribute("city", cityService.findCityById(id));
26 | return "city";
27 | }
28 |
29 | @RequestMapping(value = "/api/city", method = RequestMethod.GET)
30 | public String findAllCity(Model model) {
31 | List cityList = cityService.findAllCity();
32 | model.addAttribute("cityList",cityList);
33 | return "cityList";
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/springboot-freemarker/src/main/java/org/spring/springboot/dao/CityDao.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.dao;
2 |
3 | import org.apache.ibatis.annotations.Param;
4 | import org.spring.springboot.domain.City;
5 |
6 | import java.util.List;
7 |
8 | /**
9 | * 城市 DAO 接口类
10 | *
11 | * Created by bysocket on 07/02/2017.
12 | */
13 | public interface CityDao {
14 |
15 | /**
16 | * 获取城市信息列表
17 | *
18 | * @return
19 | */
20 | List findAllCity();
21 |
22 | /**
23 | * 根据城市 ID,获取城市信息
24 | *
25 | * @param id
26 | * @return
27 | */
28 | City findById(@Param("id") Long id);
29 |
30 | Long saveCity(City city);
31 |
32 | Long updateCity(City city);
33 |
34 | Long deleteCity(Long id);
35 | }
36 |
--------------------------------------------------------------------------------
/springboot-freemarker/src/main/java/org/spring/springboot/domain/City.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.domain;
2 |
3 | /**
4 | * 城市实体类
5 | *
6 | * Created by bysocket on 07/02/2017.
7 | */
8 | public class City {
9 |
10 | /**
11 | * 城市编号
12 | */
13 | private Long id;
14 |
15 | /**
16 | * 省份编号
17 | */
18 | private Long provinceId;
19 |
20 | /**
21 | * 城市名称
22 | */
23 | private String cityName;
24 |
25 | /**
26 | * 描述
27 | */
28 | private String description;
29 |
30 | public Long getId() {
31 | return id;
32 | }
33 |
34 | public void setId(Long id) {
35 | this.id = id;
36 | }
37 |
38 | public Long getProvinceId() {
39 | return provinceId;
40 | }
41 |
42 | public void setProvinceId(Long provinceId) {
43 | this.provinceId = provinceId;
44 | }
45 |
46 | public String getCityName() {
47 | return cityName;
48 | }
49 |
50 | public void setCityName(String cityName) {
51 | this.cityName = cityName;
52 | }
53 |
54 | public String getDescription() {
55 | return description;
56 | }
57 |
58 | public void setDescription(String description) {
59 | this.description = description;
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/springboot-freemarker/src/main/java/org/spring/springboot/service/CityService.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.service;
2 |
3 | import org.spring.springboot.domain.City;
4 |
5 | import java.util.List;
6 |
7 | /**
8 | * 城市业务逻辑接口类
9 | *
10 | * Created by bysocket on 07/02/2017.
11 | */
12 | public interface CityService {
13 |
14 | /**
15 | * 获取城市信息列表
16 | *
17 | * @return
18 | */
19 | List findAllCity();
20 |
21 | /**
22 | * 根据城市 ID,查询城市信息
23 | *
24 | * @param id
25 | * @return
26 | */
27 | City findCityById(Long id);
28 |
29 | /**
30 | * 新增城市信息
31 | *
32 | * @param city
33 | * @return
34 | */
35 | Long saveCity(City city);
36 |
37 | /**
38 | * 更新城市信息
39 | *
40 | * @param city
41 | * @return
42 | */
43 | Long updateCity(City city);
44 |
45 | /**
46 | * 根据城市 ID,删除城市信息
47 | *
48 | * @param id
49 | * @return
50 | */
51 | Long deleteCity(Long id);
52 | }
53 |
--------------------------------------------------------------------------------
/springboot-freemarker/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.service.impl;
2 |
3 | import org.spring.springboot.dao.CityDao;
4 | import org.spring.springboot.domain.City;
5 | import org.spring.springboot.service.CityService;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.stereotype.Service;
8 |
9 | import java.util.List;
10 |
11 | /**
12 | * 城市业务逻辑实现类
13 | *
14 | * Created by bysocket on 07/02/2017.
15 | */
16 | @Service
17 | public class CityServiceImpl implements CityService {
18 |
19 | @Autowired
20 | private CityDao cityDao;
21 |
22 | public List findAllCity(){
23 | return cityDao.findAllCity();
24 | }
25 |
26 | public City findCityById(Long id) {
27 | return cityDao.findById(id);
28 | }
29 |
30 | @Override
31 | public Long saveCity(City city) {
32 | return cityDao.saveCity(city);
33 | }
34 |
35 | @Override
36 | public Long updateCity(City city) {
37 | return cityDao.updateCity(city);
38 | }
39 |
40 | @Override
41 | public Long deleteCity(Long id) {
42 | return cityDao.deleteCity(id);
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/springboot-freemarker/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | ## 数据源配置
2 | spring.datasource.url=jdbc:mysql://139.224.14.39:3306/springbootdb?useUnicode=true&characterEncoding=utf8
3 | spring.datasource.username=root
4 | spring.datasource.password=Hello123!@
5 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver
6 |
7 | ## Mybatis 配置
8 | mybatis.typeAliasesPackage=org.spring.springboot.domain
9 | mybatis.mapperLocations=classpath:mapper/*.xml
10 |
11 | ## Freemarker 配置
12 | ## 文件配置路径
13 | spring.freemarker.template-loader-path=classpath:/web/
14 | spring.freemarker.cache=false
15 | spring.freemarker.charset=UTF-8
16 | spring.freemarker.check-template-location=true
17 | spring.freemarker.content-type=text/html
18 | spring.freemarker.expose-request-attributes=true
19 | spring.freemarker.expose-session-attributes=true
20 | spring.freemarker.request-context-attribute=request
21 | spring.freemarker.suffix=.ftl
--------------------------------------------------------------------------------
/springboot-freemarker/src/main/resources/mapper/CityMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | id, province_id, city_name, description
15 |
16 |
17 |
23 |
24 |
29 |
30 |
31 | insert into
32 | city(id,province_id,city_name,description)
33 | values
34 | (#{id},#{provinceId},#{cityName},#{description})
35 |
36 |
37 |
38 | update
39 | city
40 | set
41 |
42 | province_id = #{provinceId},
43 |
44 |
45 | city_name = #{cityName},
46 |
47 |
48 | description = #{description}
49 |
50 | where
51 | id = #{id}
52 |
53 |
54 |
55 | delete from
56 | city
57 | where
58 | id = #{id}
59 |
60 |
61 |
--------------------------------------------------------------------------------
/springboot-freemarker/src/main/resources/web/city.ftl:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | City: ${city.cityName}!
7 | Q:Why I like?
8 | A:${city.description}!
9 |
10 |
11 |
--------------------------------------------------------------------------------
/springboot-freemarker/src/main/resources/web/cityList.ftl:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | <#list cityList as city>
7 |
8 | City: ${city.cityName}!
9 | Q:Why I like?
10 | A:${city.description}!
11 |
12 | #list>
13 |
14 |
15 |
--------------------------------------------------------------------------------
/springboot-helloworld/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot
7 | springboot-helloworld
8 | 0.0.1-SNAPSHOT
9 | springboot-helloworld :: HelloWorld Demo
10 |
11 |
12 |
13 | org.springframework.boot
14 | spring-boot-starter-parent
15 | 1.5.1.RELEASE
16 |
17 |
18 |
19 |
20 |
21 | org.springframework.boot
22 | spring-boot-starter-web
23 |
24 |
25 |
26 |
27 | junit
28 | junit
29 | 4.12
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/springboot-helloworld/src/main/java/org/spring/springboot/Application.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * Spring Boot 应用启动类
8 | *
9 | * Created by bysocket on 16/4/26.
10 | */
11 | // Spring Boot 应用的标识
12 | @SpringBootApplication
13 | public class Application {
14 |
15 | public static void main(String[] args) {
16 | // 程序启动入口
17 | // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
18 | SpringApplication.run(Application.class,args);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/springboot-helloworld/src/main/java/org/spring/springboot/web/HelloWorldController.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.web;
2 |
3 | import org.springframework.web.bind.annotation.RequestMapping;
4 | import org.springframework.web.bind.annotation.RestController;
5 |
6 | /**
7 | * Spring Boot HelloWorld 案例
8 | *
9 | * Created by bysocket on 16/4/26.
10 | */
11 | @RestController
12 | public class HelloWorldController {
13 |
14 | @RequestMapping("/")
15 | public String sayHello() {
16 | return "Hello,World!";
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/springboot-helloworld/src/test/java/org/spring/springboot/web/HelloWorldControllerTest.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.web;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.assertEquals;
6 |
7 | /**
8 | * Spring Boot HelloWorldController 测试 - {@link HelloWorldController}
9 | *
10 | * Created by bysocket on 16/4/26.
11 | */
12 | public class HelloWorldControllerTest {
13 |
14 | @Test
15 | public void testSayHello() {
16 | assertEquals("Hello,World!",new HelloWorldController().sayHello());
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/springboot-mybatis-annotation/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot
7 | springboot-mybatis-annotation
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 | springboot-mybatis-annotation
12 | Springboot-mybatis :: 整合 Mybatis Annotation 案例
13 |
14 |
15 |
16 | org.springframework.boot
17 | spring-boot-starter-parent
18 | 1.5.1.RELEASE
19 |
20 |
21 |
22 | 1.2.0
23 | 5.1.39
24 |
25 |
26 |
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter-web
31 |
32 |
33 |
34 |
35 | org.springframework.boot
36 | spring-boot-starter-test
37 | test
38 |
39 |
40 |
41 |
42 | org.mybatis.spring.boot
43 | mybatis-spring-boot-starter
44 | ${mybatis-spring-boot}
45 |
46 |
47 |
48 |
49 | mysql
50 | mysql-connector-java
51 | ${mysql-connector}
52 |
53 |
54 |
55 |
56 | junit
57 | junit
58 | 4.12
59 |
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/springboot-mybatis-annotation/src/main/java/org/spring/springboot/Application.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class Application {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(Application.class, args);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/springboot-mybatis-annotation/src/main/java/org/spring/springboot/controller/CityRestController.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.controller;
2 |
3 | import org.spring.springboot.domain.City;
4 | import org.spring.springboot.service.CityService;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.web.bind.annotation.RequestMapping;
7 | import org.springframework.web.bind.annotation.RequestMethod;
8 | import org.springframework.web.bind.annotation.RequestParam;
9 | import org.springframework.web.bind.annotation.RestController;
10 |
11 | /**
12 | * Created by xchunzhao on 02/05/2017.
13 | */
14 | @RestController
15 | public class CityRestController {
16 |
17 | @Autowired
18 | private CityService cityService;
19 |
20 | @RequestMapping(value = "/api/city", method = RequestMethod.GET)
21 | public City findOneCity(@RequestParam(value = "cityName", required = true) String cityName) {
22 | return cityService.findCityByName(cityName);
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/springboot-mybatis-annotation/src/main/java/org/spring/springboot/dao/CityDao.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.dao;
2 |
3 | import org.apache.ibatis.annotations.*;
4 | import org.spring.springboot.domain.City;
5 |
6 | /**
7 | * 城市 DAO 接口类
8 | *
9 | * Created by xchunzhao on 02/05/2017.
10 | */
11 | @Mapper // 标志为 Mybatis 的 Mapper
12 | public interface CityDao {
13 |
14 | /**
15 | * 根据城市名称,查询城市信息
16 | *
17 | * @param cityName 城市名
18 | */
19 | @Select("SELECT * FROM city")
20 | // 返回 Map 结果集
21 | @Results({
22 | @Result(property = "id", column = "id"),
23 | @Result(property = "provinceId", column = "province_id"),
24 | @Result(property = "cityName", column = "city_name"),
25 | @Result(property = "description", column = "description"),
26 | })
27 | City findByName(@Param("cityName") String cityName);
28 | }
29 |
--------------------------------------------------------------------------------
/springboot-mybatis-annotation/src/main/java/org/spring/springboot/domain/City.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.domain;
2 |
3 | /**
4 | * 城市实体类
5 | *
6 | * Created by xchunzhao on 02/05/2017.
7 | */
8 | public class City {
9 |
10 | /**
11 | * 城市编号
12 | */
13 | private Long id;
14 |
15 | /**
16 | * 省份编号
17 | */
18 | private Long provinceId;
19 |
20 | /**
21 | * 城市名称
22 | */
23 | private String cityName;
24 |
25 | /**
26 | * 描述
27 | */
28 | private String description;
29 |
30 | public Long getId() {
31 | return id;
32 | }
33 |
34 | public void setId(Long id) {
35 | this.id = id;
36 | }
37 |
38 | public Long getProvinceId() {
39 | return provinceId;
40 | }
41 |
42 | public void setProvinceId(Long provinceId) {
43 | this.provinceId = provinceId;
44 | }
45 |
46 | public String getCityName() {
47 | return cityName;
48 | }
49 |
50 | public void setCityName(String cityName) {
51 | this.cityName = cityName;
52 | }
53 |
54 | public String getDescription() {
55 | return description;
56 | }
57 |
58 | public void setDescription(String description) {
59 | this.description = description;
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/springboot-mybatis-annotation/src/main/java/org/spring/springboot/service/CityService.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.service;
2 |
3 | import org.spring.springboot.domain.City;
4 |
5 | /**
6 | * 城市业务逻辑接口类
7 | *
8 | * Created by xchunzhao on 02/05/2017.
9 | */
10 | public interface CityService {
11 |
12 | /**
13 | * 根据城市名称,查询城市信息
14 | * @param cityName
15 | */
16 | City findCityByName(String cityName);
17 | }
18 |
--------------------------------------------------------------------------------
/springboot-mybatis-annotation/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.service.impl;
2 |
3 | import org.spring.springboot.dao.CityDao;
4 | import org.spring.springboot.domain.City;
5 | import org.spring.springboot.service.CityService;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.stereotype.Service;
8 |
9 | /**
10 | * 城市业务逻辑实现类
11 | *
12 | * Created by xchunzhao on 02/05/2017.
13 | */
14 | @Service
15 | public class CityServiceImpl implements CityService {
16 |
17 | @Autowired
18 | private CityDao cityDao;
19 |
20 | public City findCityByName(String cityName) {
21 | return cityDao.findByName(cityName);
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/springboot-mybatis-annotation/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | ## 数据源配置
2 | spring.datasource.url=jdbc:mysql://139.224.14.39:3306/springbootdb?useUnicode=true&characterEncoding=utf8
3 | spring.datasource.username=root
4 | spring.datasource.password=yt0923666
5 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver
--------------------------------------------------------------------------------
/springboot-mybatis-mutil-datasource/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot
7 | springboot-mybatis-mutil-datasource
8 | 0.0.1-SNAPSHOT
9 | springboot-mybatis-mutil-datasource :: Spring Boot 实现 Mybatis 多数据源配置
10 |
11 |
12 |
13 | org.springframework.boot
14 | spring-boot-starter-parent
15 | 1.5.1.RELEASE
16 |
17 |
18 |
19 | 1.2.0
20 | 5.1.39
21 | 1.0.18
22 |
23 |
24 |
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-web
30 |
31 |
32 |
33 |
34 | org.springframework.boot
35 | spring-boot-starter-test
36 | test
37 |
38 |
39 |
40 |
41 | org.mybatis.spring.boot
42 | mybatis-spring-boot-starter
43 | ${mybatis-spring-boot}
44 |
45 |
46 |
47 |
48 | mysql
49 | mysql-connector-java
50 | ${mysql-connector}
51 |
52 |
53 |
54 |
55 | com.alibaba
56 | druid
57 | ${druid}
58 |
59 |
60 |
61 |
62 | junit
63 | junit
64 | 4.12
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/springboot-mybatis-mutil-datasource/src/main/java/org/spring/springboot/Application.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * Spring Boot 应用启动类
8 | *
9 | * Created by bysocket on 16/4/26.
10 | */
11 | // Spring Boot 应用的标识
12 | @SpringBootApplication
13 | public class Application {
14 |
15 | public static void main(String[] args) {
16 | // 程序启动入口
17 | // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
18 | SpringApplication.run(Application.class,args);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/springboot-mybatis-mutil-datasource/src/main/java/org/spring/springboot/config/ds/ClusterDataSourceConfig.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.config.ds;
2 |
3 | import com.alibaba.druid.pool.DruidDataSource;
4 | import org.apache.ibatis.session.SqlSessionFactory;
5 | import org.mybatis.spring.SqlSessionFactoryBean;
6 | import org.mybatis.spring.annotation.MapperScan;
7 | import org.springframework.beans.factory.annotation.Qualifier;
8 | import org.springframework.beans.factory.annotation.Value;
9 | import org.springframework.context.annotation.Bean;
10 | import org.springframework.context.annotation.Configuration;
11 | import org.springframework.context.annotation.Primary;
12 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
13 | import org.springframework.jdbc.datasource.DataSourceTransactionManager;
14 |
15 | import javax.sql.DataSource;
16 |
17 | @Configuration
18 | // 扫描 Mapper 接口并容器管理
19 | @MapperScan(basePackages = ClusterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "clusterSqlSessionFactory")
20 | public class ClusterDataSourceConfig {
21 |
22 | // 精确到 cluster 目录,以便跟其他数据源隔离
23 | static final String PACKAGE = "org.spring.springboot.dao.cluster";
24 | static final String MAPPER_LOCATION = "classpath:mapper/cluster/*.xml";
25 |
26 | @Value("${cluster.datasource.url}")
27 | private String url;
28 |
29 | @Value("${cluster.datasource.username}")
30 | private String user;
31 |
32 | @Value("${cluster.datasource.password}")
33 | private String password;
34 |
35 | @Value("${cluster.datasource.driverClassName}")
36 | private String driverClass;
37 |
38 | @Bean(name = "clusterDataSource")
39 | public DataSource clusterDataSource() {
40 | DruidDataSource dataSource = new DruidDataSource();
41 | dataSource.setDriverClassName(driverClass);
42 | dataSource.setUrl(url);
43 | dataSource.setUsername(user);
44 | dataSource.setPassword(password);
45 | return dataSource;
46 | }
47 |
48 | @Bean(name = "clusterTransactionManager")
49 | public DataSourceTransactionManager clusterTransactionManager() {
50 | return new DataSourceTransactionManager(clusterDataSource());
51 | }
52 |
53 | @Bean(name = "clusterSqlSessionFactory")
54 | public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("clusterDataSource") DataSource clusterDataSource)
55 | throws Exception {
56 | final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
57 | sessionFactory.setDataSource(clusterDataSource);
58 | sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
59 | .getResources(ClusterDataSourceConfig.MAPPER_LOCATION));
60 | return sessionFactory.getObject();
61 | }
62 | }
--------------------------------------------------------------------------------
/springboot-mybatis-mutil-datasource/src/main/java/org/spring/springboot/config/ds/MasterDataSourceConfig.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.config.ds;
2 |
3 | import com.alibaba.druid.pool.DruidDataSource;
4 | import org.apache.ibatis.session.SqlSessionFactory;
5 | import org.mybatis.spring.SqlSessionFactoryBean;
6 | import org.mybatis.spring.annotation.MapperScan;
7 | import org.springframework.beans.factory.annotation.Qualifier;
8 | import org.springframework.beans.factory.annotation.Value;
9 | import org.springframework.boot.context.properties.ConfigurationProperties;
10 | import org.springframework.context.annotation.Bean;
11 | import org.springframework.context.annotation.Configuration;
12 | import org.springframework.context.annotation.Primary;
13 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
14 | import org.springframework.jdbc.datasource.DataSourceTransactionManager;
15 |
16 | import javax.sql.DataSource;
17 |
18 | @Configuration
19 | // 扫描 Mapper 接口并容器管理
20 | @MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
21 | public class MasterDataSourceConfig {
22 |
23 | // 精确到 master 目录,以便跟其他数据源隔离
24 | static final String PACKAGE = "org.spring.springboot.dao.master";
25 | static final String MAPPER_LOCATION = "classpath:mapper/master/*.xml";
26 |
27 | @Value("${master.datasource.url}")
28 | private String url;
29 |
30 | @Value("${master.datasource.username}")
31 | private String user;
32 |
33 | @Value("${master.datasource.password}")
34 | private String password;
35 |
36 | @Value("${master.datasource.driverClassName}")
37 | private String driverClass;
38 |
39 | @Bean(name = "masterDataSource")
40 | @Primary
41 | public DataSource masterDataSource() {
42 | DruidDataSource dataSource = new DruidDataSource();
43 | dataSource.setDriverClassName(driverClass);
44 | dataSource.setUrl(url);
45 | dataSource.setUsername(user);
46 | dataSource.setPassword(password);
47 | return dataSource;
48 | }
49 |
50 | @Bean(name = "masterTransactionManager")
51 | @Primary
52 | public DataSourceTransactionManager masterTransactionManager() {
53 | return new DataSourceTransactionManager(masterDataSource());
54 | }
55 |
56 | @Bean(name = "masterSqlSessionFactory")
57 | @Primary
58 | public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)
59 | throws Exception {
60 | final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
61 | sessionFactory.setDataSource(masterDataSource);
62 | sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
63 | .getResources(MasterDataSourceConfig.MAPPER_LOCATION));
64 | return sessionFactory.getObject();
65 | }
66 | }
--------------------------------------------------------------------------------
/springboot-mybatis-mutil-datasource/src/main/java/org/spring/springboot/controller/UserRestController.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.controller;
2 |
3 | import org.spring.springboot.domain.City;
4 | import org.spring.springboot.domain.User;
5 | import org.spring.springboot.service.UserService;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.web.bind.annotation.RequestMapping;
8 | import org.springframework.web.bind.annotation.RequestMethod;
9 | import org.springframework.web.bind.annotation.RequestParam;
10 | import org.springframework.web.bind.annotation.RestController;
11 |
12 | /**
13 | * 用户控制层
14 | *
15 | * Created by bysocket on 07/02/2017.
16 | */
17 | @RestController
18 | public class UserRestController {
19 |
20 | @Autowired
21 | private UserService userService;
22 |
23 | /**
24 | * 根据用户名获取用户信息,包括从库的地址信息
25 | *
26 | * @param userName
27 | * @return
28 | */
29 | @RequestMapping(value = "/api/user", method = RequestMethod.GET)
30 | public User findByName(@RequestParam(value = "userName", required = true) String userName) {
31 | return userService.findByName(userName);
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/springboot-mybatis-mutil-datasource/src/main/java/org/spring/springboot/dao/cluster/CityDao.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.dao.cluster;
2 |
3 | import org.apache.ibatis.annotations.Mapper;
4 | import org.apache.ibatis.annotations.Param;
5 | import org.spring.springboot.domain.City;
6 |
7 | /**
8 | * 城市 DAO 接口类
9 | *
10 | * Created by bysocket on 07/02/2017.
11 | */
12 | @Mapper
13 | public interface CityDao {
14 |
15 | /**
16 | * 根据城市名称,查询城市信息
17 | *
18 | * @param cityName 城市名
19 | */
20 | City findByName(@Param("cityName") String cityName);
21 | }
22 |
--------------------------------------------------------------------------------
/springboot-mybatis-mutil-datasource/src/main/java/org/spring/springboot/dao/master/UserDao.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.dao.master;
2 |
3 | import org.apache.ibatis.annotations.Mapper;
4 | import org.apache.ibatis.annotations.Param;
5 | import org.spring.springboot.domain.User;
6 |
7 | /**
8 | * 用户 DAO 接口类
9 | *
10 | * Created by bysocket on 07/02/2017.
11 | */
12 | @Mapper
13 | public interface UserDao {
14 |
15 | /**
16 | * 根据用户名获取用户信息
17 | *
18 | * @param userName
19 | * @return
20 | */
21 | User findByName(@Param("userName") String userName);
22 | }
23 |
--------------------------------------------------------------------------------
/springboot-mybatis-mutil-datasource/src/main/java/org/spring/springboot/domain/City.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.domain;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * 城市实体类
7 | *
8 | * Created by bysocket on 07/02/2017.
9 | */
10 | public class City {
11 |
12 | /**
13 | * 城市编号
14 | */
15 | private Long id;
16 |
17 | /**
18 | * 省份编号
19 | */
20 | private Long provinceId;
21 |
22 | /**
23 | * 城市名称
24 | */
25 | private String cityName;
26 |
27 | /**
28 | * 描述
29 | */
30 | private String description;
31 |
32 | public Long getId() {
33 | return id;
34 | }
35 |
36 | public void setId(Long id) {
37 | this.id = id;
38 | }
39 |
40 | public Long getProvinceId() {
41 | return provinceId;
42 | }
43 |
44 | public void setProvinceId(Long provinceId) {
45 | this.provinceId = provinceId;
46 | }
47 |
48 | public String getCityName() {
49 | return cityName;
50 | }
51 |
52 | public void setCityName(String cityName) {
53 | this.cityName = cityName;
54 | }
55 |
56 | public String getDescription() {
57 | return description;
58 | }
59 |
60 | public void setDescription(String description) {
61 | this.description = description;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/springboot-mybatis-mutil-datasource/src/main/java/org/spring/springboot/domain/User.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.domain;
2 |
3 | /**
4 | * 用户实体类
5 | *
6 | * Created by bysocket on 07/02/2017.
7 | */
8 | public class User {
9 |
10 | /**
11 | * 城市编号
12 | */
13 | private Long id;
14 |
15 | /**
16 | * 城市名称
17 | */
18 | private String userName;
19 |
20 | /**
21 | * 描述
22 | */
23 | private String description;
24 |
25 | private City city;
26 |
27 | public City getCity() {
28 | return city;
29 | }
30 |
31 | public void setCity(City city) {
32 | this.city = city;
33 | }
34 |
35 | public Long getId() {
36 | return id;
37 | }
38 |
39 | public void setId(Long id) {
40 | this.id = id;
41 | }
42 |
43 | public String getUserName() {
44 | return userName;
45 | }
46 |
47 | public void setUserName(String userName) {
48 | this.userName = userName;
49 | }
50 |
51 | public String getDescription() {
52 | return description;
53 | }
54 |
55 | public void setDescription(String description) {
56 | this.description = description;
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/springboot-mybatis-mutil-datasource/src/main/java/org/spring/springboot/service/UserService.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.service;
2 |
3 | import org.spring.springboot.domain.City;
4 | import org.spring.springboot.domain.User;
5 |
6 | /**
7 | * 用户业务接口层
8 | *
9 | * Created by bysocket on 07/02/2017.
10 | */
11 | public interface UserService {
12 |
13 | /**
14 | * 根据用户名获取用户信息,包括从库的地址信息
15 | *
16 | * @param userName
17 | * @return
18 | */
19 | User findByName(String userName);
20 | }
21 |
--------------------------------------------------------------------------------
/springboot-mybatis-mutil-datasource/src/main/java/org/spring/springboot/service/impl/UserServiceImpl.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.service.impl;
2 |
3 | import org.spring.springboot.dao.cluster.CityDao;
4 | import org.spring.springboot.dao.master.UserDao;
5 | import org.spring.springboot.domain.City;
6 | import org.spring.springboot.domain.User;
7 | import org.spring.springboot.service.UserService;
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.stereotype.Service;
10 |
11 | /**
12 | * 用户业务实现层
13 | *
14 | * Created by bysocket on 07/02/2017.
15 | */
16 | @Service
17 | public class UserServiceImpl implements UserService {
18 |
19 | @Autowired
20 | private UserDao userDao; // 主数据源
21 |
22 | @Autowired
23 | private CityDao cityDao; // 从数据源
24 |
25 | @Override
26 | public User findByName(String userName) {
27 | User user = userDao.findByName(userName);
28 | City city = cityDao.findByName("温岭市");
29 | user.setCity(city);
30 | return user;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/springboot-mybatis-mutil-datasource/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | ## master 数据源配置
2 | master.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8
3 | master.datasource.username=root
4 | master.datasource.password=123456
5 | master.datasource.driverClassName=com.mysql.jdbc.Driver
6 |
7 | ## cluster 数据源配置
8 | cluster.datasource.url=jdbc:mysql://localhost:3306/springbootdb_cluster?useUnicode=true&characterEncoding=utf8
9 | cluster.datasource.username=root
10 | cluster.datasource.password=123456
11 | cluster.datasource.driverClassName=com.mysql.jdbc.Driver
--------------------------------------------------------------------------------
/springboot-mybatis-mutil-datasource/src/main/resources/mapper/cluster/CityMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | id, province_id, city_name, description
15 |
16 |
17 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/springboot-mybatis-mutil-datasource/src/main/resources/mapper/master/UserMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | id, user_name, description
14 |
15 |
16 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/springboot-mybatis-redis-annotation/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot
7 | springboot-mybatis-redis-annotation
8 | 0.0.1-SNAPSHOT
9 | springboot-mybatis-redis-annotation :: 注解实现整合 Redis 作为缓存
10 |
11 |
12 |
13 | org.springframework.boot
14 | spring-boot-starter-parent
15 | 1.5.3.RELEASE
16 |
17 |
18 |
19 | 1.2.0
20 | 5.1.39
21 | 1.2.32
22 |
23 |
24 |
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-cache
30 |
31 |
32 |
33 |
34 | org.springframework.boot
35 | spring-boot-starter-data-redis
36 |
37 |
38 |
39 |
40 | org.springframework.boot
41 | spring-boot-starter-web
42 |
43 |
44 |
45 |
46 | org.springframework.boot
47 | spring-boot-starter-test
48 | test
49 |
50 |
51 |
52 |
53 | junit
54 | junit
55 | 4.12
56 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/springboot-mybatis-redis-annotation/src/main/java/org/spring/springboot/Application.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * Spring Boot 应用启动类
8 | *
9 | * Created by bysocket on 16/4/26.
10 | */
11 | // Spring Boot 应用的标识
12 | @SpringBootApplication
13 | public class Application {
14 |
15 | public static void main(String[] args) {
16 | // 程序启动入口
17 | // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
18 | SpringApplication.run(Application.class,args);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/springboot-mybatis-redis-annotation/src/main/java/org/spring/springboot/domain/City.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.domain;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * 城市实体类
7 | *
8 | * Created by bysocket on 07/02/2017.
9 | */
10 | public class City implements Serializable {
11 |
12 | private static final long serialVersionUID = -1L;
13 |
14 | /**
15 | * 城市编号
16 | */
17 | private Long id;
18 |
19 | /**
20 | * 省份编号
21 | */
22 | private Long provinceId;
23 |
24 | /**
25 | * 城市名称
26 | */
27 | private String cityName;
28 |
29 | /**
30 | * 描述
31 | */
32 | private String description;
33 |
34 | public City(Long id, Long provinceId, String cityName, String description) {
35 | this.id = id;
36 | this.provinceId = provinceId;
37 | this.cityName = cityName;
38 | this.description = description;
39 | }
40 |
41 | public Long getId() {
42 | return id;
43 | }
44 |
45 | public void setId(Long id) {
46 | this.id = id;
47 | }
48 |
49 | public Long getProvinceId() {
50 | return provinceId;
51 | }
52 |
53 | public void setProvinceId(Long provinceId) {
54 | this.provinceId = provinceId;
55 | }
56 |
57 | public String getCityName() {
58 | return cityName;
59 | }
60 |
61 | public void setCityName(String cityName) {
62 | this.cityName = cityName;
63 | }
64 |
65 | public String getDescription() {
66 | return description;
67 | }
68 |
69 | public void setDescription(String description) {
70 | this.description = description;
71 | }
72 |
73 | @Override
74 | public String toString() {
75 | return "City{" +
76 | "id=" + id +
77 | ", provinceId=" + provinceId +
78 | ", cityName='" + cityName + '\'' +
79 | ", description='" + description + '\'' +
80 | '}';
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/springboot-mybatis-redis-annotation/src/main/java/org/spring/springboot/service/CityService.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.service;
2 |
3 | import org.spring.springboot.domain.City;
4 |
5 | import java.util.List;
6 |
7 | /**
8 | * 城市业务逻辑接口类
9 | *
10 | * Created by bysocket on 07/02/2017.
11 | */
12 | public interface CityService {
13 |
14 | /**
15 | * 获取城市
16 | *
17 | */
18 | City getCityByName(String cityName);
19 |
20 | /**
21 | * 新增城市信息
22 | *
23 | */
24 | void saveCity(City city);
25 |
26 | /**
27 | * 更新城市信息
28 | *
29 | */
30 | void updateCityDescription(String cityName, String description);
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/springboot-mybatis-redis-annotation/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.service.impl;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.spring.springboot.domain.City;
6 | import org.spring.springboot.service.CityService;
7 | import org.springframework.cache.annotation.CachePut;
8 | import org.springframework.cache.annotation.Cacheable;
9 | import org.springframework.stereotype.Service;
10 |
11 | import java.util.HashMap;
12 | import java.util.Map;
13 |
14 | /**
15 | * 城市业务逻辑实现类
16 | *
17 | * Created by bysocket on 07/02/2017.
18 | */
19 | @Service
20 | public class CityServiceImpl implements CityService {
21 |
22 |
23 | // 模拟数据库存储
24 | private Map cityMap = new HashMap();
25 |
26 | public void saveCity(City city){
27 | // 模拟数据库插入操作
28 | cityMap.put(city.getCityName(), city);
29 | }
30 |
31 | @Cacheable(value = "baseCityInfo")
32 | public City getCityByName(String cityName){
33 | // 模拟数据库查询并返回
34 | return cityMap.get(cityName);
35 | }
36 |
37 | @CachePut(value = "baseCityInfo")
38 | public void updateCityDescription(String cityName, String description){
39 | City city = cityMap.get(cityName);
40 | city.setDescription(description);
41 | // 模拟更新数据库
42 | cityMap.put(cityName, city);
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/springboot-mybatis-redis-annotation/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | ## Redis 配置
2 | ## Redis数据库索引(默认为0)
3 | spring.redis.database=0
4 | ## Redis服务器地址
5 | spring.redis.host=127.0.0.1
6 | ## Redis服务器连接端口
7 | spring.redis.port=6379
8 | ## Redis服务器连接密码(默认为空)
9 | spring.redis.password=
10 | ## 连接池最大连接数(使用负值表示没有限制)
11 | spring.redis.pool.max-active=8
12 | ## 连接池最大阻塞等待时间(使用负值表示没有限制)
13 | spring.redis.pool.max-wait=-1
14 | ## 连接池中的最大空闲连接
15 | spring.redis.pool.max-idle=8
16 | ## 连接池中的最小空闲连接
17 | spring.redis.pool.min-idle=0
18 | ## 连接超时时间(毫秒)
19 | spring.redis.timeout=0
--------------------------------------------------------------------------------
/springboot-mybatis-redis-annotation/src/test/org/spring/springboot/ApplicationTests.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 | import org.spring.springboot.domain.City;
8 | import org.spring.springboot.service.CityService;
9 | import org.spring.springboot.service.impl.CityServiceImpl;
10 | import org.springframework.beans.factory.annotation.Autowired;
11 | import org.springframework.boot.test.context.SpringBootTest;
12 | import org.springframework.test.context.junit4.SpringRunner;
13 |
14 | /**
15 | * Created by bysocket on 05/06/2017.
16 | */
17 | @RunWith(SpringRunner.class)
18 | @SpringBootTest
19 | public class ApplicationTests {
20 |
21 | private static final Logger LOGGER = LoggerFactory.getLogger(CityServiceImpl.class);
22 |
23 |
24 | @Autowired
25 | private CityService cityService;
26 |
27 | @Test
28 | public void testRedis() {
29 | City city = getShanghai();
30 | // 向 redis 中存入数据
31 | cityService.saveCity(city);
32 |
33 | // 从 redis 中取数据
34 | City cityInfo = cityService.getCityByName("上海");
35 |
36 | LOGGER.info(cityInfo.toString());
37 |
38 | }
39 |
40 | @Test
41 | public void testRedisCache() {
42 | City city = getBeijing();
43 | // 向 redis 中存入数据
44 | cityService.saveCity(city);
45 |
46 | // 从 redis 中取数据, 第一次查询
47 | City cityInfo = cityService.getCityByName("北京");
48 | LOGGER.info("第一次查询:" + cityInfo.toString());
49 |
50 | // 从 redis 中取数据, 第二次查询
51 | cityInfo = cityService.getCityByName("北京");
52 | LOGGER.info("第二次查询:" + cityInfo.toString());
53 |
54 | // 更新 city 的描述信息后查询
55 | cityService.updateCityDescription("北京", "想不想去北京玩玩呢?");
56 | cityInfo = cityService.getCityByName("北京");
57 | LOGGER.info("更新描述后查询:" + cityInfo.toString());
58 |
59 | }
60 |
61 |
62 |
63 | private City getShanghai(){
64 | return new City(1L, 10L, "上海", "人称魔都的地方");
65 | }
66 |
67 | private City getBeijing(){
68 | return new City(2L, 20L, "北京", "中国帝都");
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/springboot-mybatis-redis/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot
7 | springboot-mybatis-redis
8 | 0.0.1-SNAPSHOT
9 | springboot-mybatis-redis :: 整合 Mybatis 并使用 Redis 作为缓存
10 |
11 |
12 |
13 | org.springframework.boot
14 | spring-boot-starter-parent
15 | 1.5.1.RELEASE
16 |
17 |
18 |
19 | 1.2.0
20 | 5.1.39
21 | 1.3.2.RELEASE
22 |
23 |
24 |
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-redis
30 | ${spring-boot-starter-redis-version}
31 |
32 |
33 |
34 |
35 | org.springframework.boot
36 | spring-boot-starter-web
37 |
38 |
39 |
40 |
41 | org.springframework.boot
42 | spring-boot-starter-test
43 | test
44 |
45 |
46 |
47 |
48 | org.mybatis.spring.boot
49 | mybatis-spring-boot-starter
50 | ${mybatis-spring-boot}
51 |
52 |
53 |
54 |
55 | mysql
56 | mysql-connector-java
57 | ${mysql-connector}
58 |
59 |
60 |
61 |
62 | junit
63 | junit
64 | 4.12
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/springboot-mybatis-redis/src/main/java/org/spring/springboot/Application.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot;
2 |
3 | import org.mybatis.spring.annotation.MapperScan;
4 | import org.spring.springboot.dao.CityDao;
5 | import org.spring.springboot.domain.City;
6 | import org.springframework.boot.CommandLineRunner;
7 | import org.springframework.boot.SpringApplication;
8 | import org.springframework.boot.autoconfigure.SpringBootApplication;
9 |
10 | /**
11 | * Spring Boot 应用启动类
12 | *
13 | * Created by bysocket on 16/4/26.
14 | */
15 | // Spring Boot 应用的标识
16 | @SpringBootApplication
17 | // mapper 接口类扫描包配置
18 | @MapperScan("org.spring.springboot.dao")
19 | public class Application {
20 |
21 | public static void main(String[] args) {
22 | // 程序启动入口
23 | // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
24 | SpringApplication.run(Application.class,args);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/springboot-mybatis-redis/src/main/java/org/spring/springboot/controller/CityRestController.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.controller;
2 |
3 | import org.spring.springboot.domain.City;
4 | import org.spring.springboot.service.CityService;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.http.MediaType;
7 | import org.springframework.web.bind.annotation.*;
8 |
9 | import java.util.List;
10 |
11 | /**
12 | * Created by bysocket on 07/02/2017.
13 | */
14 | @RestController
15 | public class CityRestController {
16 |
17 | @Autowired
18 | private CityService cityService;
19 |
20 |
21 | @RequestMapping(value = "/api/city/{id}", method = RequestMethod.GET)
22 | public City findOneCity(@PathVariable("id") Long id) {
23 | return cityService.findCityById(id);
24 | }
25 |
26 | @RequestMapping(value = "/api/city", method = RequestMethod.POST)
27 | public void createCity(@RequestBody City city) {
28 | cityService.saveCity(city);
29 | }
30 |
31 | @RequestMapping(value = "/api/city", method = RequestMethod.PUT)
32 | public void modifyCity(@RequestBody City city) {
33 | cityService.updateCity(city);
34 | }
35 |
36 | @RequestMapping(value = "/api/city/{id}", method = RequestMethod.DELETE)
37 | public void modifyCity(@PathVariable("id") Long id) {
38 | cityService.deleteCity(id);
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/springboot-mybatis-redis/src/main/java/org/spring/springboot/dao/CityDao.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.dao;
2 |
3 | import org.apache.ibatis.annotations.Param;
4 | import org.spring.springboot.domain.City;
5 |
6 | import java.util.List;
7 |
8 | /**
9 | * 城市 DAO 接口类
10 | *
11 | * Created by bysocket on 07/02/2017.
12 | */
13 | public interface CityDao {
14 |
15 | /**
16 | * 获取城市信息列表
17 | *
18 | * @return
19 | */
20 | List findAllCity();
21 |
22 | /**
23 | * 根据城市 ID,获取城市信息
24 | *
25 | * @param id
26 | * @return
27 | */
28 | City findById(@Param("id") Long id);
29 |
30 | Long saveCity(City city);
31 |
32 | Long updateCity(City city);
33 |
34 | Long deleteCity(Long id);
35 | }
36 |
--------------------------------------------------------------------------------
/springboot-mybatis-redis/src/main/java/org/spring/springboot/domain/City.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.domain;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * 城市实体类
7 | *
8 | * Created by bysocket on 07/02/2017.
9 | */
10 | public class City implements Serializable {
11 |
12 | private static final long serialVersionUID = -1L;
13 |
14 | /**
15 | * 城市编号
16 | */
17 | private Long id;
18 |
19 | /**
20 | * 省份编号
21 | */
22 | private Long provinceId;
23 |
24 | /**
25 | * 城市名称
26 | */
27 | private String cityName;
28 |
29 | /**
30 | * 描述
31 | */
32 | private String description;
33 |
34 | public Long getId() {
35 | return id;
36 | }
37 |
38 | public void setId(Long id) {
39 | this.id = id;
40 | }
41 |
42 | public Long getProvinceId() {
43 | return provinceId;
44 | }
45 |
46 | public void setProvinceId(Long provinceId) {
47 | this.provinceId = provinceId;
48 | }
49 |
50 | public String getCityName() {
51 | return cityName;
52 | }
53 |
54 | public void setCityName(String cityName) {
55 | this.cityName = cityName;
56 | }
57 |
58 | public String getDescription() {
59 | return description;
60 | }
61 |
62 | public void setDescription(String description) {
63 | this.description = description;
64 | }
65 |
66 | @Override
67 | public String toString() {
68 | return "City{" +
69 | "id=" + id +
70 | ", provinceId=" + provinceId +
71 | ", cityName='" + cityName + '\'' +
72 | ", description='" + description + '\'' +
73 | '}';
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/springboot-mybatis-redis/src/main/java/org/spring/springboot/service/CityService.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.service;
2 |
3 | import org.spring.springboot.domain.City;
4 |
5 | import java.util.List;
6 |
7 | /**
8 | * 城市业务逻辑接口类
9 | *
10 | * Created by bysocket on 07/02/2017.
11 | */
12 | public interface CityService {
13 | /**
14 | * 根据城市 ID,查询城市信息
15 | *
16 | * @param id
17 | * @return
18 | */
19 | City findCityById(Long id);
20 |
21 | /**
22 | * 新增城市信息
23 | *
24 | * @param city
25 | * @return
26 | */
27 | Long saveCity(City city);
28 |
29 | /**
30 | * 更新城市信息
31 | *
32 | * @param city
33 | * @return
34 | */
35 | Long updateCity(City city);
36 |
37 | /**
38 | * 根据城市 ID,删除城市信息
39 | *
40 | * @param id
41 | * @return
42 | */
43 | Long deleteCity(Long id);
44 | }
45 |
--------------------------------------------------------------------------------
/springboot-mybatis-redis/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.service.impl;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.spring.springboot.dao.CityDao;
6 | import org.spring.springboot.domain.City;
7 | import org.spring.springboot.service.CityService;
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.data.redis.core.RedisTemplate;
10 | import org.springframework.data.redis.core.StringRedisTemplate;
11 | import org.springframework.data.redis.core.ValueOperations;
12 | import org.springframework.stereotype.Service;
13 |
14 | import java.util.List;
15 | import java.util.concurrent.TimeUnit;
16 |
17 | /**
18 | * 城市业务逻辑实现类
19 | *
20 | * Created by bysocket on 07/02/2017.
21 | */
22 | @Service
23 | public class CityServiceImpl implements CityService {
24 |
25 | private static final Logger LOGGER = LoggerFactory.getLogger(CityServiceImpl.class);
26 |
27 | @Autowired
28 | private CityDao cityDao;
29 |
30 | @Autowired
31 | private RedisTemplate redisTemplate;
32 |
33 | /**
34 | * 获取城市逻辑:
35 | * 如果缓存存在,从缓存中获取城市信息
36 | * 如果缓存不存在,从 DB 中获取城市信息,然后插入缓存
37 | */
38 | public City findCityById(Long id) {
39 | // 从缓存中获取城市信息
40 | String key = "city_" + id;
41 | ValueOperations operations = redisTemplate.opsForValue();
42 |
43 | // 缓存存在
44 | boolean hasKey = redisTemplate.hasKey(key);
45 | if (hasKey) {
46 | City city = operations.get(key);
47 |
48 | LOGGER.info("CityServiceImpl.findCityById() : 从缓存中获取了城市 >> " + city.toString());
49 | return city;
50 | }
51 |
52 | // 从 DB 中获取城市信息
53 | City city = cityDao.findById(id);
54 |
55 | // 插入缓存
56 | operations.set(key, city, 10, TimeUnit.SECONDS);
57 | LOGGER.info("CityServiceImpl.findCityById() : 城市插入缓存 >> " + city.toString());
58 |
59 | return city;
60 | }
61 |
62 | @Override
63 | public Long saveCity(City city) {
64 | return cityDao.saveCity(city);
65 | }
66 |
67 | /**
68 | * 更新城市逻辑:
69 | * 如果缓存存在,删除
70 | * 如果缓存不存在,不操作
71 | */
72 | @Override
73 | public Long updateCity(City city) {
74 | Long ret = cityDao.updateCity(city);
75 |
76 | // 缓存存在,删除缓存
77 | String key = "city_" + city.getId();
78 | boolean hasKey = redisTemplate.hasKey(key);
79 | if (hasKey) {
80 | redisTemplate.delete(key);
81 |
82 | LOGGER.info("CityServiceImpl.updateCity() : 从缓存中删除城市 >> " + city.toString());
83 | }
84 |
85 | return ret;
86 | }
87 |
88 | @Override
89 | public Long deleteCity(Long id) {
90 |
91 | Long ret = cityDao.deleteCity(id);
92 |
93 | // 缓存存在,删除缓存
94 | String key = "city_" + id;
95 | boolean hasKey = redisTemplate.hasKey(key);
96 | if (hasKey) {
97 | redisTemplate.delete(key);
98 |
99 | LOGGER.info("CityServiceImpl.deleteCity() : 从缓存中删除城市 ID >> " + id);
100 | }
101 | return ret;
102 | }
103 |
104 | }
105 |
--------------------------------------------------------------------------------
/springboot-mybatis-redis/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | ## 数据源配置
2 | spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8
3 | spring.datasource.username=root
4 | spring.datasource.password=123456
5 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver
6 |
7 | ## Mybatis 配置
8 | mybatis.typeAliasesPackage=org.spring.springboot.domain
9 | mybatis.mapperLocations=classpath:mapper/*.xml
10 |
11 | ## Redis 配置
12 | ## Redis数据库索引(默认为0)
13 | spring.redis.database=0
14 | ## Redis服务器地址
15 | spring.redis.host=127.0.0.1
16 | ## Redis服务器连接端口
17 | spring.redis.port=6379
18 | ## Redis服务器连接密码(默认为空)
19 | spring.redis.password=
20 | ## 连接池最大连接数(使用负值表示没有限制)
21 | spring.redis.pool.max-active=8
22 | ## 连接池最大阻塞等待时间(使用负值表示没有限制)
23 | spring.redis.pool.max-wait=-1
24 | ## 连接池中的最大空闲连接
25 | spring.redis.pool.max-idle=8
26 | ## 连接池中的最小空闲连接
27 | spring.redis.pool.min-idle=0
28 | ## 连接超时时间(毫秒)
29 | spring.redis.timeout=0
--------------------------------------------------------------------------------
/springboot-mybatis-redis/src/main/resources/mapper/CityMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | id, province_id, city_name, description
15 |
16 |
17 |
23 |
24 |
29 |
30 |
31 | insert into
32 | city(id,province_id,city_name,description)
33 | values
34 | (#{id},#{provinceId},#{cityName},#{description})
35 |
36 |
37 |
38 | update
39 | city
40 | set
41 |
42 | province_id = #{provinceId},
43 |
44 |
45 | city_name = #{cityName},
46 |
47 |
48 | description = #{description}
49 |
50 | where
51 | id = #{id}
52 |
53 |
54 |
55 | delete from
56 | city
57 | where
58 | id = #{id}
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/springboot-mybatis/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot
7 | springboot-mybatis
8 | 0.0.1-SNAPSHOT
9 | springboot-mybatis :: 整合 Mybatis Demo
10 |
11 |
12 |
13 | org.springframework.boot
14 | spring-boot-starter-parent
15 | 1.5.1.RELEASE
16 |
17 |
18 |
19 | 1.2.0
20 | 5.1.39
21 |
22 |
23 |
24 |
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-web
29 |
30 |
31 |
32 |
33 | org.springframework.boot
34 | spring-boot-starter-test
35 | test
36 |
37 |
38 |
39 |
40 | org.mybatis.spring.boot
41 | mybatis-spring-boot-starter
42 | ${mybatis-spring-boot}
43 |
44 |
45 |
46 |
47 | mysql
48 | mysql-connector-java
49 | ${mysql-connector}
50 |
51 |
52 |
53 |
54 | junit
55 | junit
56 | 4.12
57 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/springboot-mybatis/src/main/java/org/spring/springboot/Application.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot;
2 |
3 | import org.mybatis.spring.annotation.MapperScan;
4 | import org.spring.springboot.dao.CityDao;
5 | import org.spring.springboot.domain.City;
6 | import org.springframework.boot.CommandLineRunner;
7 | import org.springframework.boot.SpringApplication;
8 | import org.springframework.boot.autoconfigure.SpringBootApplication;
9 |
10 | /**
11 | * Spring Boot 应用启动类
12 | *
13 | * Created by bysocket on 16/4/26.
14 | */
15 | // Spring Boot 应用的标识
16 | @SpringBootApplication
17 | // mapper 接口类扫描包配置
18 | @MapperScan("org.spring.springboot.dao")
19 | public class Application {
20 |
21 | public static void main(String[] args) {
22 | // 程序启动入口
23 | // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
24 | SpringApplication.run(Application.class,args);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/springboot-mybatis/src/main/java/org/spring/springboot/controller/CityRestController.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.controller;
2 |
3 | import org.spring.springboot.domain.City;
4 | import org.spring.springboot.service.CityService;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.http.MediaType;
7 | import org.springframework.web.bind.annotation.RequestMapping;
8 | import org.springframework.web.bind.annotation.RequestMethod;
9 | import org.springframework.web.bind.annotation.RequestParam;
10 | import org.springframework.web.bind.annotation.RestController;
11 |
12 | /**
13 | * Created by bysocket on 07/02/2017.
14 | */
15 | @RestController
16 | public class CityRestController {
17 |
18 | @Autowired
19 | private CityService cityService;
20 |
21 | @RequestMapping(value = "/api/city", method = RequestMethod.GET)
22 | public City findOneCity(@RequestParam(value = "cityName", required = true) String cityName) {
23 | return cityService.findCityByName(cityName);
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/springboot-mybatis/src/main/java/org/spring/springboot/dao/CityDao.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.dao;
2 |
3 | import org.apache.ibatis.annotations.Param;
4 | import org.spring.springboot.domain.City;
5 |
6 | /**
7 | * 城市 DAO 接口类
8 | *
9 | * Created by bysocket on 07/02/2017.
10 | */
11 | public interface CityDao {
12 |
13 | /**
14 | * 根据城市名称,查询城市信息
15 | *
16 | * @param cityName 城市名
17 | */
18 | City findByName(@Param("cityName") String cityName);
19 | }
20 |
--------------------------------------------------------------------------------
/springboot-mybatis/src/main/java/org/spring/springboot/domain/City.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.domain;
2 |
3 | /**
4 | * 城市实体类
5 | *
6 | * Created by bysocket on 07/02/2017.
7 | */
8 | public class City {
9 |
10 | /**
11 | * 城市编号
12 | */
13 | private Long id;
14 |
15 | /**
16 | * 省份编号
17 | */
18 | private Long provinceId;
19 |
20 | /**
21 | * 城市名称
22 | */
23 | private String cityName;
24 |
25 | /**
26 | * 描述
27 | */
28 | private String description;
29 |
30 | public Long getId() {
31 | return id;
32 | }
33 |
34 | public void setId(Long id) {
35 | this.id = id;
36 | }
37 |
38 | public Long getProvinceId() {
39 | return provinceId;
40 | }
41 |
42 | public void setProvinceId(Long provinceId) {
43 | this.provinceId = provinceId;
44 | }
45 |
46 | public String getCityName() {
47 | return cityName;
48 | }
49 |
50 | public void setCityName(String cityName) {
51 | this.cityName = cityName;
52 | }
53 |
54 | public String getDescription() {
55 | return description;
56 | }
57 |
58 | public void setDescription(String description) {
59 | this.description = description;
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/springboot-mybatis/src/main/java/org/spring/springboot/service/CityService.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.service;
2 |
3 | import org.spring.springboot.domain.City;
4 |
5 | /**
6 | * 城市业务逻辑接口类
7 | *
8 | * Created by bysocket on 07/02/2017.
9 | */
10 | public interface CityService {
11 |
12 | /**
13 | * 根据城市名称,查询城市信息
14 | * @param cityName
15 | */
16 | City findCityByName(String cityName);
17 | }
18 |
--------------------------------------------------------------------------------
/springboot-mybatis/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.service.impl;
2 |
3 | import org.spring.springboot.dao.CityDao;
4 | import org.spring.springboot.domain.City;
5 | import org.spring.springboot.service.CityService;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.stereotype.Service;
8 |
9 | /**
10 | * 城市业务逻辑实现类
11 | *
12 | * Created by bysocket on 07/02/2017.
13 | */
14 | @Service
15 | public class CityServiceImpl implements CityService {
16 |
17 | @Autowired
18 | private CityDao cityDao;
19 |
20 | public City findCityByName(String cityName) {
21 | return cityDao.findByName(cityName);
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/springboot-mybatis/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | ## 数据源配置
2 | spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8
3 | spring.datasource.username=root
4 | spring.datasource.password=123456
5 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver
6 |
7 | ## Mybatis 配置
8 | mybatis.typeAliasesPackage=org.spring.springboot.domain
9 | mybatis.mapperLocations=classpath:mapper/*.xml
--------------------------------------------------------------------------------
/springboot-mybatis/src/main/resources/mapper/CityMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | id, province_id, city_name, description
15 |
16 |
17 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/springboot-properties/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot
7 | springboot-properties
8 | 0.0.1-SNAPSHOT
9 | springboot-properties :: Spring boot 配置文件
10 |
11 |
12 |
13 | org.springframework.boot
14 | spring-boot-starter-parent
15 | 1.5.1.RELEASE
16 |
17 |
18 |
19 |
20 |
21 | org.springframework.boot
22 | spring-boot-starter-web
23 |
24 |
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-test
29 | test
30 |
31 |
32 |
33 |
34 | junit
35 | junit
36 | 4.12
37 |
38 |
39 | org.springframework.boot
40 | spring-boot-test
41 | 1.4.2.RELEASE
42 | test
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/springboot-properties/src/main/java/org/spring/springboot/Application.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot;
2 |
3 | import org.spring.springboot.property.HomeProperties;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.boot.CommandLineRunner;
6 | import org.springframework.boot.SpringApplication;
7 | import org.springframework.boot.autoconfigure.SpringBootApplication;
8 |
9 | /**
10 | * Spring Boot 应用启动类
11 | *
12 | * Created by bysocket on 16/4/26.
13 | */
14 | // Spring Boot 应用的标识
15 | @SpringBootApplication
16 | public class Application implements CommandLineRunner {
17 |
18 | @Autowired
19 | private HomeProperties homeProperties;
20 |
21 | public static void main(String[] args) {
22 | // 程序启动入口
23 | // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
24 | SpringApplication.run(Application.class, args);
25 | }
26 |
27 | @Override
28 | public void run(String... args) throws Exception {
29 | System.out.println("\n" + homeProperties.toString());
30 | System.out.println();
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/springboot-properties/src/main/java/org/spring/springboot/property/HomeProperties.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.property;
2 |
3 | import org.springframework.beans.factory.annotation.Value;
4 | import org.springframework.boot.context.properties.ConfigurationProperties;
5 | import org.springframework.stereotype.Component;
6 |
7 | /**
8 | * 家乡属性
9 | *
10 | * Created by bysocket on 17/04/2017.
11 | */
12 | @Component
13 | @ConfigurationProperties(prefix = "home")
14 | public class HomeProperties {
15 |
16 | /**
17 | * 省份
18 | */
19 | private String province;
20 |
21 | /**
22 | * 城市
23 | */
24 | private String city;
25 |
26 | /**
27 | * 描述
28 | */
29 | private String desc;
30 |
31 | public String getProvince() {
32 | return province;
33 | }
34 |
35 | public void setProvince(String province) {
36 | this.province = province;
37 | }
38 |
39 | public String getCity() {
40 | return city;
41 | }
42 |
43 | public void setCity(String city) {
44 | this.city = city;
45 | }
46 |
47 | public String getDesc() {
48 | return desc;
49 | }
50 |
51 | public void setDesc(String desc) {
52 | this.desc = desc;
53 | }
54 |
55 | @Override
56 | public String toString() {
57 | return "HomeProperties{" +
58 | "province='" + province + '\'' +
59 | ", city='" + city + '\'' +
60 | ", desc='" + desc + '\'' +
61 | '}';
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/springboot-properties/src/main/java/org/spring/springboot/property/UserProperties.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.property;
2 |
3 | import org.springframework.boot.context.properties.ConfigurationProperties;
4 | import org.springframework.stereotype.Component;
5 |
6 | /**
7 | * Created by bysocket on 20/04/2017.
8 | */
9 | @Component
10 | @ConfigurationProperties(prefix = "user")
11 | public class UserProperties {
12 | /**
13 | * 用户 ID
14 | */
15 | private Long id;
16 |
17 | /**
18 | * 年龄
19 | */
20 | private int age;
21 |
22 | /**
23 | * 用户名称
24 | */
25 | private String desc;
26 |
27 | /**
28 | * 用户 UUID
29 | */
30 | private String uuid;
31 |
32 | public Long getId() {
33 | return id;
34 | }
35 |
36 | public void setId(Long id) {
37 | this.id = id;
38 | }
39 |
40 | public int getAge() {
41 | return age;
42 | }
43 |
44 | public void setAge(int age) {
45 | this.age = age;
46 | }
47 |
48 | public String getDesc() {
49 | return desc;
50 | }
51 |
52 | public void setDesc(String desc) {
53 | this.desc = desc;
54 | }
55 |
56 | public String getUuid() {
57 | return uuid;
58 | }
59 |
60 | public void setUuid(String uuid) {
61 | this.uuid = uuid;
62 | }
63 |
64 |
65 | @Override
66 | public String toString() {
67 | return "UserProperties{" +
68 | "id=" + id +
69 | ", age=" + age +
70 | ", desc='" + desc + '\'' +
71 | ", uuid='" + uuid + '\'' +
72 | '}';
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/springboot-properties/src/main/java/org/spring/springboot/web/HelloWorldController.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.web;
2 |
3 | import org.springframework.web.bind.annotation.RequestMapping;
4 | import org.springframework.web.bind.annotation.RestController;
5 |
6 | /**
7 | * Spring Boot HelloWorld 案例
8 | *
9 | * Created by bysocket on 16/4/26.
10 | */
11 | @RestController
12 | public class HelloWorldController {
13 |
14 | @RequestMapping("/")
15 | public String sayHello() {
16 | return "Hello,World!";
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/springboot-properties/src/main/resources/application-dev.properties:
--------------------------------------------------------------------------------
1 | ## 家乡属性 Dev
2 | home.province=ZheJiang
3 | home.city=WenLing
4 | home.desc=dev: I'm living in ${home.province} ${home.city}.
--------------------------------------------------------------------------------
/springboot-properties/src/main/resources/application-prod.properties:
--------------------------------------------------------------------------------
1 | ## 家乡属性 Prod
2 | home.province=ZheJiang
3 | home.city=WenLing
4 | home.desc=prod: I'm living in ${home.province} ${home.city}.
--------------------------------------------------------------------------------
/springboot-properties/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | # Spring Profiles Active
2 | spring.profiles.active=dev
--------------------------------------------------------------------------------
/springboot-properties/src/test/java/org/spring/springboot/property/HomeProperties1.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.property;
2 |
3 | import org.springframework.beans.factory.annotation.Value;
4 | import org.springframework.stereotype.Component;
5 |
6 | /**
7 | * 家乡属性
8 | *
9 | * Created by bysocket on 17/04/2017.
10 | */
11 | @Component
12 | public class HomeProperties1 {
13 |
14 | /**
15 | * 省份
16 | */
17 | @Value("${home.province}")
18 | private String province;
19 |
20 | /**
21 | * 城市
22 | */
23 | @Value("${home.city}")
24 | private String city;
25 |
26 | /**
27 | * 描述
28 | */
29 | @Value("${home.desc}")
30 | private String desc;
31 |
32 | public String getProvince() {
33 | return province;
34 | }
35 |
36 | public void setProvince(String province) {
37 | this.province = province;
38 | }
39 |
40 | public String getCity() {
41 | return city;
42 | }
43 |
44 | public void setCity(String city) {
45 | this.city = city;
46 | }
47 |
48 | public String getDesc() {
49 | return desc;
50 | }
51 |
52 | public void setDesc(String desc) {
53 | this.desc = desc;
54 | }
55 |
56 | @Override
57 | public String toString() {
58 | return "HomeProperties1{" +
59 | "province='" + province + '\'' +
60 | ", city='" + city + '\'' +
61 | ", desc='" + desc + '\'' +
62 | '}';
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/springboot-properties/src/test/java/org/spring/springboot/property/PropertiesTest.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.property;
2 |
3 | import org.junit.Assert;
4 | import org.junit.Test;
5 | import org.junit.runner.RunWith;
6 | import org.slf4j.Logger;
7 | import org.slf4j.LoggerFactory;
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.boot.test.context.SpringBootTest;
10 | import org.springframework.test.context.junit4.SpringRunner;
11 |
12 | /**
13 | * 自定义配置文件测试类
14 | *
15 | * Created by bysocket on 17/04/2017.
16 | */
17 | @RunWith(SpringRunner.class)
18 | @SpringBootTest
19 | public class PropertiesTest {
20 |
21 | private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesTest.class);
22 |
23 | @Autowired
24 | private UserProperties userProperties;
25 |
26 | @Autowired
27 | private HomeProperties homeProperties;
28 |
29 | @Test
30 | public void getHomeProperties() {
31 | LOGGER.info("\n\n" + homeProperties.toString() + "\n");
32 | }
33 |
34 | @Test
35 | public void randomTestUser() {
36 | LOGGER.info("\n\n" + userProperties.toString() + "\n");
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/springboot-properties/src/test/java/org/spring/springboot/web/HelloWorldControllerTest.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.web;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.assertEquals;
6 |
7 | /**
8 | * Spring Boot HelloWorldController 测试 - {@link HelloWorldController}
9 | *
10 | * Created by bysocket on 16/4/26.
11 | */
12 | public class HelloWorldControllerTest {
13 |
14 | @Test
15 | public void testSayHello() {
16 | assertEquals("Hello,World!",new HelloWorldController().sayHello());
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/springboot-properties/src/test/resouorces/application.yml:
--------------------------------------------------------------------------------
1 | ## 家乡属性
2 | home:
3 | province: 浙江省
4 | city: 温岭松门
5 | desc: 我家住在${home.province}的${home.city}
6 |
7 | ## 随机属性
8 | user:
9 | id: ${random.long}
10 | age: ${random.int[1,200]}
11 | desc: 泥瓦匠叫做${random.value}
12 | uuid: ${random.uuid}
13 |
--------------------------------------------------------------------------------
/springboot-restful/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot
7 | springboot-restful
8 | 0.0.1-SNAPSHOT
9 | springboot-restful :: Spsringboot 实现 Restful 服务,基于 HTTP / JSON 传输 Demo
10 |
11 |
12 |
13 | org.springframework.boot
14 | spring-boot-starter-parent
15 | 1.5.1.RELEASE
16 |
17 |
18 |
19 | 1.2.0
20 | 5.1.39
21 |
22 |
23 |
24 |
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-web
29 |
30 |
31 |
32 |
33 | org.springframework.boot
34 | spring-boot-starter-test
35 | test
36 |
37 |
38 |
39 |
40 | org.mybatis.spring.boot
41 | mybatis-spring-boot-starter
42 | ${mybatis-spring-boot}
43 |
44 |
45 |
46 |
47 | mysql
48 | mysql-connector-java
49 | ${mysql-connector}
50 |
51 |
52 |
53 |
54 | junit
55 | junit
56 | 4.12
57 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/springboot-restful/src/main/java/org/spring/springboot/Application.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot;
2 |
3 | import org.mybatis.spring.annotation.MapperScan;
4 | import org.spring.springboot.dao.CityDao;
5 | import org.spring.springboot.domain.City;
6 | import org.springframework.boot.CommandLineRunner;
7 | import org.springframework.boot.SpringApplication;
8 | import org.springframework.boot.autoconfigure.SpringBootApplication;
9 |
10 | /**
11 | * Spring Boot 应用启动类
12 | *
13 | * Created by bysocket on 16/4/26.
14 | */
15 | // Spring Boot 应用的标识
16 | @SpringBootApplication
17 | // mapper 接口类扫描包配置
18 | @MapperScan("org.spring.springboot.dao")
19 | public class Application {
20 |
21 | public static void main(String[] args) {
22 | // 程序启动入口
23 | // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
24 | SpringApplication.run(Application.class,args);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/springboot-restful/src/main/java/org/spring/springboot/controller/CityRestController.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.controller;
2 |
3 | import org.spring.springboot.domain.City;
4 | import org.spring.springboot.service.CityService;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.http.MediaType;
7 | import org.springframework.web.bind.annotation.*;
8 |
9 | import java.util.List;
10 |
11 | /**
12 | * 城市 Controller 实现 Restful HTTP 服务
13 | *
14 | * Created by bysocket on 07/02/2017.
15 | */
16 | @RestController
17 | public class CityRestController {
18 |
19 | @Autowired
20 | private CityService cityService;
21 |
22 | @RequestMapping(value = "/api/city/{id}", method = RequestMethod.GET)
23 | public City findOneCity(@PathVariable("id") Long id) {
24 | return cityService.findCityById(id);
25 | }
26 |
27 | @RequestMapping(value = "/api/city", method = RequestMethod.GET)
28 | public List findAllCity() {
29 | return cityService.findAllCity();
30 | }
31 |
32 | @RequestMapping(value = "/api/city", method = RequestMethod.POST)
33 | public void createCity(@RequestBody City city) {
34 | cityService.saveCity(city);
35 | }
36 |
37 | @RequestMapping(value = "/api/city", method = RequestMethod.PUT)
38 | public void modifyCity(@RequestBody City city) {
39 | cityService.updateCity(city);
40 | }
41 |
42 | @RequestMapping(value = "/api/city/{id}", method = RequestMethod.DELETE)
43 | public void modifyCity(@PathVariable("id") Long id) {
44 | cityService.deleteCity(id);
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/springboot-restful/src/main/java/org/spring/springboot/dao/CityDao.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.dao;
2 |
3 | import org.apache.ibatis.annotations.Param;
4 | import org.spring.springboot.domain.City;
5 |
6 | import java.util.List;
7 |
8 | /**
9 | * 城市 DAO 接口类
10 | *
11 | * Created by bysocket on 07/02/2017.
12 | */
13 | public interface CityDao {
14 |
15 | /**
16 | * 获取城市信息列表
17 | *
18 | * @return
19 | */
20 | List findAllCity();
21 |
22 | /**
23 | * 根据城市 ID,获取城市信息
24 | *
25 | * @param id
26 | * @return
27 | */
28 | City findById(@Param("id") Long id);
29 |
30 | Long saveCity(City city);
31 |
32 | Long updateCity(City city);
33 |
34 | Long deleteCity(Long id);
35 | }
36 |
--------------------------------------------------------------------------------
/springboot-restful/src/main/java/org/spring/springboot/domain/City.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.domain;
2 |
3 | /**
4 | * 城市实体类
5 | *
6 | * Created by bysocket on 07/02/2017.
7 | */
8 | public class City {
9 |
10 | /**
11 | * 城市编号
12 | */
13 | private Long id;
14 |
15 | /**
16 | * 省份编号
17 | */
18 | private Long provinceId;
19 |
20 | /**
21 | * 城市名称
22 | */
23 | private String cityName;
24 |
25 | /**
26 | * 描述
27 | */
28 | private String description;
29 |
30 | public Long getId() {
31 | return id;
32 | }
33 |
34 | public void setId(Long id) {
35 | this.id = id;
36 | }
37 |
38 | public Long getProvinceId() {
39 | return provinceId;
40 | }
41 |
42 | public void setProvinceId(Long provinceId) {
43 | this.provinceId = provinceId;
44 | }
45 |
46 | public String getCityName() {
47 | return cityName;
48 | }
49 |
50 | public void setCityName(String cityName) {
51 | this.cityName = cityName;
52 | }
53 |
54 | public String getDescription() {
55 | return description;
56 | }
57 |
58 | public void setDescription(String description) {
59 | this.description = description;
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/springboot-restful/src/main/java/org/spring/springboot/service/CityService.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.service;
2 |
3 | import org.spring.springboot.domain.City;
4 |
5 | import java.util.List;
6 |
7 | /**
8 | * 城市业务逻辑接口类
9 | *
10 | * Created by bysocket on 07/02/2017.
11 | */
12 | public interface CityService {
13 |
14 | /**
15 | * 获取城市信息列表
16 | *
17 | * @return
18 | */
19 | List findAllCity();
20 |
21 | /**
22 | * 根据城市 ID,查询城市信息
23 | *
24 | * @param id
25 | * @return
26 | */
27 | City findCityById(Long id);
28 |
29 | /**
30 | * 新增城市信息
31 | *
32 | * @param city
33 | * @return
34 | */
35 | Long saveCity(City city);
36 |
37 | /**
38 | * 更新城市信息
39 | *
40 | * @param city
41 | * @return
42 | */
43 | Long updateCity(City city);
44 |
45 | /**
46 | * 根据城市 ID,删除城市信息
47 | *
48 | * @param id
49 | * @return
50 | */
51 | Long deleteCity(Long id);
52 | }
53 |
--------------------------------------------------------------------------------
/springboot-restful/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.service.impl;
2 |
3 | import org.spring.springboot.dao.CityDao;
4 | import org.spring.springboot.domain.City;
5 | import org.spring.springboot.service.CityService;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.stereotype.Service;
8 |
9 | import java.util.List;
10 |
11 | /**
12 | * 城市业务逻辑实现类
13 | *
14 | * Created by bysocket on 07/02/2017.
15 | */
16 | @Service
17 | public class CityServiceImpl implements CityService {
18 |
19 | @Autowired
20 | private CityDao cityDao;
21 |
22 | public List findAllCity(){
23 | return cityDao.findAllCity();
24 | }
25 |
26 | public City findCityById(Long id) {
27 | return cityDao.findById(id);
28 | }
29 |
30 | @Override
31 | public Long saveCity(City city) {
32 | return cityDao.saveCity(city);
33 | }
34 |
35 | @Override
36 | public Long updateCity(City city) {
37 | return cityDao.updateCity(city);
38 | }
39 |
40 | @Override
41 | public Long deleteCity(Long id) {
42 | return cityDao.deleteCity(id);
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/springboot-restful/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | ## 数据源配置
2 | spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8
3 | spring.datasource.username=root
4 | spring.datasource.password=123456
5 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver
6 |
7 | ## Mybatis 配置
8 | mybatis.typeAliasesPackage=org.spring.springboot.domain
9 | mybatis.mapperLocations=classpath:mapper/*.xml
10 |
11 |
--------------------------------------------------------------------------------
/springboot-restful/src/main/resources/mapper/CityMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | id, province_id, city_name, description
15 |
16 |
17 |
23 |
24 |
29 |
30 |
31 | insert into
32 | city(id,province_id,city_name,description)
33 | values
34 | (#{id},#{provinceId},#{cityName},#{description})
35 |
36 |
37 |
38 | update
39 | city
40 | set
41 |
42 | province_id = #{provinceId},
43 |
44 |
45 | city_name = #{cityName},
46 |
47 |
48 | description = #{description}
49 |
50 | where
51 | id = #{id}
52 |
53 |
54 |
55 | delete from
56 | city
57 | where
58 | id = #{id}
59 |
60 |
61 |
--------------------------------------------------------------------------------
/springboot-validation-over-json/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot
7 | springboot-validation-over-json
8 | 0.0.1-SNAPSHOT
9 | springboot-validation-over-json :: Validation with http over json Demo
10 |
11 |
12 |
13 | org.springframework.boot
14 | spring-boot-starter-parent
15 | 1.5.1.RELEASE
16 |
17 |
18 |
19 |
20 |
21 | org.springframework.boot
22 | spring-boot-starter-web
23 |
24 |
25 |
26 |
27 | junit
28 | junit
29 | 4.12
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/springboot-validation-over-json/src/main/java/org/spring/springboot/Application.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * Spring Boot 应用启动类
8 | *
9 | * Created by bysocket on 16/4/26.
10 | */
11 | // Spring Boot 应用的标识
12 | @SpringBootApplication
13 | public class Application {
14 |
15 | public static void main(String[] args) {
16 | // 程序启动入口
17 | // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
18 | SpringApplication.run(Application.class,args);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/springboot-validation-over-json/src/main/java/org/spring/springboot/constant/CityErrorInfoEnum.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.constant;
2 |
3 | import org.spring.springboot.result.ErrorInfoInterface;
4 |
5 | /**
6 | * 业务错误码 案例
7 | *
8 | * Created by bysocket on 14/03/2017.
9 | */
10 | public enum CityErrorInfoEnum implements ErrorInfoInterface {
11 | PARAMS_NO_COMPLETE("000001","params no complete"),
12 | CITY_EXIT("000002","city exit");
13 |
14 | private String code;
15 |
16 | private String message;
17 |
18 | CityErrorInfoEnum(String code, String message) {
19 | this.code = code;
20 | this.message = message;
21 | }
22 |
23 | public String getCode(){
24 | return this.code;
25 | }
26 |
27 | public String getMessage(){
28 | return this.message;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/springboot-validation-over-json/src/main/java/org/spring/springboot/result/ErrorInfoInterface.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.result;
2 |
3 | /**
4 | * 错误码接口
5 | *
6 | * Created by bysocket on 13/03/2017.
7 | */
8 | public interface ErrorInfoInterface {
9 | String getCode();
10 | String getMessage();
11 | }
12 |
--------------------------------------------------------------------------------
/springboot-validation-over-json/src/main/java/org/spring/springboot/result/GlobalErrorInfoEnum.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.result;
2 |
3 | /**
4 | * 应用系统级别的错误码
5 | *
6 | * Created by bysocket on 14/03/2017.
7 | */
8 | public enum GlobalErrorInfoEnum implements ErrorInfoInterface{
9 | SUCCESS("0", "success"),
10 | NOT_FOUND("-1", "service not found");
11 |
12 | private String code;
13 |
14 | private String message;
15 |
16 | GlobalErrorInfoEnum(String code, String message) {
17 | this.code = code;
18 | this.message = message;
19 | }
20 |
21 | public String getCode(){
22 | return this.code;
23 | }
24 |
25 | public String getMessage(){
26 | return this.message;
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/springboot-validation-over-json/src/main/java/org/spring/springboot/result/GlobalErrorInfoException.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.result;
2 |
3 | /**
4 | * 统一错误码异常
5 | *
6 | * Created by bysocket on 14/03/2017.
7 | */
8 | public class GlobalErrorInfoException extends Exception {
9 |
10 | private ErrorInfoInterface errorInfo;
11 |
12 | public GlobalErrorInfoException (ErrorInfoInterface errorInfo) {
13 | this.errorInfo = errorInfo;
14 | }
15 |
16 | public ErrorInfoInterface getErrorInfo() {
17 | return errorInfo;
18 | }
19 |
20 | public void setErrorInfo(ErrorInfoInterface errorInfo) {
21 | this.errorInfo = errorInfo;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/springboot-validation-over-json/src/main/java/org/spring/springboot/result/GlobalErrorInfoHandler.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.result;
2 |
3 | import org.springframework.web.bind.annotation.ExceptionHandler;
4 | import org.springframework.web.bind.annotation.RestControllerAdvice;
5 |
6 | import javax.servlet.http.HttpServletRequest;
7 |
8 | /**
9 | * 统一错误码异常处理
10 | *
11 | * Created by bysocket on 14/03/2017.
12 | */
13 | @RestControllerAdvice
14 | public class GlobalErrorInfoHandler {
15 |
16 | @ExceptionHandler(value = GlobalErrorInfoException.class)
17 | public ResultBody errorHandlerOverJson(HttpServletRequest request,
18 | GlobalErrorInfoException exception) {
19 | ErrorInfoInterface errorInfo = exception.getErrorInfo();
20 | ResultBody result = new ResultBody(errorInfo);
21 | return result;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/springboot-validation-over-json/src/main/java/org/spring/springboot/result/ResultBody.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.result;
2 |
3 | /**
4 | * 返回体
5 | *
6 | * Created by bysocket on 14/03/2017.
7 | */
8 | public class ResultBody {
9 | /**
10 | * 响应代码
11 | */
12 | private String code;
13 |
14 | /**
15 | * 响应消息
16 | */
17 | private String message;
18 |
19 | /**
20 | * 响应结果
21 | */
22 | private Object result;
23 |
24 | public ResultBody(ErrorInfoInterface errorInfo) {
25 | this.code = errorInfo.getCode();
26 | this.message = errorInfo.getMessage();
27 | }
28 |
29 | public ResultBody(Object result) {
30 | this.code = GlobalErrorInfoEnum.SUCCESS.getCode();
31 | this.message = GlobalErrorInfoEnum.SUCCESS.getMessage();
32 | this.result = result;
33 | }
34 |
35 | public String getCode() {
36 | return code;
37 | }
38 |
39 | public void setCode(String code) {
40 | this.code = code;
41 | }
42 |
43 | public String getMessage() {
44 | return message;
45 | }
46 |
47 | public void setMessage(String message) {
48 | this.message = message;
49 | }
50 |
51 | public Object getResult() {
52 | return result;
53 | }
54 |
55 | public void setResult(Object result) {
56 | this.result = result;
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/springboot-validation-over-json/src/main/java/org/spring/springboot/web/City.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.web;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * 城市实体类
7 | *
8 | * Created by bysocket on 07/02/2017.
9 | */
10 | public class City implements Serializable {
11 |
12 | private static final long serialVersionUID = -1L;
13 |
14 | /**
15 | * 城市编号
16 | */
17 | private Long id;
18 |
19 | /**
20 | * 省份编号
21 | */
22 | private Long provinceId;
23 |
24 | /**
25 | * 城市名称
26 | */
27 | private String cityName;
28 |
29 | /**
30 | * 描述
31 | */
32 | private String description;
33 |
34 | public City() {
35 | }
36 |
37 | public City(Long id, Long provinceId, String cityName, String description) {
38 | this.id = id;
39 | this.provinceId = provinceId;
40 | this.cityName = cityName;
41 | this.description = description;
42 | }
43 |
44 | public Long getId() {
45 | return id;
46 | }
47 |
48 | public void setId(Long id) {
49 | this.id = id;
50 | }
51 |
52 | public Long getProvinceId() {
53 | return provinceId;
54 | }
55 |
56 | public void setProvinceId(Long provinceId) {
57 | this.provinceId = provinceId;
58 | }
59 |
60 | public String getCityName() {
61 | return cityName;
62 | }
63 |
64 | public void setCityName(String cityName) {
65 | this.cityName = cityName;
66 | }
67 |
68 | public String getDescription() {
69 | return description;
70 | }
71 |
72 | public void setDescription(String description) {
73 | this.description = description;
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/springboot-validation-over-json/src/main/java/org/spring/springboot/web/ErrorJsonController.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.web;
2 |
3 | import org.spring.springboot.constant.CityErrorInfoEnum;
4 | import org.spring.springboot.result.GlobalErrorInfoException;
5 | import org.spring.springboot.result.ResultBody;
6 | import org.springframework.util.StringUtils;
7 | import org.springframework.web.bind.annotation.RequestMapping;
8 | import org.springframework.web.bind.annotation.RequestMethod;
9 | import org.springframework.web.bind.annotation.RequestParam;
10 | import org.springframework.web.bind.annotation.RestController;
11 |
12 | /**
13 | * 错误码案例
14 | *
15 | * Created by bysocket on 16/4/26.
16 | */
17 | @RestController
18 | public class ErrorJsonController {
19 |
20 |
21 | /**
22 | * 获取城市接口
23 | *
24 | * @param cityName
25 | * @return
26 | * @throws GlobalErrorInfoException
27 | */
28 | @RequestMapping(value = "/api/city", method = RequestMethod.GET)
29 | public ResultBody findOneCity(@RequestParam("cityName") String cityName) throws GlobalErrorInfoException {
30 | // 入参为空
31 | if (StringUtils.isEmpty(cityName)) {
32 | throw new GlobalErrorInfoException(CityErrorInfoEnum.PARAMS_NO_COMPLETE);
33 | }
34 | return new ResultBody(new City(1L,2L,"温岭","是我的故乡"));
35 | }
36 | }
--------------------------------------------------------------------------------
/springboot-validation-over-json/src/test/java/org/spring/springboot/web/ErrorJsonControllerTest.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.web;
2 |
3 | import org.junit.Test;
4 |
5 | /**
6 | * Spring Boot ErrorJsonController 测试 - {@link ErrorJsonController}
7 | *
8 | * Created by bysocket on 16/4/26.
9 | */
10 | public class ErrorJsonControllerTest {
11 |
12 | @Test
13 | public void testSayHello() {
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/springboot-webflux/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot
7 | springboot-webflux
8 | 0.0.1-SNAPSHOT
9 | springboot-webflux :: Spring Boot 实现 WebFlux HTTP Restful 服务
10 |
11 |
12 |
13 | org.springframework.boot
14 | spring-boot-starter-parent
15 | 2.0.0.M3
16 |
17 |
18 |
19 | 1.2.0
20 | 5.1.39
21 |
22 |
23 |
24 |
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-webflux
29 |
30 |
31 |
32 |
33 | org.springframework.boot
34 | spring-boot-starter-test
35 | test
36 |
37 |
38 |
39 |
40 | junit
41 | junit
42 | 4.12
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 | spring-milestones
51 | Spring Milestones
52 | https://repo.spring.io/libs-milestone
53 |
54 | false
55 |
56 |
57 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/springboot-webflux/src/main/java/org/spring/springboot/Application.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * Spring Boot 应用启动类
8 | *
9 | * Created by bysocket on 09/29/2017.
10 | */
11 | // Spring Boot 应用的标识
12 | @SpringBootApplication
13 | public class Application {
14 |
15 | public static void main(String[] args) {
16 | // 程序启动入口
17 | // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
18 | SpringApplication.run(Application.class,args);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/springboot-webflux/src/main/java/org/spring/springboot/controller/CityRestController.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.controller;
2 |
3 | import org.spring.springboot.domain.City;
4 | import org.spring.springboot.service.CityService;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.web.bind.annotation.*;
7 | import reactor.core.publisher.Flux;
8 | import reactor.core.publisher.Mono;
9 |
10 | /**
11 | * 城市 Controller 实现 Restful HTTP 服务
12 | *
13 | * Created by bysocket on 09/29/2017.
14 | */
15 | @RestController
16 | @RequestMapping(value = "/city")
17 | public class CityRestController {
18 |
19 | @Autowired
20 | private CityService cityService;
21 |
22 | @RequestMapping(value = "/{id}", method = RequestMethod.GET)
23 | public Mono findOneCity(@PathVariable("id") Long id) {
24 | return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.findCityById(id)));
25 | }
26 |
27 | @RequestMapping(method = RequestMethod.GET)
28 | public Flux findAllCity() {
29 | return Flux.create(cityFluxSink -> {
30 | cityService.findAllCity().forEach(city -> {
31 | cityFluxSink.next(city);
32 | });
33 | cityFluxSink.complete();
34 | });
35 | }
36 |
37 | @RequestMapping(method = RequestMethod.POST)
38 | public Mono createCity(@RequestBody City city) {
39 | return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.saveCity(city)));
40 | }
41 |
42 | @RequestMapping(method = RequestMethod.PUT)
43 | public Mono modifyCity(@RequestBody City city) {
44 | return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.updateCity(city)));
45 | }
46 |
47 | @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
48 | public Mono modifyCity(@PathVariable("id") Long id) {
49 | return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.deleteCity(id)));
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/springboot-webflux/src/main/java/org/spring/springboot/domain/City.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.domain;
2 |
3 | /**
4 | * 城市实体类
5 | *
6 | * Created by bysocket on 09/29/2017.
7 | */
8 | public class City {
9 |
10 | /**
11 | * 城市编号
12 | */
13 | private Long id;
14 |
15 | /**
16 | * 省份编号
17 | */
18 | private Long provinceId;
19 |
20 | /**
21 | * 城市名称
22 | */
23 | private String cityName;
24 |
25 | /**
26 | * 描述
27 | */
28 | private String description;
29 |
30 | public Long getId() {
31 | return id;
32 | }
33 |
34 | public void setId(Long id) {
35 | this.id = id;
36 | }
37 |
38 | public Long getProvinceId() {
39 | return provinceId;
40 | }
41 |
42 | public void setProvinceId(Long provinceId) {
43 | this.provinceId = provinceId;
44 | }
45 |
46 | public String getCityName() {
47 | return cityName;
48 | }
49 |
50 | public void setCityName(String cityName) {
51 | this.cityName = cityName;
52 | }
53 |
54 | public String getDescription() {
55 | return description;
56 | }
57 |
58 | public void setDescription(String description) {
59 | this.description = description;
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/springboot-webflux/src/main/java/org/spring/springboot/service/CityService.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.service;
2 |
3 | import org.spring.springboot.domain.City;
4 |
5 | import java.util.List;
6 |
7 | /**
8 | * 城市业务逻辑接口类
9 | *
10 | * Created by bysocket on 09/29/2017.
11 | */
12 | public interface CityService {
13 |
14 | /**
15 | * 获取城市信息列表
16 | *
17 | * @return
18 | */
19 | List findAllCity();
20 |
21 | /**
22 | * 根据城市 ID,查询城市信息
23 | *
24 | * @param id
25 | * @return
26 | */
27 | City findCityById(Long id);
28 |
29 | /**
30 | * 新增城市信息
31 | *
32 | * @param city
33 | * @return
34 | */
35 | Long saveCity(City city);
36 |
37 | /**
38 | * 更新城市信息
39 | *
40 | * @param city
41 | * @return
42 | */
43 | Long updateCity(City city);
44 |
45 | /**
46 | * 根据城市 ID,删除城市信息
47 | *
48 | * @param id
49 | * @return
50 | */
51 | Long deleteCity(Long id);
52 | }
53 |
--------------------------------------------------------------------------------
/springboot-webflux/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java:
--------------------------------------------------------------------------------
1 | package org.spring.springboot.service.impl;
2 |
3 | import org.spring.springboot.domain.City;
4 | import org.spring.springboot.service.CityService;
5 | import org.springframework.stereotype.Service;
6 |
7 | import java.util.ArrayList;
8 | import java.util.HashMap;
9 | import java.util.List;
10 | import java.util.Map;
11 |
12 | /**
13 | * 城市业务逻辑实现类
14 | *
15 | * Created by bysocket on 09/29/2017.
16 | */
17 | @Service
18 | public class CityServiceImpl implements CityService {
19 |
20 | // 模拟数据库,存储 City 信息
21 | private static Map CITY_DB = new HashMap<>();
22 |
23 | public List findAllCity() {
24 | return new ArrayList<>(CITY_DB.values());
25 | }
26 |
27 | public City findCityById(Long id) {
28 | return CITY_DB.get(id);
29 | }
30 |
31 | @Override
32 | public Long saveCity(City city) {
33 | city.setId(CITY_DB.size() + 1L);
34 | CITY_DB.put(city.getId(), city);
35 | return city.getId();
36 | }
37 |
38 | @Override
39 | public Long updateCity(City city) {
40 | CITY_DB.put(city.getId(), city);
41 | return city.getId();
42 | }
43 |
44 | @Override
45 | public Long deleteCity(Long id) {
46 | CITY_DB.remove(id);
47 | return id;
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/springboot-webflux/src/main/resources/application.properties:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpringForAll/springboot-learning-example/6446896ef2f858b7816f9b0c07c7019b42d0cb5c/springboot-webflux/src/main/resources/application.properties
--------------------------------------------------------------------------------