创建管理员服务提供者

学习完整课程请移步 互联网 Java 全栈工程师

本节视频

  • 【视频】项目实战-iToken-管理员服务-提供者1
  • 【视频】项目实战-iToken-管理员服务-提供者2
  • 【视频】项目实战-iToken-管理员服务-提供者3

创建项目

创建一个名为 itoken-service-admin 的服务提供者项目

POM



    4.0.0

    
        com.funtl
        itoken-dependencies
        1.0.0-SNAPSHOT
        ../itoken-dependencies/pom.xml
    

    itoken-service-admin
    jar

    itoken-service-admin
    http://www.funtl.com
    2018-Now

    
        
        
            com.alibaba
            druid-spring-boot-starter
        
        
            tk.mybatis
            mapper-spring-boot-starter
        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        

        
        
            org.springframework.cloud
            spring-cloud-starter-zipkin
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
        

        
        
            org.jolokia
            jolokia-core
        
        
            de.codecentric
            spring-boot-admin-starter-client
        
        

        
        
            mysql
            mysql-connector-java
            runtime
        
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    com.funtl.itoken.service.admin.ServiceAdminApplication
                
            

            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.5
                
                    ${basedir}/src/main/resources/generator/generatorConfig.xml
                    true
                    true
                
                
                    
                        mysql
                        mysql-connector-java
                        ${mysql.version}
                    
                    
                        tk.mybatis
                        mapper
                        3.4.4
                    
                
            
        
    

Application

package com.funtl.itoken.service.admin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@EnableEurekaClient
@MapperScan(basePackages = "com.funtl.itoken.service.admin.mapper")
public class ServiceAdminApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceAdminApplication.class, args);
    }
}

本地配置

bootstrap.yml

spring:
  cloud:
    config:
      uri: http://localhost:8888
      name: itoken-service-admin
      label: master
      profile: dev

bootstrap-prod.yml

spring:
  cloud:
    config:
      uri: http://192.168.75.137:8888
      name: itoken-service-admin
      label: master
      profile: prod

云配置

itoken-service-admin-dev.yml

spring:
  application:
    name: itoken-service-admin
  boot:
    admin:
      client:
        url: http://localhost:8084
  zipkin:
    base-url: http://localhost:9411
  datasource:
    druid:
      url: jdbc:mysql://192.168.75.133:3306/itoken-service-admin?useUnicode=true&characterEncoding=utf-8&useSSL=false
      username: root
      password: 123456
      initial-size: 1
      min-idle: 1
      max-active: 20
      test-on-borrow: true
      # MySQL 8.x: com.mysql.cj.jdbc.Driver
      driver-class-name: com.mysql.jdbc.Driver

server:
  port: 8501

mybatis:
  type-aliases-package: com.funtl.itoken.service.admin.domain
  mapper-locations: classpath:mapper/*.xml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: health,info

itoken-service-admin-prod.yml

spring:
  application:
    name: itoken-service-admin
  boot:
    admin:
      client:
        url: http://192.168.75.137:8084
  zipkin:
    base-url: http://192.168.75.137:9411
  datasource:
    druid:
      url: jdbc:mysql://192.168.75.133:3306/itoken-service-admin?useUnicode=true&characterEncoding=utf-8&useSSL=false
      username: root
      password: 123456
      initial-size: 1
      min-idle: 1
      max-active: 20
      test-on-borrow: true
      # MySQL 8.x: com.mysql.cj.jdbc.Driver
      driver-class-name: com.mysql.jdbc.Driver

server:
  port: 8501

mybatis:
  type-aliases-package: com.funtl.itoken.service.admin.domain
  mapper-locations: classpath:mapper/*.xml

eureka:
  client:
    serviceUrl:
      defaultZone: http://192.168.75.137:8761/eureka/,http://192.168.75.137:8861/eureka/,http://192.168.75.137:8961/eureka/

management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: health,info

创建接口

AdminService

package com.funtl.itoken.service.admin.service;

import com.funtl.itoken.service.admin.domain.TbSysUser;

public interface AdminService {

    /**
     * 注册
     * @param tbSysUser
     */
    void register(TbSysUser tbSysUser);

    /**
     * 登录
     * @param loginCode 登录账号
     * @param plantPassword 明文登录密码
     * @return
     */
    TbSysUser login(String loginCode, String plantPassword);
}

AdminServiceImpl

package com.funtl.itoken.service.admin.service.impl;

