SpringBoot 从入门到入门(八 ORM-JPA-4 Example查询)

// select userentity0_.id as id1_0_, userentity0_.email as email2_0_, userentity0_.name as name3_0_ from user userentity0_ where userentity0_.name=? and userentity0_.id=0
        UserEntity userEntity = new UserEntity();
        userEntity.setName("admin");
        Example<UserEntity> example = Example.of(userEntity);
        List<UserEntity> list = userRepository.findAll(example);

以findAll方法为例,可接收Example对象

	/*
	 * (non-Javadoc)
	 * @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example)
	 */
	@Override
	<S extends T> List<S> findAll(Example<S> example);

	/*
	 * (non-Javadoc)
	 * @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example, org.springframework.data.domain.Sort)
	 */
	@Override
	<S extends T> List<S> findAll(Example<S> example, Sort sort);

Example接口结构如下,可使用of方法获取实例

of(T probe) 等同于  of(T probe, ExampleMatcher.matching()) 

	/**
	 * Create a new {@link Example} including all non-null properties by default.
	 *
	 * @param probe must not be {@literal null}.
	 * @return
	 */
	static <T> Example<T> of(T probe) {
		return new TypedExample<>(probe, ExampleMatcher.matching());
	}

	/**
	 * Create a new {@link Example} using the given {@link ExampleMatcher}.
	 *
	 * @param probe must not be {@literal null}.
	 * @param matcher must not be {@literal null}.
	 * @return
	 */
	static <T> Example<T> of(T probe, ExampleMatcher matcher) {
		return new TypedExample<>(probe, matcher);
	}

ExampleMatcher接口结构如下

 示例

        // select userentity0_.id as id1_0_, userentity0_.email as email2_0_, userentity0_.name as name3_0_ from user userentity0_ where userentity0_.id=1
        UserEntity userEntity = new UserEntity();
        userEntity.setId(1);
        Example<UserEntity> example = Example.of(userEntity);
        List<UserEntity> list = userRepository.findAll(example);

ExampleMatcher.matching() 创建一个新的ExampleMatcher,默认情况下包含所有非NULL属性,以匹配从该示例派生的所有谓词。由于UserEntity中id初始值为0,不为NULL,所以下方示例中会有id字段的查询

// select userentity0_.id as id1_0_, userentity0_.email as email2_0_, userentity0_.name as name3_0_ from user userentity0_ where userentity0_.name=? and userentity0_.id=0
        UserEntity userEntity = new UserEntity();
        userEntity.setName("admin");
        Example<UserEntity> example = Example.of(userEntity);
        List<UserEntity> list = userRepository.findAll(example);

 可以使用ExampleMatcher指定不参与查询的字段

        // select userentity0_.id as id1_0_, userentity0_.email as email2_0_, userentity0_.name as name3_0_ from user userentity0_ where userentity0_.name=?
        UserEntity userEntity = new UserEntity();
        userEntity.setName("admin");
        ExampleMatcher exampleMatcher = ExampleMatcher.matching().withIgnorePaths("id");
        Example<UserEntity> example = Example.of(userEntity,exampleMatcher);
        List<UserEntity> list = userRepository.findAll(example);

可以使用ExampleMatcher的其他方法来实现稍微复杂的查询

### 全栈开发入门到实战教程 #### SSM框架入门 SSM(Spring + Spring MVC + MyBatis)是一种经典的Java全栈开发框架组合。开发者可以通过配置Spring和MyBatis来实现业务逻辑和数据库交互[^1]。以下是一个简单的步骤概述: - 配置Spring上下文,使用`applicationContext.xml`管理Bean。 - 配置Spring MVC的DispatcherServlet,定义请求映射规则。 - 使用MyBatis进行数据库操作,编写Mapper接口和XML文件。 ```xml <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.20</version> </dependency> ``` #### Spring Boot快速构建RESTful服务 Spring Boot通过自动化配置简化了Spring应用的开发过程。开发者可以通过引入`spring-boot-starter-web`依赖快速构建一个RESTful服务[^3]。以下是一个简单的示例: ```java @RestController @RequestMapping("/api") public class ExampleController { @GetMapping("/hello") public String sayHello() { return "Hello, World!"; } } ``` #### Vue.js前端开发基础 Vue.js是一个渐进式JavaScript框架,用于构建用户界面。它支持组件化开发,使得前端代码更加模块化和易于维护[^2]。以下是一个简单的Vue组件示例: ```vue <template> <div>{{ message }}</div> </template> <script> export default { data() { return { message: 'Hello Vue!' }; } }; </script> ``` #### 前后端交互与跨域问题 在全栈开发中,前后端分离是常见的架构模式。为了处理跨域问题,可以在Spring Boot中配置CORS支持[^2]: ```java @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("GET", "POST", "PUT", "DELETE"); } } ``` #### 数据库集成与JPA支持 Spring Boot提供了对JPA的支持,可以轻松实现ORM操作。开发者只需引入`spring-boot-starter-data-jpa`依赖即可: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> ``` #### 单元测试与工具支持 Spring Boot内置了对单元测试的支持,开发者可以使用JUnit和Mockito等工具进行测试[^2]。以下是一个简单的测试示例: ```java @SpringBootTest class ExampleTest { @Autowired private ExampleService exampleService; @Test void testSayHello() { String result = exampleService.sayHello(); assertEquals("Hello, World!", result); } } ``` #### 缓存机制 Spring Boot提供了缓存支持,开发者可以通过`@Cacheable`注解实现方法级缓存[^1]。以下是一个示例: ```java @Service public class ExampleService { @Cacheable("messages") public String getMessage() { return "Hello, Cache!"; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值