zoukankan      html  css  js  c++  java
  • jpa命名规则 jpa使用sql语句 @Query

    关键字
    方法命名
    sql where字句

    And
    findByNameAndPwd
    where name= ? and pwd =?

    Or
    findByNameOrSex
    where name= ? or sex=?

    Is,Equals
    findById,findByIdEquals
    where id= ?

    Between
    findByIdBetween
    where id between ? and ?

    LessThan
    findByIdLessThan
    where id < ?

    LessThanEquals
    findByIdLessThanEquals
    where id <= ?

    GreaterThan
    findByIdGreaterThan
    where id > ?

    GreaterThanEquals
    findByIdGreaterThanEquals
    where id > = ?

    After
    findByIdAfter
    where id > ?

    Before
    findByIdBefore
    where id < ?

    IsNull
    findByNameIsNull
    where name is null

    isNotNull,NotNull
    findByNameNotNull
    where name is not null

    Like
    findByNameLike
    where name like ?

    NotLike
    findByNameNotLike
    where name not like ?

    StartingWith

    findByNameStartingWith
    where name like '?%'

    EndingWith
    findByNameEndingWith
    where name like '%?'

    Containing
    findByNameContaining
    where name like '%?%'

    OrderBy
    findByIdOrderByXDesc
    where id=? order by x desc

    Not
    findByNameNot
    where name <> ?

    In
    findByIdIn(Collection<?> c)
    where id in (?)

    NotIn
    findByIdNotIn(Collection<?> c)
    where id not  in (?)

    True

    findByAaaTue

    where aaa = true

    False
    findByAaaFalse
    where aaa = false

    IgnoreCase
    findByNameIgnoreCase
    where UPPER(name)=UPPER(?)

    //where name in(?,?...) and age<?
    public List<Employee> findByNameInAndAgeLessThan(List<String> names,Integer age);

    在方法上加@Query就不需要遵守上面的规则了,可以进行自定义sql。

    有两种方式传参:

    @Query("select o from Employee o where o.name=?1 and o.age=?2")
    public List<Employee> queryParams1(String name,Integer age);
    @Query("select o from Employee o where o.name=:name and o.age=:age")
    public List<Employee> queryParams2(@Param("name") String name, @Param("age") Integer age);

    修改数据的时候需要使用到三个注解

    @Modifying
    @Query("update Employee o set o.age=:age where o.id=:id")
    public void update(@Param("id") Integer id,@Param("age") Integer age);
    首先是query这里需要加上@Modifying

    但是仅仅加上这个会报错,需要打开事务

    所以还需要在service层方法上开启事务,(service层开启事务,事务会作用于整个查询部分的方法)

    @Transactional
    public void update(Integer id,Integer age){
        employeeRepository.update(id,age);
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/csdnchen666666/article/details/78153229

  • 相关阅读:
    在spring官网上下载历史版本的spring插件,springsource-tool-suite
    转载---VB DorpDownList控件 添加选项
    VB,将"秒"转成"时分秒"格式
    VB.NET 改变datatable数据类型
    转载--- C# 图片与base64编码 互相转换
    转载--- php5.6:Call to undefined function curl_init()的解决办法
    转载--- navicat12破解版(英文、中文)
    转载---mysql导出事件、存储过程、函数、库表
    C# 读取mysql blob字段(两种方式)
    转载---安装mysql5.7,遇到的问题
  • 原文地址:https://www.cnblogs.com/suizhikuo/p/9400727.html
Copyright © 2011-2022 走看看