栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

基于Springboot+mysql+mybatis-plus+swagger+redis+rabbimq+Springcloud+eureka+feign(http)+Apollo的员工管理系统(1

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于Springboot+mysql+mybatis-plus+swagger+redis+rabbimq+Springcloud+eureka+feign(http)+Apollo的员工管理系统(1

基于Springboot+mysql+mybatis-plus+swagger+redis+rabbimq+Springcloud+eureka+feign(http)+Apollo的员工管理系统(1)

本系统基于Springboot集成各种组件进行,主要讲解思路,各个组件什么功能不进行具体描述,有兴趣的可以自行百度原理,个人原创,因此命名并不是特别规范,不喜勿喷。

Springboot

首先,先创建一个springboot项目,这里不做赘述。*(后续依赖都不做描述,根据组件名称百度就可以搜到依赖)

mysql

首先,集成mysql数据库,在数据库中得先有对应的数据表,下面贴我个人的数据库示例。

有了数据库,就要就写对应的配置文件,以及在springboot中写对应的类方便后续进行实例化。

其中@ApiModelProperty()注解暂时用不上,这是后面集成Swagger时用到的。数据库的配置文件我这里就不贴了,每个人不一样,大家根据自己对应的数据库账户名密码和数据库名字进行配对。(百度有很多教程)

集成完MySQL就进行经典的CURD操作。

mybatis-plus

为什么不选择mybatis?个人理解因为plus更加简单,但是缺点就在于用的是提供的现成的方法,很多细致的操作无法实现。

首先,集成mybatis-plus需要添加依赖。

然后,需要在配置文件里添加依赖,百度即可,拿来即用。

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

然后需要引入一个方法,让系统知道我们用的是mybatisplus

public interface Usermapper extends BaseMapper {
}

BaseMapper就是我们给系统的一个信息,后续就可以直接调用现成的方法。(注意这里是interface)

随后建一个controller,用来进行操作。

@Autowired
private Usermapper usermapper;

将接口化实例化,并通过@Autowird进行自动装配,这时候就可以进行快捷操作。

    @PostMapping("/add")
public user testsert( user user2){
        usermapper.insert(user2);//自带的方法
   return user2;
    }

    @PostMapping("/del")
    public  user deltest( user user2){
        usermapper.deleteById(user2);
        return  user2;

    }
    
    @PostMapping("/update")
    public user   datetest( user user2 )  {
        usermapper.updateById(user2);
        return  user2;
    }
    
    @GetMapping("/select/{user2}") 
    public  user selecttest( @PathVariable long user2) {      
          user user3 = usermapper.selectById(user2);
        return   user3;
 

    }

通过mybatisplus操作数据库就是这么简单,不过这里没有结果展示,因为没有做网页进行展示,那要怎么看程序的查询或者增加结果呢,我们接下来集成Swagger。

swagger

首先引入依赖。

然后在你想操作的接口上方,添加一个注解,给他改一个名字,方便操作,比如我这里跟增删改查的几个接口进行了命名。

下面的删除查询增加接口也是同理。

然后打开localhost:9998/swagger-ui.html就能看到如图

然后我们随便开一个,我这里开的是增加,别的同理。找到右上角的Try it out 点击完输入数据。

数据输完点击下方的Execute,下方会出现结果,打开数据库也会出现对应 的数据。


到这里,Swagger集成完成。

redis

首先,添加依赖,然后添加配置文件,个人可以按兴趣来。

redis:
  database: 0
  host: 127.0.0.1
  port: 6379
  password:
  jedis:
    pool:
      max-active: 8
      max-wait: -1
      max-idle: 8
      min-idle: 0
  timeout: 1200

然后,需要新建一个redisConfig类,对其进行配置,这个东西不用自己写,百度有现成的,拿来即用。

package out.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
//百度拿来即用
@Configuration
public class redisConfig{
    @Bean
    @SuppressWarnings("all")
    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

然后,再百度一个RdeisUtil类,这里rdis的工具类,同样拿来即用,不需要自己写。

package out.control;

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.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component    //拿来即用
public final class RedisUtil {
    @Autowired
    private RedisTemplate redisTemplate;

    // =============================common============================
    
    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;
        }
    }

    
    public long getExpire(String key) {
        return redisTemplate.getExpire(key, TimeUnit.SECONDS);
    }


    
    public boolean hasKey(String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    
    @SuppressWarnings("unchecked")
    public void del(String... key) {
        if (key != null && key.length > 0) {
            if (key.length == 1) {
                redisTemplate.delete(key[0]);
            } else {
                redisTemplate.delete((Collection) CollectionUtils.arrayToList(key));
            }
        }
    }


    // ============================String=============================

    
    public Object get(String key) {
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }

    

    public boolean set(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return 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;
        }
    }


    
    public long incr(String key, long delta) {
        if (delta < 0) {
            throw new RuntimeException("递增因子必须大于0");
        }
        return redisTemplate.opsForValue().increment(key, delta);
    }


    
    public long decr(String key, long delta) {
        if (delta < 0) {
            throw new RuntimeException("递减因子必须大于0");
        }
        return redisTemplate.opsForValue().increment(key, -delta);
    }


    // ================================Map=================================

    
    public Object hget(String key, String item) {
        return redisTemplate.opsForHash().get(key, item);
    }

    
    public Map hmget(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    
    public boolean hmset(String key, Map map) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    
    public boolean hmset(String key, Map map, long time) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    
    public boolean hset(String key, String item, Object value) {
        try {
            redisTemplate.opsForHash().put(key, item, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    
    public boolean hset(String key, String item, Object value, long time) {
        try {
            redisTemplate.opsForHash().put(key, item, value);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    
    public void hdel(String key, Object... item) {
        redisTemplate.opsForHash().delete(key, item);
    }


    
    public boolean hHasKey(String key, String item) {
        return redisTemplate.opsForHash().hasKey(key, item);
    }


    
    public double hincr(String key, String item, double by) {
        return redisTemplate.opsForHash().increment(key, item, by);
    }


    
    public double hdecr(String key, String item, double by) {
        return redisTemplate.opsForHash().increment(key, item, -by);
    }


    // ============================set=============================

    
    public Set sGet(String key) {
        try {
            return redisTemplate.opsForSet().members(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    
    public boolean sHasKey(String key, Object value) {
        try {
            return redisTemplate.opsForSet().isMember(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    
    public long sSet(String key, Object... values) {
        try {
            return redisTemplate.opsForSet().add(key, values);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }


    
    public long sSetAndTime(String key, long time, Object... values) {
        try {
            Long count = redisTemplate.opsForSet().add(key, values);
            if (time > 0)
                expire(key, time);
            return count;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }


    
    public long sGetSetSize(String key) {
        try {
            return redisTemplate.opsForSet().size(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }


    

    public long setRemove(String key, Object... values) {
        try {
            Long count = redisTemplate.opsForSet().remove(key, values);
            return count;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    // ===============================list=================================

    
    public List lGet(String key, long start, long end) {
        try {
            return redisTemplate.opsForList().range(key, start, end);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    
    public long lGetListSize(String key) {
        try {
            return redisTemplate.opsForList().size(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }


    
    public Object lGetIndex(String key, long index) {
        try {
            return redisTemplate.opsForList().index(key, index);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    
    public boolean lSet(String key, Object value) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    
    public boolean lSet(String key, Object value, long time) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            if (time > 0)
                expire(key, time);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }


    
    public boolean lSet(String key, List value) {
        try {
            redisTemplate.opsForList().rightPushAll(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }


    
    public boolean lSet(String key, List value, long time) {
        try {
            redisTemplate.opsForList().rightPushAll(key, value);
            if (time > 0)
                expire(key, time);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    

    public boolean lUpdateIndex(String key, long index, Object value) {
        try {
            redisTemplate.opsForList().set(key, index, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    

    public long lRemove(String key, long count, Object value) {
        try {
            Long remove = redisTemplate.opsForList().remove(key, count, value);
            return remove;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
 

然后开始编写你想缓存的部分,这里我实现的是查询缓存,如果是第一次查询就从数据库拿数据,如果之前查询过了,就会在redis中就缓存数据,直接从redis中拿数据。

找到你刚才做增删改查的mysql控制类。添加上

@Autowired
private RedisTemplate redisTemplate;

在对应的查询语句的部分,加上下列代码:

@ApiOperation("查询")//swagger里的名称
@GetMapping("/select/{user2}") //{}中的数值与@pathvarible相同可以取到
public  user selecttest(@ApiParam @PathVariable long user2) {


    Object user4=redisTemplate.opsForValue().get(Long.toString(user2));
    //从redis缓存中取对应序号的值

    if (user4==null)
    {
        user user3 = usermapper.selectById(user2);
        redisTemplate.opsForValue().set(Long.toString(user2), JSONValue.toJSONString(user3));
        //存入redis
        System.out.println("从数据库读取");
        return user3;

    }

    JSONObject jsonObject=JSONObject.fromObject(user4);
    user   user5=(user)JSONObject.toBean(jsonObject,user.class);
    System.out.println("从缓存读取");
    return   user5;

这时候进行编译,再进入Swagger,同样的查询操作,这里我查询的是6,第一次因为没有查询过,所以显示为数据库,因为原来已经有6了缓存数据了所以显示缓存。


转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号