当前位置:网站首页>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 |
边栏推荐
- VS2010编写动态链接库DLL和单元测试,转让DLL测试的正确性
- Pyqt control part (I)
- Creative mode 1 - single case mode
- 424. The longest repeated character after replacement ●●
- There are 14 God note taking methods. Just choose one move to improve your learning and work efficiency by 100 times!
- Dynamic planning: robbing families and houses
- poj 2762 Going from u to v or from v to u? (infer whether it is a weak link diagram)
- Dynamic memory management (malloc/calloc/realloc)
- 21.PWM应用编程
- MySQL delete uniqueness constraint unique
猜你喜欢
TVS管和ESD管的技术指标和选型指南-嘉立创推荐
Fiddler Everywhere 3.2.1 Crack
4 points tell you the advantages of the combination of real-time chat and chat robots
Go language implementation principle -- lock implementation principle
Data analysis - Thinking foreshadowing
Neural structured learning - Part 3: training with synthesized graphs
4点告诉你实时聊天与聊天机器人组合的优势
同事悄悄告诉我,飞书通知还能这样玩
【原创】程序员团队管理的核心是什么?
Rasa 3.x 学习系列-Rasa 3.2.1 新版本发布
随机推荐
JVM的简介
Spécifications techniques et lignes directrices pour la sélection des tubes TVS et ESD - Recommandation de jialichuang
VS2010 writes DLL and unit test of dynamic link library, and transfers the correctness of DLL test
C# Linq Demo
In C#, why can't I modify the member of a value type instance in a foreach loop?
Russian Foreign Ministry: Japan and South Korea's participation in the NATO summit affects security and stability in Asia
4点告诉你实时聊天与聊天机器人组合的优势
Switching power supply buck circuit CCM and DCM working mode
2022.6.20-6.26 AI行业周刊(第103期):新的小生命
MySQL delete uniqueness constraint unique
Rasa 3. X learning series -rasa 3.2.1 new release
Leetcode sword finger offer brush questions - day 21
rsync远程同步
Neural structured learning 4 antagonistic learning for image classification
Go language implementation principle -- map implementation principle
ts类型声明declare
Part III Verilog enterprise real topic of "Niuke brush Verilog"
STM32__ 06 - single channel ADC
Golang code checking tool
进击的技术er——自动化