fastclassbyspringcglib.invoke,fastmybatis 2.8.1 发布,支持JPA Query Method查询

科技资讯 投稿 7300 0 评论

fastclassbyspringcglib.invoke,fastmybatis 2.8.1 发布,支持JPA Query Method查询

fastclassbyspringcglib.invoke,fastmybatis 2.8.1 发布,本次更新内如下:

    新增JPA Query Method查询
2.8.1 开始新增JPA Query Method查询,只需要在接口中定义方法就能进行查询,无须编写SQL语句。
原理:根据方法名称中的关键字自动推导出SQL语
目前实现了大部分功能,参考: JPA Query Method 除了Distinct不支持,其它已全部支持
在Mapper中定义一个方法,以 findBy 开头

 

/** * … where x.lastname = ?1 and x.firstname = ?2 * @param lastname * @param firstname * @return */ List<Student> findByLastnameAndFirstname(String lastname, String firstname);

在 Mapper中定义这个方法,就可以直接使用了,无须编写 XML。
使用:
    @Test
    public void findByLastnameAndFirstname() {
        List<Student> users = mapper.findByLastnameAndFirstname("张", "三");
        Assert.assertEquals(1, users.size());
        users.forEach(System.out::println);
    }
更多使用示例:
And findByLastnameAndFirstname … where x.lastname = ?1 and x.firstname = ?2
Or findByLastnameOrFirstname … where x.lastname = ?1 or x.firstname = ?2
Is, Equals findByFirstname,findByFirstnameIs,findByFirstnameEquals … where x.firstname = ?1
Between findByStartDateBetween … where x.startDate between ?1 and ?2
LessThan findByAgeLessThan … where x.age < ?1
LessThanEqual findByAgeLessThanEqual … where x.age <= ?1
GreaterThan findByAgeGreaterThan … where x.age > ?1
GreaterThanEqual findByAgeGreaterThanEqual … where x.age >= ?1
After findByStartDateAfter … where x.startDate > ?1
Before findByStartDateBefore … where x.startDate < ?1
IsNull, Null findByAge(Is)Null … where x.age is null
IsNotNull, NotNull findByAge(Is)NotNull … where x.age not null
Like findByFirstnameLike … where x.firstname like '%?1%'
NotLike findByFirstnameNotLike … where x.firstname not like '%?1%'
StartingWith findByFirstnameStartingWith … where x.firstname like '?1%'
EndingWith findByFirstnameEndingWith … where x.firstname like '%?1'
Containing findByFirstnameContaining … where x.firstname like '%?1%'
OrderBy findByAgeOrderByLastnameDesc … where x.age = ?1 order by x.lastname desc
Not findByLastnameNot … where x.lastname <> ?1
In findByAgeIn(Collection<Age> ages) … where x.age in ?1
NotIn findByAgeNotIn(Collection<Age> ages) … where x.age not in ?1
True findByActiveTrue() … where x.active = true
False findByActiveFalse() … where x.active = false
IgnoreCase findByFirstnameIgnoreCase … where UPPER(x.firstname) = UPPER(?1)

完整测试用例:

