Java ORM对象映射-EntityFramework

本文档展示了数据库操作的DatabaseFirst和CodeFirst两种方法,包括模型类定义、上下文类构建、数据增删查改及联合查询。通过实例详细解释了如何在Java环境中进行数据库操作,涉及表结构设计、数据注入、查询筛选以及数据库同步。
摘要由CSDN通过智能技术生成

代码演示

表结构

TBUser

字段名类型备注
Guidint主键,非空
Namenchar(10)名称

TBInfo

字段名类型备注
Guidint主键,非空
Ageint年龄
Scoreint分数

DatabaseFirst

模型类

在这里插入图片描述

在这里插入图片描述

public class TBUser implements IModel {
    public int Guid;
    public String Name;
}
public class TBInfo implements IModel {
    public int Guid;
    public int Age;
    public int Score;
}

PS:在模型类中,默认主键为第一个元素

上下文类
public class MyContext extends DbContext {

    public MyContext() throws Exception {
        onCreate();
    }

    public DbSet<TBUser> TbUser = new DbSet<>(TBUser.class);
    public DbSet<TBInfo> TbInfo = new DbSet<>(TBInfo.class);


    @Override
    protected void onCreate() throws Exception {
        this.useSqlOperation(MySqlOperation.useDefault());
        this.run();
    }
}

所有上下文需要继承DbContext,然后把所有的表模型初始化到DbSet中,记得重写onCreate函数

函数名参数用途
useSqlOperationIOperation初始化SQL连接对象,并对所有的DbSet进行初始化注入
run初始化DbSet池,载入数据库的元素
build进行CodeFirst创建数据库
实例化使用

先创建5条User数据

public class Main {
    public static void main(String[] args) throws Exception {

        MyContext context = new MyContext();

        context.TbUser.add(new TBUser(0, "Alice"));
        context.TbUser.add(new TBUser(1, "Bob"));
        context.TbUser.add(new TBUser(2, "Elima"));
        context.TbUser.add(new TBUser(3, "Win"));
        context.TbUser.add(new TBUser(4, "Gin"));

        context.saveChanges();

    }
}

查看这5条数据

public class Main {
    public static void main(String[] args) throws Exception {

        MyContext context = new MyContext();

        var result = context.TbUser
                .select(s->String.format("%s的编号为%d", s.Name, s.Guid));

        QueryLoader.StdOutput(result);

    }
}
>>>>>
Alice的编号为0
Bob的编号为1
Elima的编号为2
Win的编号为3
Gin的编号为4

进程已结束,退出代码0

我们生成一部分的TbInfo

public class Main {
    public static void main(String[] args) throws Exception {
        MyContext context = new MyContext();
        Random random = new Random();

        context.TbUser
                .where(s -> s.Name.length() <= 3 || s.Name.startsWith("A"))
                .select(s -> new TBInfo(s.Guid, random.nextInt(30), random.nextInt(100)))
                .toList()
                .forEach((Consumer<? super TBInfo>) s -> {
                    try {
                        context.TbInfo.add(s);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                });

        context.saveChanges();
    }
}

我们先查询到Elima的数据并删除它

public class Main {
    public static void main(String[] args) throws Exception {
        MyContext context = new MyContext();
        var elima = context.TbUser.where(s->s.Name.equals("Elima")).firstOrDefault();
        //var elima = context.TbUser.firstOrDefault(s->s.Name.equals("Elima")); 同样可以使用
        context.TbUser.remove(elima);
        context.saveChanges();
    }
}

然后我们联合查询一下两张表,使用join函数,并更新Score排序

public class Main {
    public static void main(String[] args) throws Exception {
        MyContext context = new MyContext();
        var result = context
                .TbUser
                .join(context.TbInfo,
                        s->s.Guid,
                        s->s.Guid,
                        s->s)
                .orderBy(s->s.B.Score)
                .reverse()
                .select(s->String.format("编号%d的名称为%s,年龄为%d,得分为%d",s.A.Guid,s.A.Name,s.B.Age,s.B.Score));
        QueryLoader.StdOutput(result);
    }
}
>>>>>
编号1的名称为Bob,年龄为3,得分为67
编号3的名称为Win,年龄为18,得分为56
编号0的名称为Alice,年龄为15,得分为36
编号4的名称为Gin,年龄为14,得分为25

进程已结束,退出代码0

CodeFirst

模型类

首先构建模型类

public class TBUser implements IModel {
    @MainKey
    @Type(type = DataType.integer)
    public int Guid;

    @Type(type = DataType.nchar, length = 10)
    @Nullable
    public String Name;
    public TBUser(){}
    public TBUser(int guid, String name){
        Guid = guid;
        Name = name;
    }
}
public class TBInfo implements IModel {
    @MainKey
    @Type(type = DataType.integer)
    public int Guid;
    @Type(type = DataType.integer)
    @Nullable
    public int Age;
    @Type(type = DataType.integer)
    @Nullable
    public int Score;
    public TBInfo(){}
    public TBInfo(int guid, int age, int score){
        Guid = guid;
        Age = age;
        Score = score;
    }
}

@MainKey唯一指定,主键
@Nullable可空类型
@Length字符类型的指定长度
@Type指定字段类型
目前支持的类型有

  • nchar
  • int
  • datetime
  • varchar
上下文类

与DatabaseFirst相比,有一点差距就是onLoad的重写,其他都是一样的

@Override
    protected void onCreate() throws Exception {
        this.useSqlOperation(MySqlOperation.useDefault());
        this.build();
        this.run();
    }
实例化使用
public class Main {
    public static void main(String[] args) throws Exception {
        MyContext context = new MyContext();
    }
}

在这里插入图片描述

在这里插入图片描述
创建成功后,请把onCreate中删除build语句,不然会重复创建报错
后续操作与DatabaseFirst相同

项目结构

请添加图片描述

具体下载链接见后续帖子,或者留言私信作者…

持续更新中…

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

纸墨青鸢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值