若依框架修改雪花ID主键
- 共 2,755 次检阅

需要添加雪花ID生成策略文件和修改的文件:

 

PfUsers.java文件:
    @Id
    @GeneratedValue(generator = "snowflake")
    @GenericGenerator(name = "snowflake", strategy = "com.ruoyi.service.SnowflakeIdGenerator")
    private Long id;

 

PfUsersController.java添加部分:

    @RequiresPermissions("pfuser:pfusers:add")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PfUsers pfUsers)
    {
        // 生成雪花ID并设置到实体对象中
        Serializable id = SnowflakeIdGenerator.getInstance().generate(null, pfUsers);
        pfUsers.setId((Long) id);

        // 调用服务层插入数据
        int result = pfUsersService.insertPfUsers(pfUsers);
        return toAjax(result);
    }

PfUsersServiceImpl.java文件:

    @Override
    public int insertPfUsers(PfUsers pfUsers)
    {
        // 生成雪花ID并设置到实体对象中
        Serializable id = SnowflakeIdGenerator.getInstance().generate(null, pfUsers);
        pfUsers.setId((Long) id);

        return pfUsersMapper.insertPfUsers(pfUsers);
    }

PfUsersMapper.xml文件:

<?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.ruoyi.system.mapper.PfUsersMapper">

    <resultMap type="PfUsers" id="PfUsersResult">
        <result property="id" column="id" />
        <result property="nickname" column="nickname" />
        <result property="sex" column="sex" />
        <result property="phone" column="phone" />
        <result property="wechat" column="wechat" />
        <result property="password" column="password" />
        <result property="regtime" column="regtime" />
        <result property="lasttime" column="lasttime" />
    </resultMap>

    <sql id="selectPfUsersVo">
        select id, nickname, sex, phone, wechat, password, regtime, lasttime from pf_users
    </sql>

    <select id="selectPfUsersList" parameterType="PfUsers" resultMap="PfUsersResult">
        <include refid="selectPfUsersVo" />
        <where>
            <if test="nickname != null and nickname != ''"> and nickname like concat('%', #{nickname}, '%')</if>
            <if test="phone != null and phone != ''"> and phone = #{phone}</if>
        </where>
    </select>

    <select id="selectPfUsersById" parameterType="Long" resultMap="PfUsersResult">
        <include refid="selectPfUsersVo" />
        where id = #{id}
    </select>

    <insert id="insertPfUsers" parameterType="PfUsers">
        insert into pf_users (id, nickname, sex, phone, wechat, password, regtime, lasttime)
        values (#{id}, #{nickname}, #{sex}, #{phone}, #{wechat}, #{password}, #{regtime}, #{lasttime})
    </insert>

    <update id="updatePfUsers" parameterType="PfUsers">
        update pf_users
        <trim prefix="SET" suffixOverrides=",">
            <if test="nickname != null">nickname = #{nickname},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="wechat != null">wechat = #{wechat},</if>
            <if test="password != null">password = #{password},</if>
            <if test="regtime != null">regtime = #{regtime},</if>
            <if test="lasttime != null">lasttime = #{lasttime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePfUsersById" parameterType="Long">
        delete from pf_users where id = #{id}
    </delete>

    <delete id="deletePfUsersByIds" parameterType="String">
        delete from pf_users where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>

 

SnowflakeIdGenerator.java  采用默认ID策略

package com.ruoyi.service;

import com.github.yitter.contract.IdGeneratorOptions;
import com.github.yitter.idgen.YitIdHelper;
import org.hibernate.MappingException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentityGenerator;

import java.io.Serializable;

public class SnowflakeIdGenerator extends IdentityGenerator {
    private static final short METHOD = 1;

    private static final SnowflakeIdGenerator INSTANCE = new SnowflakeIdGenerator();

    private SnowflakeIdGenerator() {
        init();
    }

    public static SnowflakeIdGenerator getInstance() {
        return INSTANCE;
    }

    public static void init() {
        IdGeneratorOptions options = new IdGeneratorOptions(METHOD);
        YitIdHelper.setIdGenerator(options);
    }

    @Override
    public Serializable generate(SharedSessionContractImplementor session, Object object) throws MappingException {
        Serializable id = YitIdHelper.nextId();
        if (id != null) {
            return id;
        }
        return super.generate(session, object);
    }
}

 

分享到:

这篇文章还没有评论

发表评论