├── .gitignore
├── LICENSE
├── README.md
├── algorithm
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── wuwii
│ └── leetcode
│ ├── AddTwoNumbers.java
│ ├── LinkedListSummary.java
│ └── TwoSum.java
├── base-point
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── wuwii
│ └── regular
│ ├── Regular01.java
│ ├── Regular02.java
│ ├── Regular03.java
│ ├── Regular04.java
│ └── package-info.java
├── design-pattern
├── README.md
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── wuwii
│ ├── factory
│ ├── CoderFactory.java
│ ├── CoderJava.java
│ ├── CoderPhp.java
│ ├── CoderPython.java
│ └── Coding.java
│ ├── observer
│ ├── BuyObserver.java
│ ├── CouponOberver.java
│ ├── Observer.java
│ ├── ObserverMain.java
│ ├── ProductObservable.java
│ └── RecommendOberver.java
│ ├── proxy
│ ├── HouseOwner.java
│ ├── HouseProxy.java
│ └── RentHouse.java
│ ├── singleton
│ ├── SingletonDemo1.java
│ ├── SingletonDemo2.java
│ ├── SingletonDemo3.java
│ ├── SingletonDemo4.java
│ └── SingletonDemo5.java
│ └── strategy
│ ├── Bike.java
│ ├── StrategyContext.java
│ ├── StrategyDemo.java
│ ├── Subway.java
│ ├── Taxi.java
│ └── Vehicle.java
├── dynamic-schedule
├── .gitignore
├── .mvn
│ └── wrapper
│ │ ├── maven-wrapper.jar
│ │ └── maven-wrapper.properties
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── wuwii
│ │ │ └── DynamicScheduleApplication.java
│ └── resources
│ │ └── application.properties
│ └── test
│ └── java
│ └── com
│ └── wuwii
│ └── DynamicScheduleApplicationTests.java
├── fork-join
├── README.md
├── pom.xml
└── src
│ └── main
│ └── java
│ ├── PrintTask.java
│ └── SumTask.java
├── html5-butterflies
├── .gitignore
├── README.md
├── config.rb
├── css
│ ├── clouds_back.jpg
│ ├── clouds_front.jpg
│ ├── clouds_left.jpg
│ ├── clouds_right.jpg
│ ├── clouds_top.jpg
│ ├── hazy_lake_top.jpg
│ ├── main.css
│ ├── reset.css
│ └── style.css
├── index.html
├── index1.html
├── js
│ └── index.js
├── main.css
└── scss
│ └── style.scss
├── interview-code
├── pom.xml
└── src
│ ├── main
│ └── java
│ │ └── com
│ │ └── wuwii
│ │ ├── cas
│ │ ├── CasDemo.java
│ │ ├── Mutex.java
│ │ └── TwinsLock.java
│ │ ├── countdownlatch
│ │ └── CountDownLatchDemo.java
│ │ ├── cyclicbarrier
│ │ └── CyclicBarrierDemo.java
│ │ └── semaphore
│ │ └── SemaphoreDemo.java
│ └── test
│ └── java
│ └── com
│ └── wuwii
│ └── cas
│ ├── MutexTest.java
│ └── TwinsLockTest.java
├── java8-function-demo
├── README.md
├── pom.xml
└── src
│ └── main
│ └── java
│ └── Demo.java
├── jdbc-muti-datasource
├── .gitignore
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── wuwii
│ │ │ ├── JdbcMutiDatasourceApplication.java
│ │ │ └── config
│ │ │ └── DataSourceConfig.java
│ └── resources
│ │ └── application.yml
│ └── test
│ └── java
│ └── com
│ └── wuwii
│ ├── JdbcMutiDatasourceApplicationTests.java
│ └── TestJDBC.java
├── jpa-criteria
├── .gitignore
├── .mvn
│ └── wrapper
│ │ ├── maven-wrapper.jar
│ │ └── maven-wrapper.properties
├── README.md
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── wuwii
│ │ │ ├── JpaCriteriaApplication.java
│ │ │ ├── controller
│ │ │ └── EmployeeController.java
│ │ │ ├── dao
│ │ │ ├── EmployeeDao.java
│ │ │ └── JobDao.java
│ │ │ ├── entity
│ │ │ ├── Employee.java
│ │ │ ├── EmployeeDetail.java
│ │ │ ├── EmployeeDetail_.java
│ │ │ ├── Employee_.java
│ │ │ ├── Job.java
│ │ │ └── Job_.java
│ │ │ ├── form
│ │ │ ├── EmployeeResult.java
│ │ │ └── EmployeeSearch.java
│ │ │ └── service
│ │ │ ├── EmployeeService.java
│ │ │ ├── HelloService.java
│ │ │ └── impl
│ │ │ ├── EmployeeServiceImpl.java
│ │ │ └── HelloServiceImpl.java
│ └── resources
│ │ ├── application.yml
│ │ └── learn.sql
│ └── test
│ └── java
│ └── com
│ └── wuwii
│ ├── JpaCriteriaApplicationTests.java
│ ├── controller
│ ├── EmployeeController1Test.java
│ ├── EmployeeController2Test.java
│ └── EmployeeControllerTest.java
│ ├── dao
│ └── EmployeeDaoTest.java
│ ├── paramter
│ └── ParameterTest.java
│ ├── service
│ └── impl
│ │ ├── EmployeeServiceImplTest.java
│ │ └── HelloServiceImplTest.java
│ └── suit
│ ├── SuitTest.java
│ ├── TaskOneTest.java
│ ├── TaskThreeTest.java
│ └── TaskTwoTest.java
├── jpa-muti-datasource
├── .gitignore
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── wuwii
│ │ │ ├── JpaMutiDatasourceApplication.java
│ │ │ └── module
│ │ │ ├── system
│ │ │ ├── EmployeeDao.java
│ │ │ ├── config
│ │ │ │ └── FirstDataSourceConfig.java
│ │ │ ├── dao
│ │ │ │ └── EmployeeDao.java
│ │ │ └── entity
│ │ │ │ └── Employee.java
│ │ │ └── user
│ │ │ ├── config
│ │ │ ├── SecondDataSource.java
│ │ │ └── SecondDataSourceConfig.java
│ │ │ ├── dao
│ │ │ └── UserDao.java
│ │ │ └── entity
│ │ │ └── User.java
│ └── resources
│ │ └── application.yml
│ └── test
│ └── java
│ └── com
│ └── wuwii
│ ├── JpaMutiDatasourceApplicationTests.java
│ └── TestDemo.java
├── learn-hystrix
├── README.md
├── pom.xml
└── src
│ ├── main
│ └── java
│ │ ├── HelloWorldHystrixCommand.java
│ │ └── HelloWorldHystrixObservableCommand.java
│ └── resources
│ └── log4j.properties
├── nio-netty
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── wuwii
│ ├── netty
│ ├── chat
│ │ ├── ChatClient.java
│ │ ├── ChatClientHandler.java
│ │ ├── ChatServer.java
│ │ ├── ChatServerHandler.java
│ │ ├── ChatServerInitializer.java
│ │ └── package-info.java
│ ├── core
│ │ ├── BaseClientTemplate.java
│ │ └── BaseServerTemplate.java
│ ├── discard
│ │ ├── DisCardServerHandler.java
│ │ ├── DiscardServer.java
│ │ └── package-info.java
│ ├── echo
│ │ ├── EchoServer.java
│ │ ├── EchoServerHandler.java
│ │ └── package-info.java
│ ├── pojo
│ │ ├── PojoClient.java
│ │ ├── PojoClientHandler.java
│ │ ├── PojoServer.java
│ │ ├── PojoServerHandler.java
│ │ ├── TimeDecoder.java
│ │ ├── TimeEncoder.java
│ │ ├── UnixTime.java
│ │ └── package-info.java
│ └── time
│ │ ├── TimeClient.java
│ │ ├── TimeClientHandler.java
│ │ ├── TimeServer.java
│ │ ├── TimeServerHandler.java
│ │ └── package-info.java
│ └── nio1
│ ├── Client.java
│ └── Server.java
├── pom.xml
├── rocketmq-demo
├── .gitignore
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── wuwii
│ │ │ ├── Consumer.java
│ │ │ ├── MQConsumeMsgListenerProcessor.java
│ │ │ ├── Productor.java
│ │ │ └── RocketmqDemoApplication.java
│ └── resources
│ │ └── application.yaml
│ └── test
│ └── java
│ └── com
│ └── wuwii
│ └── RocketmqDemoApplicationTests.java
├── sort-algorithm
├── .gitignore
├── README.md
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── sort
│ │ │ └── LearnSort.java
│ └── resources
│ │ └── log4j.properties
│ └── test
│ └── java
│ └── sort
│ └── LearnSortTest.java
├── spring-boot-test
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── wuwii
│ │ │ ├── Application.java
│ │ │ ├── controller
│ │ │ └── UserController.java
│ │ │ ├── model
│ │ │ └── User.java
│ │ │ ├── repository
│ │ │ └── IUserRepository.java
│ │ │ └── service
│ │ │ ├── IUserService.java
│ │ │ └── impl
│ │ │ └── UserServiceImpl.java
│ └── resources
│ │ └── application.yml
│ └── test
│ ├── java
│ └── com
│ │ └── wuwii
│ │ └── service
│ │ ├── IUserServiceTest.java
│ │ └── SpringDataBaseTest.java
│ └── resources
│ ├── application-h2.yml
│ ├── application-mysql.yml
│ ├── application.yml
│ ├── data.sql
│ └── drop.sql
├── spring-event
├── pom.xml
└── src
│ ├── main
│ └── java
│ │ └── com
│ │ └── wuwii
│ │ ├── PropertiesChangeEvent.java
│ │ ├── PropertiesChangeListener.java
│ │ ├── PropertiesChangePublisher.java
│ │ └── SpringEventApplication.java
│ └── test
│ └── java
│ └── com
│ └── wuwii
│ └── PropertiesChangePublisherTest.java
├── springboot-actuator
├── .gitignore
├── README.md
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── wuwii
│ │ │ └── SpringbootActuatorApplication.java
│ └── resources
│ │ └── application.yml
│ └── test
│ └── java
│ └── com
│ └── wuwii
│ └── SpringbootActuatorApplicationTests.java
├── springboot-docker
├── README.md
├── docker-compose-swarm.yml
├── docker-compose.yml
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
│ ├── docker
│ └── Dockerfile
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── wuwii
│ │ │ ├── SpringbootDockerApplication.java
│ │ │ ├── dao
│ │ │ └── UserDao.java
│ │ │ └── entity
│ │ │ └── User.java
│ └── resources
│ │ └── application.yaml
│ └── test
│ └── java
│ └── com
│ └── wuwii
│ └── SpringbootDockerApplicationTests.java
├── springboot-querydsl
├── .gitignore
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── wuwii
│ │ │ ├── SpringbootQuerydslApplication.java
│ │ │ ├── config
│ │ │ └── JPAQueryConfig.java
│ │ │ ├── dao
│ │ │ └── EmployeeDao.java
│ │ │ ├── entity
│ │ │ ├── Employee.java
│ │ │ ├── EmployeeDetail.java
│ │ │ ├── Job.java
│ │ │ ├── QEmployee.java
│ │ │ ├── QEmployeeDetail.java
│ │ │ └── QJob.java
│ │ │ └── service1
│ │ │ ├── EmployeeService1.java
│ │ │ ├── impl
│ │ │ └── EmployeeServiceImpl1.java
│ │ │ └── package-info.java
│ └── resources
│ │ └── application.yml
│ └── test
│ └── java
│ └── com
│ └── wuwii
│ └── SpringbootQuerydslApplicationTests.java
├── springboot-redis-cache
├── .gitignore
├── README.md
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── wuwii
│ │ │ ├── SpringbootRedisCacheApplication.java
│ │ │ ├── controller.java
│ │ │ ├── dao
│ │ │ └── EmployeeDao.java
│ │ │ ├── entity
│ │ │ └── Employee.java
│ │ │ └── service
│ │ │ ├── EmployeeService.java
│ │ │ └── impl
│ │ │ └── EmployeeServiceImpl.java
│ └── resources
│ │ └── application.yml
│ └── test
│ └── java
│ └── com
│ └── wuwii
│ ├── SpringbootRedisCacheApplicationTests.java
│ └── service
│ └── impl
│ └── EmployeeServiceImplTest.java
├── springboot-security
├── .gitignore
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── wuwii
│ │ │ ├── SpringbootSecurityApplication.java
│ │ │ ├── config
│ │ │ ├── Swagger2Config.java
│ │ │ └── security
│ │ │ │ ├── SecurityModelFactory.java
│ │ │ │ ├── UserDetailServiceImpl.java
│ │ │ │ ├── UserDetailsImpl.java
│ │ │ │ ├── v1
│ │ │ │ ├── CustomAuthenticationProvider.java
│ │ │ │ ├── JwtAuthenticationFilter.java
│ │ │ │ ├── LoginFilter.java
│ │ │ │ └── WebSecurityConfig.java
│ │ │ │ └── v2
│ │ │ │ ├── AuthenticationFilter.java
│ │ │ │ ├── WebSecurityConfig.java
│ │ │ │ └── authenticate
│ │ │ │ ├── LoginAuthenticationProvider.java
│ │ │ │ └── TokenAuthenticateProvider.java
│ │ │ ├── controller
│ │ │ └── UserController.java
│ │ │ ├── dao
│ │ │ ├── RoleDao.java
│ │ │ ├── UserDao.java
│ │ │ └── UserRoleDao.java
│ │ │ ├── entity
│ │ │ ├── Role.java
│ │ │ ├── User.java
│ │ │ └── UserRole.java
│ │ │ ├── excetion
│ │ │ ├── ExceptionHandler.java
│ │ │ └── KCException.java
│ │ │ ├── service
│ │ │ ├── UserService.java
│ │ │ └── impl
│ │ │ │ └── UserServiceImpl.java
│ │ │ ├── util
│ │ │ └── JwtUtil.java
│ │ │ └── vo
│ │ │ ├── UserAddDTO.java
│ │ │ └── UserVO.java
│ └── resources
│ │ ├── application.yml
│ │ ├── config
│ │ └── ehcache.xml
│ │ └── static
│ │ ├── index.html
│ │ ├── login.html
│ │ └── logout.html
│ └── test
│ └── java
│ └── com
│ └── wuwii
│ ├── SpringbootSecurityApplicationTests.java
│ └── password
│ └── BCryptPasswordEncoderTest.java
└── template-design-spring
├── README.md
├── pom.xml
└── src
└── main
└── java
└── com
└── wuwii
├── Application.java
└── service
├── AbstractPay.java
├── AliPay.java
├── Pay.java
└── WeChatPay.java
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 |
13 | # Package Files #
14 | *.jar
15 | *.war
16 | *.ear
17 | *.zip
18 | *.tar.gz
19 | *.rar
20 |
21 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
22 | hs_err_pid*
23 |
24 | target/
25 | !.mvn/wrapper/maven-wrapper.jar
26 |
27 | ### STS ###
28 | .apt_generated
29 | .classpath
30 | .factorypath
31 | .project
32 | .settings
33 | .springBeans
34 |
35 | ### IntelliJ IDEA ###
36 | .idea
37 | *.iws
38 | *.iml
39 | *.ipr
40 | */.mvn/
41 |
42 | ### NetBeans ###
43 | nbproject/private/
44 | build/
45 | nbbuild/
46 | dist/
47 | nbdist/
48 | .nb-gradle/
49 | /.mvn/
50 |
51 | **/target
52 | **.iml
53 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # learning-code
2 | Coding for studying
3 | 平常的一些代码的积累
4 |
5 | * design-pattern: 设计模式
6 | * fork-join: fork-join模型
7 | * jdbc-muti-datasource: springboot 中使用jdbc多数据源
8 | * jpa-criteria:Spring Boot 中使用 JPA 完成复杂查询
9 | * jpa-muti-datasource:Spring Boot 中 JPA 多数据源(无分布式事务)
10 | * rxjava
11 | * sort-algorithm:八种基本排序
12 | * springboot-actuator:spring-boot 2.0 中使用 actuator
13 | * springboot-redis-cache:Spring Boot 中使用 redis 完成集中缓存
14 |
--------------------------------------------------------------------------------
/algorithm/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 | * medium 7 | *
8 | * You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. 9 | *
10 | * You may assume the two numbers do not contain any leading zero, except the number 0 itself. 11 | *
12 | * Example 13 | *
14 | * Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) 15 | * Output: 7 -> 0 -> 8 16 | * Explanation: 342 + 465 = 807. 17 | *
18 | * Definition for singly-linked list. 19 | * public class ListNode { 20 | * int val; 21 | * ListNode next; 22 | * ListNode(int x) { val = x; } 23 | * } 24 | * 25 | * @author KronChan 26 | * @version 1.0 27 | * @since
2017/12/21 11:4828 | */ 29 | public class AddTwoNumbers { 30 | class ListNode { 31 | public ListNode next; 32 | public int val; 33 | 34 | public ListNode(int x) { 35 | val = x; 36 | } 37 | } 38 | 39 | public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 40 | ListNode result = new ListNode(0); 41 | // 进位 42 | int hamal = 0; 43 | ListNode middleware = result; 44 | while (l1 != null && l2 != null) { 45 | int sum = l1.val + l2.val + hamal; 46 | hamal = sum / 10; 47 | middleware.next = new ListNode(sum % 10); 48 | middleware = middleware.next; 49 | l1 = l1.next; 50 | l2 = l2.next; 51 | } 52 | return result.next; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /algorithm/src/main/java/com/wuwii/leetcode/LinkedListSummary.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.leetcode; 2 | 3 | /** 4 | * http://blog.csdn.net/kerryfish/article/details/24043099 5 | * https://www.cnblogs.com/winorgohome/p/6028309.html 6 | * 7 | * @author KronChan 8 | * @version 1.0 9 | * @since
2017/12/22 21:4810 | */ 11 | public class LinkedListSummary { 12 | 13 | /** 14 | * 获取链表存储元素的数量 15 | * 16 | * @param node 链表 17 | * @return int 18 | */ 19 | public static int size(Node node) { 20 | int i = 0; 21 | for (; node != null; ) { 22 | i++; 23 | node = node.next; 24 | } 25 | return i; 26 | } 27 | 28 | public static Node reversal(Node node) { 29 | Node indirection = node; 30 | for (; indirection != null; ) { 31 | 32 | } 33 | return null; 34 | } 35 | 36 | class Node { 37 | int val; 38 | Node next; 39 | 40 | Node(int val) { 41 | this.val = val; 42 | } 43 | } 44 | 45 | public static void main(String[] args) { 46 | 47 | } 48 | 49 | 50 | } 51 | -------------------------------------------------------------------------------- /algorithm/src/main/java/com/wuwii/leetcode/TwoSum.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/two-sum/description/ 5 | *
6 | * Easy 7 | *
8 | * Given an array of integers, return indices of the two numbers such that they add up to a specific target. 9 | * You may assume that each input would have exactly one solution, and you may not use the same element twice. 10 | *
11 | * Example: 12 | * Given nums = [2, 7, 11, 15], target = 9, 13 | *
14 | * Because nums[0] + nums[1] = 2 + 7 = 9, 15 | * return [0, 1]. 16 | *
17 | * My thoughts: 18 | * 19 | * @author KronChan 20 | * @version 1.0 21 | * @since
2017/12/20 22:4922 | */ 23 | public class TwoSum { 24 | 25 | public int[] twoSum(int[] nums, int target) { 26 | // complexity O (n^2) 27 | for (int i = 0, len = nums.length; i < len; i++) { 28 | for (int j = i + 1; j < len; j++) { 29 | if (nums[i] + nums[j] == target) { 30 | return new int[]{i, j}; 31 | } 32 | } 33 | } 34 | throw new IllegalArgumentException(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /base-point/pom.xml: -------------------------------------------------------------------------------- 1 | 2 |
2018/3/21 18:5010 | */ 11 | public class CoderFactory { 12 | public static Object getCoder(@NotNull Class extends Coding> clazz) { 13 | Object object = null; 14 | try { 15 | object = Class.forName(clazz.getName()).newInstance(); 16 | } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { 17 | e.printStackTrace(); 18 | } 19 | return object; 20 | } 21 | 22 | public static void main(String[] args) { 23 | Coding java = (CoderJava) CoderFactory.getCoder(CoderJava.class); 24 | java.code(); 25 | Coding python = (CoderPython) CoderFactory.getCoder(CoderPython.class); 26 | python.code(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /design-pattern/src/main/java/com/wuwii/factory/CoderJava.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.factory; 2 | 3 | /** 4 | * @author Zhang Kai 5 | * @version 1.0 6 | * @since
2018/3/21 18:567 | */ 8 | public class CoderJava implements Coding { 9 | @Override 10 | public void code() { 11 | System.out.println("java"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /design-pattern/src/main/java/com/wuwii/factory/CoderPhp.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.factory; 2 | 3 | /** 4 | * @author Zhang Kai 5 | * @version 1.0 6 | * @since
2018/3/21 18:577 | */ 8 | public class CoderPhp implements Coding { 9 | @Override 10 | public void code() { 11 | System.out.println("PHP"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /design-pattern/src/main/java/com/wuwii/factory/CoderPython.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.factory; 2 | 3 | /** 4 | * @author Zhang Kai 5 | * @version 1.0 6 | * @since
2018/3/21 18:577 | */ 8 | public class CoderPython implements Coding { 9 | @Override 10 | public void code() { 11 | System.out.println("Python"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /design-pattern/src/main/java/com/wuwii/factory/Coding.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.factory; 2 | 3 | /** 4 | * @author Zhang Kai 5 | * @version 1.0 6 | * @since
2018/3/21 18:497 | */ 8 | public interface Coding { 9 | void code(); 10 | } 11 | -------------------------------------------------------------------------------- /design-pattern/src/main/java/com/wuwii/observer/BuyObserver.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.observer; 2 | 3 | /** 4 | * Created by KronChan on 18/6/26 上午10:16. 5 | */ 6 | public class BuyObserver implements Observer { 7 | @Override 8 | public void receive(String product) { 9 | System.out.println(String.format("You have bought the %s successfully", product)); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /design-pattern/src/main/java/com/wuwii/observer/CouponOberver.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.observer; 2 | 3 | /** 4 | * Created by KronChan on 18/6/26 上午10:19. 5 | */ 6 | public class CouponOberver implements Observer { 7 | @Override 8 | public void receive(String product) { 9 | System.out.println(String.format("Give you some coupons for %s.", product)); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /design-pattern/src/main/java/com/wuwii/observer/Observer.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.observer; 2 | 3 | /** 4 | * Created by KronChan on 18/6/26 上午10:01. 5 | */ 6 | public interface Observer { 7 | /** 8 | * The observer receive the semaphore. 9 | */ 10 | void receive(String product); 11 | } 12 | -------------------------------------------------------------------------------- /design-pattern/src/main/java/com/wuwii/observer/ObserverMain.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.observer; 2 | 3 | /** 4 | * Created by KronChan on 18/6/26 上午10:24. 5 | */ 6 | public class ObserverMain { 7 | public static void main(String[] args) { 8 | Observer buyObserver = new BuyObserver(); 9 | Observer couponOberver = new CouponOberver(); 10 | Observer recommendOberver = new RecommendOberver(); 11 | ProductObservable productObservable = new ProductObservable(buyObserver, couponOberver, recommendOberver); 12 | productObservable.BuyProductSuccessfully("Computer"); 13 | productObservable.BuyProductSuccessfully("mouse"); 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /design-pattern/src/main/java/com/wuwii/observer/ProductObservable.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.observer; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | import java.util.concurrent.CopyOnWriteArrayList; 6 | 7 | /** 8 | * Created by KronChan on 18/6/26 上午10:06. 9 | */ 10 | public class ProductObservable { 11 | private List
2018/3/21 15:539 | */ 10 | public class SingletonDemo1 { 11 | private static SingletonDemo1 instance; 12 | 13 | private SingletonDemo1() { 14 | 15 | } 16 | 17 | public static synchronized SingletonDemo1 getInstance() { 18 | if (instance == null) { 19 | instance = new SingletonDemo1(); 20 | } 21 | return instance; 22 | } 23 | 24 | public static void main(String[] args) { 25 | SingletonDemo1 demo = SingletonDemo1.getInstance(); 26 | demo.show(); 27 | } 28 | 29 | public void show() { 30 | System.out.println("Hello World."); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /design-pattern/src/main/java/com/wuwii/singleton/SingletonDemo2.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.singleton; 2 | 3 | /** 4 | * 饿汉式,线程安全,没有懒加载 5 | * 6 | * @author Zhang Kai 7 | * @version 1.0 8 | * @since
2018/3/21 16:349 | */ 10 | public class SingletonDemo2 { 11 | private static SingletonDemo2 instance = new SingletonDemo2(); 12 | 13 | private SingletonDemo2() { 14 | 15 | } 16 | 17 | public static SingletonDemo2 getInstance() { 18 | return instance; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /design-pattern/src/main/java/com/wuwii/singleton/SingletonDemo3.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.singleton; 2 | 3 | /** 4 | * Double-Check 双重检查,线程安全,懒加载, jdk > 1.5 5 | * 6 | * @author Zhang Kai 7 | * @version 1.0 8 | * @since
2018/3/21 16:359 | */ 10 | public class SingletonDemo3 { 11 | private volatile static SingletonDemo3 instance; 12 | 13 | private SingletonDemo3() { 14 | 15 | } 16 | 17 | /** 18 | * 为了减少同步的开销,采用双重检查 19 | */ 20 | public static SingletonDemo3 getInstance() { 21 | if (instance == null) { 22 | synchronized (SingletonDemo3.class) { 23 | if (instance == null) { 24 | instance = new SingletonDemo3(); 25 | } 26 | } 27 | } 28 | return instance; 29 | } 30 | 31 | public static void main(String[] args) { 32 | SingletonDemo3 demo = SingletonDemo3.getInstance(); 33 | demo.show(); 34 | } 35 | 36 | public void show() { 37 | System.out.println("Hello World."); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /design-pattern/src/main/java/com/wuwii/singleton/SingletonDemo4.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.singleton; 2 | 3 | /** 4 | * 登记式,静态内部类,线程安全,懒加载 5 | * 6 | * @author Zhang Kai 7 | * @version 1.0 8 | * @since
2018/3/21 16:399 | */ 10 | public class SingletonDemo4 { 11 | private SingletonDemo4() { 12 | 13 | } 14 | 15 | /** 16 | * 注意的是 》类的静态属性只会在第一次加载类的时候初始化, 17 | * 在初次加载 getInstance 方法时候才会去装载 SingletonHolder 静态类,从而有 Lazy-Loading的作用。 18 | */ 19 | public static SingletonDemo4 getInstance() { 20 | return SingletonHolder.instance; 21 | } 22 | 23 | public static void main(String[] args) { 24 | SingletonDemo4.getInstance().show(); 25 | } 26 | 27 | public void show() { 28 | System.out.println("Hello World."); 29 | } 30 | 31 | private static class SingletonHolder { 32 | static SingletonDemo4 instance = new SingletonDemo4(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /design-pattern/src/main/java/com/wuwii/singleton/SingletonDemo5.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.singleton; 2 | 3 | /** 4 | * 使用枚举,枚举本身是单例静态的,线程安全,没有懒加载 5 | * 6 | * @author Zhang Kai 7 | * @version 1.0 8 | * @since
2018/3/21 17:039 | */ 10 | public enum SingletonDemo5 { 11 | INSTANCE; 12 | 13 | public static void main(String[] args) { 14 | SingletonDemo5.INSTANCE.show(); 15 | } 16 | 17 | public void show() { 18 | System.out.println("Hello World."); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /design-pattern/src/main/java/com/wuwii/strategy/Bike.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.strategy; 2 | 3 | /** 4 | * 5 | */ 6 | public class Bike implements Vehicle { 7 | @Override 8 | public String take() { 9 | return "By bike is good for your health"; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /design-pattern/src/main/java/com/wuwii/strategy/StrategyContext.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.strategy; 2 | 3 | import com.sun.istack.internal.Nullable; 4 | 5 | /** 6 | * 使用上下文, 7 | */ 8 | public class StrategyContext { 9 | @Nullable 10 | public static Vehicle strategyVehicle(Class extends Vehicle> clazz) { 11 | if (clazz == null) { 12 | return null; 13 | } 14 | try { 15 | return (Vehicle) Class.forName(clazz.getName()).newInstance(); 16 | } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { 17 | e.printStackTrace(); 18 | } 19 | return null; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /design-pattern/src/main/java/com/wuwii/strategy/StrategyDemo.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.strategy; 2 | 3 | /** 4 | * @author KronChan 5 | */ 6 | public class StrategyDemo { 7 | 8 | public static String vehicle(String vehicle) { 9 | switch (vehicle) { 10 | case "taxi": 11 | return "Take the taxi is expensive"; 12 | case "subway": 13 | return "Take the subway is inexpensive"; 14 | default: 15 | return null; 16 | } 17 | } 18 | 19 | public static void main(String[] args) { 20 | System.out.println(StrategyDemo.vehicle("taxi")); 21 | System.out.println(StrategyContext.strategyVehicle(Bike.class).take()); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /design-pattern/src/main/java/com/wuwii/strategy/Subway.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.strategy; 2 | 3 | /** 4 | * 5 | */ 6 | public class Subway implements Vehicle { 7 | @Override 8 | public String take() { 9 | return "Take the subway is inexpensive"; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /design-pattern/src/main/java/com/wuwii/strategy/Taxi.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.strategy; 2 | 3 | /** 4 | * 5 | */ 6 | public class Taxi implements Vehicle { 7 | 8 | @Override 9 | public String take() { 10 | return "Take the taxi is expensive"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /design-pattern/src/main/java/com/wuwii/strategy/Vehicle.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.strategy; 2 | 3 | /** 4 | * 策略行为 5 | */ 6 | public interface Vehicle { 7 | String take(); 8 | } 9 | -------------------------------------------------------------------------------- /dynamic-schedule/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /dynamic-schedule/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaimz/learning-code/a6f9ae2d0d2456eea198d23900903df19caa9db7/dynamic-schedule/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /dynamic-schedule/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.3/apache-maven-3.5.3-bin.zip 2 | -------------------------------------------------------------------------------- /dynamic-schedule/pom.xml: -------------------------------------------------------------------------------- 1 | 2 |
2018/3/20 18:388 | */ 9 | // 没有返回值的 fork / join 任务框架 10 | public class PrintTask extends RecursiveAction { 11 | private static final int THRESHOLD = 5; 12 | private int start; 13 | private int end; 14 | 15 | PrintTask(int start, int end) { 16 | this.start = start; 17 | this.end = end; 18 | } 19 | 20 | public static void main(String[] args) { 21 | PrintTask task = new PrintTask(0, 25); 22 | // 分配四个线程给它 23 | ForkJoinPool pool = new ForkJoinPool(4); 24 | pool.execute(task); 25 | pool.shutdown(); 26 | } 27 | 28 | @Override 29 | protected void compute() { 30 | if (THRESHOLD >= (end - start)) { 31 | // 满足小任务条件,分配打印任务 32 | for (int i = start; i < end; i++) { 33 | System.out.println(Thread.currentThread().getName() + ": " + i); 34 | } 35 | } else { 36 | // 任务还能继续拆分 37 | int division = (start + end) >> 1; 38 | PrintTask task1 = new PrintTask(start, division); 39 | PrintTask task2 = new PrintTask(division, end); 40 | invokeAll(task1, task2); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /fork-join/src/main/java/SumTask.java: -------------------------------------------------------------------------------- 1 | import java.util.concurrent.ForkJoinPool; 2 | import java.util.concurrent.RecursiveTask; 3 | import java.util.concurrent.TimeUnit; 4 | 5 | /** 6 | * @author Zhang Kai 7 | * @version 1.0 8 | * @since
2018/3/21 8:499 | */ 10 | // 有返回值的 fork / join 任务框架 RecursiveTask
2018/3/14 18:1619 | */ 20 | @Configuration 21 | public class DataSourceConfig { 22 | 23 | /** 24 | * 注册 data source 25 | * 26 | * @return 27 | */ 28 | @ConfigurationProperties(prefix = "spring.datasource") 29 | @Bean("firstDataSource") 30 | @Primary // 有相同实例优先选择 31 | public DataSource firstDataSource() { 32 | return DataSourceBuilder.create().build(); 33 | } 34 | 35 | @ConfigurationProperties(prefix = "spring.second-datasource") 36 | @Bean("secondDataSource") 37 | public DataSource secondDataSource() { 38 | return DataSourceBuilder.create().build(); 39 | } 40 | 41 | @Bean("firstJdbcTemplate") 42 | @Primary 43 | public JdbcTemplate firstJdbcTemplate(DataSource dataSource) { 44 | return new JdbcTemplate(dataSource); 45 | } 46 | 47 | @Bean("secondJdbcTemplate") 48 | public JdbcTemplate secondJdbcTemplate(@Qualifier("secondDataSource") DataSource dataSource) { 49 | return new JdbcTemplate(dataSource); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /jdbc-muti-datasource/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8022 3 | spring: 4 | datasource: 5 | url: jdbc:mysql://localhost:3306/learn?useSSL=false&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8 6 | username: root 7 | password: 123456 8 | driver-class-name: com.mysql.jdbc.Driver 9 | 10 | second-datasource: 11 | url: jdbc:mysql://localhost:3306/learn1?useSSL=false&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8 12 | username: root 13 | password: 123456 14 | driver-class-name: com.mysql.jdbc.Driver 15 | -------------------------------------------------------------------------------- /jdbc-muti-datasource/src/test/java/com/wuwii/JdbcMutiDatasourceApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.wuwii; 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 JdbcMutiDatasourceApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /jdbc-muti-datasource/src/test/java/com/wuwii/TestJDBC.java: -------------------------------------------------------------------------------- 1 | package com.wuwii; 2 | 3 | import org.hamcrest.Matchers; 4 | import org.junit.Assert; 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.beans.factory.annotation.Qualifier; 10 | import org.springframework.boot.test.context.SpringBootTest; 11 | import org.springframework.jdbc.core.JdbcTemplate; 12 | import org.springframework.test.context.junit4.SpringRunner; 13 | 14 | /** 15 | * @author Zhang Kai 16 | * @version 1.0 17 | * @since
2018/3/14 18:2918 | */ 19 | @SpringBootTest 20 | @RunWith(SpringRunner.class) 21 | public class TestJDBC { 22 | @Autowired 23 | private JdbcTemplate jdbcTemplate; 24 | @Autowired 25 | @Qualifier("secondJdbcTemplate") 26 | private JdbcTemplate jdbcTemplate1; 27 | 28 | @Before 29 | public void before() { 30 | jdbcTemplate.update("DELETE FROM employee"); 31 | jdbcTemplate1.update("DELETE FROM employee"); 32 | } 33 | 34 | @Test 35 | public void testJDBC() { 36 | jdbcTemplate.update("insert into employee(id,name,age) VALUES (1, 'wuwii', 24)"); 37 | jdbcTemplate1.update("insert into employee(id,name,age) VALUES (1, 'kronchan', 23)"); 38 | Assert.assertThat("wuwii", Matchers.equalTo(jdbcTemplate.queryForObject("SELECT name FROM employee WHERE id=1", String.class))); 39 | Assert.assertThat("kronchan", Matchers.equalTo(jdbcTemplate1.queryForObject("SELECT name FROM employee WHERE id=1", String.class))); 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /jpa-criteria/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ 25 | /.mvn/ 26 | -------------------------------------------------------------------------------- /jpa-criteria/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaimz/learning-code/a6f9ae2d0d2456eea198d23900903df19caa9db7/jpa-criteria/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /jpa-criteria/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.2/apache-maven-3.5.2-bin.zip 2 | -------------------------------------------------------------------------------- /jpa-criteria/README.md: -------------------------------------------------------------------------------- 1 | jpa Specification 复杂查询和 JPA criteria 查询 2 | 3 | 文章地址: 4 | http://blog.wuwii.com/jpa-%20specification.html -------------------------------------------------------------------------------- /jpa-criteria/src/main/java/com/wuwii/JpaCriteriaApplication.java: -------------------------------------------------------------------------------- 1 | package com.wuwii; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class JpaCriteriaApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(JpaCriteriaApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /jpa-criteria/src/main/java/com/wuwii/controller/EmployeeController.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.controller; 2 | 3 | import com.wuwii.form.EmployeeResult; 4 | import com.wuwii.service.EmployeeService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.MediaType; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | import java.util.List; 13 | 14 | /** 15 | * Created by KronChan on 2018/4/25 15:43. 16 | */ 17 | @RestController 18 | @RequestMapping(value = "/emp", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) 19 | public class EmployeeController { 20 | private final EmployeeService employeeService; 21 | 22 | @Autowired 23 | public EmployeeController(EmployeeService employeeService) { 24 | this.employeeService = employeeService; 25 | } 26 | 27 | @GetMapping 28 | public ResponseEntity
2018/3/8 10:0313 | */ 14 | @Repository 15 | public interface EmployeeDao extends JpaSpecificationExecutor
2018/3/8 10:4414 | */ 15 | @Repository 16 | public interface JobDao extends JpaSpecificationExecutor
2018/3/8 9:5212 | */ 13 | @Entity 14 | @Data 15 | public class Employee implements Serializable { 16 | @Id 17 | @GeneratedValue(strategy = GenerationType.IDENTITY) 18 | private Long id; 19 | 20 | @OneToOne(cascade = CascadeType.ALL) 21 | // 拥有级联维护的一方,参考http://westerly-lzh.github.io/cn/2014/12/JPA-CascadeType-Explaining/ 22 | @JoinColumn(foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT)) 23 | private EmployeeDetail detail; 24 | 25 | @ManyToOne(fetch = FetchType.LAZY) // 默认 lazy ,通过懒加载,知道需要使用级联的数据,才去数据库查询这个数据,提高查询效率。 26 | // 设置外键的问题,参考http://mario1412.github.io/2016/06/27/JPA%E4%B8%AD%E5%B1%8F%E8%94%BD%E5%AE%9E%E4%BD%93%E9%97%B4%E5%A4%96%E9%94%AE/ 27 | @JoinColumn(name = "jobId", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT)) 28 | private Job job; 29 | } 30 | -------------------------------------------------------------------------------- /jpa-criteria/src/main/java/com/wuwii/entity/EmployeeDetail.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.entity; 2 | 3 | import lombok.Data; 4 | 5 | import javax.persistence.Entity; 6 | import javax.persistence.GeneratedValue; 7 | import javax.persistence.GenerationType; 8 | import javax.persistence.Id; 9 | import java.io.Serializable; 10 | 11 | /** 12 | * @author Zhang Kai 13 | * @version 1.0 14 | * @since
2018/3/8 10:0015 | */ 16 | @Entity 17 | @Data 18 | public class EmployeeDetail implements Serializable { 19 | @Id 20 | @GeneratedValue(strategy = GenerationType.IDENTITY) 21 | private Long id; 22 | 23 | private String name; 24 | 25 | private String phone; 26 | 27 | private Integer age; 28 | } 29 | -------------------------------------------------------------------------------- /jpa-criteria/src/main/java/com/wuwii/entity/EmployeeDetail_.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.entity; 2 | 3 | import javax.persistence.metamodel.SingularAttribute; 4 | import javax.persistence.metamodel.StaticMetamodel; 5 | 6 | /** 7 | * @author Zhang Kai 8 | * @version 1.0 9 | * @since
2018/3/8 10:1810 | */ 11 | @StaticMetamodel(EmployeeDetail.class) 12 | public class EmployeeDetail_ { 13 | public static volatile SingularAttribute
2018/3/8 10:1613 | */ 14 | @StaticMetamodel(Employee.class) 15 | public class Employee_ { 16 | public static volatile SingularAttribute
2018/3/8 9:5413 | */ 14 | @Entity 15 | @Data 16 | public class Job implements Serializable { 17 | @Id 18 | @GeneratedValue(strategy = GenerationType.IDENTITY) 19 | private Long id; 20 | 21 | private String name; 22 | 23 | @OneToMany(targetEntity = Employee.class, mappedBy = "job") // mappedBy 只有在双向关联的时候设置,表示关系维护的一端,否则会生成中间表A_B 24 | @org.hibernate.annotations.ForeignKey(name = "none") // 注意这里不能使用 @JoinColumn 不然会生成外键 25 | private Set
2018/3/8 10:2111 | */ 12 | @StaticMetamodel(Job.class) 13 | public class Job_ { 14 | public static volatile SingularAttribute
2018/3/8 19:2413 | */ 14 | @EqualsAndHashCode(callSuper = true) 15 | @Data 16 | @NoArgsConstructor 17 | @AllArgsConstructor 18 | public class EmployeeResult extends Employee { 19 | private String name; 20 | private Integer age; 21 | } 22 | -------------------------------------------------------------------------------- /jpa-criteria/src/main/java/com/wuwii/form/EmployeeSearch.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.form; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Getter; 5 | 6 | /** 7 | * @author Zhang Kai 8 | * @version 1.0 9 | * @since
2018/3/8 11:0510 | */ 11 | @Getter 12 | @AllArgsConstructor 13 | public class EmployeeSearch { 14 | private Long employeeId; 15 | private String employeeName; 16 | private String jobName; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /jpa-criteria/src/main/java/com/wuwii/service/EmployeeService.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.service; 2 | 3 | import com.wuwii.entity.Employee; 4 | import com.wuwii.form.EmployeeResult; 5 | import com.wuwii.form.EmployeeSearch; 6 | import org.springframework.data.domain.Page; 7 | import org.springframework.data.domain.Pageable; 8 | 9 | import javax.persistence.Tuple; 10 | import java.util.List; 11 | 12 | /** 13 | * @author Zhang Kai 14 | * @version 1.0 15 | * @since
2018/3/8 10:0416 | */ 17 | public interface EmployeeService { 18 | /** 19 | * 分页查询 20 | * 21 | * @param search 查询属性 22 | * @param pageable 分页和排序 23 | * @return 分页数据 24 | */ 25 | Page
2018/4/26 22:5123 | */ 24 | @RunWith(SpringRunner.class) 25 | @WebMvcTest(EmployeeController.class) 26 | public class EmployeeController2Test { 27 | @Autowired 28 | private MockMvc mvc; 29 | 30 | @MockBean 31 | private EmployeeService employeeService; 32 | 33 | public void setUp() { 34 | // 设置返回的 body 是空的 35 | Mockito.when(employeeService.findEmployee()).thenReturn(new ArrayList<>()); 36 | } 37 | 38 | @Test 39 | public void listAll() throws Exception { 40 | mvc.perform(MockMvcRequestBuilders.get("/emp")) 41 | .andExpect(status().isOk()) // 期待返回状态吗码200 42 | // JsonPath expression https://github.com/jayway/JsonPath 43 | //.andExpect(jsonPath("$[1].name").exists()) // 这里是期待返回值是数组,并且第二个值的 name 存在,所以这里测试是失败的 44 | .andDo(print()); // 打印返回的 http response 信息 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /jpa-criteria/src/test/java/com/wuwii/controller/EmployeeControllerTest.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.controller; 2 | 3 | import org.junit.Before; 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.http.MediaType; 9 | import org.springframework.test.context.junit4.SpringRunner; 10 | import org.springframework.test.web.servlet.MockMvc; 11 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 12 | import org.springframework.web.context.WebApplicationContext; 13 | 14 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 15 | import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; 16 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; 17 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 18 | 19 | /** 20 | * Created by KronChan on 2018/4/25 15:56. 21 | */ 22 | @RunWith(SpringRunner.class) 23 | @SpringBootTest 24 | public class EmployeeControllerTest { 25 | /** 26 | * Interface to provide configuration for a web application. 27 | */ 28 | @Autowired 29 | private WebApplicationContext ctx; 30 | 31 | private MockMvc mockMvc; 32 | 33 | /** 34 | * 初始化 MVC 的环境 35 | */ 36 | @Before 37 | public void before() { 38 | mockMvc = MockMvcBuilders.webAppContextSetup(ctx).build(); 39 | } 40 | 41 | @Test 42 | public void listAll() throws Exception { 43 | mockMvc 44 | .perform(get("/emp") // 测试的相对地址 45 | .accept(MediaType.APPLICATION_JSON_UTF8) // accept response content type 46 | ) 47 | .andExpect(status().isOk()) // 期待返回状态吗码200 48 | // JsonPath expression https://github.com/jayway/JsonPath 49 | .andExpect(jsonPath("$[1].name").exists()) // 这里是期待返回值是数组,并且第二个值的 name 存在 50 | .andDo(print()); // 打印返回的 http response 信息 51 | } 52 | } -------------------------------------------------------------------------------- /jpa-criteria/src/test/java/com/wuwii/dao/EmployeeDaoTest.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.dao; 2 | 3 | import com.wuwii.entity.Employee; 4 | import com.wuwii.entity.EmployeeDetail; 5 | import org.hamcrest.Matchers; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; 10 | import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; 11 | import org.springframework.test.context.junit4.SpringRunner; 12 | import org.springframework.transaction.annotation.Transactional; 13 | 14 | import static org.junit.Assert.assertThat; 15 | 16 | /** 17 | * Spring Data JPA 测试 18 | * Created by KronChan on 2018/4/26 8:30. 19 | */ 20 | @RunWith(SpringRunner.class) 21 | @DataJpaTest 22 | @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) 23 | public class EmployeeDaoTest { 24 | 25 | @Autowired 26 | private EmployeeDao employeeDao; 27 | 28 | @Test 29 | @Transactional 30 | public void testSave() { 31 | Employee employee = new Employee(); 32 | EmployeeDetail detail = new EmployeeDetail(); 33 | detail.setName("kronchan"); 34 | detail.setAge(24); 35 | employee.setDetail(detail); 36 | assertThat(detail.getName(), Matchers.is(employeeDao.save(employee).getDetail().getName())); 37 | ; 38 | } 39 | } -------------------------------------------------------------------------------- /jpa-criteria/src/test/java/com/wuwii/paramter/ParameterTest.java: -------------------------------------------------------------------------------- 1 | package com.wuwii.paramter; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.junit.runners.Parameterized; 6 | 7 | import java.util.Arrays; 8 | import java.util.List; 9 | 10 | import static org.hamcrest.Matchers.is; 11 | import static org.junit.Assert.assertThat; 12 | 13 | /** 14 | * Created by KronChan on 2018/4/25 17:18. 15 | */ 16 | //1.更改默认的测试运行器为RunWith(Parameterized.class) 17 | @RunWith(Parameterized.class) 18 | public class ParameterTest { 19 | // 2.声明变量存放预期值和测试数据 20 | private String firstName; 21 | private String lastName; 22 | 23 | //3.声明一个返回值 为Collection的公共静态方法,并使用@Parameters进行修饰 24 | @Parameterized.Parameters // 25 | public static List