郴州网站建设哪家好,宁波外贸seo网站建设,源码网站建设教程,专业做网站制作自助建站系统TableName(“表名”)
假设
表名是 book#xff0c;实体类类名是 Book
MyBatisPlus会进行自动映射
但如果 表名是 tab_book#xff0c;实体类类名是 Book
那么MyBatisPlus就无法进行自动映射#xff0c;需要我们使用 TableName注解 去指定实体类对应的表
如下
TableNa…TableName(“表名”)
假设
表名是 book实体类类名是 Book
MyBatisPlus会进行自动映射
但如果 表名是 tab_book实体类类名是 Book
那么MyBatisPlus就无法进行自动映射需要我们使用 TableName注解 去指定实体类对应的表
如下
TableName(tab_book)
Data
public class Book {private Integer id;private String name;private String type;private String description;
}TableId()
查看TableId 注解源码如下
Documented
Retention(RetentionPolicy.RUNTIME)
Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
public interface TableId {String value() default ;IdType type() default IdType.NONE;
}我们发现有两个属性 value 和 type
其中的 type 用于指定Id的生成策略我再另外一篇博客进行了非常详细的整理地址如下
MyBatisPlus之id生成策略
value的作用类似TableName当 实体类的 主键 属性名 与 数据表 中的 主键 字段名不同时就通过value做映射如下表中的字段名为 uid
TableName(tab_book)
Data
public class Book {TableId(value uid)private Integer id;private String name;private String type;private String description;
}如果属性名为value则value可以不显式指明 TableId(uid)private Integer id;TableField
实体类的 主键 属性名 与 数据表 中的 主键 字段名不同时通过 TableId() 中的value属性做映射
那实体类的 非主键 属性名 与 数据表 中的 非主键 字段名不同时就使用 TableField
比如实体类属性名为name而表中字段名为book_name那么就需要做如下映射
TableName(tab_book)
Data
public class Book {TableId(value uid)private Integer id;TableField(book_name)private String name;private String type;private String description;
}但是 如果实体类属性名为bookName而表中字段名为book_name那么我们就无需再指定MyBatisPlus 能自动处理这种映射
TableLogic
真实删除 – 使用 delete语句 将数据库中的记录直接删除但是这种做法是十分危险的因此开发中的删除一般是逻辑删除
逻辑删除 – 使用一个删除标记字段如 is_deleted 初始值为0置为 1 为逻辑上删除
因此逻辑删除实际上是修改功能
那如何使用呢
首先在表中添加 is_deleted 字段类型为int 长度为1默认值为 0
在实体类中添加 isDeleted 属性然后在该属性上添加 TableLogic 注解如下
TableName(tab_book)
Data
public class Book {TableId(value uid)private Integer id;TableField(book_name)private String name;private String type;private String description;TableLogicprivate Integer isDeleted;
}运行删除方法 Testvoid testGetById() {bookService.removeById(1);}查看日志
JDBC Connection [HikariProxyConnection1430358188 wrapping com.mysql.cj.jdbc.ConnectionImpl1f43cab7] will not be managed by SpringPreparing: UPDATE tab_book SET is_deleted1 WHERE id? AND is_deleted0Parameters: 1(Integer)Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession21a9a705]发现是update 并且是将 is_deleted 字段由 0 变 1