🎨
application-framework
  • Introduction
  • 设计模式篇
    • 设计原则
      • 单一职责原则
      • 里氏替换原则
      • 依赖倒置原则
      • 接口隔离原则
      • 迪米特法则
      • 开闭原则
    • 代理模式
    • 工厂模式
    • 策略模式
    • 等等..设计模式
    • 常用设计模式在开源软件的应用
    • Template设计模式介绍
  • SpringBoot篇
    • SpringBoot官方教程结构图
    • SpringBoot启动过程源码分析
    • SpringBoot启动过程定制化
    • SpringBoot实现自动配置的基础
    • SpringBoot实现自动配置的原理
    • SpringBoot启动类源码分析以及@EnableAutoConfiguration和@SpringBootApplication讲解
    • EnableAutoConfigurationImportSelector 是如何工作的 ?
    • ConfigurationClassParser 是如何工作的 ?
    • SpringBoot源码分析之Spring上下文refresh(重点)
    • SpringBoot中的ApplicationContext - 执行ApplicationContextInitializer初始化器
    • SpringBoot常用配置-Profile
    • Spring Boot API 版本权限控制
  • Mybatis篇
    • Mybatis基础教程
    • Mybatis-Spring基础教程
    • Sqlsession原理
    • Mybatis代码架构分析
    • Mybatis事务
    • Mybatis与Spring集成事务相关问题
    • 结果/参数绑定
    • Mybatis插件拓展/插件原理
    • Mybatis 使用Ehcache缓存机制//自带缓存与Spring结合使用
    • 使用代码生成器快速开发
    • Mybatis使用时的一些注意事项
    • Mybatis配置打印SQL语句
    • 持久层框架mybatis如何防止sql注入
    • SqlSessionTemplate与SqlSessionDaoSupport讲解
    • MapperFactoryBean与MapperScannerConfigurer讲解
    • Spring+MyBatis多数据源配置实现
    • Mybatis与Spring集成事务相关问题
  • Spring源码解读篇
    • Spring 架构图
    • Spring核心结构及组件分析
    • Spring5 Framework体系结构
    • Spring源码剖析
      • BeanFactory
      • BeanPostProcessor源码讲解
      • BeanFactoryPostProcessor源码讲解
      • BeanDefinition源码解析
      • RootBeanDefinition源码解析
      • AnnotatedBeanDefinition源码解析
      • ApplicationContext源码讲解
      • IoC容器的初始化?
      • @Configuration源码讲解
      • Bean的注解(annotation)
      • @ImportSelector、@Import、ImportResource工作原理分析
      • Bean的生命周期
    • IOC机制从设计理念/实现原理到源码解读
    • AOP实现原理
      • aop编程思想
      • aop在Spring中的应用
      • cglib和jdk动态代理
        • java/jdk代理实现与原理详细分析
        • cglib实现动态代理
    • Transaction事务处理源码分析及高级特性
      • 事务概念
      • Spring事务传播
      • 事务隔离级别
      • 事务实现源码分析
      • Spring事物应用实战(一)
      • Spring事务应用实战(二)之spring+hibernate+JTA 分布式事务的例子
    • SpringMVC源码解读
      • DispatcherServlet说明
      • 核心流程剖析及原理分析
      • 请求映射机制
      • 参数绑定与转换机制
      • 页面渲染机制
      • ContextLoader加载过程
      • web.xml 中的listener、 filter、servlet 加载顺序及其详解
      • Spring中WebApplicationContext、DispatcherServlet与web容器的ServletContext关系梳理
    • Spring新版本特性解读
  • JPA篇
    • 简单叙述
    • 基础教程
    • SpringData Jpa、Hibernate、Jpa 三者之间的关系
    • Spring data jpa 全面解析
    • 数据库schema含义
    • 数据库schema与catalog简介
    • Jpa关联映射以及字段映射注解讲解
      • @Entity、@Table、@id
      • @GeneratedValue
      • @Basic、@Column、@Transient
      • @MappedSuperclass、@Embedded、@OrderBy、@Lob、@Data
      • @OneToOne级联配置
      • @OneToMany、@ManyToOne级联配置
      • 更新的同时删除多的一方的旧数据
      • cascade级联属性讲解
      • JpaSpecificationExecutor接口
      • @Query 创建查询
      • @NamedQueries创建查询
      • @CreateDate @LastModifiedDate @EntityListeners、@SQLDelete、@Where
      • 注解关联时报错总结
      • JPA 多对多关联 中间表带有属性 两个外键作为中间表的联合主键时 直接操作中间表查询修改的方法
    • Jpa 使用@Query查询时 (参数可能为空)语句
    • Jpa校验/验证注解
      • Jpa的list校验方式
      • Jpa的基础校验/验证注解
  • Hibernate篇
    • Hibernate基础教程
    • Hibernate主键生成策略
    • Hibernate的体系结构
    • Hibernate面试题
    • 自定义一个方言类——Hibernate Dialect
    • Hibernate 不同数据库的连接及SQL方言
    • Hibernate中一级缓存和二级缓存的具体区别是什么?
    • Hibernate中,对象有三种状态:
