forked from dyc87112/SpringBoot-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
419 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
HELP.md | ||
/target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
/build/ | ||
|
||
### VS Code ### | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.1.3.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<groupId>com.didispace</groupId> | ||
<artifactId>chapter3-8</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<description>使用spring-data-jpa的多数据源配置</description> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jpa</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-actuator</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
25 changes: 25 additions & 0 deletions
25
2.1.x/chapter3-8/src/main/java/com/didispace/chapter38/Chapter38Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.didispace.chapter34; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@SpringBootApplication | ||
public class Chapter34Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Chapter34Application.class, args); | ||
} | ||
|
||
@RestController | ||
static class TextController { | ||
|
||
@GetMapping("/hello") | ||
public String hello() { | ||
return "hello world"; | ||
} | ||
|
||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
2.1.x/chapter3-8/src/main/java/com/didispace/chapter38/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.didispace.chapter34; | ||
|
||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
|
||
@Entity | ||
@Data | ||
@NoArgsConstructor | ||
public class User { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
private String name; | ||
private Integer age; | ||
|
||
public User(String name, Integer age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
2.1.x/chapter3-8/src/main/java/com/didispace/chapter38/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.didispace.chapter34; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
/** | ||
* Created by 程序猿DD/翟永超 on 2020/2/15. | ||
* <p> | ||
* Blog: http://blog.didispace.com/ | ||
* Github: https://github.com/dyc87112/ | ||
*/ | ||
public interface UserRepository extends JpaRepository<User, Long> { | ||
|
||
User findByName(String name); | ||
|
||
User findByNameAndAge(String name, Integer age); | ||
|
||
@Query("from User u where u.name=:name") | ||
User findUser(@Param("name") String name); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
spring.datasource.url=jdbc:mysql://localhost:3306/test | ||
spring.datasource.username=root | ||
spring.datasource.password= | ||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver | ||
|
||
spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop |
57 changes: 57 additions & 0 deletions
57
2.1.x/chapter3-8/src/test/java/com/didispace/chapter34/Chapter34ApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.didispace.chapter34; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.List; | ||
|
||
@Slf4j | ||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
public class Chapter34ApplicationTests { | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Test | ||
public void test() throws Exception { | ||
// 创建10条记录 | ||
userRepository.save(new User("AAA", 10)); | ||
userRepository.save(new User("BBB", 20)); | ||
userRepository.save(new User("CCC", 30)); | ||
userRepository.save(new User("DDD", 40)); | ||
userRepository.save(new User("EEE", 50)); | ||
userRepository.save(new User("FFF", 60)); | ||
userRepository.save(new User("GGG", 70)); | ||
userRepository.save(new User("HHH", 80)); | ||
userRepository.save(new User("III", 90)); | ||
userRepository.save(new User("JJJ", 100)); | ||
|
||
// 测试findAll, 查询所有记录 | ||
Assert.assertEquals(10, userRepository.findAll().size()); | ||
|
||
// 测试findByName, 查询姓名为FFF的User | ||
Assert.assertEquals(60, userRepository.findByName("FFF").getAge().longValue()); | ||
|
||
// 测试findUser, 查询姓名为FFF的User | ||
Assert.assertEquals(60, userRepository.findUser("FFF").getAge().longValue()); | ||
|
||
// 测试findByNameAndAge, 查询姓名为FFF并且年龄为60的User | ||
Assert.assertEquals("FFF", userRepository.findByNameAndAge("FFF", 60).getName()); | ||
|
||
// 测试删除姓名为AAA的User | ||
userRepository.delete(userRepository.findByName("AAA")); | ||
|
||
// 测试findAll, 查询所有记录, 验证上面的删除是否成功 | ||
Assert.assertEquals(9, userRepository.findAll().size()); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
HELP.md | ||
/target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
/build/ | ||
|
||
### VS Code ### | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.1.3.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<groupId>com.didispace</groupId> | ||
<artifactId>chapter3-9</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<description>使用MyBatis的多数据源配置</description> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.mybatis.spring.boot</groupId> | ||
<artifactId>mybatis-spring-boot-starter</artifactId> | ||
<version>2.1.1</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
15 changes: 15 additions & 0 deletions
15
2.1.x/chapter3-9/src/main/java/com/didispace/chapter39/Chapter39Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.didispace.chapter36; | ||
|
||
import org.mybatis.spring.annotation.MapperScan; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@MapperScan("com.didispace.chapter36.mapper") | ||
@SpringBootApplication | ||
public class Chapter36Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Chapter36Application.class, args); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
2.1.x/chapter3-9/src/main/java/com/didispace/chapter39/entity/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.didispace.chapter36.entity; | ||
|
||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
|
||
@Data | ||
@NoArgsConstructor | ||
public class User { | ||
|
||
private Long id; | ||
|
||
private String name; | ||
private Integer age; | ||
|
||
public User(String name, Integer age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
2.1.x/chapter3-9/src/main/java/com/didispace/chapter39/mapper/UserMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.didispace.chapter36.mapper; | ||
|
||
import com.didispace.chapter36.entity.User; | ||
import org.apache.ibatis.annotations.Param; | ||
|
||
/** | ||
* Created by 程序猿DD/翟永超 on 2020/2/28. | ||
* <p> | ||
* Blog: http://blog.didispace.com/ | ||
* Github: https://github.com/dyc87112/ | ||
*/ | ||
public interface UserMapper { | ||
|
||
User findByName(@Param("name") String name); | ||
|
||
int insert(@Param("name") String name, @Param("age") Integer age); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
spring.datasource.url=jdbc:mysql://localhost:3306/test | ||
spring.datasource.username=root | ||
spring.datasource.password=12345678 | ||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver | ||
|
||
mybatis.mapper-locations=classpath:mapper/*.xml |
Oops, something went wrong.