SpringBoot关于JPA中关于查询findOne和findById

背景

1.SpringBoot升级2.0之后,原本的findOne(String id)方法做了更改,可以使用

@Autowired
private ProductInfoRepository repository;

//这里假设实体类是ProductInfo
ProductInfo productInfo = new ProductInfo();
productInfo.setProductId("111");
Example<ProductInfo> example = Example.of(productInfo);
Optional<ProductInfo> result = repository.findOne(example);

这种方法吧,不知道为什么有的时候查询会失败,但却是也能查

但更多使用findById(String id)方法

Optional<ProductInfo> result = repository.findById(productId);
ProductInfo productInfo = result.get();

2.findById(id).get()使用时,如果数据库中查询无符合条件的记录便会抛出

No value present

的错误,导致自己写的捕获异常的语句失效。

解决方法就是使用isPresent()方法判断有无记录,有则返回记录,无则返回null,如此自己写的查询无结果的异常便能正常触发

具体方法为

Optional<ProductInfo> result = repository.findById(productId);
return result.isPresent()?result.get():null;
 
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot可以很方便地整合JPA和MyBatis。 整合JPA: 1. 在pom.xml添加JPA依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> ``` 2. 配置数据源和JPA属性: ``` spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect ``` 3. 创建实体类和Repository接口: ``` @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "name") private String name; @Column(name = "age") private Integer age; // getter和setter } public interface UserRepository extends JpaRepository<User, Long> { } ``` 4. 在Service使用Repository: ``` @Service public class UserService { @Autowired private UserRepository userRepository; public User getUserById(Long id) { return userRepository.findById(id).orElse(null); } public void saveUser(User user) { userRepository.save(user); } public void deleteUserById(Long id) { userRepository.deleteById(id); } } ``` 整合MyBatis: 1. 在pom.xml添加MyBatis依赖: ``` <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.</version> </dependency> ``` 2. 配置数据源和MyBatis属性: ``` spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.example.demo.entity ``` 3. 创建实体类和Mapper接口: ``` public class User { private Long id; private String name; private Integer age; // getter和setter } @Mapper public interface UserMapper { User getUserById(Long id); void saveUser(User user); void deleteUserById(Long id); } ``` 4. 在Service使用Mapper: ``` @Service public class UserService { @Autowired private UserMapper userMapper; public User getUserById(Long id) { return userMapper.getUserById(id); } public void saveUser(User user) { userMapper.saveUser(user); } public void deleteUserById(Long id) { userMapper.deleteUserById(id); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值