Skip to content

Commit ccb5f01

Browse files
committedMay 7, 2020
use h2
1 parent 5632a8b commit ccb5f01

File tree

16 files changed

+213
-214
lines changed

16 files changed

+213
-214
lines changed
 

‎source-code/basis/jpa-demo/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
<artifactId>mysql-connector-java</artifactId>
3333
<scope>runtime</scope>
3434
</dependency>
35+
<dependency>
36+
<groupId>com.h2database</groupId>
37+
<artifactId>h2</artifactId>
38+
<scope>runtime</scope>
39+
</dependency>
3540
<dependency>
3641
<groupId>org.projectlombok</groupId>
3742
<artifactId>lombok</artifactId>
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_jpa?useSSL=false&serverTimezone=CTT
1+
# 数据库url地址
2+
spring.datasource.url=jdbc:h2:mem:jpa-demo
23
spring.datasource.username=root
34
spring.datasource.password=123456
5+
spring.datasource.platform=h2
6+
spring.datasource.driverClassName =org.h2.Driver
7+
8+
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
49
# 打印出 sql 语句
510
spring.jpa.show-sql=true
611
#常用的有四种:
712
# 1.create:每次重新启动项目都会重新创新表结构,会导致数据丢失
813
# 2.create-drop:每次启动项目创建表结构,关闭项目删除表结构
914
# 3.update:每次启动项目会更新表结构
1015
# 4. validate:验证表结构,不对数据库进行任何更改
11-
spring.jpa.hibernate.ddl-auto=create
16+
spring.jpa.hibernate.ddl-auto=update
1217
spring.jpa.open-in-view=false
13-
# 创建的表的 ENGINE 为 InnoDB
14-
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect
18+
server.port=8080
19+
#H2控制台
20+
spring.h2.console.enabled=true

‎source-code/basis/springboot-handle-exception-improved/.idea/modules/webApp.main.iml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎source-code/basis/springboot-handle-exception-improved/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
plugins {
2-
id 'org.springframework.boot' version '2.1.9.RELEASE'
2+
id 'org.springframework.boot' version '2.1.8.RELEASE'
33
id 'java'
44
}
55

‎source-code/start/hello-world/src/main/java/com/example/helloworld/controller/BookController.java

-48
This file was deleted.

‎source-code/start/hello-world/src/main/java/com/example/helloworld/controller/CountDownLatchExample1.java

-44
This file was deleted.

‎source-code/start/hello-world/src/main/java/com/example/helloworld/controller/HelloWorldController.java

-14
This file was deleted.

‎source-code/start/hello-world/src/main/java/com/example/helloworld/controller/MyBloomFilter.java

