Skip to content

Commit 5f80be5

Browse files
committed
doing
1 parent e2fc129 commit 5f80be5

File tree

15 files changed

+419
-0
lines changed

15 files changed

+419
-0
lines changed

2.1.x/chapter3-8/.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
HELP.md
2+
/target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
20+
### NetBeans ###
21+
/nbproject/private/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
/build/
27+
28+
### VS Code ###
29+
.vscode/

2.1.x/chapter3-8/pom.xml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.1.3.RELEASE</version>
10+
<relativePath/> <!-- lookup parent from repository -->
11+
</parent>
12+
13+
<groupId>com.didispace</groupId>
14+
<artifactId>chapter3-8</artifactId>
15+
<version>0.0.1-SNAPSHOT</version>
16+
<description>使用spring-data-jpa的多数据源配置</description>
17+
18+
<properties>
19+
<java.version>1.8</java.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-web</artifactId>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-jpa</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-actuator</artifactId>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>mysql</groupId>
40+
<artifactId>mysql-connector-java</artifactId>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.projectlombok</groupId>
45+
<artifactId>lombok</artifactId>
46+
<scope>provided</scope>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-starter-test</artifactId>
52+
<scope>test</scope>
53+
</dependency>
54+
</dependencies>
55+
56+
<build>
57+
<plugins>
58+
<plugin>
59+
<groupId>org.springframework.boot</groupId>
60+
<artifactId>spring-boot-maven-plugin</artifactId>
61+
</plugin>
62+
</plugins>
63+
</build>
64+
65+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.didispace.chapter34;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@SpringBootApplication
9+
public class Chapter34Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(Chapter34Application.class, args);
13+
}
14+
15+
@RestController
16+
static class TextController {
17+
18+
@GetMapping("/hello")
19+
public String hello() {
20+
return "hello world";
21+
}
22+
23+
}
24+
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.didispace.chapter34;
2+
3+
import lombok.Data;
4+
import lombok.NoArgsConstructor;
5+
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.Id;
9+
10+
@Entity
11+
@Data
12+
@NoArgsConstructor
13+
public class User {
14+
15+
@Id
16+
@GeneratedValue
17+
private Long id;
18+
19+
private String name;
20+
private Integer age;
21+
22+
public User(String name, Integer age) {
23+
this.name = name;
24+
this.age = age;
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.didispace.chapter34;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.data.jpa.repository.Query;
5+
import org.springframework.data.repository.query.Param;
6+
7+
/**
8+
* Created by 程序猿DD/翟永超 on 2020/2/15.
9+
* <p>
10+
* Blog: http://blog.didispace.com/
11+
* Github: https://github.com/dyc87112/
12+
*/
13+
public interface UserRepository extends JpaRepository<User, Long> {
14+
15+
User findByName(String name);
16+
17+
User findByNameAndAge(String name, Integer age);
18+
19+
@Query("from User u where u.name=:name")
20+
User findUser(@Param("name") String name);
21+
22+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
spring.datasource.url=jdbc:mysql://localhost:3306/test
2+
spring.datasource.username=root
3+
spring.datasource.password=
4+
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
5+
6+
spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.didispace.chapter34;
2+
3+
import lombok.extern.slf4j.Slf4j;
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.boot.test.context.SpringBootTest;
10+
import org.springframework.test.context.junit4.SpringRunner;
11+
12+
import javax.sql.DataSource;
13+
import java.util.List;
14+
15+
@Slf4j
16+
@RunWith(SpringRunner.class)
17+
@SpringBootTest
18+
public class Chapter34ApplicationTests {
19+
20+
@Autowired
21+
private UserRepository userRepository;
22+
23+
@Test
24+
public void test() throws Exception {
25+
// 创建10条记录
26+
userRepository.save(new User("AAA", 10));
27+
userRepository.save(new User("BBB", 20));
28+
userRepository.save(new User("CCC", 30));
29+
userRepository.save(new User("DDD", 40));
30+
userRepository.save(new User("EEE", 50));
31+
userRepository.save(new User("FFF", 60));
32+
userRepository.save(new User("GGG", 70));
33+
userRepository.save(new User("HHH", 80));
34+
userRepository.save(new User("III", 90));
35+
userRepository.save(new User("JJJ", 100));
36+
37+
// 测试findAll, 查询所有记录
38+
Assert.assertEquals(10, userRepository.findAll().size());
39+
40+
// 测试findByName, 查询姓名为FFF的User
41+
Assert.assertEquals(60, userRepository.findByName("FFF").getAge().longValue());
42+
43+
// 测试findUser, 查询姓名为FFF的User
44+
Assert.assertEquals(60, userRepository.findUser("FFF").getAge().longValue());
45+
46+
// 测试findByNameAndAge, 查询姓名为FFF并且年龄为60的User
47+
Assert.assertEquals("FFF", userRepository.findByNameAndAge("FFF", 60).getName());
48+
49+
// 测试删除姓名为AAA的User
50+
userRepository.delete(userRepository.findByName("AAA"));
51+
52+
// 测试findAll, 查询所有记录, 验证上面的删除是否成功
53+
Assert.assertEquals(9, userRepository.findAll().size());
54+
55+
}
56+
57+
}

2.1.x/chapter3-9/.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
HELP.md
2+
/target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
20+
### NetBeans ###
21+
/nbproject/private/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
/build/
27+
28+
### VS Code ###
29+
.vscode/

2.1.x/chapter3-9/pom.xml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.1.3.RELEASE</version>
10+
<relativePath/> <!-- lookup parent from repository -->
11+
</parent>
12+
13+
<groupId>com.didispace</groupId>
14+
<artifactId>chapter3-9</artifactId>
15+
<version>0.0.1-SNAPSHOT</version>
16+
<description>使用MyBatis的多数据源配置</description>
17+
18+
<properties>
19+
<java.version>1.8</java.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.mybatis.spring.boot</groupId>
25+
<artifactId>mybatis-spring-boot-starter</artifactId>
26+
<version>2.1.1</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>mysql</groupId>
31+
<artifactId>mysql-connector-java</artifactId>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.projectlombok</groupId>
36+
<artifactId>lombok</artifactId>
37+
<scope>provided</scope>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-test</artifactId>
43+
<scope>test</scope>
44+
</dependency>
45+
</dependencies>
46+
47+
<build>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-maven-plugin</artifactId>
52+
</plugin>
53+
</plugins>
54+
</build>
55+
56+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.didispace.chapter36;
2+
3+
import org.mybatis.spring.annotation.MapperScan;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
@MapperScan("com.didispace.chapter36.mapper")
8+
@SpringBootApplication
9+
public class Chapter36Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(Chapter36Application.class, args);
13+
}
14+
15+
}

0 commit comments

Comments
 (0)