介绍Lazy Jpa 是lazy orm框架衍生出类似Spring JPA 框架

介绍Lazy Jpa 是lazy orm框架衍生出类似Spring JPA 框架

非Spring项目使用

安装
        <dependency>
            <groupId>top.wu2020</groupId>
            <artifactId>wu-database-lazy-jpa</artifactId>
            <version>1.2.6-JDK17-SNAPSHOT</version>
        </dependency>
声明接口

public interface TestLazyJpaRepository extends LazyJpaRepository<SysUser,Long> {
}

接口调用(基础CRUD)
    public static void main(String[] args) {
        LazyLambdaStream lazyLambdaStream = LazyLambdaStreamFactory.createLazyLambdaStream("jdbc:mysql://127.0.0.1:3306/wu_database_lazy_simple?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai",
                "root", "wujiawei");
        TestLazyJpaRepository lazyJpaRepository = LazyRepositoryFactorySupport.createLazyJpaRepository(lazyLambdaStream, TestLazyJpaRepository.class);
    
        // 统计数据库数量
        long count = lazyJpaRepository.count();
        // 获取所有数据
        Iterable<SysUser> all = lazyJpaRepository.findAll();
        for (SysUser sysUser : all) {
            System.out.println(sysUser);
        }
    }

Spring 项目使用

安装
        <dependency>
            <groupId>top.wu2020</groupId>
            <artifactId>wu-framework-lazy-orm-spring-starter</artifactId>
            <version>1.2.6-JDK17-SNAPSHOT</version>
        </dependency>
声明接口
@LazyRepository
public interface TestLazyJpaRepository extends LazyJpaRepository<SysUser,Long> {
}
接口配置
@LazyRepositoryScan(scanBasePackages = "com.wu.xxx.mapper")// 扫描你使用的repository接口
@SpringBootApplication
public class WuDatabaseLazyStarterSimpleApplication {

    public static void main(String[] args) {
        SpringApplication.run(WuDatabaseLazyStarterSimpleApplication.class, args);
    }
}

接口使用

@LazyRepository
public interface SysMenuLazyJpaRepository extends LazyJpaRepository<SysMenu, Integer> {
    
    // 自定义查询
    @LazySelect("select * from sys_menu where parent_id={parentId}")
    List<SysMenu> findListByParentId(Integer parentId);


    @LazySelect("select * from sys_menu where id ={id}")
    SysMenu findMenuById(@LazyParam("id") Integer menuId);


    @LazySelect("select * from sys_menu where id ={id}")
    List<Map<?, ?>> findListMapByParentId(@LazyParam("id") Integer menuId);

    @LazyInsert("INSERT Ignore INTO `sys_menu` ( `id`, `name`, `url`, `icon`, `parent_id`,`parent_name`,`sort`,`status`) VALUES ({id},{name},{url},{icon},{parentId},{parentName},{sort},{status});")
    void insert(Integer id,
                String name,
                String url,
                String icon,
                Integer parentId,
                String parentName,
                Integer sort,
                Integer status
    );

    @LazyUpdate("UPDATE `sys_menu` SET  `name` = {name} WHERE `id` = {id};")
    void updateNameById(Integer id, String name);


    @LazyDelete("DELETE FROM sys_menu  WHERE `id` = {id};")
    void removeById(int id);
    /**
     * 执行sql select * from sys_menu where id={id}
     * 根据ID获取数据
     * @param id 数据ID
     */
    SysMenu findById(int id);

    /**
     * 执行sql select id,name,url,icon from sys_menu where id={id}
     * 获取ID、名称、url、icon
     * @param id 主键ID
     * @return 查询信息
     */
    SysMenu findIdAndNameAndUrlAndIconById(int id);
}
当前项目地址
测试案例地址
  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
JPA中,lazy loading是一种特性,用于延迟加载关联实体或集合。当使用lazy loading时,只有在实际需要访问关联数据时,才会从数据库加载该数据。这有助于提高性能和减少资源消耗。 在JPA中,可以通过在实体类的关联属性上使用@OneToMany(fetch = FetchType.LAZY)注解来指定懒加载。这样,在访问该属性时,JPA会在需要的时候从数据库中加载关联实体。 例如,在使用Hibernate的情况下,可以通过以下代码片段来演示懒加载的查询: ``` @Test public void testFind() { EntityManager em = MyJPAUtils.getEntityManager(); EntityTransaction tx = em.getTransaction(); tx.begin(); Customer customer = em.find(Customer.class, Integer.parseInt("3")); System.out.println(customer); Set<Contact> contacts = customer.getContacts(); System.out.println(contacts); tx.commit(); em.close(); } ``` 在这个例子中,我们通过调用`em.find()`方法来获取一个Customer实体,并通过`customer.getContacts()`方法来获取与该实体关联的联系人集合。由于使用了懒加载,只有在调用`customer.getContacts()`方法时,才会从数据库中加载联系人数据。 然而,需要注意的是,在某些情况下,如在IDEA的debug模式下,懒加载可能会失效。这是因为在debug模式下,会自动进行级联查询,导致懒加载失效。所以在这种情况下,可能会现无法延迟加载的情况。 总之,JPA中的懒加载是一种实现延迟加载的机制,可以提高性能和减少资源消耗。在使用JPA查询时,可以使用相关注解来指定懒加载的属性,并在需要访问关联数据时才会从数据库加载。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [jtechlog-lazy:JPA延迟加载](https://download.csdn.net/download/weixin_42134143/19273765)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [JPA中的对象导航查询及其lazy属性](https://blog.csdn.net/dimples_qian/article/details/80907825)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [jpa一对多OneToMany关联查询;debug下lazy失效原因](https://blog.csdn.net/weixin_43944305/article/details/122336231)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小吴小吴bug全无

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

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

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

打赏作者

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

抵扣说明:

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

余额充值