2024年Java最新立即可用的实战源码(springboot+redis+mybatis+restTemplate),2024阿里、网易、京东等大厂最新Java面试题

总结

面试难免让人焦虑不安。经历过的人都懂的。但是如果你提前预测面试官要问你的问题并想出得体的回答方式,就会容易很多。

此外,都说“面试造火箭,工作拧螺丝”,那对于准备面试的朋友,你只需懂一个字:刷!

给我刷刷刷刷,使劲儿刷刷刷刷刷!今天既是来谈面试的,那就必须得来整点面试真题,这不花了我整28天,做了份“Java一线大厂高岗面试题解析合集:JAVA基础-中级-高级面试+SSM框架+分布式+性能调优+微服务+并发编程+网络+设计模式+数据结构与算法等”

image

且除了单纯的刷题,也得需准备一本【JAVA进阶核心知识手册】:JVM、JAVA集合、JAVA多线程并发、JAVA基础、Spring 原理、微服务、Netty与RPC、网络、日志、Zookeeper、Kafka、RabbitMQ、Hbase、MongoDB、Cassandra、设计模式、负载均衡、数据库、一致性算法、JAVA算法、数据结构、加密算法、分布式缓存、Hadoop、Spark、Storm、YARN、机器学习、云计算,用来查漏补缺最好不过。

image

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  1. 创建数据库demo和表student,表结构非常简单:

CREATE DATABASE demo;

USE demo;

CREATE TABLE student (id INT(11), name VARCHAR(128));

快速部署redis

为了快速准备好redis服务用来测试,我这里是用docker来完成的,一行命令即可:

docker run --name redis -p 6379:6379 -idt redis:5.0.6 redis-server --appendonly yes

父工程

  1. 首先创建名为pinpoint185demo的maven工程,pom.xml内容如下:
<?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”>

4.0.0

service-a

service-b

org.springframework.boot

spring-boot-starter-parent

2.0.5.RELEASE

com.bolingcavalry

pinpoint185demo

1.0-SNAPSHOT

pom

<java.version>1.8</java.version>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>1.8</java.version>

<spring.boot.version>2.0.5.RELEASE</spring.boot.version>

org.apache.maven.plugins

maven-compiler-plugin

${java.version}

${java.version}

${project.build.sourceEncoding}

子工程service-a编码

  1. 在此工程下创建子工程service-a,其pom.xml内容如下:
<?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”>

4.0.0

com.bolingcavalry

pinpoint185demo

1.0-SNAPSHOT

service-a

service-a

服务a

<java.version>1.8</java.version>

org.springframework.boot

spring-boot-starter-web

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

mysql

mysql-connector-java

com.alibaba

fastjson

1.2.28

org.apache.commons

commons-lang3

3.9

org.springframework.boot

spring-boot-maven-plugin

以上内容有几点需要注意:

a. 要提供web服务,所以依赖了spring-boot-starter-web;

b. 要使用mybaits,所以依赖了mybatis-spring-boot-starter;

c. 要连接mysql,所以依赖了mysql-connector-java;

d. commons-lang3、fastjson作为工具类,在代码中会用到它们的API;

  1. service-a的系统配置application.yml内容如下,注意数据库和mybaits的配置:

server:

port: 8080

spring:

datasource:

username: root

password: 888888

url: jdbc:mysql://192.168.121.143:3306/demo?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC

driver-class-name: com.mysql.jdbc.Driver

mybatis:

mapper-locations: classpath:mapping/*Mapping.xml

type-aliases-package: com.bolingcavalry.servicea.entity

logging:

level:

com:

bolingcavalry:

servicea: debug

service-b的服务url

serviceb:

baseurl: http://localhost:8081/

  1. mybatis的mapping文件在src\main\resources\mapping目录,名为StudentMapping.xml,对应student表的定义和操作:
<?xml version="1.0" encoding="UTF-8"?>

select * from student where id = #{id}

insert into student (id, name) values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR})

  1. student表对应的实体类:

package com.bolingcavalry.servicea.entity;

/**

  • @Description: student entity

  • @author: willzhao E-mail: zq2599@gmail.com

  • @date: 2019/10/3 9:55

*/

public class Student {

private Integer id;

private String name;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

  1. student对应的mapper接口:

package com.bolingcavalry.servicea.mapper;

import com.bolingcavalry.servicea.entity.Student;

import org.springframework.stereotype.Repository;

/**

  • @Description: mybatis mapper

  • @author: willzhao E-mail: zq2599@gmail.com

  • @date: 2019/10/3 9:58

*/

@Repository

public interface StudentMapper {

Student getById(Integer id);

void insert(Student student);

}