-90
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.example.helloworld.controller;
2+
3+
import java.util.Date;
4+
5+
/**
6+
* 这是一个简单的Runnable类,需要大约5秒钟来执行其任务。
7+
* @author shuang.kou
8+
*/
9+
public class MyRunnable implements Runnable {
10+
11+
private String command;
12+
13+
public MyRunnable(String s) {
14+
this.command = s;
15+
}
16+
17+
@Override
18+
public void run() {
19+
System.out.println(Thread.currentThread().getName() + " Start. Time = " + new Date());
20+
processCommand();
21+
System.out.println(Thread.currentThread().getName() + " End. Time = " + new Date());
22+
}
23+
24+
private void processCommand() {
25+
try {
26+
Thread.sleep(5000);
27+
} catch (InterruptedException e) {
28+
e.printStackTrace();
29+
}
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return this.command;
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.example.helloworld.controller;
2+
3+
public class Test {
4+
public Test() {
5+
System.out.print("默认构造方法!--");
6+
}
7+
8+
//非静态代码块
9+
{
10+
System.out.print("非静态代码块!--");
11+
}
12+
13+
//静态代码块
14+
static {
15+
System.out.print("静态代码块!--");
16+
}
17+
18+
private static void test() {
19+
System.out.print("静态方法中的内容! --");
20+
{
21+
System.out.print("静态方法中的代码块!--");
22+
}
23+
24+
}
25+
26+
public static void main(String[] args) {
27+
Test test = new Test();
28+
Test.test();//静态代码块!--静态方法中的内容! --静态方法中的代码块!--
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.example.helloworld.controller;
2+
3+
import java.util.concurrent.ArrayBlockingQueue;
4+
import java.util.concurrent.ThreadPoolExecutor;
5+
import java.util.concurrent.TimeUnit;
6+
7+
public class ThreadPoolExecutorDemo {
8+
9+
private static final int CORE_POOL_SIZE = 5;
10+
private static final int MAX_POOL_SIZE = 10;
11+
private static final int QUEUE_CAPACITY = 100;
12+
private static final Long KEEP_ALIVE_TIME = 1L;
13+
14+
public static void main(String[] args) {
15+
16+
//使用阿里巴巴推荐的创建线程池的方式
17+
//通过ThreadPoolExecutor构造函数自定义参数创建
18+
ThreadPoolExecutor executor = new ThreadPoolExecutor(
19+
CORE_POOL_SIZE,
20+
MAX_POOL_SIZE,
21+
KEEP_ALIVE_TIME,
22+
TimeUnit.SECONDS,
23+
new ArrayBlockingQueue<>(QUEUE_CAPACITY),
24+
new ThreadPoolExecutor.CallerRunsPolicy());
25+
26+
for (int i = 0; i < 10; i++) {
27+
//创建WorkerThread对象(WorkerThread类实现了Runnable 接口)
28+
Runnable worker = new MyRunnable("" + i);
29+
//执行Runnable
30+
executor.execute(worker);
31+
}
32+
//终止线程池
33+
executor.shutdown();
34+
while (executor.isTerminated()) {
35+
System.out.println("Finished all threads");
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.example.helloworld.controller;
2+
3+
import com.example.helloworld.dto.UserDto;
4+
import com.example.helloworld.entity.User;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import java.util.stream.IntStream;
13+
14+
@RestController
15+
@RequestMapping("users")
16+
public class UserController {
17+
18+
ArrayList<User> users = new ArrayList<User>() {{
19+
add(new User(11111L, "Guide哥", "koushuangbwcx@163.com", "123456"));
20+
add(new User(22222L, "李真", "koushuangbwcx@163.com", "123456"));
21+
add(new User(33333L, "帅", "koushuangbwcx@163.com", "123456"));
22+
}};
23+
24+
@GetMapping
25+
public ResponseEntity<List<UserDto>> getUsers() {
26+
27+
return null;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.helloworld.dto;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import lombok.ToString;
6+
7+
@Getter
8+
@Setter
9+
@ToString
10+
public class UserDto {
11+
String name;
12+
String email;
13+
}

‎source-code/start/hello-world/src/main/java/com/example/helloworld/entity/Book.java

-13
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.helloworld.entity;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
6+
@Data
7+
@AllArgsConstructor
8+
public class User {
9+
Long id;
10+
String name;
11+
String email;
12+
String password;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.example.dto2entity;
2+
3+
import com.example.helloworld.entity.User;
4+
import com.example.helloworld.dto.UserDto;
5+
import org.junit.Test;
6+
import org.springframework.beans.BeanUtils;
7+
8+
import static org.junit.Assert.assertFalse;
9+
import static org.springframework.test.util.AssertionErrors.assertTrue;
10+
11+
public class BeanUtilsTest {
12+
13+
@Test
14+
public void should_copy_user_properties_to_userDto() {
15+
User user = new User(123213L, "Guide哥", "koushuangbwcx@163.com", "123456");
16+
UserDto userDto = new UserDto();
17+
BeanUtils.copyProperties(user, userDto);
18+
assertTrue("name copied", userDto.getName().equals(user.getName()));
19+
assertTrue("email copied", userDto.getEmail().equals(user.getEmail()));
20+
}
21+
22+
@Test
23+
public void should_copy_user_properties_to_userDto_but_not_copy_email() {
24+
User user = new User(123213L, "Guide哥", "koushuangbwcx@163.com", "123456");
25+
UserDto userDto = new UserDto();
26+
BeanUtils.copyProperties(user, userDto, "email");
27+
assertTrue("name copied", userDto.getName().equals(user.getName()));
28+
assertTrue("email not copied", userDto.getEmail() == null);
29+
}
30+
}

0 commit comments

Comments
 (0)
Please sign in to comment.