一、实例类注解
1 @TableName(“XXX”)
常用于解决库表名和实体名的驼峰命名不一致的问题
@TableName("user")public class User {}
源码
package com.baomidou.mybatisplus.annotation;
import java.lang.annotation.*;
/**
* 数据库表相关
*
* @author hubin, hanchunlin
* @since 2016-01-23
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface TableName {
/**
* 实体对应的表名
*/
String value() default "";
/**
* schema
*
* @since 3.1.1
*/
String schema() default "";
/**
* 是否保持使用全局的 tablePrefix 的值
* <p> 只生效于 既设置了全局的 tablePrefix 也设置了上面 {@link #value()} 的值 </p>
* <li> 如果是 false , 全局的 tablePrefix 不生效 </li>
*
* @since 3.1.1
*/
boolean keepGlobalPrefix() default false;
/**
* 实体映射结果集
*/
String resultMap() default "";
}
2 @TableId(type = IdType.AUTO)
用于配置实体主键的自增策略:
/*
* Copyright (c) 2011-2020, baomidou (jobob@qq.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.annotation;
import java.lang.annotation.*;
/**
* 表主键标识
*
* @author hubin
* @since 2016-01-23
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TableId {
/**
* 字段值(驼峰命名方式,该值可无)
*/
String value() default "";
/**
* 主键ID
* {@link IdType}
*/
IdType type() default IdType.NONE;
}
MP支持的id策略:
/*
* Copyright (c) 2011-2020, baomidou (jobob@qq.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.annotation;
import lombok.Getter;
/**
* 生成ID类型枚举类
*
* @author hubin
* @since 2015-11-10
*/
@Getter
public enum IdType {
/**
* 数据库ID自增:需要库表已经设置为自增
*/
AUTO(0),
/**
* 该类型为未设置主键类型
*/
NONE(1),
/**
* 用户输入ID
* <p>该类型可以通过自己注册自动填充插件进行填充</p>
*/
INPUT(2),
/* 以下3种类型、只有当插入对象ID 为空,才自动填充。 */
/**
* 全局唯一ID (idWorker)
*/
ID_WORKER(3),
/**
* 全局唯一ID (UUID)
*/
UUID(4),
/**
* 字符串全局唯一ID (idWorker 的字符串表示)
*/
ID_WORKER_STR(5);
private final int key;
IdType(int key) {
this.key = key;
}
}
修改User
package com.hua.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data // getter setter toString
@NoArgsConstructor
@AllArgsConstructor
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
}
注意:需要库表先开启自动增长
3 @TableField
com.baomidou.mybatisplus.annotation.TableId
在MP中通过@TableField注解可以指定字段的一些属性,用于解决如下问题。
对象中的属性名和字段名不一致的问题(非驼峰)
@TableField("email") private String mail;字段在数据库中不存在的情况
@TableField(exist = false) private String address;指定字段不加入查询
@TableField(select = false) private String name;示例:

效果:
