typeorm之Entity Listeners
typeorm有七个实体监听器:
发布日期:2021-05-28 16:24:05
浏览次数:32
分类:技术文章
本文共 1934 字,大约阅读时间需要 6 分钟。
AfterLoad: 实体加载监听器
BeforeInsert:插入之前监听器
AfterInsert:插入之后监听器
BeforeUpdate:更新之前监听器
AfterUpdate:更新之后监听器
BeforeRemove:移除之前监听器
AfterRemove:移除之后监听器
但是由于可插入实体方法有insert、save,更新实体方法有save、update、updateById,移除实体方法有delete、deleteById、remove、removeById等,并不是所有方法都能触发实体监听器
实体类:
import { Entity, Column, PrimaryColumn, Index, OneToOne, JoinColumn, OneToMany, AfterLoad,BeforeInsert,AfterInsert,BeforeUpdate,AfterUpdate,BeforeRemove,AfterRemove} from 'typeorm';@Entity({ name: 'bucket'})export class Bucket { //主键,需要设置插入,1默认为公有空间配置,2默认为私有空间配置 @PrimaryColumn({ name: 'id', type: 'int' }) id: number; @Column({ name: 'age', type: 'int' }) age:number @AfterLoad() al(){ console.log('加载之后') } @BeforeInsert() bi(){ console.log('插入之前') } @AfterInsert() ai(){ console.log('插入之后') } @BeforeUpdate() bu(){ console.log('更新之前') } @AfterUpdate() au(){ console.log('更新之后') } @BeforeRemove() br(){ console.log('删除之前') } @AfterRemove() ar(){ console.log('删除之后') }}操作:
import { createConnection, Repository, Connection } from 'typeorm'import { Bucket } from './Bucket'createConnection({ name: 'test', type: "mysql", host: "localhost", port: 3306, username: "root", password: "123456", database: "test", entities:[Bucket], synchronize:true, dropSchema:true}).then(async (connection:Connection) => { let re = connection.getRepository(Bucket) let bucket : Bucket = re.create({id:1,age:222}) console.log('save插入') await re.save(bucket) bucket.age = 1111 console.log('save更新') await re.save(bucket) console.log('remove') await re.remove(bucket)})输出
save插入插入之前插入之后save更新加载之后更新之前更新之后remove加载之后删除之前删除之后说明:save方法插入可以触发insert监听器,save方法更新可以触发afterload、update监听器,remove方法可以触发afterload、remove监听器,更新、移除之前会先加载对应实体
经验证:除此之外,还有find、findByIds、findOne、findOneById这四个方法可以触发afterload监听器,其他insert、update、updateById、delete、deleteById等方法都不会触发监听器
转载地址:https://blog.csdn.net/qq_27868061/article/details/79291050 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年12月26日 10时53分26秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Linux网站压力测试工具webbench
2019-06-23
富士通X86平板ARROWS Tab
2019-06-23
Fedora 20 创建桌面快捷方式
2019-06-23
centOS文件动作的命令
2019-06-23
我的友情链接
2019-06-23
我的友情链接
2019-06-23
android 实用的Bitmap处理函数
2019-06-23
linux下LibCurl编程
2019-06-23
java开发模型MVC
2019-06-23
IntelliJ IDEA 发布最新版本13.0.1
2019-06-23
我的第一个的python抓取 单页面爬虫
2019-06-23
利用redis replication实现redis服务器热迁移
2019-06-23
angularjs弹出层
2019-06-23
Android系统的架构
2019-06-23
springboot+aop,拦截操作日志,登录日志
2019-06-23
我的友情链接
2019-06-23
htmlparser 源码说明
2019-06-23
Spring AOP
2019-06-23
扩展log4j日志级别
2019-06-23