最近在使用mybatis plus3.0時,測驗了一下id自動生成的策略,發現@TableId(value = "id",type = IdType.ID_WORKER_STR)不會有id產生,貼下我的配置代碼,在springboot中我是沒有使用mapper.xml檔案的
yml配置:
#服務名稱及埠設定
server:
port: 8080
servlet:
context-path: /hr
#DataSource Config
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/hr?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT
username: root
password: 123456
# 下面為連接池的補充設定,應用到上面的所有資料中
# 初始化大小,最小、最大
initialSize: 5
minIdle: 5
maxActive: 20
# 配置獲取連接等待超時的時間
maxWait: 60000
# 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一個連接在池中最小生存的時間,單位是毫秒
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打開PSCache,并且指定每個連接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置監控統計攔截的filters,去掉后監控界面sql無法統計,'wall'用于防火墻
filters: stat,wall,logback
logSlowSql: true
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多個DruidDataSource的監控資料
useGlobalDataSourceStat: true
# web-stat-filter:
# enabled: false
# jsp-servlet:
# class-name: com.alibaba.druid.support.http.StatViewServlet
# init-parameters:
# loginUsername: druid
# loginPassword: druid
# Logger Config
#mybatis plus
mybatis-plus:
# mapper-locations: classpath:/mapper/*.xml
#物體掃描,多個package用逗號或者分號分隔
typeAliasesPackage: com.zxl.hr.pojo.*
#typeEnumsPackage: com.baomidou.springboot.entity.enums
global-config:
#重繪mapper 除錯神器
db-config:
#主鍵型別 0:"資料庫ID自增", 1:"用戶輸入ID",2:"全域唯一ID (數字型別唯一ID)", 3:"全域唯一ID UUID";
# id-type: UUID
#欄位策略 0:"忽略判斷",1:"非 NULL 判斷"),2:"非空判斷"
field-strategy: not_empty
#駝峰下劃線轉換
column-underline: true
#資料庫大寫下劃線轉換
#capital-mode: true
#邏輯洗掉配置
logic-delete-value: 1
logic-not-delete-value: 0
db-type: mysql
refresh: true
#自定義填充策略介面實作
#meta-object-handler: com.baomidou.springboot.xxx
#自定義SQL注入器
#sql-injector: com.baomidou.mybatisplus.extension.injector.LogicSqlInjector
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
物體類
package com.zxl.hr.pojo.sys;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import java.io.Serializable;
/**
* @author zxl
* @description: 用戶
* @date 2020/3/16
*/
@Data
@TableName("t_sys_user")
public class SysUserTest extends Model {
/**
* 主鍵
*/
@TableId(value = "id",type = IdType.ID_WORKER_STR)
private String id;
/**
* 創建時間
*/
@TableField(value = "create_date",fill = FieldFill.INSERT)
private String createDate;
/**
* 修改時間
*/
@TableField(value = "modify_date", fill = FieldFill.INSERT_UPDATE)
private String modifyDate;
/**
* 用戶名
*/
@TableField("user_name")
private String userName;
/**
* 密碼
*/
@TableField("user_pwd")
private String userPwd;
/**
* 加密密碼的鹽
*/
@TableField("salt")
private String salt;
/**
* 用戶真實名稱
*/
@TableField("real_name")
private String realName;
/**
* 性別:0男1女
*/
@TableField("sex")
private String sex;
/**
* 郵箱
*/
@TableField("email")
private String email;
/**
* 電話
*/
@TableField("telephone")
private String telephone;
/**
* 手機
*/
@TableField("mobile")
private String mobile;
/**
* 狀態
* 用戶狀態,
* 0:可用,
* 1:禁止,
*/
@TableField("status")
private String status;
/**
* 是否領導:0是1否
*/
@TableField("is_leader")
private String isLeader;
/**
* 是否洗掉(0:未洗掉 1:已洗掉)
*/
/**
* TableLogic 表示邏輯洗掉,加上后洗掉時資料庫物理不洗掉,將此資料打上洗掉標記,若希望物理洗掉則不加該注解即可;該標記同樣屏蔽查詢和更新
*/
@TableField("deleted")
@TableLogic
private Integer deleted;
/**
* 備注
*/
@TableField("remark")
private String remark;
@Override
protected Serializable pkVal() {
return this.id;
}
/**
* 密碼鹽. 重新對鹽重新進行了定義,用戶名+salt,這樣就不容易被破解,可以采用多種方式定義加鹽
* @return
*/
public String getCredentialsSalt(){
return this.userName+this.salt;
}
}
mapper介面
public interface SysUserMapper extends BaseMapper<SysUser> {
@Insert("insert into t_sys_user (user_name) values(#{userName})")
void insertTest(SysUserTest user);
}
service呼叫
@Resource
SysUserMapper userMapper;
@Override
public void insertTest(SysUserTest user) {
userMapper.insertTest(user);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/113252.html
標籤:Java相關
