美文网首页
houyi平台-开发指南-持久层开发

houyi平台-开发指南-持久层开发

作者: do_young | 来源:发表于2020-01-19 17:38 被阅读0次

缓存API使用指南

导入Redis依赖工程

<dependency>
    <groupId>com.hxhealth.saas</groupId>
    <artifactId>redis-spring-boot-starter</artifactId>
</dependency>

声明Redis读写模板

    @Autowired
    private RedisRepository redisRepository;

基本操作

  1. 往Redis中存放数据

     redisRepository.set("abc_str", "abc");
    
  2. 读取Redis中的数据

     String redisValue = (String) redisRepository.get("abc_str");
    
  3. 删除Redis中的数据

     redisRepository.del("abc_str");
    
  4. 设置数据失效时间

    • 参数分别是:key;keyValue;失效时间(单位:秒);

      //abc_str在Redis中的值在5s后失效
      redisRepository.setExpire("abc_str", "abc", 5);
      

List操作

表头操作

  1. 向列表头添加数据

     redisRepository.leftPush("abcList", "aaa");
    
  2. 向列表头删除数据

     //leftPop为删除的数据(String leftPop = "aaa")
     String leftPop = (String) redisRepository.leftPop("abcList");
    

表尾操作

  1. 向列表尾添加数据

     redisRepository.in("abcList", "bbb");
    
  2. 向列表尾删除数据

     //rightPop为删除的数据(String leftPop = "bbb")
     String rightPop = (String) redisRepository.rightPop("abcList");
    

其他操作

  1. 查看列表大小

     Long length = redisRepository.length("abcList");
    

HashMap操作

  1. 存放值

     redisRepository.putHashValue("myMap", "abc", 123);
    
  2. 读取值

     //读取整个Map值
     Map<String, Object> hashMap = redisRepository.getHashValues("myMap");
     //读取map中的某个值
     Integer hashValue = (Integer) redisRepository.getHashValue("myMap", "abc");
    
  3. 删除值

     //参数:map;...mapkey
     redisRepository.delHashValues("myMap", "abc", "abc1");
    

分布式锁

  1. 声明

     @Autowired
     RedisDistributedLock redisDistributedLock;
    
  2. 锁住资源

     lockResult锁的结果
     boolean lockResult = redisDistributedLock.lock("abc_str");
    
  3. 释放锁

     redisDistributedLock.releaseLock(RedisTestConts.REDIS_VAR_PREFIX + "abc_str");
    

持久层开发指南

MyBatis-Plus

项目使用数据库操作工具MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具。MyBatis-Plus中文文档地址: https://mp.baomidou.com/guide/

使用步骤指南:

以user-center项目的SysUser为示例

1.添加pom文件依赖

    <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <optional>true</optional>
     </dependency>

2.配置文件

spring:
  datasource: //数据库链接配置
    url:  //db 链接
    username: //username
    password: //password
    driver-class-name: com.mysql.jdbc.Driver //类型

mybatis-plus: //mybatis配置
  mapper-locations: classpath:/mapper/*Mapper.xml
  #实体扫描,多个package用逗号或者分号分隔
  typeAliasesPackage: com.hxhealth.common.model
  global-config:
    db-config:
      id-type: auto

3.新建SysUser

以SysUser(@TableName("sys_user")) 为例子;
所有实体父类:
common-spring-boot-starter -> com.hxhealth.common.model

import lombok.Getter;
import lombok.Setter;
@Setter
@Getter  //注解 可不用写set/get方法
public class SuperEntity<T extends Model> extends Model<T> {
    /**
     * 主键ID
     */
    @TableId
    private Long id;
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    @Override
    protected Serializable pkVal() {
        return this.id;
    }
}

新建SysUser

@Data
@EqualsAndHashCode(callSuper = false)
@TableName("sys_user")
public class SysUser extends SuperEntity {
    private static final long serialVersionUID = -5886012896705137070L;

    private String username;
    private String password;
    private String nickname;
    private String headImgUrl;
    private String mobile;
    private Integer sex;
    private Boolean enabled;
    private String type;
    private String openId;
    @TableLogic
    private boolean isDel;

    @TableField(exist = false)    //@TableField(exist = false)表示排除SysUser类中的属性.
    private List<SysRole> roles;
    @TableField(exist = false)
    private String roleId;
    @TableField(exist = false)
    private String oldPassword;
    @TableField(exist = false)
    private String newPassword;
}

