为什么Java的id用long但是做JavaWeb项目时id用String而不是long

本文讨论了Java中使用long类型作为id而非int的原因,包括更大的数值范围、数据库兼容性、序列化和反序列化的精确性、以及代码可读性和安全性。同时强调了在Web层面上,String的灵活性和可理解性的重要性。

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

为什么Java中id用long?

1.从整数的范围来看

int:
        最小值:-2,147,483,648
        最大值:2,147,483,647
        在Java中,int类型是32位有符号整数,范围是从 -2^31 到 2^31-1。

long:
        最小值:-9,223,372,036,854,775,808
        最大值:9,223,372,036,854,775,807
        在Java中,long类型是64位有符号整数,范围是从 -2^63 到 2^63-1。

long类型的取值范围要比int类型的大得多。意味着如果用long就能为更多的实体对象分配唯一的id,避免了id的重复。

而且在数据量大的时候,使用int类型作为id可能会导致溢出,long类型可以有效的避免这种情况的发生,能够确保id的唯一性和稳定性。

2.从数据库兼容性来看

        在数据库设计中,id通常是用来标识记录的主键,通常选择一个足够大且不会很快耗尽的类型。数据库中的bigint类型(在MySQL中对应的是BIGINT)可以存从-9,223,372,036,854,775,808到9,223,372,036,854,775,807的整数,这与Java的long类型范围一致。如果使用int类型,当对象数量巨大时,可能会遇到id重复或溢出的问题,而在数据库中可能会引发数据不一致。因此,为了保持数据的完整性和一致性,以及在数据库操作时的高效性

Java Web中,实现book和author的列表功能通常涉及到JSP、Servlet和数据库操作。这里是一个简单的示例,假设我们使用了Spring MVC框架和MySQL数据库: 首先,创建Book实体类(Book.java): ```java import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Book { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; // getters and setters } ``` 然后,Author实体类(Author.java)类似: ```java import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Author { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; // getters and setters } ``` 创建对应的Repository接口(如BookRepository.java): ```java import org.springframework.data.jpa.repository.JpaRepository; public interface BookRepository extends JpaRepository<Book, Long> { } ``` 同样,对于AuthorRepository。 接下来,在Service层(BookService.java)处理业务逻辑: ```java import org.springframework.beans.factory.annotation.Autowired; import java.util.List; @Service public class BookService { @Autowired private BookRepository bookRepository; public List<Book> getAllBooks() { return bookRepository.findAll(); } public List<Author> getAllAuthors() { return authorRepository.findAll(); } } ``` 最后,在控制器层(Controller)处理HTTP请求: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") public class BookController { @Autowired private BookService bookService; @GetMapping("/books") public List<Book> getAllBooks() { return bookService.getAllBooks(); } @GetMapping("/authors") public List<Author> getAllAuthors() { return bookService.getAllAuthors(); } } ``` 在JSP页面上显示列表: ```jsp <table> <tr th:each="book : ${books}"> <td th:text="${book.name}">Book Name</td> <!-- 更多列... --> </tr> </table> <!-- authors表同理 --> ``` 这只是一个基本的示例,实际应用中可能还需要分页、排序、搜索等功能,并且数据可能存储在更复杂的数据库结构中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值