13 | //当前方法返回的值会被注册成bean
14 | //bean默认的名称是方法名
15 | //如果需要设置自定义名称修改@Bean中name属性
16 | @Bean(name = "t")
17 | public Test test(){
18 | return new Test();
19 | }
20 |
21 | //需要依赖其他bean,在方法参数中加入即可
22 | @Bean
23 | public Test1 test1(Test test){
24 | Test1 test1 = new Test1();
25 | test1.setTest(test);
26 | return test1;
27 | }
28 |
29 | //或者在当前类使用@Autowired注解装配bean,方法参数就可以为空
30 | // @Autowired
31 | // private Test test;
32 | // @Bean
33 | // public Test1 test1(){
34 | // Test1 test1 = new Test1();
35 | // test1.setTest(test);
36 | // return test1;
37 | // }
38 | }
39 |
--------------------------------------------------------------------------------
/bean/src/test/java/demo/springboot/bean/BeanApplicationTests.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.bean;
2 |
3 | import demo.springboot.bean.bean.Test1;
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.test.context.junit4.SpringRunner;
9 |
10 | @RunWith(SpringRunner.class)
11 | @SpringBootTest
12 | public class BeanApplicationTests {
13 |
14 | @Autowired
15 | private demo.springboot.bean.bean.Test test;
16 | @Autowired
17 | private Test1 test1;
18 |
19 | @Test
20 | public void testBean() {
21 | test.setName("test");
22 | System.out.println(test);
23 | System.out.println(test1);
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/cache/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot-demo
7 | demo.springboot
8 | 1.0-SNAPSHOT
9 |
10 | cache
11 | cache
12 | Demo project for Spring Boot
13 |
14 |
15 | 1.8
16 |
17 |
18 |
19 |
20 | org.springframework.boot
21 | spring-boot-starter-web
22 |
23 |
24 |
25 |
26 | org.springframework.boot
27 | spring-boot-starter-cache
28 |
29 |
30 |
31 | org.springframework.boot
32 | spring-boot-starter-data-redis
33 |
34 |
35 |
36 |
37 | ${project.name}
38 |
39 |
40 | org.springframework.boot
41 | spring-boot-maven-plugin
42 |
43 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/cache/src/main/java/demo/springboot/cache/CacheApplication.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.cache;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.cache.annotation.EnableCaching;
6 |
7 | @SpringBootApplication
8 | @EnableCaching
9 | public class CacheApplication {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(CacheApplication.class, args);
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/cache/src/main/java/demo/springboot/cache/service/CacheService.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.cache.service;
2 |
3 | import org.springframework.cache.annotation.CacheEvict;
4 | import org.springframework.cache.annotation.CachePut;
5 | import org.springframework.cache.annotation.Cacheable;
6 | import org.springframework.stereotype.Service;
7 |
8 | /**
9 | * @author dean.lee
10 | */
11 | @Service
12 | public class CacheService {
13 |
14 | /**
15 | * 如果缓存中有则直接走缓存,如果没有则执行方法,将方法的返回值作为缓存
16 | * @param id
17 | * @return
18 | */
19 | @Cacheable(value = "cache-test", key = "'getName:' + #p0")
20 | public String getName(long id){
21 | System.out.println("等待3秒。。。。");
22 | try {
23 | Thread.sleep(3000L);
24 | } catch (InterruptedException e) {
25 | e.printStackTrace();
26 | }
27 |
28 | return id + ":name";
29 | }
30 |
31 | /**
32 | * 将方法的返回值更新到缓存
33 | * @param id
34 | * @return
35 | */
36 | @CachePut(value = "cache-test", key = "'getName:' + #p0")
37 | public String updateName(long id){
38 | System.out.println("更新名称");
39 | return id + ":nickname";
40 | }
41 |
42 | /**
43 | * 删除缓存
44 | * @param id
45 | */
46 | @CacheEvict(value = "cache-test", key = "'getName:' + #p0")
47 | public void deleteName(long id){
48 | System.out.println("删除名称");
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/cache/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | redis:
3 | #\u5730\u5740
4 | host: localhost
5 | #\u7AEF\u53E3
6 | port: 6379
7 | #\u7D22\u5F15\u5E93
8 | database: 1
9 | #\u5BC6\u7801
10 | password:
11 | #\u8D85\u65F6\u65F6\u95F4
12 | timeout: 5000ms
13 | cache:
14 | #\u8BBE\u7F6E\u7F13\u5B58\u7C7B\u578B
15 | type: redis
16 | redis:
17 | #\u7F13\u5B58\u5B58\u6D3B\u65F6\u95F4\uFF0C\u4E0D\u8BBE\u7F6E\u5219\u6CA1\u6709\u8FC7\u671F\u65F6\u95F4
18 | time-to-live: 1800000ms
19 |
--------------------------------------------------------------------------------
/cache/src/test/java/demo/springboot/cache/CacheApplicationTests.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.cache;
2 |
3 | import demo.springboot.cache.service.CacheService;
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.test.context.junit4.SpringRunner;
9 |
10 | @RunWith(SpringRunner.class)
11 | @SpringBootTest
12 | public class CacheApplicationTests {
13 |
14 | @Autowired
15 | private CacheService cacheService;
16 |
17 | @Test
18 | public void testCache() {
19 | System.out.println(cacheService.getName(1L));
20 | System.out.println(cacheService.getName(1L));
21 | cacheService.updateName(1L);
22 | System.out.println(cacheService.getName(1L));
23 | cacheService.deleteName(1L);
24 | System.out.println(cacheService.getName(1L));
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/dubbo/dubbo-consumer/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
6 | dubbo
7 | demo.springboot
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | dubbo-consumer
13 |
14 | dubbo-consumer
15 |
16 |
17 | 1.8
18 |
19 |
20 |
21 |
22 | demo.springboot
23 | dubbo-service
24 | 1.0-SNAPSHOT
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-web
30 |
31 |
32 |
33 | com.alibaba.boot
34 | dubbo-spring-boot-starter
35 | 0.2.0
36 |
37 |
38 |
39 |
40 |
41 | ${project.name}
42 |
43 |
44 | org.springframework.boot
45 | spring-boot-maven-plugin
46 |
47 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/dubbo/dubbo-consumer/src/main/java/demo/springboot/ConsumerApplication.java:
--------------------------------------------------------------------------------
1 | package demo.springboot;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * @author dean.lee
8 | */
9 | @SpringBootApplication
10 | public class ConsumerApplication {
11 |
12 | public static void main(String[] args) {
13 | SpringApplication.run(ConsumerApplication.class, args);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/dubbo/dubbo-consumer/src/main/java/demo/springboot/ctrl/TestController.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.ctrl;
2 |
3 | import com.alibaba.dubbo.config.annotation.Reference;
4 | import demo.springboot.domain.User;
5 | import demo.springboot.service.TestService;
6 | import org.springframework.web.bind.annotation.GetMapping;
7 | import org.springframework.web.bind.annotation.RestController;
8 |
9 | /**
10 | * @author dean.lee
11 | */
12 | @RestController
13 | public class TestController {
14 |
15 | @Reference(version = "${application.version}")
16 | private TestService testService;
17 |
18 | @GetMapping("/getString")
19 | public String getString(String src){
20 | return testService.getString(src);
21 | }
22 |
23 | @GetMapping("/getUser")
24 | public User getUser(String name, Integer age){
25 | return testService.getUser(name, age);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/dubbo/dubbo-consumer/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 8080
3 | servlet:
4 | context-path: /
5 |
6 | application:
7 | #版本号
8 | version: 1.0.0
9 |
10 | dubbo:
11 | application:
12 | #应用名称,每个dubbo应用的名称都是唯一的
13 | name: dubbo-consumer
14 | registry:
15 | #注册中心
16 | address: zookeeper://172.16.77.131:2181
17 |
--------------------------------------------------------------------------------
/dubbo/dubbo-producer/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
6 | dubbo
7 | demo.springboot
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | dubbo-producer
13 |
14 | dubbo-producer
15 |
16 |
17 | 1.8
18 |
19 |
20 |
21 |
22 | demo.springboot
23 | dubbo-service
24 | 1.0-SNAPSHOT
25 |
26 |
27 |
28 | com.alibaba.boot
29 | dubbo-spring-boot-starter
30 | 0.2.0
31 |
32 |
33 | org.springframework.boot
34 | spring-boot-starter
35 |
36 |
37 |
38 |
39 | ${project.name}
40 |
41 |
42 | org.springframework.boot
43 | spring-boot-maven-plugin
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/dubbo/dubbo-producer/src/main/java/demo/springboot/ProducerApplication.java:
--------------------------------------------------------------------------------
1 | package demo.springboot;
2 |
3 | import org.springframework.boot.WebApplicationType;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.boot.builder.SpringApplicationBuilder;
6 |
7 | /**
8 | * @author dean.lee
9 | */
10 | @SpringBootApplication
11 | public class ProducerApplication {
12 |
13 | public static void main(String[] args) {
14 | new SpringApplicationBuilder(ProducerApplication.class)
15 | .web(WebApplicationType.NONE) //非web应用
16 | .run(args);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/dubbo/dubbo-producer/src/main/java/demo/springboot/service/impl/TestServiceImpl.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.service.impl;
2 |
3 | import com.alibaba.dubbo.config.annotation.Service;
4 | import demo.springboot.domain.User;
5 | import demo.springboot.service.TestService;
6 |
7 | /**
8 | * @author dean.lee
9 | */
10 | @Service(version = "${application.version}")
11 | public class TestServiceImpl implements TestService {
12 | @Override
13 | public String getString(String src) {
14 | return src;
15 | }
16 |
17 | @Override
18 | public User getUser(String name, int age) {
19 | User user = new User();
20 | user.setName(name);
21 | user.setAge(age);
22 | return user;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/dubbo/dubbo-producer/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | application:
2 | #版本号
3 | version: 1.0.0
4 |
5 | dubbo:
6 | application:
7 | #应用名称,每个dubbo应用的名称都是唯一的
8 | name: dubbo-producer
9 | registry:
10 | #注册中心
11 | address: zookeeper://172.16.77.131:2181
12 | protocol:
13 | #协议名称
14 | name: dubbo
15 | #服务暴露端口
16 | port: 20880
17 | scan:
18 | #扫描服务注册bean
19 | basePackages: demo.springboot.service.impl
--------------------------------------------------------------------------------
/dubbo/dubbo-service/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
6 | dubbo
7 | demo.springboot
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | dubbo-service
13 |
14 | dubbo-service
15 |
16 |
17 | UTF-8
18 | 1.8
19 | 1.8
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/dubbo/dubbo-service/src/main/java/demo/springboot/domain/User.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.domain;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * @author dean.lee
7 | */
8 | public class User implements Serializable {
9 |
10 | private static final long serialVersionUID = -1563308959112481804L;
11 |
12 | private String name;
13 |
14 | private Integer age;
15 |
16 | public String getName() {
17 | return name;
18 | }
19 |
20 | public void setName(String name) {
21 | this.name = name;
22 | }
23 |
24 | public Integer getAge() {
25 | return age;
26 | }
27 |
28 | public void setAge(Integer age) {
29 | this.age = age;
30 | }
31 |
32 | @Override
33 | public String toString() {
34 | return "User{" +
35 | "name='" + name + '\'' +
36 | ", age='" + age + '\'' +
37 | '}';
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/dubbo/dubbo-service/src/main/java/demo/springboot/service/TestService.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.service;
2 |
3 | import demo.springboot.domain.User;
4 |
5 | /**
6 | * @author dean.lee
7 | */
8 | public interface TestService {
9 |
10 |
11 | String getString(String src);
12 |
13 | User getUser(String name, int age);
14 | }
15 |
--------------------------------------------------------------------------------
/dubbo/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
6 | springboot-demo
7 | demo.springboot
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | dubbo
13 | pom
14 |
15 |
16 | dubbo-service
17 | dubbo-producer
18 | dubbo-consumer
19 |
20 |
21 |
22 | UTF-8
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/jedis-spring-boot-starter/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
6 | springboot-demo
7 | demo.springboot
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | jedis-spring-boot-starter
13 |
14 | jedis-spring-boot-starter
15 |
16 |
17 | UTF-8
18 | 1.8
19 | 1.8
20 |
21 |
22 |
23 |
24 | org.springframework.boot
25 | spring-boot-autoconfigure
26 | 2.0.6.RELEASE
27 |
28 |
29 |
30 | redis.clients
31 | jedis
32 | 3.0.0
33 | provided
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/jedis-spring-boot-starter/src/main/java/demo/springboot/JedisAutoConfiguration.java:
--------------------------------------------------------------------------------
1 | package demo.springboot;
2 |
3 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
4 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
5 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
6 | import org.springframework.context.annotation.Bean;
7 | import org.springframework.context.annotation.Configuration;
8 | import redis.clients.jedis.Jedis;
9 |
10 | /**
11 | * @author dean.lee
12 | */
13 | @Configuration
14 | //当类路径下有Jedis依赖时进行配置
15 | @ConditionalOnClass(Jedis.class)
16 | //引入JedisProperties对象
17 | @EnableConfigurationProperties(JedisProperties.class)
18 | public class JedisAutoConfiguration {
19 |
20 | @Bean
21 | //当spring容器中没有该Bean时注册
22 | @ConditionalOnMissingBean(Jedis.class)
23 | public Jedis jedis(JedisProperties jedisProperties){
24 | //设置地址端口
25 | Jedis jedis = new Jedis(jedisProperties.getHost(), jedisProperties.getPort());
26 | //验证密码
27 | jedis.auth(jedisProperties.getPassword());
28 | //设置索引库
29 | jedis.select(jedisProperties.getDatabase());
30 | jedis.connect();
31 | return jedis;
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/jedis-spring-boot-starter/src/main/java/demo/springboot/JedisProperties.java:
--------------------------------------------------------------------------------
1 | package demo.springboot;
2 |
3 | import org.springframework.boot.context.properties.ConfigurationProperties;
4 |
5 | /**
6 | * @author dean.lee
7 | */
8 | //获取配置文件中的属性
9 | @ConfigurationProperties(prefix = "jedis")
10 | public class JedisProperties {
11 |
12 | private int database = 0;
13 | private String host = "localhost";
14 | private String password = "";
15 | private int port = 6379;
16 |
17 | public int getDatabase() {
18 | return database;
19 | }
20 |
21 | public void setDatabase(int database) {
22 | this.database = database;
23 | }
24 |
25 | public String getHost() {
26 | return host;
27 | }
28 |
29 | public void setHost(String host) {
30 | this.host = host;
31 | }
32 |
33 | public String getPassword() {
34 | return password;
35 | }
36 |
37 | public void setPassword(String password) {
38 | this.password = password;
39 | }
40 |
41 | public int getPort() {
42 | return port;
43 | }
44 |
45 | public void setPort(int port) {
46 | this.port = port;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/jedis-spring-boot-starter/src/main/resources/META-INF/spring.factories:
--------------------------------------------------------------------------------
1 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=demo.springboot.JedisAutoConfiguration
--------------------------------------------------------------------------------
/limiter/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot-demo
7 | demo.springboot
8 | 1.0-SNAPSHOT
9 |
10 | limiter
11 | limiter
12 | Demo project for Spring Boot
13 |
14 |
15 | 1.8
16 |
17 |
18 |
19 |
20 | org.springframework.boot
21 | spring-boot-starter-web
22 |
23 |
24 |
25 | org.springframework.boot
26 | spring-boot-starter-aop
27 |
28 |
29 |
30 | org.springframework.boot
31 | spring-boot-starter-data-redis
32 |
33 |
34 |
35 |
36 | ${project.name}
37 |
38 |
39 | org.springframework.boot
40 | spring-boot-maven-plugin
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/limiter/src/main/java/demo/springboot/limiter/LimiterApplication.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.limiter;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class LimiterApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(LimiterApplication.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/limiter/src/main/java/demo/springboot/limiter/annotation/Limiter.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.limiter.annotation;
2 |
3 | import java.lang.annotation.Documented;
4 | import java.lang.annotation.ElementType;
5 | import java.lang.annotation.Retention;
6 | import java.lang.annotation.RetentionPolicy;
7 | import java.lang.annotation.Target;
8 |
9 | /**
10 | * @author dean.lee
11 | */
12 | @Target(ElementType.METHOD)
13 | @Retention(RetentionPolicy.RUNTIME)
14 | @Documented
15 | public @interface Limiter {
16 |
17 | /**
18 | * 从第一次访问接口的时间到cycle周期时间内,无法超过frequency次
19 | *
20 | * @return
21 | */
22 | int frequency() default 20;
23 |
24 | /**
25 | * 周期时间,单位ms:
26 | * 默认周期时间为一分钟
27 | *
28 | * @return
29 | */
30 | long cycle() default 60 * 1000;
31 |
32 | /**
33 | * 返回的错误信息
34 | *
35 | * @return
36 | */
37 | String message() default "请求过于频繁";
38 |
39 | /**
40 | * 到期时间,单位s:
41 | * 如果在cycle周期时间内超过frequency次,则默认1分钟内无法继续访问
42 | * @return
43 | */
44 | long expireTime() default 1 * 60;
45 | }
46 |
--------------------------------------------------------------------------------
/limiter/src/main/java/demo/springboot/limiter/aspect/LimitingAspect.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.limiter.aspect;
2 |
3 | import demo.springboot.limiter.annotation.Limiter;
4 | import demo.springboot.limiter.exception.FrequentRequestsException;
5 | import demo.springboot.limiter.util.WebUtil;
6 | import org.aspectj.lang.ProceedingJoinPoint;
7 | import org.aspectj.lang.annotation.Around;
8 | import org.aspectj.lang.annotation.Aspect;
9 | import org.aspectj.lang.annotation.Pointcut;
10 | import org.springframework.beans.factory.annotation.Autowired;
11 | import org.springframework.data.redis.core.RedisTemplate;
12 | import org.springframework.stereotype.Component;
13 |
14 | import java.util.concurrent.TimeUnit;
15 |
16 | /**
17 | * ip防刷api功能实现。
18 | * 该功能使用redis作为存储,方便在集群中使用。
19 | * 如果是单项目部署,可以将redis换成本地缓存。
20 | *
21 | * @author dean.lee
22 | *
23 | */
24 | @Aspect
25 | @Component
26 | public class LimitingAspect {
27 | private static final String LIMITING_KEY = "limiting:%s:%s";
28 | private static final String LIMITING_BEGINTIME = "beginTime";
29 | private static final String LIMITING_EXFREQUENCY = "exFrequency";
30 |
31 | @Autowired
32 | private RedisTemplate redisTemplate;
33 |
34 | @Pointcut("@annotation(limiter)")
35 | public void pointcut(Limiter limiter) {
36 | }
37 |
38 | @Around("pointcut(limiter)")
39 | public Object around(ProceedingJoinPoint pjp, Limiter limiter) throws Throwable {
40 | //获取请求的ip和方法
41 | String ipAddress = WebUtil.getIpAddress();
42 | String methodName = pjp.getSignature().toLongString();
43 |
44 | //获取方法的访问周期和频率
45 | long cycle = limiter.cycle();
46 | int frequency = limiter.frequency();
47 | long currentTime = System.currentTimeMillis();
48 |
49 | //获取redis中周期内第一次访问方法的时间和执行的次数
50 | Long beginTimeLong = (Long) redisTemplate.opsForHash().get(String.format(LIMITING_KEY, ipAddress, methodName), LIMITING_BEGINTIME);
51 | Integer exFrequencyLong = (Integer) redisTemplate.opsForHash().get(String.format(LIMITING_KEY, ipAddress, methodName), LIMITING_EXFREQUENCY);
52 |
53 | long beginTime = beginTimeLong == null ? 0L : beginTimeLong;
54 | int exFrequency = exFrequencyLong == null ? 0 : exFrequencyLong;
55 |
56 | //如果当前时间减去周期内第一次访问方法的时间大于周期时间,则正常访问
57 | //并将周期内第一次访问方法的时间和执行次数初始化
58 | if (currentTime - beginTime > cycle) {
59 | redisTemplate.opsForHash().put(String.format(LIMITING_KEY, ipAddress, methodName), LIMITING_BEGINTIME, currentTime);
60 | redisTemplate.opsForHash().put(String.format(LIMITING_KEY, ipAddress, methodName), LIMITING_EXFREQUENCY, 1);
61 | redisTemplate.expire(String.format(LIMITING_KEY, ipAddress, methodName), limiter.expireTime(), TimeUnit.SECONDS);
62 | return pjp.proceed();
63 | } else {
64 | //如果在周期时间内,执行次数小于频率,则正常访问
65 | //并将执行次数加一
66 | if (exFrequency < frequency) {
67 | redisTemplate.opsForHash().put(String.format(LIMITING_KEY, ipAddress, methodName), LIMITING_EXFREQUENCY, exFrequency + 1);
68 | redisTemplate.expire(String.format(LIMITING_KEY, ipAddress, methodName), limiter.expireTime(), TimeUnit.SECONDS);
69 | return pjp.proceed();
70 | } else {
71 | //否则抛出访问频繁异常
72 | throw new FrequentRequestsException(limiter.message());
73 | }
74 | }
75 | }
76 | }
--------------------------------------------------------------------------------
/limiter/src/main/java/demo/springboot/limiter/bean/BeanLoad.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.limiter.bean;
2 |
3 | import org.springframework.context.annotation.Bean;
4 | import org.springframework.context.annotation.Configuration;
5 | import org.springframework.data.redis.connection.RedisConnectionFactory;
6 | import org.springframework.data.redis.core.RedisTemplate;
7 | import org.springframework.data.redis.serializer.StringRedisSerializer;
8 |
9 | /**
10 | * @author dean.lee
11 | */
12 | @Configuration
13 | public class BeanLoad {
14 | /**
15 | * 设置redisTemplate key序列化
16 | * @param factory
17 | * @return
18 | */
19 | @Bean
20 | public RedisTemplate redisTemplate(RedisConnectionFactory factory){
21 | RedisTemplate redisTemplate = new RedisTemplate<>();
22 | redisTemplate.setConnectionFactory(factory);
23 |
24 | StringRedisSerializer keySerializer = new StringRedisSerializer();
25 | redisTemplate.setKeySerializer(keySerializer);
26 | redisTemplate.setHashKeySerializer(keySerializer);
27 |
28 | redisTemplate.afterPropertiesSet();
29 | return redisTemplate;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/limiter/src/main/java/demo/springboot/limiter/ctrl/TestController.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.limiter.ctrl;
2 |
3 | import demo.springboot.limiter.annotation.Limiter;
4 | import org.springframework.web.bind.annotation.GetMapping;
5 | import org.springframework.web.bind.annotation.RestController;
6 |
7 | /**
8 | * @author dean.lee
9 | */
10 | @RestController
11 | public class TestController {
12 |
13 | //限制在周期内只能访问3次
14 | @Limiter(frequency = 3)
15 | @GetMapping("getString")
16 | public String getString(){
17 | return "hello";
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/limiter/src/main/java/demo/springboot/limiter/exception/FrequentRequestsException.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.limiter.exception;
2 |
3 | /**
4 | * @author dean.lee
5 | */
6 | public class FrequentRequestsException extends RuntimeException {
7 |
8 | public FrequentRequestsException() {
9 | super();
10 | }
11 |
12 | public FrequentRequestsException(String message) {
13 | super(message);
14 | }
15 |
16 | public FrequentRequestsException(String message, Throwable cause) {
17 | super(message, cause);
18 | }
19 |
20 | public FrequentRequestsException(Throwable cause) {
21 | super(cause);
22 | }
23 |
24 | protected FrequentRequestsException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
25 | super(message, cause, enableSuppression, writableStackTrace);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/limiter/src/main/java/demo/springboot/limiter/util/WebUtil.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.limiter.util;
2 |
3 | import org.springframework.web.context.request.RequestContextHolder;
4 | import org.springframework.web.context.request.ServletRequestAttributes;
5 |
6 | import javax.servlet.http.HttpServletRequest;
7 | import javax.servlet.http.HttpServletResponse;
8 |
9 | /**
10 | * @author dean.lee
11 | */
12 | public class WebUtil {
13 |
14 | private static final String UNKNOWN = "unknown";
15 |
16 | //获取request
17 | public static HttpServletRequest getRequest() {
18 | return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
19 | }
20 |
21 | //获取response
22 | public static HttpServletResponse getResponse() {
23 | return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
24 | }
25 |
26 | public static String getIpAddress() {
27 | HttpServletRequest request = getRequest();
28 | String ip = request.getHeader("x-forwarded-for");
29 | if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
30 | ip = request.getHeader("Proxy-Client-IP");
31 | }
32 |
33 | if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
34 | ip = request.getHeader("WL-Proxy-Client-IP");
35 | }
36 |
37 | if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
38 | ip = request.getRemoteAddr();
39 | }
40 |
41 | if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
42 | ip = request.getHeader("HTTP_CLIENT_IP");
43 | }
44 |
45 | if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
46 | ip = request.getHeader("X-Real-IP");
47 | }
48 |
49 | if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
50 | ip = request.getHeader("HTTP_X_FORWARDED_FOR");
51 | }
52 |
53 | String regex = ",";
54 | if (ip != null && ip.indexOf(regex) > 0) {
55 | ip = ip.split(regex)[0];
56 | }
57 |
58 | return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
59 | }
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/limiter/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | logging:
2 | level:
3 | root: error
4 |
5 | spring:
6 | redis:
7 | host: localhost
8 | port: 6379
9 | database: 1
10 | password:
11 | timeout: 5000ms
12 |
--------------------------------------------------------------------------------
/mail/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot-demo
7 | demo.springboot
8 | 1.0-SNAPSHOT
9 |
10 | mail
11 | mail
12 | Demo project for Spring Boot
13 |
14 |
15 | 1.8
16 |
17 |
18 |
19 |
20 | org.springframework.boot
21 | spring-boot-starter-mail
22 |
23 |
24 | org.springframework.boot
25 | spring-boot-starter-web
26 |
27 |
28 |
29 |
30 | ${project.name}
31 |
32 |
33 | org.springframework.boot
34 | spring-boot-maven-plugin
35 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/mail/src/main/java/demo/springboot/mail/MailApplication.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.mail;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class MailApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(MailApplication.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/mail/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | mail:
3 | default-encoding: UTF-8
4 | host: smtp.163.com
5 | port: 465
6 | username: username@163.com
7 | password: password
8 | properties:
9 | mail.smtp.ssl.enable: true
--------------------------------------------------------------------------------
/mail/src/test/java/demo/springboot/mail/MailApplicationTests.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.mail;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.beans.factory.annotation.Value;
7 | import org.springframework.boot.test.context.SpringBootTest;
8 | import org.springframework.mail.SimpleMailMessage;
9 | import org.springframework.mail.javamail.JavaMailSender;
10 | import org.springframework.test.context.junit4.SpringRunner;
11 |
12 | @RunWith(SpringRunner.class)
13 | @SpringBootTest
14 | public class MailApplicationTests {
15 |
16 | @Autowired
17 | private JavaMailSender mailSender;
18 |
19 | @Value("${spring.mail.username}")
20 | private String username;
21 |
22 | @Test
23 | public void testMail() {
24 | //建立邮件消息
25 | SimpleMailMessage mailMessage = new SimpleMailMessage();
26 | //发送者
27 | mailMessage.setFrom(username);
28 | //接收者
29 | mailMessage.setTo("dean.lee@aliyun.com");
30 | //发送的标题
31 | mailMessage.setSubject("主题");
32 | //发送的内容
33 | mailMessage.setText("内容");
34 | //发送邮件
35 | mailSender.send(mailMessage);
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/multiple-datasource/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot-demo
7 | demo.springboot
8 | 1.0-SNAPSHOT
9 |
10 | multiple-datasource
11 | multiple-datasource
12 | Demo project for Spring Boot
13 |
14 |
15 | 1.8
16 |
17 |
18 |
19 |
20 | org.springframework.boot
21 | spring-boot-starter-web
22 |
23 |
24 | org.mybatis.spring.boot
25 | mybatis-spring-boot-starter
26 | 2.0.1
27 |
28 |
29 | com.alibaba
30 | druid-spring-boot-starter
31 | 1.1.14
32 |
33 |
34 |
35 | mysql
36 | mysql-connector-java
37 | runtime
38 |
39 |
40 |
41 | org.projectlombok
42 | lombok
43 | true
44 |
45 |
46 |
47 |
48 | ${project.name}
49 |
50 |
51 | org.springframework.boot
52 | spring-boot-maven-plugin
53 |
54 |
55 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/multiple-datasource/src/main/java/demo/springboot/multipledatasource/MultipleDatasourceApplication.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.multipledatasource;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class MultipleDatasourceApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(MultipleDatasourceApplication.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/multiple-datasource/src/main/java/demo/springboot/multipledatasource/config/DataSource1Config.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.multipledatasource.config;
2 |
3 | import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
4 | import org.apache.ibatis.session.SqlSessionFactory;
5 | import org.mybatis.spring.SqlSessionFactoryBean;
6 | import org.mybatis.spring.SqlSessionTemplate;
7 | import org.mybatis.spring.annotation.MapperScan;
8 | import org.springframework.beans.factory.annotation.Qualifier;
9 | import org.springframework.boot.context.properties.ConfigurationProperties;
10 | import org.springframework.context.annotation.Bean;
11 | import org.springframework.context.annotation.Configuration;
12 | import org.springframework.context.annotation.Primary;
13 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
14 | import org.springframework.jdbc.datasource.DataSourceTransactionManager;
15 |
16 | import javax.sql.DataSource;
17 |
18 | @Configuration
19 | //指定扫描的dao包和SqlSession实例
20 | @MapperScan(basePackages = "demo.springboot.multipledatasource.dao1", sqlSessionTemplateRef="oneSqlSessionTemplate")
21 | public class DataSource1Config {
22 |
23 | /**
24 | * 连接池
25 | * @return
26 | */
27 | @Bean
28 | @Primary
29 | @ConfigurationProperties("spring.datasource.druid.one")
30 | public DataSource oneDataSource(){
31 | return DruidDataSourceBuilder.create().build();
32 | }
33 |
34 | /**
35 | * SqlSessionFactory
36 | * @param dataSource
37 | * @return
38 | * @throws Exception
39 | */
40 | @Bean
41 | @Primary
42 | public SqlSessionFactory oneSqlSessionFactory(@Qualifier("oneDataSource") DataSource dataSource) throws Exception {
43 | SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
44 | bean.setDataSource(dataSource);
45 | //mapper文件位置
46 | bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/one/*.xml"));
47 | return bean.getObject();
48 | }
49 |
50 | /**
51 | * 事务管理器
52 | * 注解@Primary:当程序中使用@Transactional时,优先使用该事务管理器。
53 | * 如果不使用@Primary注解,需要在@Transactional(value="oneTransactionManager")指定事务管理器
54 | * @param dataSource
55 | * @return
56 | */
57 | @Bean
58 | @Primary
59 | public DataSourceTransactionManager oneTransactionManager(@Qualifier("oneDataSource")DataSource dataSource){
60 | return new DataSourceTransactionManager(dataSource);
61 | }
62 |
63 | /**
64 | * SqlSession实例
65 | * @param sqlSessionFactory
66 | * @return
67 | */
68 | @Bean
69 | @Primary
70 | public SqlSessionTemplate oneSqlSessionTemplate(@Qualifier("oneSqlSessionFactory")SqlSessionFactory sqlSessionFactory) {
71 | return new SqlSessionTemplate(sqlSessionFactory);
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/multiple-datasource/src/main/java/demo/springboot/multipledatasource/config/DataSource2Config.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.multipledatasource.config;
2 |
3 | import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
4 | import org.apache.ibatis.session.SqlSessionFactory;
5 | import org.mybatis.spring.SqlSessionFactoryBean;
6 | import org.mybatis.spring.SqlSessionTemplate;
7 | import org.mybatis.spring.annotation.MapperScan;
8 | import org.springframework.beans.factory.annotation.Qualifier;
9 | import org.springframework.boot.context.properties.ConfigurationProperties;
10 | import org.springframework.context.annotation.Bean;
11 | import org.springframework.context.annotation.Configuration;
12 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
13 | import org.springframework.jdbc.datasource.DataSourceTransactionManager;
14 |
15 | import javax.sql.DataSource;
16 |
17 | @Configuration
18 | @MapperScan(basePackages = "demo.springboot.multipledatasource.dao2", sqlSessionTemplateRef="twoSqlSessionTemplate")
19 | public class DataSource2Config {
20 |
21 | @Bean
22 | @ConfigurationProperties("spring.datasource.druid.two")
23 | public DataSource twoDataSource(){
24 | return DruidDataSourceBuilder.create().build();
25 | }
26 |
27 | @Bean
28 | public SqlSessionFactory twoSqlSessionFactory(@Qualifier("twoDataSource") DataSource dataSource) throws Exception{
29 | SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
30 | bean.setDataSource(dataSource);
31 | bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/two/*.xml"));
32 | return bean.getObject();
33 | }
34 |
35 | @Bean
36 | public DataSourceTransactionManager twoTransactionManager(@Qualifier("twoDataSource")DataSource dataSource){
37 | return new DataSourceTransactionManager(dataSource);
38 | }
39 |
40 | @Bean
41 | public SqlSessionTemplate twoSqlSessionTemplate(@Qualifier("twoSqlSessionFactory")SqlSessionFactory sqlSessionFactory) {
42 | return new SqlSessionTemplate(sqlSessionFactory);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/multiple-datasource/src/main/java/demo/springboot/multipledatasource/dao1/User1DAO.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.multipledatasource.dao1;
2 |
3 | import demo.springboot.multipledatasource.domain.User;
4 |
5 | import java.util.List;
6 |
7 | public interface User1DAO {
8 |
9 | List selectAll();
10 | }
11 |
--------------------------------------------------------------------------------
/multiple-datasource/src/main/java/demo/springboot/multipledatasource/dao2/User2DAO.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.multipledatasource.dao2;
2 |
3 | import demo.springboot.multipledatasource.domain.User;
4 |
5 | import java.util.List;
6 |
7 | public interface User2DAO {
8 |
9 | List selectAll();
10 | }
11 |
--------------------------------------------------------------------------------
/multiple-datasource/src/main/java/demo/springboot/multipledatasource/domain/User.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.multipledatasource.domain;
2 |
3 | import lombok.Data;
4 |
5 | @Data
6 | public class User {
7 |
8 | private String username;
9 |
10 | private String age;
11 | }
12 |
--------------------------------------------------------------------------------
/multiple-datasource/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | datasource:
3 | #使用druid连接池
4 | druid:
5 | #数据源1的名称
6 | one:
7 | driver-class-name: com.mysql.jdbc.Driver
8 | url: jdbc:mysql://192.168.211.128:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
9 | username: root
10 | password: root
11 | #数据源2的名称
12 | two:
13 | driver-class-name: com.mysql.jdbc.Driver
14 | url: jdbc:mysql://192.168.211.129:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
15 | username: root
16 | password: root
17 | logging:
18 | level:
19 | root: error
20 |
--------------------------------------------------------------------------------
/multiple-datasource/src/main/resources/mapper/one/User1Mapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
15 |
--------------------------------------------------------------------------------
/multiple-datasource/src/main/resources/mapper/two/User2Mapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
15 |
--------------------------------------------------------------------------------
/multiple-datasource/src/test/java/demo/springboot/multipledatasource/MultipleDatasourceApplicationTests.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.multipledatasource;
2 |
3 | import demo.springboot.multipledatasource.dao1.User1DAO;
4 | import demo.springboot.multipledatasource.dao2.User2DAO;
5 | import demo.springboot.multipledatasource.domain.User;
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 java.util.List;
13 |
14 | @RunWith(SpringRunner.class)
15 | @SpringBootTest
16 | public class MultipleDatasourceApplicationTests {
17 |
18 | @Autowired
19 | private User1DAO user1DAO;
20 |
21 | @Autowired
22 | private User2DAO user2DAO;
23 |
24 | @Test
25 | public void test() {
26 | List users1 = user1DAO.selectAll();
27 | System.out.println(users1);
28 | List users2 = user2DAO.selectAll();
29 | System.out.println(users2);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/mybatis-mapper-pagehelper/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot-demo
7 | demo.springboot
8 | 1.0-SNAPSHOT
9 |
10 | mybatis-mapper-pagehelper
11 | mybatis-mapper-pagehelper
12 | Demo project for Spring Boot
13 |
14 |
15 | 1.8
16 |
17 |
18 |
19 |
20 | org.springframework.boot
21 | spring-boot-starter-web
22 |
23 |
24 |
25 |
26 | org.mybatis.spring.boot
27 | mybatis-spring-boot-starter
28 | 2.0.0
29 |
30 |
31 |
32 | tk.mybatis
33 | mapper-spring-boot-starter
34 | 2.0.0
35 |
36 |
37 |
38 | mysql
39 | mysql-connector-java
40 | runtime
41 |
42 |
43 |
44 | com.alibaba
45 | druid-spring-boot-starter
46 | 1.1.14
47 |
48 |
49 |
50 | com.github.pagehelper
51 | pagehelper-spring-boot-starter
52 | 1.2.10
53 |
54 |
55 |
56 | org.projectlombok
57 | lombok
58 | true
59 |
60 |
61 |
62 |
63 | ${project.name}
64 |
65 |
66 | org.springframework.boot
67 | spring-boot-maven-plugin
68 |
69 |
70 |
71 |
72 |
73 |
--------------------------------------------------------------------------------
/mybatis-mapper-pagehelper/src/main/java/demo/springboot/mybatismapperpagehelper/MybatisMapperPagehelperApplication.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.mybatismapperpagehelper;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import tk.mybatis.spring.annotation.MapperScan;
6 |
7 | @SpringBootApplication
8 | //这里使用的扫描注解不是mybatis的,是通用mapper的注解
9 | @MapperScan("demo.springboot.mybatismapperpagehelper.dao")
10 | public class MybatisMapperPagehelperApplication {
11 |
12 | public static void main(String[] args) {
13 | SpringApplication.run(MybatisMapperPagehelperApplication.class, args);
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/mybatis-mapper-pagehelper/src/main/java/demo/springboot/mybatismapperpagehelper/basedao/BaseMapper.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.mybatismapperpagehelper.basedao;
2 |
3 | import tk.mybatis.mapper.common.Mapper;
4 | import tk.mybatis.mapper.common.MySqlMapper;
5 |
6 | /**
7 | * @author dean.lee
8 | */
9 | //继承tkmapper中的接口,更多可查看官方文档
10 | //Mapper是基础的增删改查,根据Example查询等
11 | //MySqlMapper是对mysql的一些操作
12 | public interface BaseMapper extends Mapper, MySqlMapper {
13 | }
14 |
--------------------------------------------------------------------------------
/mybatis-mapper-pagehelper/src/main/java/demo/springboot/mybatismapperpagehelper/dao/SysUserDAO.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.mybatismapperpagehelper.dao;
2 |
3 | import demo.springboot.mybatismapperpagehelper.basedao.BaseMapper;
4 | import demo.springboot.mybatismapperpagehelper.domain.SysUser;
5 |
6 | /**
7 | * @author dean.lee
8 | */
9 | public interface SysUserDAO extends BaseMapper {
10 | }
11 |
--------------------------------------------------------------------------------
/mybatis-mapper-pagehelper/src/main/java/demo/springboot/mybatismapperpagehelper/domain/SysUser.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.mybatismapperpagehelper.domain;
2 |
3 | import lombok.Data;
4 |
5 | import javax.persistence.Id;
6 |
7 | /**
8 | * @author dean.lee
9 | */
10 | @Data
11 | public class SysUser {
12 | //根据主键查询时必须要有@Id注解,否则会报错
13 | @Id
14 | private Long id;
15 |
16 | private String username;
17 |
18 | private String password;
19 |
20 | private String nickname;
21 |
22 | private Integer age;
23 | }
24 |
--------------------------------------------------------------------------------
/mybatis-mapper-pagehelper/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 8080
3 |
4 | spring:
5 | datasource:
6 | type: com.alibaba.druid.pool.DruidDataSource
7 | driver-class-name: com.mysql.jdbc.Driver
8 | url: jdbc:mysql://106.14.186.54:3306/admin?useUnicode=true&characterEncoding=UTF-8&useSSL=false
9 | username: root
10 | password: mysql4aliyun
11 |
12 | mybatis:
13 | #\u52A0\u8F7Dmapper\u6587\u4EF6
14 | mapper-locations: classpath:mapper/*.xml
15 | #\u8BBE\u7F6E\u5B9E\u4F53\u7C7B\u522B\u540D
16 | #type-aliases-package: demo.springboot.mybatismapperpagehelper.domain
17 | #\u52A0\u8F7Dmybatis\u5168\u5C40\u914D\u7F6E\u6587\u4EF6
18 | #config-location: classpath:mybatis/SqlMapConfig.xml
19 |
20 | mapper:
21 | #\u901A\u7528mapper\u7684\u7C7B\uFF0C\u53EF\u4EE5\u591A\u4E2A\uFF0C\u4E0D\u8981\u548C\u4E1A\u52A1dao\u653E\u5230\u4E00\u8D77
22 | mappers: demo.springboot.mybatismapperpagehelper.basedao.BaseMapper
23 |
24 | logging:
25 | level:
26 | root: error
27 | demo.springboot: debug
28 |
--------------------------------------------------------------------------------
/mybatis-mapper-pagehelper/src/test/java/demo/springboot/mybatismapperpagehelper/MybatisMapperPagehelperApplicationTests.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.mybatismapperpagehelper;
2 |
3 | import com.github.pagehelper.PageHelper;
4 | import com.github.pagehelper.PageInfo;
5 | import demo.springboot.mybatismapperpagehelper.dao.SysUserDAO;
6 | import demo.springboot.mybatismapperpagehelper.domain.SysUser;
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 | import org.springframework.beans.factory.annotation.Autowired;
10 | import org.springframework.boot.test.context.SpringBootTest;
11 | import org.springframework.test.context.junit4.SpringRunner;
12 |
13 | import java.util.List;
14 |
15 | @RunWith(SpringRunner.class)
16 | @SpringBootTest
17 | public class MybatisMapperPagehelperApplicationTests {
18 |
19 | @Autowired
20 | private SysUserDAO userDAO;
21 |
22 | @Test
23 | public void testSelectByPrimaryKey() {
24 | //根据主键查询
25 | SysUser sysUser = userDAO.selectByPrimaryKey(1L);
26 | System.out.println(sysUser);
27 | }
28 |
29 | @Test
30 | public void testSelectAll() {
31 | //查询所有数据
32 | List sysUsers = userDAO.selectAll();
33 | System.out.println(sysUsers);
34 | }
35 |
36 | @Test
37 | public void testPageHelper(){
38 | //第一个参数为页数,第二个为条数。对这个方法后的第一个sql生效,
39 | //单表分页,一对一分页正确。一对多,多对多分页会出现错误
40 | //PageHelper.startPage(1, 10);
41 | //根据id倒序排序
42 | PageHelper.startPage(1, 2, "id desc");
43 | List sysUsers = userDAO.selectAll();
44 |
45 | //获取分页数据
46 | PageInfo pageInfo = new PageInfo<>(sysUsers);
47 | //获取总条数
48 | System.out.println(pageInfo.getTotal());
49 | //获取分页页数
50 | System.out.println(pageInfo.getPageNum());
51 | //获取分页条数
52 | System.out.println(pageInfo.getPageSize());
53 | //获取分页数据,即上面查询到的sysUsers
54 | System.out.println(pageInfo.getList());
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/mybatis/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot-demo
7 | demo.springboot
8 | 1.0-SNAPSHOT
9 |
10 | mybatis
11 | mybatis
12 | Demo project for Spring Boot
13 |
14 |
15 | 1.8
16 |
17 |
18 |
19 |
20 | org.springframework.boot
21 | spring-boot-starter-web
22 |
23 |
24 | org.mybatis.spring.boot
25 | mybatis-spring-boot-starter
26 | 2.0.1
27 |
28 |
29 | com.alibaba
30 | druid-spring-boot-starter
31 | 1.1.14
32 |
33 |
34 |
35 | mysql
36 | mysql-connector-java
37 | runtime
38 |
39 |
40 |
41 | org.projectlombok
42 | lombok
43 | true
44 |
45 |
46 |
47 |
48 | ${project.name}
49 |
50 |
51 | org.springframework.boot
52 | spring-boot-maven-plugin
53 |
54 |
55 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/mybatis/src/main/java/demo/springboot/mybatis/MybatisApplication.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.mybatis;
2 |
3 | import org.mybatis.spring.annotation.MapperScan;
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 |
7 | @SpringBootApplication
8 | @MapperScan("demo.springboot.mybatis.dao")
9 | public class MybatisApplication {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(MybatisApplication.class, args);
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/mybatis/src/main/java/demo/springboot/mybatis/dao/SysUserMapper.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.mybatis.dao;
2 |
3 | import demo.springboot.mybatis.domain.SysUser;
4 | import org.apache.ibatis.annotations.Param;
5 | import org.apache.ibatis.annotations.Update;
6 |
7 | /**
8 | * @author dean.lee
9 | */
10 | public interface SysUserMapper {
11 | SysUser selectById(long id);
12 |
13 | @Update("update sys_user set age = #{age} where id = #{id}")
14 | int updateAgeById(@Param("age") int age, @Param("id") long id);
15 | }
16 |
--------------------------------------------------------------------------------
/mybatis/src/main/java/demo/springboot/mybatis/domain/SysUser.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.mybatis.domain;
2 |
3 | import lombok.Data;
4 |
5 | /**
6 | * @author dean.lee
7 | */
8 | @Data
9 | public class SysUser {
10 | private Long id;
11 | private String username;
12 | private String nickname;
13 | private Integer age;
14 | }
15 |
--------------------------------------------------------------------------------
/mybatis/src/main/java/demo/springboot/mybatis/service/UserService.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.mybatis.service;
2 |
3 | import demo.springboot.mybatis.dao.SysUserMapper;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.stereotype.Service;
6 | import org.springframework.transaction.annotation.Transactional;
7 |
8 | /**
9 | * @author dean.lee
10 | */
11 | @Service
12 | public class UserService {
13 | @Autowired
14 | private SysUserMapper userMapper;
15 |
16 | @Transactional(rollbackFor = RuntimeException.class)
17 | public void updateUserAge(){
18 | userMapper.updateAgeById(40, 1L);
19 | int a = 1/0;
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/mybatis/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | datasource:
3 | type: com.alibaba.druid.pool.DruidDataSource
4 | driver-class-name: com.mysql.jdbc.Driver
5 | url: jdbc:mysql://localhost:3306/admin?useUnicode=true&characterEncoding=UTF-8&useSSL=false
6 | username: root
7 | password: root
8 |
9 | mybatis:
10 | #加载mapper文件
11 | mapper-locations: classpath:mapper/*.xml
12 | #设置实体类别名
13 | #type-aliases-package: demo.springboot.mybatis.domain
14 | #加载mybatis全局配置文件
15 | #config-location: classpath:mybatis/SqlMapConfig.xml
--------------------------------------------------------------------------------
/mybatis/src/main/resources/mapper/SysUserMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
18 |
--------------------------------------------------------------------------------
/mybatis/src/main/resources/mybatis/SqlMapConfig.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/mybatis/src/test/java/demo/springboot/mybatis/MybatisApplicationTests.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.mybatis;
2 |
3 | import demo.springboot.mybatis.dao.SysUserMapper;
4 | import demo.springboot.mybatis.domain.SysUser;
5 | import demo.springboot.mybatis.service.UserService;
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 | @RunWith(SpringRunner.class)
13 | @SpringBootTest
14 | public class MybatisApplicationTests {
15 |
16 | @Autowired
17 | private SysUserMapper userMapper;
18 |
19 | @Autowired
20 | private UserService userService;
21 |
22 | @Test
23 | public void testMybatis() {
24 | SysUser sysUser = userMapper.selectById(1L);
25 | System.out.println(sysUser);
26 | }
27 |
28 | @Test
29 | public void testTransactional(){
30 | System.out.println(userMapper.selectById(1L));
31 |
32 | try {
33 | userService.updateUserAge();
34 | }catch (RuntimeException e){
35 |
36 | }
37 |
38 | System.out.println(userMapper.selectById(1L));
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/mysql/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | springboot-demo
7 | demo.springboot
8 | 1.0-SNAPSHOT
9 |
10 | mysql
11 | mysql
12 | Demo project for Spring Boot
13 |
14 |
15 | 1.8
16 |
17 |
18 |
19 |
20 | org.springframework.boot
21 | spring-boot-starter-web
22 |
23 |
24 |
25 | mysql
26 | mysql-connector-java
27 | runtime
28 |
29 |
30 | org.springframework.boot
31 | spring-boot-starter-jdbc
32 |
33 |
34 | com.alibaba
35 | druid-spring-boot-starter
36 | 1.1.14
37 |
38 |
39 |
40 |
41 | ${project.name}
42 |
43 |
44 | org.springframework.boot
45 | spring-boot-maven-plugin
46 |
47 |
48 |
49 |
50 |
51 |
--------------------------------------------------------------------------------
/mysql/src/main/java/demo/springboot/mysql/MysqlApplication.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.mysql;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class MysqlApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(MysqlApplication.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/mysql/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | datasource:
3 | type: com.alibaba.druid.pool.DruidDataSource
4 | driver-class-name: com.mysql.jdbc.Driver
5 | url: jdbc:mysql://localhost:3306/admin?useUnicode=true&characterEncoding=UTF-8&useSSL=false
6 | username: root
7 | password: root
8 |
9 | logging:
10 | level:
11 | root: error
12 |
--------------------------------------------------------------------------------
/mysql/src/test/java/demo/springboot/mysql/MysqlApplicationTests.java:
--------------------------------------------------------------------------------
1 | package demo.springboot.mysql;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.boot.test.context.SpringBootTest;
7 | import org.springframework.jdbc.core.JdbcTemplate;
8 | import org.springframework.test.context.junit4.SpringRunner;
9 |
10 | import java.util.List;
11 | import java.util.Map;
12 |
13 | @RunWith(SpringRunner.class)
14 | @SpringBootTest
15 | public class MysqlApplicationTests {
16 |
17 | @Autowired
18 | private JdbcTemplate jdbcTemplate;
19 |
20 | @Test
21 | public void testJdbc() {
22 | List