1. 程式人生 > 實用技巧 >採用uuid作為主鍵的例子以及程式碼

採用uuid作為主鍵的例子以及程式碼

實體類的程式碼 user

package com.qbd.dhj.bbs.model;

import java.io.Serializable;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Table;

@Entity

@Table(name="tb_user")

public class User implements Serializable{

private static final long serialVersionUID = 1L;

private String id;

private String name;

private String password;

private int power;

@Id

public String getId() {

    return id;

}

public void setId(String id) {

    this.id = id;

}

public String getName() {

    return name;

}

public void setName(String name) {

    this.name = name;

}

public String getPassword() {

    return password;

}

public void setPassword(String password) {

    this.password = password;

}

public int getPower() {

    return power;

}

public void setPower(int power) {

    this.power = power;

}

}

2.action程式碼

public String execute() throws Exception{

    User user1=userService.login(user);

    if(user1==null){

        user.setId(UUIDUtils.getUUIDCode());

        userService.add(user);

        return "login";

    }

    return "register";

}

3.再插入資料之前設定號uuid