当前位置:网站首页>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 |
边栏推荐
- 【LeetCode】5. Valid Palindrome·有效回文
- Mathematical formula screenshot recognition artifact mathpix unlimited use tutorial
- 保研笔记一 软件工程与计算卷二(1-7章)
- 3: Chapter 1: understanding JVM specification 2: JVM specification, introduction;
- GFS分布式文件系统
- (4) UART application design and simulation verification 2 - RX module design (stateless machine)
- Comparison between webgl and webgpu [3] - vertex buffer
- 无刷驱动设计——浅谈MOS驱动电路
- (4)UART应用设计及仿真验证2 —— TX模块设计(无状态机)
- YML configuration, binding and injection, verification, unit of bean
猜你喜欢
Development specification: interface unified return value format [resend]
如何获取localStorage中存储的所有值
Neural structured learning - Part 2: training with natural graphs
Practice of concurrent search
GFS分布式文件系统
Pyqt control part (I)
Hcip course notes-16 VLAN, three-tier architecture, MPLS virtual private line configuration
[original] what is the core of programmer team management?
Rsync remote synchronization
The PNG image is normal when LabVIEW is opened, and the full black image is obtained when Photoshop is opened
随机推荐
C# 文件与文件夹操作
《牛客刷verilog》Part III Verilog企业真题
idea 连接mysql ,直接贴配置文件的url 比较方便
Objective C message dispatch mechanism
QCombox(重写)+QCompleter(自动补全,自动加载qcombox的下拉选项,设置背景颜色)
How to insert data into MySQL database- How can I insert data into a MySQL database?
Rasa 3. X learning series -rasa 3.2.1 new release
Design and implementation of secsha system
UVA11294-Wedding(2-SAT)
Live tiktok shop 2022 latest gameplay card slot overseas live e-commerce new traffic
Common static methods of math class
The interface of grafana tool displays an error, incluxdb error
Qcombox (rewrite) + qcompleter (auto completion, auto loading the drop-down options of qcombox, setting the background color)
el-cascader的使用以及报错解决
CIS基准测试工具kube-bench使用
98. 验证二叉搜索树 ●●
ts类型声明declare
带外和带内的区别
Code farmers to improve productivity
同事悄悄告诉我,飞书通知还能这样玩