Powered by GitBook
On this page
  • 概述
  • 接口实现

Was this helpful?

  1. JPA篇
  2. Jpa关联映射以及字段映射注解讲解

@NamedQueries创建查询

概述

命名查询是 JPA 提供的一种将查询语句从方法体中独立出来,以供多个方法共用的功能。Spring Data JPA 对命名查询也提供了很好的支持。用户只需要按照 JPA 规范在 orm.xml 文件或者在代码中使用 @NamedQuery(或 @NamedNativeQuery)定义好查询语句,唯一要做的就是为该语句命名时,需要满足”DomainClass.methodName()”的 命名规则。

接口实现

public interface FindUserByNamedQueryRepository extends JpaRepository<User, Integer> {
    User findUserWithName(@Param("name") String name);
}
@Entity
@NamedQueries(value={
        @NamedQuery(name="User.findUserWithName",query="select u from User u where u.name = :name")
})
// 注意:此处如果是多个方法,那么需要使用@NamedQueries,如果只有一个方法,则可以使用@NamedQuery,写法如下:@NamedQuery(name="User.findUserWithName",query="select u from User u where u.name = :name")
public class FindUserByNamedQuery {
    /**
     * 注意:此处必须要给这个实体类定义一个唯一标识,否则会报异常
     */
    @Id
    @GeneratedValue
    private Integer id;
}

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext-config.xml" })
@TransactionConfiguration(defaultRollback = false)
@Transactional
public class FindUserByNamedQueryRepositoryTest {
    @Autowired
    private FindUserByNamedQueryRepository dao;

    @Test
    public void testFindUserByName(){
        User user = dao.findUserWithName("caican");
        System.out.println(JSON.toJSONString(user));
    }
}

通过解析方法名创建查询。顾名思义,就是根据方法的名字,就能创建查询,也许初听起来,感觉很不可思议,等测试后才发现,原来一切皆有可能。

接口实现

public interface SimpleConditionQueryRepository extends JpaRepository<User, Integer> {
    /**
     * 说明:按照Spring data 定义的规则,查询方法以find|read|get开头
     * 涉及条件查询时,条件的属性用条件关键字连接,要注意的是:条件属性首字母需大写
     */



    /**
     * 注:此处这个接口相当于发送了一条SQL:select u from User u where u.name = :name and u.email = :email
     * 参数名大写,条件名首字母大写,并且接口名中参数出现的顺序必须和参数列表中的参数顺序一致
     */
    User findByNameAndEmail(String name, String email);

    /**
     * 注:此处这个接口相当于发送了一条SQL:select u from User u where u.name = ?1 or u.password = ?2
     */
    List<User> findByNameOrPassword(String name, String password);

