绿岛的使用和一些细节的总结
最编程
2024-06-24 10:53:42
...
-
GreenDao
- 一简介
-
一greenDao的配置
- projectbuildgradle
- appbuildgradle
-
二使用
- 配置注解映射实体类
- 获取DaoSession对数据进行操作
-
细节问题
- 凡是涉及ID的一定要使用Long类型的包装类
- 关联实体的类的getset方法一定要使用Generate
GreenDao
一、简介
GreenDao是一个对象映射数据解决方案的快速开发框架,很多sql语句直接变换简单的代码。
一、greenDao的配置
project/build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
app/build.gradle
apply plugin: 'org.greenrobot.greendao'
android {
………………
}
dependencies {
………………
//数据库操作
compile 'org.greenrobot:greendao-generator:3.2.0'
compile 'org.greenrobot:greendao:3.2.0'
}
二、使用
配置注解映射实体类
greendao是插件化开发的,所以配置完注解,直接整个工程rebuild一下,就会自动生成Dao部分代码
@Entity(nameInDb = "t_book")
public class Book {
@Id(autoincrement = true)
@Property(nameInDb = "id")
private Long id;
@Property(nameInDb = "f_book")
private String book;
@ToMany(referencedJoinProperty = "bookId")
private List<Unit> units;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getBook() {
return book;
}
public void setBook(String book) {
this.book = book;
}
@Generated//一定要标识这个@Generated,不要标识@Keep
public List<Unit> getUnits() {
return units;
}
@Generated//一定要标识这个@Generated,不要标识@Keep
public void setUnits(List<Unit> units) {
this.units = null;
}
}
@Id(autoincrement = true)
@Property(nameInDb = "id")
private long id;
@Property(nameInDb = "f_book_id")
private long bookId;//书籍id
@ToOne(joinProperty = "bookId")//请注意:joinProperty这是变量名,不是数据库字段
private Book book;
@Property(nameInDb = "f_unit")
private String unit;//单元
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getBookId() {
return bookId;
}
public void setBookId(long bookId) {
this.bookId = bookId;
}
@Generated(hash = 8805314)//一定要标识这个@Generated,不要标识@Keep
public Book getBook() {
return book;
}
@Generated//一定要标识这个@Generated,不要标识@Keep
public void setBook( Book book) {
this.book=book;
}
@Entity:标识这是一个数据库映射类
@Id(autoincrement = true):标识主键,autoincrement = true设置主键自增
@Property(nameInDb = “id”):标识为数据库字段,nameInDb = “id”表示对应数据库的字段名。如果不使用nameInDb,将默认采用变量名作为数据库的字段名。
@ToMany(referencedJoinProperty = “bookId”):表示一对多的关系,referencedJoinProperty = “bookId”,表示引用关联实体的属性“bookId”,此属性名对应引用实体类的变量名。
@ToOne(joinProperty = “bookId”):表示一对一,或者多对一的关系,joinProperty = “bookId”,表示分享属性“bookId”,此属性名对应本类的变量名。
获取DaoSession对数据进行操作
DaoMaster.DevOpenHelper mHelper =new DaoMaster.DevOpenHelper(App.getInstance(), DB_NAME, null);//DB_NAME,可以设置为已经存在的外部数据库,如果没有,会自动创建
SQLiteDatabase db = mHelper.getWritableDatabase();
DaoSession mDaoSession = mDaoMaster.newSession();//同步操作时,需要使用DaoSession
AsyncSession mAsyncSession= mDaoSession.startAsyncSession();//异步处理时,需要使用AsyncSession
查询数据
//直接查询全部
UnitDao unitDao = GreenDaoHelper.getDaoSession().getUnitDao();
unitDao.loadAll();
//根据条件查询
UnitDao unitDao = mDaoSession .getUnitDao();
WhereCondition eq = unitDao.Properties.Id.eq(1);
WhereCondition eq5 = unitDao.Properties.BookId.eq(1);
List<Unit> list = unitDao.queryBuilder().where(eq1, eq2, eq3, eq4).build().list();
//异步数据查询
StudyWordScoreDao studyWordScoreDao = GreenDaoHelper.getDaoSession().getStudyWordScoreDao();
WhereCondition eq1 = StudyWordScoreDao.Properties.BookId.eq(bookId);
WhereCondition eq2 = StudyWordScoreDao.Properties.UnitId.eq(unitId);
WhereCondition eq3 = StudyWordScoreDao.Properties.UnitId.eq(userId);
WhereCondition eq4 = StudyWordScoreDao.Properties.UnitId.eq(recordId);
Query<StudyWordScore> build = studyWordScoreDao.queryBuilder().where(eq1, eq2, eq3, eq4).build();
// WhereCondition and = wordDao.queryBuilder().and(eq, eq1);
asyncSession.queryList(build);
asyncSession.setListenerMainThread(asyncOperationListener);
细节问题
凡是涉及ID的一定要使用Long类型的包装类
凡是涉及ID的一定要使用Long类型的包装类,不然,autoincrement = true会失效,我之前就是这样,插入的数据会直接赋为“0”
//根据官网的方法,说主键赋值为空,主键就会自增,但是编译器不通过,报错,因为初始为0,不是null,所以我就直接不设置主键值,果然就可以了
new Book(null,"必修一");
关联实体的类的get/set方法一定要使用@Generate
//只有使用了Generated,greenDao的插件才会将getset修改成以下代码,如果使用@keep那就保留getset代码,也就没有关联效果了
@Generated(hash = 835179934)
public List<Unit> getUnits() {
if (units == null) {
final DaoSession daoSession = this.daoSession;
if (daoSession == null) {
throw new DaoException("Entity is detached from DAO context");
}
UnitDao targetDao = daoSession.getUnitDao();
List<Unit> unitsNew = targetDao._queryBook_Units(id);
synchronized (this) {
if (units == null) {
units = unitsNew;
}
}
}
return units;
}
/** Resets a to-many relationship, making the next get call to query for a fresh result. */
@Generated(hash = 121816020)
public synchronized void resetUnits() {
units = null;
}
暂时只总结部分,后续会继续更新此文章
推荐阅读
-
Java Spring 中的 @Autowired、@Resource、@Qualifier 和 @Inject 注解:使用细节和注意事项
-
SpringCloud--持久层框架MyBatis Plus的使用方法和原理详解--V.MyBatis Plus 使用总结
-
Android WebView 和 H5 交互的一些总结
-
总结一些在C语言中使用for循环的技巧
-
实战分享:在JMeter中使用KeepAlive进行http请求的技巧和经验总结
-
HHKB BT 蓝牙键盘:装逼必备利器?" 使用HHKB BT 蓝牙键盘的心得体会 在互联网的世界里,越来越多的人都追求个性化和独特性,无论是穿着打扮,还是使用的电子产品,都能看到人们的创意和想法。最近我在网上看到了一款备受推崇的机械键盘——HHKB BT 蓝牙键盘。 HHKB BT 蓝牙键盘是一款由日本知名键盘制造商 Filco 生产的产品,被誉为程序员的信仰。其最大的特点就是采用了静电容轴,按键更加灵敏且耐用。此外,HHKB BT 蓝牙键盘还具有非常紧凑的设计,仅60个键位,非常适合长时间工作或编程的人士使用。 然而,当我收到 HHKB BT 蓝牙键盘并开始使用时,我发现了一些问题。首先,数字键整体向右移动了一位,这对于习惯使用数字键的人来说是一种困扰。其次,HHKB BT 蓝牙键盘的价格较高,并且只能通过代购购买,售后服务也不完善。 尽管存在这些问题,我还是对 HHKB BT 蓝牙键盘产生了浓厚的兴趣。我尝试将数字键重新排列,使其更适合我的使用习惯,同时我也开始学习如何更有效地使用 HHKB BT 蓝牙键盘进行编程。 总的来说,HHKB BT 蓝牙键盘是一款非常专业且高效的机械键盘,如果你是一位需要长时间使用键盘的开发者或者程序员,那么它绝对值得你考虑。当然,高昂的价格和售后服务的问题也需要你权衡一下是否值得投资。
-
【摩尔线程+Colossal-AI强强联手】MusaBert登上CLUE榜单TOP10:技术细节揭秘 - 技术实力:摩尔线程凭借"软硬兼备"的技术底蕴,让MusaBert得以从底层优化到顶层。其内置多功能GPU配备AI加速和并行计算模块,提供了全面的AI与科学计算支持,为AI推理和低资源条件下的大模型训练等场景带来了高效、经济且环保的算力。 - 算法层面亮点:依托Colossal-AI AI大模型开发系统,MusaBert在训练过程中展现出了卓越的并行性能与易用性,特别在预处理阶段对DataLoader进行了优化,适应低资源环境高效处理海量数据。同时,通过精细的建模优化、领域内数据增强以及Adan优化器等手段,挖掘和展示了预训练语言模型出色的语义理解潜力。基于MusaBert,摩尔线程自主研发的MusaSim通过对比学习方法微调,结合百万对标注数据,MusaSim在多个任务如语义相似度、意图识别和情绪分析中均表现出色。 - 数据资源丰富:MusaBert除了自家高质量语义相似数据外,还融合了悟道开源200GB数据、CLUE社区80GB数据,以及浪潮公司提供的1TB高质量数据,保证模型即便在较小规模下仍具备良好性能。 当前,MusaBert已成功应用于摩尔线程的智能客服与数字人项目,并广泛服务于语义相似度、情绪识别、阅读理解与声韵识别等领域。为了降低大模型开发和应用难度,MusaBert及其相关高质量模型代码已在Colossal-AI仓库开源,可快速训练优质中文BERT模型。同时,通过摩尔线程与潞晨科技的深度合作,仅需一张多功能GPU单卡便能高效训练MusaBert或更大规模的GPT2模型,显著降低预训练成本,进一步推动双方在低资源大模型训练领域的共享目标。 MusaBert荣登CLUE榜单TOP10,象征着摩尔线程与潞晨科技联合研发团队在中文预训练研究领域的领先地位。展望未来,双方将携手探索更大规模的自然语言模型研究,充分运用上游数据资源,产出更为强大的模型并开源。持续强化在摩尔线程多功能GPU上的大模型训练能力,特别是在消费级显卡等低资源环境下,致力于降低使用大模型训练的门槛与成本,推动人工智能更加普惠。而潞晨科技作为重要合作伙伴,将继续发挥关键作用。
-
关于物模型的一些理解和总结
-
【2022新手指南】Java编程进阶之路 - 六、技术架构篇 ### MySQL索引底层解析与优化实战 - 你会讲解MySQL索引的数据结构吗?性能调优技巧知多少? - Redis深度揭秘:你知道多少?从基础到哨兵、主从复制全梳理 - Redis持久化及哨兵模式详解,还有集群搭建和Leader选举黑箱打开 - Zookeeper是个啥?特性和应用场景大公开 - ZooKeeper集群搭建攻略及 Leader选举、读写一致性、共享锁实现细节 - 探究ZooKeeper中的Leader选举机制及其在分布式环境中的作用 - Zab协议深入剖析:原理、功能与在Zookeeper中的核心地位 - RabbitMQ全方位解读:工作模式、消费限流、可靠投递与配置策略 - 设计者视角:RabbitMQ过期时间、死信队列与延时队列实践指南 - RocketMQ特性和应用场景揭示:理解其精髓与差异化优势 - Kafka详细介绍:特性及广泛应用于实时数据处理的场景解析 - ElasticSearch实力揭秘:特性概述与作为搜索引擎的广泛应用 - MongoDB认知升级:非关系型数据库的优势阐述,安装与使用实战教学 - BIO/NIO/AIO网络模型对比:掌握它们的区别与在网络编程中的实际应用 - Netty带你飞:理解其超快速度背后的秘密,包括线程模型分析 - 网络通信黑科技:Netty编解码原理与常用编解码器的应用,Protostuff实战演示 - 解密Netty粘包与拆包现象,怎样有效应对这一常见问题 - 自定义Netty心跳检测机制,轻松调整检测间隔时间的艺术 - Dubbo轻骑兵介绍:核心特性概览,服务降级实战与其实现益处 - Dubbo三大神器解读:本地存根与本地伪装的实战运用与优势呈现 ----------------------- 七、结语与回顾
-
使用Spring Boot和Mockito进行单元测试的经验分享与体会总结