仿牛客社区
目录
仿牛客社区
springboot集成第三方
集成reids
-
导包 spring-boot-starter-data-redis
1 2 3 4
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
-
配置Redis
-
配置数据源
1 2 3 4 5
spring: redis: database: 11 host: localhost port: 6379
-
编写配置类,构造RedisTemplate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
@Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); // 设置key的序列化方式 template.setKeySerializer(RedisSerializer.string()); // 设置value的序列化方式 template.setValueSerializer(RedisSerializer.json()); // 设置hash的key的序列化方式 template.setHashKeySerializer(RedisSerializer.string()); // 设置hash的value的序列化方式 template.setHashValueSerializer(RedisSerializer.json()); template.afterPropertiesSet(); return template; } }
-
-
访问Redis
集成kafka
kafka启动
-
启动zookeeper
1
bin\windows\zookeeper-server-start.bat config\zookeeper.properties
-
启动kafka
1
bin\windows\kafka-server-start.bat config\server.properties
-
创建主题
1
kafka-topics.bat --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test
springboot整合kafka
-
引入依赖,spring-kafka
1 2 3 4
<dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency>
-
配置kafka,配置server、consumer
1 2 3 4 5 6 7
spring: kafka: bootstrap-servers: localhost:9092 consumer: group-id: test-consumer-group enable-auto-commit: true auto-commit-interval: 3000
-
访问kafka
- 生产者使用
kafkaTemplate.send(topic,content);
发送消息 - 消费者使用@KafkaListener(topics = {“test”})注解监听某些主题,消息封装成ConsumerRecord类型
handleMessage(ConsumerRecord consumerRecord)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
@SpringBootTest(classes = NowcoderCommunityApplication.class) public class KafkaTest { @Autowired private KafkaProducer kafkaProducer; @Test public void testKafka(){ kafkaProducer.sendMessage("test","这是第一条kafkaProducer"); kafkaProducer.sendMessage("test","hah"); kafkaProducer.sendMessage("test","你好"); try { Thread.sleep(1000*5); } catch (InterruptedException e) { e.printStackTrace(); } } } //生产者 @Component class KafkaProducer{ @Autowired private KafkaTemplate kafkaTemplate; public void sendMessage(String topic,String content){ kafkaTemplate.send(topic,content); } } //消费者 @Component class KafkaConsumer{ @Autowired private KafkaTemplate kafkaTemplate; //监听某些主题 @KafkaListener(topics = {"test"}) public void handleMessage(ConsumerRecord consumerRecord){ System.out.println(consumerRecord.value()); } }
- 生产者使用
功能实现
动实现分页
-
分页sql语句
1
select * from discuss_post order by type desc, create_time desc limit #{offset}, #{limit}
-
定义分页信息对象,page类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
/** * 封装分页相关的信息. */ public class Page { // 当前页码 private int current = 1; // 显示上限 private int limit = 10; // 数据总数(用于计算总页数) private int rows; // 查询路径(用于复用分页链接) private String path; public int getCurrent() { return current; } public void setCurrent(int current) { if (current >= 1) { this.current = current; } } public int getLimit() { return limit; } public void setLimit(int limit) { if (limit >= 1 && limit <= 100) { this.limit = limit; } } public int getRows() { return rows; } public void setRows(int rows) { if (rows >= 0) { this.rows = rows; } } public String getPath() { return path; } public void setPath(String path) { this.path = path; } /** * 获取当前页的起始行 * * @return */ public int getOffset() { // current * limit - limit return (current - 1) * limit; } /** * 获取总页数 * * @return */ public int getTotal() { // rows / limit [+1] if (rows % limit == 0) { return rows / limit; } else { return rows / limit + 1; } } /** * 获取起始页码 * * @return */ public int getFrom() { int from = current - 2; return from < 1 ? 1 : from; } /** * 获取结束页码 * * @return */ public int getTo() { int to = current + 2; int total = getTotal(); return to > total ? total : to; } }
-
controller中接收分页信息并调用语句分页查找
发送邮件
-
邮箱设置
启用客户端smtp服务,可以室友163邮箱、qq邮箱等等
-
Spring boot 集成javamail
-
导入jar包
1 2 3 4 5
<!-- java mail--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
-
配置邮箱参数
1 2 3 4 5 6 7 8 9
spring: mail: host: smtp.163.com port: 465 username: xxxx@163.com password: xxxxx protocol: smtps # 指定协议 properties: mail.smtp.ssl.enable: true
-
代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
@Component public class MailClient { private static final Logger logger = LoggerFactory.getLogger(MailClient.class); @Autowired private JavaMailSender mailSender; @Value("${spring.mail.username}") private String from; public void sendMail(String to, String subject, String context) { try { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage); mimeMessageHelper.setFrom(from); mimeMessageHelper.setTo(to); mimeMessageHelper.setSubject(subject); mimeMessageHelper.setText(context, true); mailSender.send(mimeMessage); } catch (MessagingException e) { logger.error("发送邮件失败:" + e.getMessage()); } } }
-
测试,使用thymeleaf模板发送html的邮件
1 2 3 4 5 6 7
@Test public void testSendHTMLMail(){ Context context=new Context(); context.setVariable("username","userxx"); String content = templateEngine.process("/mail/mailexample", context); mailClient.sendMail("user@qq.com","suject",content); }
-
经验
ajxa请求经过springmvc拦截器后response重定向问题
- 问题:ajxa请求经过springmvc拦截器后response重定向后,浏览器会再次请求重定向的页面,但是不会刷新显示
- 解决:可以设置ajax请求拦截后将重定向页面url作为json数据返回,在ajax回调处理函数中进行页面跳转
- 代码
java代码
|
|
js代码
|
|