import com.funtl.itoken.service.admin.domain.TbSysUser;
import com.funtl.itoken.service.admin.mapper.TbSysUserMapper;
import com.funtl.itoken.service.admin.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class AdminServiceImpl implements AdminService {

    @Autowired
    private TbSysUserMapper tbSysUserMapper;

    @Transactional(readOnly = false)
    @Override
    public void register(TbSysUser tbSysUser) {
        tbSysUserMapper.insert(tbSysUser);
    }

    @Override
    public TbSysUser login(String loginCode, String plantPassword) {
        Example example = new Example(TbSysUser.class);
        example.createCriteria().andEqualTo("loginCode", loginCode);

        TbSysUser tbSysUser = tbSysUserMapper.selectOneByExample(example);
        if (tbSysUser != null) {
            String password = DigestUtils.md5DigestAsHex(plantPassword.getBytes());
            if (password.equals(tbSysUser.getPassword())) {
                return tbSysUser;
            }
        }

        return null;
    }
}

MyBatis 代码自动生成配置

generatorConfig.xml





    
    

    
        
        

        
        
            
        

        
        
        

        
        

        
        

        
        

        
        
        

jdbc.properties

# MySQL 8.x: com.mysql.cj.jdbc.Driver
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://192.168.75.133:3306/itoken-service-admin?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.username=root
jdbc.password=123456

服务所需数据库脚本

/*
SQLyog  v12.2.6 (64 bit)
MySQL - 5.7.22 : Database - itoken-service-admin
*********************************************************************
*/


/*!40101 SET NAMES utf8 */;

/*!40101 SET SQL_MODE=''*/;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`itoken-service-admin` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;

USE `itoken-service-admin`;

DROP TABLE IF EXISTS tb_sys_user;

-- 管理员表
CREATE TABLE tb_sys_user
(
    user_code varchar(100) NOT NULL COMMENT '用户编码',
    login_code varchar(100) NOT NULL COMMENT '登录账号',
    user_name varchar(100) NOT NULL COMMENT '用户昵称',
    password varchar(100) NOT NULL COMMENT '登录密码',
    email varchar(300) COMMENT '电子邮箱',
    mobile varchar(100) COMMENT '手机号码',
    phone varchar(100) COMMENT '办公电话',
    sex char(1) COMMENT '用户性别',
    avatar varchar(1000) COMMENT '头像路径',
    sign varchar(200) COMMENT '个性签名',
    wx_openid varchar(100) COMMENT '绑定的微信号',
    mobile_imei varchar(100) COMMENT '绑定的手机串号',
    user_type varchar(16) NOT NULL COMMENT '用户类型',
    ref_code varchar(64) COMMENT '用户类型引用编号',
    ref_name varchar(100) COMMENT '用户类型引用姓名',
    mgr_type char(1) NOT NULL COMMENT '管理员类型(0非管理员 1系统管理员  2二级管理员)',
    pwd_security_level decimal(1) COMMENT '密码安全级别(0初始 1很弱 2弱 3安全 4很安全)',
    pwd_update_date datetime COMMENT '密码最后更新时间',
    pwd_update_record varchar(1000) COMMENT '密码修改记录',
    pwd_question varchar(200) COMMENT '密保问题',
    pwd_question_answer varchar(200) COMMENT '密保问题答案',
    pwd_question_2 varchar(200) COMMENT '密保问题2',
    pwd_question_answer_2 varchar(200) COMMENT '密保问题答案2',
    pwd_question_3 varchar(200) COMMENT '密保问题3',
    pwd_question_answer_3 varchar(200) COMMENT '密保问题答案3',
    pwd_quest_update_date datetime COMMENT '密码问题修改时间',
    last_login_ip varchar(100) COMMENT '最后登陆IP',
    last_login_date datetime COMMENT '最后登陆时间',
    freeze_date datetime COMMENT '冻结时间',
    freeze_cause varchar(200) COMMENT '冻结原因',
    user_weight decimal(8) DEFAULT 0 COMMENT '用户权重(降序)',
    status char NOT NULL COMMENT '状态(0正常 1删除 2停用 3冻结)',
    create_by varchar(64) NOT NULL COMMENT '创建者',
    create_date datetime NOT NULL COMMENT '创建时间',
    update_by varchar(64) NOT NULL COMMENT '更新者',
    update_date datetime NOT NULL COMMENT '更新时间',
    remarks varchar(500) COMMENT '备注信息',
    corp_code varchar(64) DEFAULT '0' NOT NULL COMMENT '归属集团Code',
    corp_name varchar(100) DEFAULT 'iToken' NOT NULL COMMENT '归属集团Name',
    extend_s1 varchar(500) COMMENT '扩展 String 1',
    extend_s2 varchar(500) COMMENT '扩展 String 2',
    extend_s3 varchar(500) COMMENT '扩展 String 3',
    extend_s4 varchar(500) COMMENT '扩展 String 4',
    extend_s5 varchar(500) COMMENT '扩展 String 5',
    extend_s6 varchar(500) COMMENT '扩展 String 6',
    extend_s7 varchar(500) COMMENT '扩展 String 7',
    extend_s8 varchar(500) COMMENT '扩展 String 8',
    extend_i1 decimal(19) COMMENT '扩展 Integer 1',
    extend_i2 decimal(19) COMMENT '扩展 Integer 2',
    extend_i3 decimal(19) COMMENT '扩展 Integer 3',
    extend_i4 decimal(19) COMMENT '扩展 Integer 4',
    extend_f1 decimal(19,4) COMMENT '扩展 Float 1',
    extend_f2 decimal(19,4) COMMENT '扩展 Float 2',
    extend_f3 decimal(19,4) COMMENT '扩展 Float 3',
    extend_f4 decimal(19,4) COMMENT '扩展 Float 4',
    extend_d1 datetime COMMENT '扩展 Date 1',
    extend_d2 datetime COMMENT '扩展 Date 2',
    extend_d3 datetime COMMENT '扩展 Date 3',
    extend_d4 datetime COMMENT '扩展 Date 4',
    PRIMARY KEY (user_code)
) COMMENT = '用户表';