  1. 由于要用到restTemplate访问service-b,因此要增加对应的配置类:

package com.bolingcavalry.servicea.config;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.http.client.ClientHttpRequestFactory;

import org.springframework.http.client.SimpleClientHttpRequestFactory;

import org.springframework.web.client.RestTemplate;

/**

  • @Description: 远程请求的配置

  • @author: willzhao E-mail: zq2599@gmail.com

  • @date: 2019/10/3 11:45

*/

@Configuration

public class RestTemplateConfig {

@Bean

public RestTemplate restTemplate(ClientHttpRequestFactory factory){

return new RestTemplate(factory);

}

@Bean

public ClientHttpRequestFactory simpleClientHttpRequestFactory(){

SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();

factory.setConnectTimeout(15000);

factory.setReadTimeout(5000);

return factory;

}

}

  1. 接下来是提供web服务的controller:

package com.bolingcavalry.servicea.controller;

import com.alibaba.fastjson.JSON;

import com.bolingcavalry.servicea.entity.Student;

import com.bolingcavalry.servicea.mapper.StudentMapper;

import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.client.RestTemplate;

/**

  • @Description: 提供学生服务相关的web接口

  • @author: willzhao E-mail: zq2599@gmail.com

  • @date: 2019/10/3 8:51

*/

@RestController

public class StudentController {

@Autowired

StudentMapper studentMapper;

@Autowired

private RestTemplate restTemplate;

@Value(“${serviceb.baseurl}”)

private String baseUrl;

@RequestMapping(value = “/get/{id}”, method = RequestMethod.GET)

public String get(@PathVariable(“id”) Integer id) {

String rlt = restTemplate.getForObject(baseUrl + “get/” + id,String.class);

if(StringUtils.isNotBlank(rlt)){

return “From redis :” + rlt;

}

//redis中查不到,就去数据库里面查

Student student = studentMapper.getById(id);

//mysql里面也没有就报错

if(null==student) {

return “student with id[”+id+“] not exist”;

}

//调用service-b的接口,写入redis

restTemplate.getForObject(baseUrl + “set/”+id+“/”+student.getName(),String.class);

return “From mysql :”+JSON.toJSONString(student);

}

@RequestMapping(value = “/add/{id}/{name}”, method = RequestMethod.GET)

public String add(@PathVariable(“id”) Integer id, @PathVariable(“name”) String name) {

Student student = new Student();

student.setId(id);

student.setName(name);

//写入数据库

studentMapper.insert(student);

//调用service-b的接口,写入redis

restTemplate.getForObject(baseUrl + “set/”+id+“/”+name,String.class);

return “insert success”;

}

}

  1. 整个应用的启动类,增加了MapperScan注解:

package com.bolingcavalry.servicea;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

@MapperScan(“com.bolingcavalry.servicea.mapper”)

public class ServiceAApplication {

public static void main(String[] args) {

SpringApplication.run(ServiceAApplication.class, args);

}

}

以上就是service-a的全部源码了,包括mybatis和restTmeplate操作;

service-b源码

  1. 创建名为service-b的子工程,pom.xml如下,要注意的是依赖了spring-boot-starter-data-redis和commons-pool2:
<?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”>

4.0.0

com.bolingcavalry

pinpoint185demo

1.0-SNAPSHOT

service-b

service-b

服务b

org.springframework.boot

spring-boot-starter-data-redis

org.springframework.boot

spring-boot-starter-web

com.alibaba

fastjson

1.2.28

org.apache.commons

commons-pool2

2.0

org.springframework.boot

spring-boot-maven-plugin

  1. 应用的配置文件application.properties内容如下:

server.port=8081

#redis

spring.redis.host=192.168.121.144

spring.redis.port=6379

数据库连接超时时间,2.0 中该参数的类型为Duration,这里在配置的时候需要指明单位

spring.redis.timeout=60s

连接池配置,2.0中直接使用jedis或者lettuce配置连接池

最大活跃连接数,负数为不限制

spring.redis.lettuce.pool.max-active=50

等待可用连接的最大时间,负数为不限制

spring.redis.lettuce.pool.max-wait=-1ms

最大空闲连接数

spring.redis.lettuce.pool.max-idle=10

最小空闲连接数

spring.redis.lettuce.pool.min-idle=2

