• 全局唯一化 主键


    用户的主键

    1、全局唯一化。已经把组件预先的引用了进来。就是下面三个包,

    2、这里有个sid,其实就是工具类。

    package org.n3r.idworker;
    
    import org.n3r.idworker.strategy.DefaultWorkerIdStrategy;
    import org.n3r.idworker.utils.Utils;
    import org.springframework.stereotype.Component;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    @Component
    public class Sid {
        private static WorkerIdStrategy workerIdStrategy;
        private static IdWorker idWorker;
    
        static {
            configure(DefaultWorkerIdStrategy.instance);
        }
    
    
        public static synchronized void configure(WorkerIdStrategy custom) {
            if (workerIdStrategy != null) workerIdStrategy.release();
            workerIdStrategy = custom;
            idWorker = new IdWorker(workerIdStrategy.availableWorkerId()) {
                @Override
                public long getEpoch() {
                    return Utils.midnightMillis();
                }
            };
        }
    
        /**
         * 一天最大毫秒86400000,最大占用27比特
         * 27+10+11=48位 最大值281474976710655(15字),YK0XXHZ827(10字)
         * 6位(YYMMDD)+15位,共21位
         *
         * @return 固定21位数字字符串
         */
    
        public static String next() {
            long id = idWorker.nextId();
            String yyMMdd = new SimpleDateFormat("yyMMdd").format(new Date());
            return yyMMdd + String.format("%014d", id);
        }
    
    
        /**
         * 返回固定16位的字母数字混编的字符串。
         */
        public String nextShort() {
            long id = idWorker.nextId();
            String yyMMdd = new SimpleDateFormat("yyMMdd").format(new Date());
            return yyMMdd + Utils.padLeft(Utils.encode(id), 10, '0');
        }
        
    //    public static void main(String[] args) {
    //        String aa = new Sid().nextShort();
    //        String bb = new Sid().next();
    //
    //        System.out.println(aa);
    //        System.out.println(bb);
    //    }
    }
    View Code

    3、这三个包包是org.n3r.idworker。要想注入进来。就必须包能被容器扫描到。

    package com.imooc;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ComponentScan;
    import tk.mybatis.spring.annotation.MapperScan;
    
    @SpringBootApplication
    @MapperScan(basePackages = "com.imooc.mapper")
    @ComponentScan(basePackages = {"com.imooc","org.n3r.idworker"})
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class,args);
        }
    }
    View Code

    4、这样sid就已经注入进来,可以用了。

    package com.imooc.service.impl;
    
    
    import com.imooc.enums.Sex;
    import com.imooc.mapper.UsersMapper;
    import org.n3r.idworker.Sid;
    import com.imooc.pojo.Users;
    import com.imooc.pojo.bo.UserBO;
    import com.imooc.service.UserService;
    import com.imooc.utils.DateUtil;
    import com.imooc.utils.MD5Utils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    import tk.mybatis.mapper.entity.Example;
    
    import java.util.Date;
    
    
    @Service
    public class UserServiceImpl implements UserService {
    
        @Autowired
        private UsersMapper usersMapper;
    
        @Autowired
        private Sid sid;
    
        private static final String USER_FACE="";
        @Transactional(propagation = Propagation.SUPPORTS)
    
        @Override
        public boolean queryUsernameIsExist(String username) {
    //        try {
    //            Thread.sleep(3500);
    //        } catch (InterruptedException e) {
    //            e.printStackTrace();
    //        }
            Example userExample = new Example(Users.class);
            Example.Criteria criteria = userExample.createCriteria();
            criteria.andEqualTo("username", username);
            Users result=usersMapper.selectOneByExample(userExample);
            return result==null ? false : true;
        }
    
        @Transactional(propagation = Propagation.REQUIRED)
        @Override
        public Users creatUser(UserBO userBO) {
    
            String userId= sid.nextShort();
            Users user=new Users();
            user.setId(userId);
            user.setUsername(userBO.getUsername());
            try {
                user.setPassword(MD5Utils.getMD5Str(userBO.getPassword()));
            }catch (Exception e) {
                e.printStackTrace();
            }
            user.setNickname(userBO.getUsername());
            user.setFace(USER_FACE);
            user.setBirthday(DateUtil.convertToDate("1900-1-1"));
            user.setSex(Sex.secret.type);
            user.setCreatedTime(new Date());
            user.setUpdatedTime(new Date());
            usersMapper.insert(user);
            return user;
        }
    
    
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public Users queryUserForLogin(String username, String password) {
            Example userExample = new Example(Users.class);
            Example.Criteria criteria = userExample.createCriteria();
            criteria.andEqualTo("username", username);
            criteria.andEqualTo("password",password);
            Users result=usersMapper.selectOneByExample(userExample);
            return result;
    
        }
    }
    View Code
  • 相关阅读:
    poj 2000
    poj1316
    poj1922
    poj2017
    poj1833 排列
    poj1338
    poj2136
    poj2242
    IE兼容html5标签
    绑定事件后,某些情况下需要解绑该事件
  • 原文地址:https://www.cnblogs.com/callbin/p/14483015.html
Copyright © 2020-2023  润新知