In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is the Java database access framework?" the content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what the Java database access framework has.
Suppose you are developing a Java program, and there are many ways to connect your application to a database. The following will list the applicable scenarios of each database access framework, which can help you select the development framework suitable for the project.
JDBC: simple database query
The easiest way is to use the Java API provided by JDBC. Enter the query SQL statement to call API to return the result:
ResultSet rs = stmt.executeQuery ("SELECT id, name FROM Employees")
While (rs.hasNext ()) {
Log.info ("Employee id:" + rs.getInt ("id") + "has name:" + rs.getString ("name"))
}
Applicable scenarios: do not want to learn a new framework, require lightweight solutions, need to customize queries, do not need long-term maintenance
Not suitable: do not want to write a lot of code, may need to do database migration in the future.
To reduce template code, consider using jdbc-template tools such as Spring JDBC template or Apache DBUtils. For example, when dealing with request, Spring template can send request with parameters in one sentence, deserialize the data, and close the connection:
User user = jdbc.qeuryForObject ("SELECT * FROM USERS WHERE ID =?", 1, User.class)
JOOQ: face Java object query
JOOQ provides a DSL to solve query problems. This language provides compile-time secure (compile-time-safe) queries based on the generated entity objects. JOOQ supports different databases and reduces template code.
UserRecord user = new UserRecord (); user.setId (1); user.setName ("Peter"); Result books1 = DSL.using (configuration) .selectFrom (USERS) .where (condition (user)) .fetch ()
Applicable scenario: JDBC query is required to ensure compile-time security, migrate to different databases, and generate CRUD JDBC API automatically
Not suitable: some advanced functions are charged.
MyBatis: a simple ORM with query function
ORM (object-relational mapping) provides another way to deal with databases, and its core idea is to map Java objects (entity) to corresponding database tables. MyBatis is one of them.
MyBatis is a lightweight framework that uses JPA provider (non-bean structures) for mapping. Here is a simple example query (no configuration file):
/ / Bean mapping public interface BlogMapper {3@Select ("SELECT * FROM blog WHERE id = # {id}") Blog selectBlog (int id);} / / get data BlogMapper mapper = session.getMapper (BlogMapper.class); Blog blog = mapper.selectBlog (101)
Applicable scenarios: need to query flexibly in ORM, lightweight ORM
Not suitable: don't like XML.
Hibernate and Spring Data
Both support JPA (Java persistence API), which means that both support deployment to the application server. The JPA standard requires that the database table/column corresponds to the Java object (entity). For example, the entity for the USER table is as follows:
@ Data / / this is not a hibernate comment, but a lombok getter/setter@Entity@Table (name = "USERS") public class User {@ Id@Column (name = "id") private Integer id;@Column (name = "name") private String name;}
Hibernate is the most popular ORM framework, providing many out-of-the-box features. Hibernate released its first version as early as 2001. HQL language is also supported for custom SQL queries.
Session session = sessionFactory.openSession (); User oldUser = (User) session.get (User.class, 1); / / get userUser newUser = newUser (123, "John"); session.save (developer); / / add user//HQL sample Query query = session.createQuery ("FROM Users"); List users = query.list ()
Applicable scenarios: rapid prototyping, need to provide internal cache, use a variety of different databases, access complex schema
Not suitable: do not like to generate other Java entity classes, do not want to learn the new framework, need to control the underlying details.
Spring Data: new ORM abstraction layer
Spring Data provides rich CRUD API and query expression language based on JPA entity. Its biggest advantage is that it can be done with only 2-3 lines of code. The generated API is based on the function naming convention.
/ / only need to implement CrudRepository interfacepublic interface UserRepository extends CrudRepository {User findByName (String name); User findById (long id); @ Query ("SELECT u.ID FROM USERS u WHERE like? 1") / / Custom expression List findByUserName (String name);} / / query example User johnUser = userRepository.findByName ("John"); User johnUser = userRepository.findById (id); List usersIdsWithOVPostfix = userRepository.findByUserName ("% OV%")
Summary
The following table summarizes. Note: it only represents the author's personal point of view and does not do rigorous analysis and testing.
Thank you for your reading, the above is the content of "what is the Java database access framework?" after the study of this article, I believe you have a deeper understanding of what the Java database access framework has, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.