forked from lenve/javaboy-code-samples
-
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
128 changed files
with
4,980 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,41 @@ | ||
<?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 https://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.7.0</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>org.javaboy</groupId> | ||
<artifactId>aware_demo</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>aware_demo</name> | ||
<description>Demo project for Spring Boot</description> | ||
<properties> | ||
<java.version>17</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-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> |
13 changes: 13 additions & 0 deletions
13
aware_demo/src/main/java/org/javaboy/aware_demo/AwareDemoApplication.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,13 @@ | ||
package org.javaboy.aware_demo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class AwareDemoApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(AwareDemoApplication.class, args); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
aware_demo/src/main/java/org/javaboy/aware_demo/BeanUtils.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,29 @@ | ||
package org.javaboy.aware_demo; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.BeanFactory; | ||
import org.springframework.beans.factory.BeanFactoryAware; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @author 江南一点雨 | ||
* @微信公众号 江南一点雨 | ||
* @网站 http://www.itboyhub.com | ||
* @国际站 http://www.javaboy.org | ||
* @微信 a_java_boy | ||
* @GitHub https://github.com/lenve | ||
* @Gitee https://gitee.com/lenve | ||
*/ | ||
@Component | ||
public class BeanUtils implements BeanFactoryAware { | ||
private static BeanFactory beanFactory = null; | ||
|
||
@Override | ||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { | ||
BeanUtils.beanFactory = beanFactory; | ||
} | ||
|
||
public static <T> T getBean(String beanName) { | ||
return (T) beanFactory.getBean(beanName); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
aware_demo/src/main/java/org/javaboy/aware_demo/UserService.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 org.javaboy.aware_demo; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* @author 江南一点雨 | ||
* @微信公众号 江南一点雨 | ||
* @网站 http://www.itboyhub.com | ||
* @国际站 http://www.javaboy.org | ||
* @微信 a_java_boy | ||
* @GitHub https://github.com/lenve | ||
* @Gitee https://gitee.com/lenve | ||
*/ | ||
@Service | ||
public class UserService { | ||
|
||
public void hello() { | ||
System.out.println("hello javaboy!"); | ||
} | ||
} |
Empty file.
15 changes: 15 additions & 0 deletions
15
aware_demo/src/test/java/org/javaboy/aware_demo/AwareDemoApplicationTests.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 org.javaboy.aware_demo; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest | ||
class AwareDemoApplicationTests { | ||
|
||
@Test | ||
void contextLoads() { | ||
UserService userService = BeanUtils.getBean("userService"); | ||
userService.hello(); | ||
} | ||
|
||
} |
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,55 @@ | ||
<?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 https://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.7.0</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>org.javaboy</groupId> | ||
<artifactId>dd_demo</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>dd_demo</name> | ||
<description>Demo project for Spring Boot</description> | ||
<properties> | ||
<java.version>17</java.version> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mybatis.spring.boot</groupId> | ||
<artifactId>mybatis-spring-boot-starter</artifactId> | ||
<version>2.2.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.baomidou</groupId> | ||
<artifactId>dynamic-datasource-spring-boot-starter</artifactId> | ||
<version>3.5.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<scope>runtime</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> |
13 changes: 13 additions & 0 deletions
13
dd_demo/src/main/java/org/javaboy/dd_demo/DdDemoApplication.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,13 @@ | ||
package org.javaboy.dd_demo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class DdDemoApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(DdDemoApplication.class, args); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
dd_demo/src/main/java/org/javaboy/dd_demo/Test08Service.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 org.javaboy.dd_demo; | ||
|
||
import com.baomidou.dynamic.datasource.annotation.DS; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* @author 江南一点雨 | ||
* @微信公众号 江南一点雨 | ||
* @网站 http://www.itboyhub.com | ||
* @国际站 http://www.javaboy.org | ||
* @微信 a_java_boy | ||
* @GitHub https://github.com/lenve | ||
* @Gitee https://gitee.com/lenve | ||
*/ | ||
@Service | ||
public class Test08Service { | ||
|
||
@Autowired | ||
UserMapper userMapper; | ||
// @DS("master") | ||
public void addUser() { | ||
userMapper.addUser("maba22", 9999); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
dd_demo/src/main/java/org/javaboy/dd_demo/Test09Service.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 org.javaboy.dd_demo; | ||
|
||
import com.baomidou.dynamic.datasource.annotation.DS; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* @author 江南一点雨 | ||
* @微信公众号 江南一点雨 | ||
* @网站 http://www.itboyhub.com | ||
* @国际站 http://www.javaboy.org | ||
* @微信 a_java_boy | ||
* @GitHub https://github.com/lenve | ||
* @Gitee https://gitee.com/lenve | ||
*/ | ||
@Service | ||
public class Test09Service { | ||
|
||
@Autowired | ||
UserMapper userMapper; | ||
// @DS("slave") | ||
public void addUser() { | ||
userMapper.addUser("qianshi22", 9999); | ||
} | ||
} |
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,23 @@ | ||
package org.javaboy.dd_demo; | ||
|
||
import org.apache.ibatis.annotations.Insert; | ||
import org.apache.ibatis.annotations.Mapper; | ||
import org.apache.ibatis.annotations.Select; | ||
|
||
/** | ||
* @author 江南一点雨 | ||
* @微信公众号 江南一点雨 | ||
* @网站 http://www.itboyhub.com | ||
* @国际站 http://www.javaboy.org | ||
* @微信 a_java_boy | ||
* @GitHub https://github.com/lenve | ||
* @Gitee https://gitee.com/lenve | ||
*/ | ||
@Mapper | ||
public interface UserMapper { | ||
@Select("select count(*) from user") | ||
Integer getCount(); | ||
|
||
@Insert("insert into user(username,age) values(#{username},#{age})") | ||
Integer addUser(String username, int age); | ||
} |
48 changes: 48 additions & 0 deletions
48
dd_demo/src/main/java/org/javaboy/dd_demo/UserService.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,48 @@ | ||
package org.javaboy.dd_demo; | ||
|
||
import com.baomidou.dynamic.datasource.annotation.DS; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
/** | ||
* @author 江南一点雨 | ||
* @微信公众号 江南一点雨 | ||
* @网站 http://www.itboyhub.com | ||
* @国际站 http://www.javaboy.org | ||
* @微信 a_java_boy | ||
* @GitHub https://github.com/lenve | ||
* @Gitee https://gitee.com/lenve | ||
*/ | ||
@Service | ||
public class UserService { | ||
|
||
@Autowired | ||
UserMapper userMapper; | ||
|
||
@Autowired | ||
Test08Service test08Service; | ||
|
||
@Autowired | ||
Test09Service test09Service; | ||
|
||
@Transactional | ||
@DS("master") | ||
public Integer count() { | ||
return userMapper.getCount(); | ||
} | ||
|
||
@Transactional | ||
@DS("slave") | ||
public void addUser2() { | ||
test08Service.addUser(); | ||
int i = 1 / 0; | ||
test09Service.addUser(); | ||
} | ||
|
||
@Transactional | ||
@DS("master") | ||
public Integer addUser() { | ||
return userMapper.addUser("fengqi", 101); | ||
} | ||
} |
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,14 @@ | ||
spring: | ||
datasource: | ||
dynamic: | ||
primary: master #设置默认的数据源或者数据源组,默认值即为master | ||
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源 | ||
datasource: | ||
master: | ||
url: jdbc:mysql://localhost:3306/test08?serverTimezone=Asia/Shanghai&useSSL=false | ||
username: root | ||
password: 123 | ||
slave_1: | ||
url: jdbc:mysql://localhost:3306/test09?serverTimezone=Asia/Shanghai&useSSL=false | ||
username: root | ||
password: 123 |
22 changes: 22 additions & 0 deletions
22
dd_demo/src/test/java/org/javaboy/dd_demo/DdDemoApplicationTests.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 org.javaboy.dd_demo; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest | ||
class DdDemoApplicationTests { | ||
|
||
@Autowired | ||
UserService userService; | ||
|
||
@Test | ||
void contextLoads() { | ||
// System.out.println("userService.count() = " + userService.count()); | ||
// System.out.println("userService.addUser() = " + userService.addUser()); | ||
userService.addUser2(); | ||
} | ||
|
||
|
||
|
||
} |
Oops, something went wrong.