CREATE INDEX idx_sys_user_lc ON tb_sys_user (login_code ASC);
CREATE INDEX idx_sys_user_email ON tb_sys_user (email ASC);
CREATE INDEX idx_sys_user_mobile ON tb_sys_user (mobile ASC);
CREATE INDEX idx_sys_user_wo ON tb_sys_user (wx_openid ASC);
CREATE INDEX idx_sys_user_imei ON tb_sys_user (mobile_imei ASC);
CREATE INDEX idx_sys_user_rt ON tb_sys_user (user_type ASC);
CREATE INDEX idx_sys_user_rc ON tb_sys_user (ref_code ASC);
CREATE INDEX idx_sys_user_mt ON tb_sys_user (mgr_type ASC);
CREATE INDEX idx_sys_user_us ON tb_sys_user (user_weight ASC);
CREATE INDEX idx_sys_user_ud ON tb_sys_user (update_date ASC);
CREATE INDEX idx_sys_user_status ON tb_sys_user (status ASC);
CREATE INDEX idx_sys_user_cc ON tb_sys_user (corp_code ASC);

单元测试代码

package com.funtl.itoken.service.admin.test.service;

import com.funtl.itoken.service.admin.ServiceAdminApplication;
import com.funtl.itoken.service.admin.domain.TbSysUser;
import com.funtl.itoken.service.admin.service.AdminService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.UUID;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ServiceAdminApplication.class)
@ActiveProfiles(value = "prod")
@Transactional
@Rollback
public class AdminServiceTest {

    @Autowired
    private AdminService adminService;

    /**
     * 注册
     */
    @Test
    public void testRegister() {
        TbSysUser tbSysUser = new TbSysUser();
        tbSysUser.setUserCode(UUID.randomUUID().toString());
        tbSysUser.setLoginCode("[email protected]");
        tbSysUser.setUserName("Lusifer");
        tbSysUser.setUserType("1");
        tbSysUser.setMgrType("1");
        tbSysUser.setStatus("0");
        tbSysUser.setCreateBy(tbSysUser.getUserCode());
        tbSysUser.setCreateDate(new Date());
        tbSysUser.setUpdateBy(tbSysUser.getUserCode());
        tbSysUser.setUpdateDate(new Date());
        tbSysUser.setCorpCode("0");
        tbSysUser.setCorpName("iToken");
        tbSysUser.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        adminService.register(tbSysUser);
    }

    /**
     * 登录
     */
    @Test
    public void testLogin() {
        TbSysUser tbSysUser = adminService.login("[email protected]", "123456");
        Assert.assertNotNull(tbSysUser);
    }
}

特别说明

由于服务代码大同小异,故仅 管理员服务 提供相关代码案例,其它服务无特殊情况不再以博客性质提供代码,具体编程手法请参照我的视频。

你可能感兴趣的:(创建管理员服务提供者)