    /**
     * 注:此处这个接口相当于发送了一条SQL:select u from User u where u.id between ?1 and ?2
     */
    List<User> findByIdBetween(Integer start, Integer end);

    /**
     * 注:此处这个接口相当于发送了一条SQL:select u from User u where u.id < ?1
     */
    List<User> findByIdLessThan(Integer end);

    /**
     * 注:此处这个接口相当于发送了一条SQL:select u from User u where u.id > ?1
     */
    List<User> findByIdGreaterThan(Integer start);

    /**
     * 注:此处这个接口相当于发送了一条SQL:select u from User u where u.name is null
     */
    List<User> findByNameIsNull();

    /**
     * 注:此处这个接口相当于发送了一条SQL:select u from User u where u.name is not null
     */
    List<User> findByNameIsNotNull();

    /**
     * 注:此处这个接口相当于发送了一条SQL:select u from User u where u.name like ?1
     */
    List<User> findByNameLike(String name);

    /**
     * 注:此处这个接口相当于发送了一条SQL:select u from User u where u.name not like ?1
     */
    List<User> findByNameNotLike(String name);

    /**
     * 注:此处这个接口相当于发送了一条SQL:select u from User u where u.password = ?1 order by u.id desc
     */
    List<User> findByPasswordOrderByIdDesc(String password);

    /**
     * 注:此处这个接口相当于发送了一条SQL:select u from User u where u.name <> ?1
     */
    List<User> findByNameNot(String name);

    /**
     * 注:此处这个接口相当于发送了一条SQL:select u from User u where u.id in ?1
     */
    List<User> findByIdIn(List<Integer> ids);

    /**
     * 注:此处这个接口相当于发送了一条SQL:select u from User u where u.id not in ?1
     */
    List<User> findByIdNotIn(List<Integer> ids);
}

测试类(注释部分为实际发送的sql语句):

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext-config.xml" })
@TransactionConfiguration(defaultRollback = false)
@Transactional
public class SimpleConditionQueryRepositoryTest {
    @Autowired
    private SimpleConditionQueryRepository dao;

    /**
     * select
        user0_.id as id0_,
        user0_.account as account0_,
        user0_.email as email0_,
        user0_.name as name0_,
        user0_.password as password0_ 
    from
        USER user0_ 
    where
        user0_.name=? 
        and user0_.email=? limit ?
     */
    @Test
    public void testFindUserByNameAndEmail(){
        User user = dao.findByNameAndEmail("chhliu", "chhliu@.com");
        System.out.println(JSON.toJSONString(user));
    }

    /**
     * select
        user0_.id as id1_,
        user0_.account as account1_,
        user0_.email as email1_,
        user0_.name as name1_,
        user0_.password as password1_ 
    from
        USER user0_ 
    where
        user0_.name=? 
        or user0_.password=?
     */
    @Test
    public void testFindUserByNameOrPassword(){
        List<User> users = dao.findByNameOrPassword("chhliu", "123456");
        System.out.println(JSON.toJSONString(users));
    }

    /**
     * select
        user0_.id as id1_,
        user0_.account as account1_,
        user0_.email as email1_,
        user0_.name as name1_,
        user0_.password as password1_ 
    from
        USER user0_ 
    where
        user0_.id between ? and ?
     */
    @Test
    public void testFindByIdBetween(){
        List<User> users = dao.findByIdBetween(5, 8);
        System.out.println(JSON.toJSONString(users));
    }

    /**
     * select
        user0_.id as id1_,
        user0_.account as account1_,
        user0_.email as email1_,
        user0_.name as name1_,
        user0_.password as password1_ 
    from
        USER user0_ 
    where
        user0_.id<?
     */
    @Test
    public void testFindByIdLessThan(){
        List<User> users = dao.findByIdLessThan(4);
        System.out.println(JSON.toJSONString(users));
    }