  1. redis的配置类:

package com.example.serviceb.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;

import com.fasterxml.jackson.annotation.PropertyAccessor;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.cache.annotation.CachingConfigurerSupport;

import org.springframework.cache.annotation.EnableCaching;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.redis.connection.RedisConnectionFactory;

import org.springframework.data.redis.core.*;

import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import org.springframework.data.redis.serializer.StringRedisSerializer;

/**

  • @Description: (这里用一句话描述这个类的作用)

  • @author: willzhao E-mail: zq2599@gmail.com

  • @date: 2019/10/3 11:06

*/

@Configuration

@EnableCaching

public class RedisConfig extends CachingConfigurerSupport {

/**

  • retemplate相关配置

  • @param factory

  • @return

*/

@Bean

public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {

RedisTemplate<String, Object> template = new RedisTemplate<>();

// 配置连接工厂

template.setConnectionFactory(factory);

//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)

Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);

ObjectMapper om = new ObjectMapper();

// 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public

om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

// 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常

om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

jacksonSeial.setObjectMapper(om);

// 值采用json序列化

template.setValueSerializer(jacksonSeial);

//使用StringRedisSerializer来序列化和反序列化redis的key值

template.setKeySerializer(new StringRedisSerializer());

// 设置hash key 和value序列化模式

template.setHashKeySerializer(new StringRedisSerializer());

template.setHashValueSerializer(jacksonSeial);

template.afterPropertiesSet();

return template;

}

/**

  • 对hash类型的数据操作

  • @param redisTemplate

  • @return

*/

@Bean

public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {

return redisTemplate.opsForHash();

}

/**

  • 对redis字符串类型数据操作

  • @param redisTemplate

  • @return

*/

@Bean

public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {

return redisTemplate.opsForValue();

}

/**

  • 对链表类型的数据操作

  • @param redisTemplate

  • @return

*/

@Bean

public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {

return redisTemplate.opsForList();

}

/**

  • 对无序集合类型的数据操作

  • @param redisTemplate

  • @return

*/

@Bean

public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {

return redisTemplate.opsForSet();

}

/**

  • 对有序集合类型的数据操作

  • @param redisTemplate

  • @return

*/

@Bean

public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {

return redisTemplate.opsForZSet();

}

}

  1. redis的工具类:

package com.example.serviceb.util;

/**

  • @Description: (这里用一句话描述这个类的作用)

  • @author: willzhao E-mail: zq2599@gmail.com

  • @date: 2019/10/3 11:09

*/

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.stereotype.Component;

import org.springframework.util.CollectionUtils;

import java.util.List;

import java.util.Map;

import java.util.Set;

import java.util.concurrent.TimeUnit;

/**

  • redisTemplate封装

*/

@Component

public class RedisUtil {

@Autowired

private RedisTemplate<String, Object> redisTemplate;

public RedisUtil(RedisTemplate<String, Object> redisTemplate) {

this.redisTemplate = redisTemplate;

}

/**

  • 指定缓存失效时间

  • @param key 键

  • @param time 时间(秒)

  • @return

*/

public boolean expire(String key,long time){

try {

if(time>0){

redisTemplate.expire(key, time, TimeUnit.SECONDS);

}

return true;

} catch (Exception e) {

e.printStackTrace();

return false;

}

}

/**

  • 根据key 获取过期时间

  • @param key 键 不能为null

  • @return 时间(秒) 返回0代表为永久有效

*/

public long getExpire(String key){

return redisTemplate.getExpire(key,TimeUnit.SECONDS);

}

/**

  • 判断key是否存在

  • @param key 键

  • @return true 存在 false不存在

*/

public boolean hasKey(String key){

try {

return redisTemplate.hasKey(key);

} catch (Exception e) {

e.printStackTrace();

return false;

}

}

/**

  • 删除缓存

  • @param key 可以传一个值 或多个

*/

@SuppressWarnings(“unchecked”)

public void del(String … key){

if(key!=null&&key.length>0){

if(key.length==1){

redisTemplate.delete(key[0]);

}else{

redisTemplate.delete(CollectionUtils.arrayToList(key));

}

}

}

//String=

/**

  • 普通缓存获取

  • @param key 键

  • @return 值

*/

public Object get(String key){

return key==null?null:redisTemplate.opsForValue().get(key);

}

/**

  • 普通缓存放入

  • @param key 键

  • @param value 值

  • @return true成功 false失败

*/

public boolean set(String key,Object value) {

try {

redisTemplate.opsForValue().set(key, value);

return true;

} catch (Exception e) {

e.printStackTrace();

return false;

}

}

/**

  • 普通缓存放入并设置时间

  • @param key 键

  • @param value 值

  • @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期

  • @return true成功 false 失败

*/

public boolean set(String key,Object value,long time){

try {

if(time>0){

redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);

}else{

set(key, value);

}

return true;

} catch (Exception e) {

e.printStackTrace();

return false;

}

}

/**

  • 递增

  • @param key 键

  • @param delta 要增加几(大于0)

  • @return

*/

public long incr(String key, long delta){

if(delta<0){

throw new RuntimeException(“递增因子必须大于0”);

}

return redisTemplate.opsForValue().increment(key, delta);

}

/**

  • 递减

  • @param key 键

  • @param delta 要减少几(小于0)

  • @return

*/

public long decr(String key, long delta){

if(delta<0){

throw new RuntimeException(“递减因子必须大于0”);

}

return redisTemplate.opsForValue().increment(key, -delta);

}

