Java ORM对象映射-EntityFramework

发布于:2022-12-17 ⋅ 阅读:(133) ⋅ 点赞:(0)

代码演示

表结构

TBUser

字段名 类型 备注
Guid int 主键,非空
Name nchar(10) 名称

TBInfo

字段名 类型 备注
Guid int 主键,非空
Age int 年龄
Score int 分数

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函数

函数名 参数 用途
useSqlOperation IOperation 初始化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相同

项目结构

请添加图片描述

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

持续更新中…


网站公告

今日签到

点亮在社区的每一天
去签到