JPA 使用EntityManager

本文介绍了一种使用Java Persistence API (JPA) 和 EntityManager 的方法,通过一个具体的服务类示例,展示了如何进行数据库操作,包括执行原生SQL查询来统计特定条件下的记录数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JPA 使用EntityManager

@Service
public class MyJpaService {

    @PersistenceContext
    EntityManager entityManager;

    /**
     * 方法如下
     * @param
     */
    public void continuousFailed() {

        // 获取当天0点时间
        String startTime = DateUtil.getCurrDate();

        // 统计当天魔盒挖矿失败次数
        String sql = "select deviceid,count(id) as total from error_log where type=1 and create_time<'"+startTime+"' group by deviceid";
        Query query = entityManager.createNativeQuery(sql);
        List rows = query.getResultList();
        for (Object row : rows) {
            Object[] cells = (Object[]) row;
            System.out.println(cells[0]);
            System.out.println(cells[1]);
        }
    }
}

在Spring Boot中,使用JPA可以方便地实现基本的增删改查操作。具体步骤如下: 1. 添加JPA和数据库驱动的依赖。在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> ``` 2. 配置数据源。在application.properties文件中添加以下配置: ```properties spring.datasource.url=jdbc:h2:mem:test spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.jpa.database-platform=org.hibernate.dialect.H2Dialect ``` 3. 创建实体类。在Java包中创建实体类,使用@Entity和@Id注解标记主键。 ```java @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private String email; // getter/setter方法 } ``` 4. 创建Repository接口。在Java包中创建一个接口,继承JpaRepository,该接口自动继承了基本的CRUD方法。 ```java public interface UserRepository extends JpaRepository<User, Long> { } ``` 5. 创建Service层。在Java包中创建Service类,使用@Autowired注解注入Repository,并实现基本的增删改查方法。 ```java @Service public class UserService { @Autowired private UserRepository userRepository; public User save(User user) { return userRepository.save(user); } public void delete(Long id) { userRepository.deleteById(id); } public User findById(Long id) { Optional<User> optional = userRepository.findById(id); return optional.orElse(null); } public List<User> findAll() { return userRepository.findAll(); } } ``` 6. 创建Controller层。在Java包中创建Controller类,使用@Autowired注解注入Service,并实现对应的接口。 ```java @RestController @RequestMapping("/users") public class UserController { @Autowired private UserService userService; @PostMapping("") public User save(@RequestBody User user) { return userService.save(user); } @DeleteMapping("/{id}") public void delete(@PathVariable Long id) { userService.delete(id); } @GetMapping("/{id}") public User findById(@PathVariable Long id) { return userService.findById(id); } @GetMapping("") public List<User> findAll() { return userService.findAll(); } } ``` 以上就是集成JPA实现基础增删改查方法的步骤。在使用中,只需通过Controller层调用对应的接口即可完成对数据库的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值