4.新建Dao层接口SysUserMapper:

dao接口需要实现SuperMapper,这样就能够使用封装好的很多通用方法,另外也可以自己编写方法,引用自下步的SysUserMapper.xml文件

public interface SysUserMapper extends SuperMapper<SysUser> {
    /**
     * 分页查询用户列表
     * @param page
     * @param params
     * @return
     */
    List<SysUser> findList(Page<SysUser> page, @Param("u") Map<String, Object> params);
}

5.新建SysUserMapper配置文件:

新建SysUserMapper.xml 放置于工程的src/main/resources 下文件夹mapper

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hxhealth.user.mapper.SysUserMapper">
    <sql id="where">
        <where>
            t.is_del=0
            <if test="u.id != null and u.id != ''">
                and t.id like concat('%', #{u.id}, '%')
            </if>
            <if test="u.username != null and u.username != ''">
                and t.username like concat('%', #{u.username}, '%')
            </if>
            <if test="u.nickname != null and u.nickname != ''">
                and t.nickname like concat('%', #{u.nickname}, '%')
            </if>
            <if test="u.enabled != null and u.enabled != ''">
                and t.enabled = #{u.enabled}
            </if>
            <if test="u.type != null and u.type != ''">
                and t.type = #{u.type}
            </if>
            <if test="u.searchKey != null and u.searchKey != '' and u.searchKey=='user_id'">
                and t.id  like concat('%', #{u.searchValue}, '%')
            </if>
            <if test="u.searchKey != null and u.searchKey != '' and u.searchKey=='username'">
                and t.username  like concat('%', #{u.searchValue}, '%')
            </if>
            <if test="u.searchKey != null and u.searchKey != '' and u.searchKey=='nick_name'">
                and t.nickname  like concat('%', #{u.searchValue}, '%')
            </if>
            <if test="u.searchKey != null and u.searchKey != '' and u.searchKey=='mobile'">
                and t.mobile  like concat('%', #{u.searchValue}, '%')
            </if>
        </where>
    </sql>

    <select id="findList" resultType="com.hxhealth.common.model.SysUser">
        select * from sys_user t
        <include refid="where" />
        order by t.id desc
    </select>
</mapper>

6.新建service层类ISysUserService:

接口类ISysUserService继承ISuperService(common-spring-boot-starter -> com.hxhealth.common.service)
ISuperService封装了通用方法

/**
 * service接口父类
 *
 * @Author: hxhealth
 * @date 2019/1/10
 */
public interface ISuperService<T> extends IService<T> {
    /**
     * 幂等性新增记录
     *
     * @param entity       实体对象
     * @param lock         锁实例
     * @param lockKey      锁的key
     * @param countWrapper 判断是否存在的条件
     * @param msg          对象已存在提示信息
     * @return
     */
    boolean saveIdempotency(T entity, DistributedLock lock, String lockKey, Wrapper<T> countWrapper, String msg);

    /**
     * 幂等性新增记录
     *
     * @param entity       实体对象
     * @param lock         锁实例
     * @param lockKey      锁的key
     * @param countWrapper 判断是否存在的条件
     * @return
     */
    boolean saveIdempotency(T entity, DistributedLock lock, String lockKey, Wrapper<T> countWrapper);

    /**
     * 幂等性新增或更新记录
     *
     * @param entity       实体对象
     * @param lock         锁实例
     * @param lockKey      锁的key
     * @param countWrapper 判断是否存在的条件
     * @param msg          对象已存在提示信息
     * @return
     */
    boolean saveOrUpdateIdempotency(T entity, DistributedLock lock, String lockKey, Wrapper<T> countWrapper, String msg);

    /**
     * 幂等性新增或更新记录
     *
     * @param entity       实体对象
     * @param lock         锁实例
     * @param lockKey      锁的key
     * @param countWrapper 判断是否存在的条件
     * @return
     */
    boolean saveOrUpdateIdempotency(T entity, DistributedLock lock, String lockKey, Wrapper<T> countWrapper);
}

相关文章

网友评论

      本文标题:houyi平台-开发指南-持久层开发

      本文链接:https://www.haomeiwen.com/subject/utarzctx.html