SpringDataJPA之Repository

Repository 接口是 Spring Data JPA 中为我我们提供的所有接口中的顶层接口,而且是个标志接口,Repository 提供了两种查询方式的支持
1)基于方法名称命名规则查询
2)基于@Query 注解查询

Repository

一、基于方法名称命名规则查询

1.方法名称命名规则查询

规则:findBy(关键字)+属性名称(属性名称的首字母大写)+查询条件(首字母大写)

关键字 方法名 sql where 子句
And findByNameAndPwd where name= ? and pwd =?
Or findByNameOrSex where name= ? or sex=?
Is,Equal findById,findByIdEquals where id= ?
Between findByIdBetween where id between ? and ?
LessThan findByIdLessThan where id < ?
LessThanEqual findByIdLessThanEquals where id <= ?
GreaterThan findByIdGreaterThan where id > ?
GreaterThanEqual findByIdGreaterThanEquals where id > = ?
After findByIdAfter where id > ?
Before findByIdBefore where id < ?
IsNull findByNameIsNull where name is null
isNotNull,Not findByNameNotNull where name is not Null 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(?)

2.具体使用

创建接口,并定义相关方法

/**
 * Repository 接口使用
 * 定义的方法名称 参考文档定义
 */
public interface UserDao extends Repository<Users,Integer> {
    
    
    List<Users> findByUsernameIs(String string);
    List<Users> findByUsernameLike(String string);
    List<Users> findByUsernameAndUserageGreaterThanEqual(String name,Integer age);
}
123456789

单元测试

/**
 * @program: spring-hibernate
 * @description: 单元测试
 * @author: 波波烤鸭
 * @create: 2019-05-18 09:48
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestDemo {
    
    

    @Autowired
    private UserDao usersDao;


    /**
     * 需求:使用用户名作为查询条件
     */
    @Test
    public void test1() {
    
    
        /**
         * 判断相等的条件,有三种表示方式
         * 1,什么都不写,默认的就是做相等判断
         * 2,Is
         * 3,Equal
         */
        List<Users> list = this.usersDao.findByUsernameIs("王五");
        for (Users users : list) {
    
    
            System.out.println(users);
        }
    }

    /**
     * 需求:根据用户姓名做 Like 处理
     * Like:条件关键字
     */
    @Test
    public void test2() {
    
    
        List<Users> list = this.usersDao.findByUsernameLike("王%");
        for (Users users : list) {
    
    
            System.out.println(users);
        }
    }

    /**
     * 需求:查询名称为王五,并且他的年龄大于等于 22 岁
     */
    @Test
    public void test3() {
    
    
        List<Users> list =
                this.usersDao.findByUsernameAndUserageGreaterThanEqual("王五", 22);
        for (Users users : list) {
    
    
            System.out.println(users);
        }
    }


}

查询结果

在这里插入图片描述

二、基于@Query 注解查询

通过方法命令的方式使用的方式如果查询条件比较复杂,那么方法的名称就会很长,不是很方便,这时我们可以通过@Query注解的方式来实现。

2.1通过 JPQL 语句查询

JPQL:通过 Hibernate 的 HQL 演变过来的。他和 HQL 语法及其相似。
创建接口

//使用@Query 注解查询
@Query(value="from Users where username = ?")
List<Users> queryUserByNameUseJPQL(String name);
@Query("from Users where username like ?")
List<Users> queryUserByLikeNameUseJPQL(String name);
@Query("from Users where username = ? and userage >= ?")
List<Users> queryUserByNameAndAge(String name,Integer age);
1234567

单元测试

   /**
     * 测试@Query 查询 JPQL
     */
    @Test
    public void test4(){
    
    
        List<Users> list = this.usersDao.queryUserByNameUseJPQL("王五");
        for (Users users : list) {
    
    
            System.out.println(users);
        }
    }
    /**
     * 测试@Query 查询 JPQL
     */
    @Test
    public void test5(){
    
    
        List<Users> list = this.usersDao.queryUserByLikeNameUseJPQL(" 王%");
        for (Users users : list) {
    
    
            System.out.println(users);
        }
    }
    /**
     * 测试@Query 查询 JPQL
     */
    @Test
    public void test6(){
    
    
        List<Users> list = this.usersDao.queryUserByNameAndAge("王五", 22);
        for (Users users : list) {
    
    
            System.out.println(users);
        }
    }
123456789101112131415161718192021222324252627282930

2.2通过SQL语句查询

我们也可以直接在定义的方法头部通过@Query注解来添加Sql语句来实现,具体如下:

//使用@Query 注解查询 SQL
//nativeQuery:默认的是 false.表示不开启 sql 查询。是否对 value 中的语句做转义。
@Query(value="select * from users where username = ?",nativeQuery=true)
        List<Users> queryUserByNameUseSQL(String name);
@Query(value="select * from users where username like ?",nativeQuery=true)
        List<Users> queryUserByLikeNameUseSQL(String name);
@Query(value="select * from users where username = ? and userage >= ?",nativeQuery=true)
List<Users> queryUserByNameAndAgeUseSQL(String name,Integer age);
12345678

单元测试

  /**
     * 测试@Query 查询 SQL
     */
    @Test
    public void test7(){
    
    
        List<Users> list = this.usersDao.queryUserByNameUseSQL("王五");
        for (Users users : list) {
    
    
            System.out.println(users);
        }
    }
    /**
     * 测试@Query 查询 SQL
     */
    @Test
    public void test8(){
    
    
        List<Users> list = this.usersDao.queryUserByLikeNameUseSQL(" 王%");
        for (Users users : list) {
    
    
            System.out.println(users);
        }
    }
    /**
     * 测试@Query 查询 SQL
     */
    @Test
    public void test9(){
    
    
        List<Users> list = this.usersDao.queryUserByNameAndAgeUseSQL(" 王五", 22);
        for (Users users : list) {
    
    
            System.out.println(users);
        }
    }
123456789101112131415161718192021222324252627282930

在这里插入图片描述

2.3 通过@Query 注解完成数据更新

@Query注解可以完成数据的更新操作,但是不能实现添加和删除数据的操作,实现如下:
接口中声明方法:

@Query("update Users set userage = ? where userid = ?")
@Modifying //@Modifying 当前语句是一个更新语句
void updateUserAgeById(Integer age,Integer id);
123

单元测试

/**
 * 测试@Query update
 */
@Test
@Transactional
@Rollback(false)
public void test10(){
    
    
    this.usersDao.updateUserAgeById(24, 6);
}
123456789

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/WziH_CSDN/article/details/114089262