当前位置:网站首页>Use mapper: --- tkmapper
Use mapper: --- tkmapper
2022-07-05 23:39:00 【Chen Hu_ sixty-three】
Introduce dependencies :
Import dependence
<!--tkMapper Integrate mybatis+SpringBoot-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<!--tkMapper Integrate mybatis-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.1.2</version>
</dependency>
To configure :
One 、 Set up TkMapper and GukeMapper Put it in a bag
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface TkMapper<T> extends Mapper<T>, MySqlMapper<T> {
}
GukeMapper Inherit TkMapper Use
public interface GukeMapper extends TkMapper<Guke>{
}
Database corresponds to entity class :
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name = " Table name ")
public class Guke{
// Primary key automatic hump mapping
@Id // Primary key ID Fields in the table test_id
// @GeneratedValue(strategy = GenerationType.AUTO)// Automatically switch the primary key generation strategy according to the underlying database
@GeneratedValue(strategy = GenerationType.IDENTITY)// Use mysql The primary key generation strategy of
// @GeneratedValue(strategy = GenerationType.SEQUENCE)// Use Oracle The primary key generation strategy of
private Integer id;
private String testId;
@Id // Primary key ID Fields in the table test_no
private String testNo;
@Column(name = "name_no")
private String nameNo;
get/set
}
Four 、 Add scanning in the startup class TestMapper
@tk.mybatis.spring.annotation.MapperScan("com.swhysc.mapper.*")
Common methods and precautions :
public interface BaseMapper<T> extends tk.mybatis.mapper.common.BaseMapper<T>, IdsMapper<T>, MySqlMapper<T>, OracleMapper<T> {
}
notes : For demonstration, we also quote MySqlMapper and OracleMapper Normally, only one method can be cited because they have the same method insertList(List list), When using, it will report errors
Generic ( Entity class ) The type of must meet the requirements
Entity classes are converted to database tables according to the following rules , Notes are all JPA The annotations in :
- The table name uses the class name by default , Hump underline ( Only uppercase letters are processed ), Such as UserInfo The default corresponding table name is user_info.
- The table name can use @Table(name = “tableName”) Make a designation , For those that do not conform to the first default rule, you can specify the table name in this way .
- Fields default to and @Column equally , Will be used as table fields , The table field defaults to Java Object's Field Name hump to underline .
- have access to @Column(name = “fieldName”) The designation does not conform to article 3 Field name of rule .
- Use @Transient Annotations can ignore fields , Fields with this annotation will not be used as table fields .
- The suggestion must be that there is one @Id Annotate the field as the primary key , There can be multiple @Id Annotated fields are used as joint primary keys .
- // Primary key automatic hump mapping
@Id // Primary key ID Fields in the table test_id
// @GeneratedValue(strategy = GenerationType.AUTO)// Automatically switch the primary key generation strategy according to the underlying database
@GeneratedValue(strategy = GenerationType.IDENTITY)// Use mysql The primary key generation strategy of
// @GeneratedValue(strategy = GenerationType.SEQUENCE)// Use Oracle The primary key generation strategy of
private Integer id;
be-all mapper Inheriting this class will have the following general methods
Query methods
BaseSelectMapper General method under
Method name | effect |
---|---|
List selectAll(); | Query all data |
oolean existsWithPrimaryKey(Object key); | Query whether this primary key exists through the primary key |
T selectByPrimaryKey(Object key); | Query by primary key |
– | – |
T selectOne(T record); | Query single data through entities |
– | – |
List select(T record); | Query multiple data through entities |
– | – |
int selectCount(T record); | Query the number of entities through entities |
SelectByIdsMapper General method under
Method name | effect |
---|---|
List selectByIds(String var1); | Query data through multiple primary keys |
边栏推荐
- Neural structured learning - Part 2: training with natural graphs
- Rsync remote synchronization
- Rasa 3.x 学习系列-Rasa 3.2.1 新版本发布
- 动态规划 之 打家劫舍
- Go language introduction detailed tutorial (I): go language in the era
- Technical specifications and model selection guidelines for TVs tubes and ESD tubes - recommended by jialichuang
- Introduction to JVM
- What is the process of building a website
- Practice of concurrent search
- 【LeetCode】5. Valid Palindrome·有效回文
猜你喜欢
21.PWM应用编程
2: Chapter 1: understanding JVM specification 1: introduction to JVM;
动态规划 之 打家劫舍
TVS管和ESD管的技術指標和選型指南-嘉立創推薦
98. Verify the binary search tree ●●
Fiddler Everywhere 3.2.1 Crack
Dynamic memory management (malloc/calloc/realloc)
SpreadJS 15.1 CN 与 SpreadJS 15.1 EN
Hcip course notes-16 VLAN, three-tier architecture, MPLS virtual private line configuration
4点告诉你实时聊天与聊天机器人组合的优势
随机推荐
《牛客刷verilog》Part III Verilog企业真题
基于脉冲神经网络的物体检测
C# 文件与文件夹操作
regular expression
98. Verify the binary search tree ●●
Neural structured learning - Part 3: training with synthesized graphs
698. 划分为k个相等的子集 ●●
Redis高可用——主从复制、哨兵模式、集群
无刷驱动设计——浅谈MOS驱动电路
How to enable relationship view in phpMyAdmin - how to enable relationship view in phpMyAdmin
[Yu Yue education] NC machining technology reference materials of Shaanxi University of science and technology
Mathematical formula screenshot recognition artifact mathpix unlimited use tutorial
Hcip course notes-16 VLAN, three-tier architecture, MPLS virtual private line configuration
Bao Yan notes II software engineering and calculation volume II (Chapter 13-16)
Attacking technology Er - Automation
When to use useImperativeHandle, useLayoutEffect, and useDebugValue
Introduction to JVM
CIS基准测试工具kube-bench使用
TVS管和ESD管的技术指标和选型指南-嘉立创推荐
俄外交部:日韩参加北约峰会影响亚洲安全稳定