    /**
     * select
        user0_.id as id0_,
        user0_.account as account0_,
        user0_.email as email0_,
        user0_.name as name0_,
        user0_.password as password0_ 
    from
        USER user0_ 
    where
        user0_.id>?
     */
    @Test
    public void testFindByIdGreaterThan(){
        List<User> users = dao.findByIdGreaterThan(6);
        System.out.println(JSON.toJSONString(users));
    }

    /**
     * select
        user0_.id as id0_,
        user0_.account as account0_,
        user0_.email as email0_,
        user0_.name as name0_,
        user0_.password as password0_ 
    from
        USER user0_ 
    where
        user0_.name is null
     */
    @Test
    public void testFindByNameIsNull(){
        List<User> users = dao.findByNameIsNull();
        System.out.println(JSON.toJSONString(users));
    }

    /**
     * select
        user0_.id as id1_,
        user0_.account as account1_,
        user0_.email as email1_,
        user0_.name as name1_,
        user0_.password as password1_ 
    from
        USER user0_ 
    where
        user0_.name is not null
     */
    @Test
    public void testFindByNameIsNotNull(){
        List<User> users = dao.findByNameIsNotNull();
        System.out.println(JSON.toJSONString(users));
    }

    /**
     * select
        user0_.id as id1_,
        user0_.account as account1_,
        user0_.email as email1_,
        user0_.name as name1_,
        user0_.password as password1_ 
    from
        USER user0_ 
    where
        user0_.name like ?
     */
    @Test
    public void testFindByNameLike(){
        List<User> users = dao.findByNameLike("chhliu");
        System.out.println(JSON.toJSONString(users));
    }

    /**
     * select
        user0_.id as id0_,
        user0_.account as account0_,
        user0_.email as email0_,
        user0_.name as name0_,
        user0_.password as password0_ 
    from
        USER user0_ 
    where
        user0_.name not like ?
     */
    @Test
    public void testFindByNameNotLike(){
        List<User> users = dao.findByNameNotLike("chhliu");
        System.out.println(JSON.toJSONString(users));
    }

    /**
     * select
        user0_.id as id0_,
        user0_.account as account0_,
        user0_.email as email0_,
        user0_.name as name0_,
        user0_.password as password0_ 
    from
        USER user0_ 
    where
        user0_.password=? 
    order by
        user0_.id desc
     */
    @Test
    public void testFindByPasswordOrderByIdDesc(){
        List<User> users = dao.findByPasswordOrderByIdDesc("123456");
        System.out.println(JSON.toJSONString(users));
    }

    /**
     * select
        user0_.id as id1_,
        user0_.account as account1_,
        user0_.email as email1_,
        user0_.name as name1_,
        user0_.password as password1_ 
    from
        USER user0_ 
    where
        user0_.name<>?
     */
    @Test
    public void testFindByNameNot(){
        List<User> users = dao.findByNameNot("chhliu");
        System.out.println(JSON.toJSONString(users));
    }

    /**
     * select
        user0_.id as id1_,
        user0_.account as account1_,
        user0_.email as email1_,
        user0_.name as name1_,
        user0_.password as password1_ 
    from
        USER user0_ 
    where
        user0_.id in (
            ? , ? , ? , ?
        )
     */
    @Test
    public void testFindByIdIn(){
        List<User> users = dao.findByIdIn(new ArrayList<Integer>(Arrays.asList(3,4,6,8)));
        System.out.println(JSON.toJSONString(users));
    }

    /**
     * select
        user0_.id as id0_,
        user0_.account as account0_,
        user0_.email as email0_,
        user0_.name as name0_,
        user0_.password as password0_ 
    from
        USER user0_ 
    where
        user0_.id not in  (
            ? , ? , ? , ?
        )
     */
    @Test
    public void testFindByIdNotIn(){
        List<User> users = dao.findByIdNotIn(new ArrayList<Integer>(Arrays.asList(3,4,6,8)));
        System.out.println(JSON.toJSONString(users));
    }
}
Previous@Query 创建查询Next@CreateDate @LastModifiedDate @EntityListeners、@SQLDelete、@Where

Last updated 5 years ago

Was this helpful?