//Map=

/**

  • HashGet

  • @param key 键 不能为null

  • @param item 项 不能为null

  • @return 值

*/

public Object hget(String key,String item){

return redisTemplate.opsForHash().get(key, item);

}

/**

  • 获取hashKey对应的所有键值

  • @param key 键

  • @return 对应的多个键值

*/

public Map<Object,Object> hmget(String key){

return redisTemplate.opsForHash().entries(key);

}

/**

  • HashSet

  • @param key 键

  • @param map 对应多个键值

  • @return true 成功 false 失败

*/

public boolean hmset(String key, Map<String,Object> map){

try {

redisTemplate.opsForHash().putAll(key, map);

return true;

} catch (Exception e) {

e.printStackTrace();

return false;

}

}

/**

  • HashSet 并设置时间

  • @param key 键

  • @param map 对应多个键值

  • @param time 时间(秒)

  • @return true成功 false失败

*/

public boolean hmset(String key, Map<String,Object> map, long time){

try {

redisTemplate.opsForHash().putAll(key, map);

if(time>0){

expire(key, time);

}

return true;

} catch (Exception e) {

e.printStackTrace();

return false;

}

总结

一般像这样的大企业都有好几轮面试,所以自己一定要花点时间去收集整理一下公司的背景,公司的企业文化,俗话说「知己知彼百战不殆」,不要盲目的去面试,还有很多人关心怎么去跟HR谈薪资。

这边给大家一个建议,如果你的理想薪资是30K,你完全可以跟HR谈33~35K,而不是一下子就把自己的底牌暴露了出来,不过肯定不能说的这么直接,比如原来你的公司是25K,你可以跟HR讲原来的薪资是多少,你们这边能给到我的是多少?你说我这边希望可以有一个20%涨薪。

最后再说几句关于招聘平台的,总之,简历投递给公司之前,请确认下这家公司到底咋样,先去百度了解下,别被坑了,每个平台都有一些居心不良的广告党等着你上钩,千万别上当!!!

Java架构学习资料,学习技术内容包含有:Spring,Dubbo,MyBatis, RPC, 源码分析,高并发、高性能、分布式,性能优化,微服务 高级架构开发等等。

还有Java核心知识点+全套架构师学习资料和视频+一线大厂面试宝典+面试简历模板可以领取+阿里美团网易腾讯小米爱奇艺快手哔哩哔哩面试题+Spring源码合集+Java架构实战电子书。
在这里插入图片描述

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

y对应的所有键值

  • @param key 键

  • @return 对应的多个键值

*/

public Map<Object,Object> hmget(String key){

return redisTemplate.opsForHash().entries(key);

}

/**

  • HashSet

  • @param key 键

  • @param map 对应多个键值

  • @return true 成功 false 失败

*/

public boolean hmset(String key, Map<String,Object> map){

try {

redisTemplate.opsForHash().putAll(key, map);

return true;

} catch (Exception e) {

e.printStackTrace();

return false;

}

}

/**

  • HashSet 并设置时间

  • @param key 键

  • @param map 对应多个键值

  • @param time 时间(秒)

  • @return true成功 false失败

*/

public boolean hmset(String key, Map<String,Object> map, long time){

try {

redisTemplate.opsForHash().putAll(key, map);

if(time>0){

expire(key, time);

}

return true;

} catch (Exception e) {

e.printStackTrace();

return false;

}

总结

一般像这样的大企业都有好几轮面试,所以自己一定要花点时间去收集整理一下公司的背景,公司的企业文化,俗话说「知己知彼百战不殆」,不要盲目的去面试,还有很多人关心怎么去跟HR谈薪资。

这边给大家一个建议,如果你的理想薪资是30K,你完全可以跟HR谈33~35K,而不是一下子就把自己的底牌暴露了出来,不过肯定不能说的这么直接,比如原来你的公司是25K,你可以跟HR讲原来的薪资是多少,你们这边能给到我的是多少?你说我这边希望可以有一个20%涨薪。

最后再说几句关于招聘平台的,总之,简历投递给公司之前,请确认下这家公司到底咋样,先去百度了解下,别被坑了,每个平台都有一些居心不良的广告党等着你上钩,千万别上当!!!

Java架构学习资料,学习技术内容包含有:Spring,Dubbo,MyBatis, RPC, 源码分析,高并发、高性能、分布式,性能优化,微服务 高级架构开发等等。

还有Java核心知识点+全套架构师学习资料和视频+一线大厂面试宝典+面试简历模板可以领取+阿里美团网易腾讯小米爱奇艺快手哔哩哔哩面试题+Spring源码合集+Java架构实战电子书。
[外链图片转存中…(img-G6CXBFqv-1714967896798)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • 20
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值