增删改查

一.查询

1.find方法【立即加载】

(1)代码块

/**
     * 测试根据id查询客户
     * 方式一
     * find方法在执行时,则获取的结果是实体类对象。立即加载
     */
    @Test
    public void test3(){
        //1.通过工具类获取对象
        EntityManager entityManager = JPAUtil.getEntityManager();
        //2.获取事务对象,并开启事务
        EntityTransaction tx = entityManager.getTransaction();
        tx.begin();
        //3.执行sql语句【查询操作】
        //参数一:实体类的字节码,参数二:id主键的属性值
        Customer customer = entityManager.find(Customer.class, 1l);
//        System.out.println(customer);
        //4.事务提交
        tx.commit();
        //5.关闭资源
        entityManager.close();
//        factory.close();  //工厂需要共享,不需要关闭
    }

(2)范例

图片

2.getTransaction【延迟加载】

(1)代码块

/**
     * 测试根据id查询客户
     * 方式二
     * getReference方法获取的是代理对象,当调用实体类对象时,则发送sql语法进行查询
     */
    @Test
    public void test4(){
        //1.通过工具类获取对象
        EntityManager entityManager = JPAUtil.getEntityManager();
        //2.获取事务对象,并开启事务
        EntityTransaction tx = entityManager.getTransaction();
        tx.begin();
        //3.执行sql语句【查询操作】
        //参数一:实体类的字节码,参数二:id主键的属性值
        Customer customer = entityManager.getReference(Customer.class, 1l);
//        System.out.println(customer);
        //4.事务提交
        tx.commit();
        //5.关闭资源
        entityManager.close();
//        factory.close();  //工厂需要共享,不需要关闭
    }

(2)范例

图片

二.删除

1.代码块

/**
     * 测试根据id删除客户
     * getReference方法根据id查询用户
     * remove方法根据id进行删除
     */
    @Test
    public void test5(){
        //1.通过工具类获取对象
        EntityManager entityManager = JPAUtil.getEntityManager();
        //2.获取事务对象,并开启事务
        EntityTransaction tx = entityManager.getTransaction();
        tx.begin();
        //3.执行sql语句【查询操作】
        //3.1根据id查询用户
        Customer customer = entityManager.getReference(Customer.class, 1l);
        //3.2使用用户对象删除记录
        //参数:id主键的属性值
        entityManager.remove(customer);//自动查找实体类对象中的主键属性
        //4.事务提交
        tx.commit();
        //5.关闭资源
        entityManager.close();
//        factory.close();  //工厂需要共享,不需要关闭
    }

2.范例

图片

三.更新

1.代码块

/**
     * 测试根据id更新客户
     * remove方法根据id进行查询
     */
    @Test
    public void test6(){
        //1.通过工具类获取对象
        EntityManager entityManager = JPAUtil.getEntityManager();
        //2.获取事务对象,并开启事务
        EntityTransaction tx = entityManager.getTransaction();
        tx.begin();
        //3.执行sql语句【查询操作】
        //3.1根据id查询用户
        Customer customer = entityManager.getReference(Customer.class, 1l);
        customer.setCustName("小李");
        //3.2使用用户对象更新记录
        //参数:实体类对象
        entityManager.merge(customer);//自动查找实体类对象中的主键属性
        //4.事务提交
        tx.commit();
        //5.关闭资源
        entityManager.close();
//        factory.close();  //工厂需要共享,不需要关闭
    }

2.范例

图片

四.添加操作

1.代码块

/**
 * 测试jpa的保存
 */
/*
        jpa的操作步骤
            1.加载配置文件创建工厂(实体管理工厂)对象
                作用:创建工厂
            2.通过工厂获取实体管理器
                作用:获取EntityManager对象
                EntityManager对象内部维护了很多内容。
                    (1)维护了数据库信息
                    (2)维护缓存信息
                    (3)维护了所以实体类对象
                    (4)在创建EntityManager对象通过配置文件创建数据库表中映射的实体类对象
                总结:创建EntityManager对象比较浪费资源.
                特点:线程安全。
            3.开启事务
            4.执行sql语句【增删改查】
            EntityManager对象的方法
                persist:保存方法
                merge:修改方法
                remove:删除方法
                find/getReference:查询方法【根据id】
            5.提交事务
            6.关闭资源
     */
@Test
public void test1(){
    //1.加载配置文件创建工厂(实体管理工厂)对象
    //参数是持久化单元名称
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("myJpa");
    //2.通过工厂获取实体管理器
    EntityManager entityManager = factory.createEntityManager();
    //3.获取事务对象,并开启事务
    EntityTransaction tx = entityManager.getTransaction();
    tx.begin();
    //4.执行sql语句【保存操作】
    //4.1创建对象
    Customer customer = new Customer();
    customer.setCustName("aaa");
    customer.setCustIndustry("辽宁大连");
    //4.2保存操作
    entityManager.persist(customer);
    //5.事务提交
    tx.commit();
    //6.关闭资源
    entityManager.close();
    factory.close();
}

2.范例

图片

图片

五.源码

day01.rar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android开发中,增删改查是常见的数据库操作。首先,我们需要创建一个数据库帮助类来定义数据库的结构和版本号,然后在该类中实现数据库的创建和升级操作。接着,我们可以创建一个数据模型类来定义数据库中的表和字段。接下来,我们通过SQLiteDatabase类来实现增删改查操作。 增加数据:我们可以通过ContentValues类来存储要插入的数据,然后调用SQLiteDatabase的insert()方法将数据插入到数据库中。 删除数据:我们可以使用SQLiteDatabase的delete()方法来删除符合特定条件的数据。我们可以通过提供相应的条件参数来删除特定的数据行。 更新数据:要更新数据,我们可以使用SQLiteDatabase的update()方法来执行更新操作。我们需要提供要更新的数据和更新条件作为参数。 查询数据:最后,我们可以使用SQLiteDatabase的query()方法来执行查询操作。我们可以指定要查询的表、列、条件、排序等参数来获取我们需要的数据。 除了使用原生的SQLiteDatabase类进行增删改查操作,我们还可以使用更高级的框架比如Room来简化数据库操作。Room提供了一个更加方便的方式来定义数据库和操作数据,使得增删改查变得更加简单。 总之,在Android开发中,数据库操作是一个非常重要的部分。通过以上方法,我们可以实现对数据的增删改查操作,并且可以根据实际需求选择合适的方法和框架来进行操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值