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

来源: 投稿
2024-05-16 15:52:00

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

非Spring项目使用

安装

        <dependency>
            <groupId>top.wu2020groupId>
            <artifactId>wu-database-lazy-jpaartifactId>
            <version>1.2.6-JDK17-SNAPSHOTversion>
        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.wu2020groupId>
            <artifactId>wu-framework-lazy-orm-spring-starterartifactId>
            <version>1.2.6-JDK17-SNAPSHOTversion>
        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);
}

当前项目地址

测试案例地址

展开阅读全文
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
分享
返回顶部
顶部