介绍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_menuWHERE `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);
}
当前项目地址
测试案例地址