/** * 演示JPA findBy查询,根据方法名称自动生成查询语句,无需编写SQL * @author tanghc */ public interface StudentMapper extends CrudMapper<Student, Integer> { /** * … where x.lastname = ?1 and x.firstname = ?2 * @param lastname * @param firstname * @return */ List<Student> findByLastnameAndFirstname(String lastname, String firstname); /** * * … where x.lastname = ?1 or x.firstname = ?2 * @param lastname * @param firstname * @return */ List<Student> findByLastnameOrFirstname(String lastname, String firstname); /** * where x.firstname = ? * @param firstname * @return */ List<Student> findByFirstname(String firstname); List<Student> findByFirstnameIs(String firstname); List<Student> findByFirstnameEquals(String firstname); /** * … where x.startDate between ?1 and ?2 * @param begin * @param end * @return */ List<Student> findByStartDateBetween(Date begin, Date end); /** * … where x.age < ?1 * @param age * @return */ List<Student> findByAgeLessThan(int age); /** * … where x.age <= ?1 * @param age * @return */ List<Student> findByAgeLessThanEqual(int age); /** * … where x.age > ?1 * @param age * @return */ List<Student> findByAgeGreaterThan(int age); /** * … where x.age >= ?1 * @param age * @return */ List<Student> findByAgeGreaterThanEqual(int age); /** * … where x.startDate > ?1 * @param date * @return */ List<Student> findByStartDateAfter(Date date); /** * … where x.startDate < ?1 * @param date * @return */ List<Student> findByStartDateBefore(Date date); /** * … where x.age is null * @return */ List<Student> findByAgeNull(); List<Student> findByAgeIsNull(); /** * … where x.firstname like ?1 * @param firstname * @return * @see #findByFirstnameContaining(String) */ List<Student> findByFirstnameLike(String firstname); /** * … where x.firstname not like ?1 * @param firstname * @return */ List<Student> findByFirstnameNotLike(String firstname); /** * … where x.firstname like 'xx%' * @param firstname * @return */ List<Student> findByFirstnameStartingWith(String firstname); /** * … where x.firstname like '%xx' * @param firstname * @return */ List<Student> findByFirstnameEndingWith(String firstname); /** * 等同于like * … where x.firstname like '%xx%' * @param firstname * @return */ List<Student> findByFirstnameContaining(String firstname); /** * … where x.age = ?1 order by x.lastname desc * @param age * @return */ List<Student> findByAgeOrderByLastnameDesc(int age); /** * … where x.lastname <> ?1 * @param lastname * @return */ List<Student> findByLastnameNot(String lastname); /** * … where x.age in ?1 * * @param ages * @return */ List<Student> findByAgeIn(Collection<Integer> ages); /** * … where x.age not in ?1 * * @param ages * @return */ List<Student> findByAgeNotIn(Collection<Integer> ages); List<Student> findByAgeNotInAndIdIn(Collection<Integer> ages, List<Integer> ids); /** * … where x.active = 1 * @return */ List<Student> findByActiveTrue(); /** * … where x.active = 0 * @return */ List<Student> findByActiveFalse(); /** * … where UPPER(x.firstname) = UPPER(?1) * @param firstname * @return */ List<Student> findByFirstnameIgnoreCase(String firstname); // 复杂的例子 List<Student> findByLastnameOrFirstnameAndIdBetweenOrderByAgeDescIdAsc(String lastname, String firstname, int id1, int id2); }

JPA查询使用注意事项

    方法名称必须findBy开头
  • 数据库字段名必须遵从下划线模式,如:name, add_time,驼峰命名方式不支持JPA查询

关于fastmybatis

fastmybatis是一个mybatis开发框架,其宗旨为:简单、快速、有效。

    零配置快速上手,无需依赖Spring
  • 无需编写xml文件即可完成增删改查操作,支持LambdaQuery查询、支持JPA Query Method查询(findByXxx)
  • 支持mysql、sqlserver、oracle、postgresql、sqlite、StarRocks(原DorisDB)
  • 支持自定义sql,对于基本的增删改查不需要写SQL,对于其它特殊SQL(如统计SQL)可写在xml中
  • 支持与spring-boot集成,依赖starter即可,支持Springboot3.0
  • 支持插件编写
  • 支持ActiveRecord模式
  • 支持多租户
  • 提供通用Service
  • API丰富,多达40+方法,满足日常开发需求
  • 轻量级,无侵入性,是官方mybatis的一种扩展

 

人大毕业生盗取全校学生信息建颜值打分网站,已被刑拘 员工窃取公司游戏源代码,半年盈利 1.5 亿 美国将限制中国使用亚马逊、微软等提供训练 AI 模型的云服务 基于 NT 架构的全新 QQ Windows 版正式发布 资金严重短缺,又一流行开源项目宣布停止功能开发 2023 年收入最高的技术岗位 LeaferJS 发布:开源、性能强悍的 2D 图形库 Linus Torvalds 动手改进 Linux 用户模式堆栈扩展 deepin 采用 Asahi Linux 适配 Apple M1 全球桌面浏览器市场份额排名,Safari 继续稳坐第二

编程笔记 » fastclassbyspringcglib.invoke,fastmybatis 2.8.1 发布,支持JPA Query Method查询

赞同 (40) or 分享 (0)
游客 发表我的评论   换个身份
取消评论

表情
(0)个小伙伴在吐槽