`
frank127
  • 浏览: 10926 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

JPA notes 1

    博客分类:
  • JPA
阅读更多

 

Creating an Entity

Regular Java classes are easily transformed into entities simply by annotating them. In fact, by adding a couple of annotations, virtually any class with a no-arg constructor can become an entity. Let’s start by creating a regular Java class for an employee.

 

Listing 2-1 shows a simple Employee class.

 

Listing 2-1. Employee Class

 

 

public class Employee {
	private int id;
	private String name;
	private long salary;

	public Employee() {
	}

	public Employee(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public long getSalary() {
		return salary;
	}

	public void setSalary(long salary) {
		this.salary = salary;
	}
}

 

 

 

You may notice that this class resembles a JavaBean-style class with three properties: id, name, and salary. Each of these properties is represented by a pair of accessor methods to get and set the property, and is backed by a member field. Properties or member fields are the units of state within the entity that we want to persist.

 

 

To turn Employee into an entity, we first need to annotate the class with @Entity. This is primarily just a marker annotation to indicate to the persistence engine that the class is an entity.

The second annotation that we need to add is @Id. This annotates the particular field or property that holds the persistent identity of the entity (the primary key) and is needed so the provider knows

which field or property to use as the unique identifying key in the table. Adding these two annotations to our Employee class, we end up with pretty much the same class that we had before, except that now it is an entity.

 

Listing 2-2 shows the entity class.

 

 

 

@Entity
public class Employee {
	@Id private int id;
	private String name;
	private long salary;

	public Employee() {
	}

	public Employee(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public long getSalary() {
		return salary;
	}

	public void setSalary(long salary) {
		this.salary = salary;
	}
}

 

 When we say that the @Id annotation is placed on the field or property, we mean that the user can choose to annotate either the declared field or the getter method of a JavaBean-style property. Either field or property strategy is allowed, depending on the needs and tastes of the entity developer. We have chosen in this example to annotate the field because it is simpler; in general, this will be the easiest and most direct approach. We will discuss the details of annotating persistent state using field or property access in subsequent chapters.

 

 

 

Entity Manager

 

 

Entity managers are configured to be able to persist or manage specific types of objects, read and write to a given database, and be implemented by a particular persistence provider (or provider for short). It is the provider that supplies the backing implementation engine for the entire Java Persistence API, from the EntityManager through to implementation of the query classes and SQL generation.

All entity managers come from factories of type EntityManagerFactory. The configuration for an entity manager is templated from the EntityManagerFactory that created it, but it is defined separately as a persistence unit. A persistence unit dictates either implicitly or explicitly the settings and entity classes used by all entity managers obtained from the unique EntityManagerFactory instance bound to that persistence unit. There is, therefore, a one-to-one correspondence between a persistence unit and its concrete EntityManagerFactory.

Persistence units are named to allow differentiation of one EntityManagerFactory from another. This gives the application control over which configuration or persistence unit is to be used for operating on a particular entity.



 

 

Figure 2-1 shows that for each persistence unit there is an EntityManagerFactory and that many

entity managers can be created from a single EntityManagerFactory. The part that may come as a

surprise is that many entity managers can point to the same persistence context. We have talked only about an entity manager and its persistence context, but later on we will see that there may in fact be multiple references to different entity managers all pointing to the same group of managed entities. This will enable the control flow to traverse container components but continue to be able access the same persistence context.

 

 

Obtaining an Entity Manager

An entity manager is always obtained from an EntityManagerFactory. The factory from which it was obtained determines the configuration parameters that govern its operation. While there are shortcuts

that veil the factory from the user view when running in a Java EE application server environment, in

the Java SE environment we can use a simple bootstrap class called Persistence. The static createEntityManagerFactory() method in the Persistence class returns the EntityManagerFactory for

the specified persistence unit name. The following example demonstrates creating an EntityManagerFactory for the persistence unit named “EmployeeService”:

 

 

 

 

EntityManagerFactory emf =
Persistence.createEntityManagerFactory("EmployeeService");

 

 

 

The name of the specified persistence unit “EmployeeService” passed into the

createEntityManagerFactory() method identifies the given persistence unit configuration that determines such things as the connection parameters that entity managers generated from this factory will use when connecting to the database.

 

 

Now that we have a factory, we can easily obtain an entity manager from it. The following example demonstrates creating an entity manager from the factory that we acquired in the previous example:

 

 

EntityManager em = emf.createEntityManager();

 

 

With this entity manager, we are now in a position to start working with persistent entities.

 

Persisting an Entity

 

 

Persisting an entity is the operation of taking a transient entity, or one that does not yet have any persistent representation in the database, and storing its state so that it can be retrieved later. This is really the basis of persistence—creating state that may outlive the process that created it. We are going to start by using the entity manager to persist an instance of Employee. Here is a code example that does just that:

 

 

 

Employee emp = new Employee(158);
em.persist(emp);

 

 

 

Listing 2-3 shows how to incorporate this into a simple method that creates a new employee and persists it to the database.

 

Listing 2-3. Method for Creating an Employee

 

public Employee createEmployee(int id, String name, long salary) {
	Employee emp = new Employee(id);
	emp.setName(name);
	emp.setSalary(salary);
	em.persist(emp);
	return emp;
}

 

 

 

This method assumes the existence of an entity manager in the em field of the instance and uses it

to persist the Employee. Note that we do not need to worry about the failure case in this example. It will

result in a runtime PersistenceException being thrown, which will get propagated up to the caller.

 

Finding an Entity

 

 

Once an entity is in the database, the next thing one typically wants to do is find it again. In this section, we will show how an entity can be found using the entity manager. There is really only one line that we need to show:

 

 

Employee emp = em.find(Employee.class, 158);

 

 

 

We are passing in the class of the entity that is being sought (in this example, we are looking for an instance of Employee) and the id or primary key that identifies the particular entity (in our case we want to find the entity that we just created). This is all the information needed by the entity manager to find the instance in the database, and when the call completes, the employee that gets returned will be a managed entity, meaning that it will exist in the current persistence context associated with the entity manager. Passing in the class as a parameter also allows the find method to be parameterized and return an object of same type that was passed in, saving the caller an extra cast.

 

 

What happens if the object has been deleted or if we supplied the wrong id by accident? In the event that the object was not found, then the find() call simply returns null. We would need to ensure that a null check is performed before the next time the emp variable is used

 

 

 

 

 

The code for a method that looks up and returns the Employee with a given id is now trivial and shown in Listing 2-4.

 

Listing 2-4. Method for Finding an Employee

 

 

public Employee findEmployee(int id) {
return em.find(Employee.class, id);
}

 

 

 

In the case where no employee exists for the id that is passed in, then the method will return null because that is what find() will return.

 

 

Removing an Entity

 

Removal of an entity from the database is not as common as you might think. Many applications never delete objects, or if they do they just flag the data as being out of date or no longer valid and then just keep it out of sight of clients. We are not talking about that kind of application-level logical removal, where the data is not even removed from the database. We are talking about something that results in a DELETE statement being made across one or more tables.

 

In order to remove an entity, the entity itself must be managed, meaning that it is present in the persistence context. This means that the calling application should have already loaded or accessed the entity and is now issuing a command to remove it. This is not normally a problem given that most often the application will have caused it to become managed as part of the process of determining that this was the object that it wanted to remove.

 

A simple example of removing an employee is the following:

 

 

 

 

Employee emp = em.find(Employee.class, 158);
em.remove(emp);

 

 

 

In this example, we are first finding the entity using the find() call, which returns a managed instance of Employee, and then removing the entity using the remove() call on the entity manager. Of course, you learned in the previous section that if the entity was not found, then the find() method will return null. We would get a java.lang.IllegalArgumentException if it turned out that we passed null into the remove() call because we forgot to include a null check before calling remove().

In our application method for removing an employee, we can fix the problem by checking for the existence of the employee before we issue the remove() call, as shown in Listing 2-5.

 

Listing 2-5. Method for Removing an Employee

 

public void removeEmployee(int id) {
	Employee emp = em.find(Employee.class, id);
	if (emp != null) {
		em.remove(emp);
	}
}

 

 

 

This method will ensure that the employee with the given id, provided the id is not null, is removed from the database. It will return successfully whether the employee exists or not.

 

 

Updating an Entity

 

There are a few different ways of updating an entity, but for now we will illustrate the simplest and most common case. This is where we have a managed entity and want to make changes to it. If we do not have a reference to the managed entity, then we must first get one using find() and then perform our modifying operations on the managed entity. This code adds $1,000 to the salary of the employee with id 158:

 

 

Employee emp = em.find(Employee.class, 158);
emp.setSalary(emp.getSalary() + 1000);

 

 

Note the difference between this operation and the others. In this case we are not calling into the entity manager to modify the object, but directly calling the object itself. For this reason it is important that the entity be a managed instance; otherwise, the persistence provider will have no means of detecting the change, and no changes will be made to the persistent representation of the employee. Our method to raise the salary of a given employee will take the id and amount of the raise, find the employee, and change the salary to the adjusted one. Listing 2-6 demonstrates this approach.

 

Listing 2-6. Method for Updating an Employee

 

 

public Employee raiseEmployeeSalary(int id, long raise) {
	Employee emp = em.find(Employee.class, id);
	if (emp != null) {
		emp.setSalary(emp.getSalary() + raise);
	}
	return emp;
}

 

 

If we can’t find the employee, we return null so the caller will know that no change could be made. We indicate success by returning the updated employee.

 

 

Transactions

 

You may feel that the code so far seems inconsistent with what we said earlier about transactionality when working with entities. There were no transactions in any of the preceding examples, even though we said that changes to entities must be made persistent using a transaction.

 

In all the examples except the one that called only find(), we assume that a transaction enclosed each method. The find() call is not a mutating operation, so it may be called any time, with or without a transaction.

 

Once again, the key is the environment in which the code is being executed. The typical situation when running inside the Java EE container environment is that the standard Java Transaction API (JTA) is used. The transaction model when running in the container is to assume the application will ensure that a transactional context is present when one is required. If a transaction is not present, then either the modifying operation will throw an exception or the change will simply never be persisted to the data store. We will come back to discussing transactions in the Java EE environment in more detail in chapter3

 

In our example in this chapter, though, we are not running in Java EE. We are in a Java SE environment, and the transaction service that should be used in Java SE is the EntityTransaction service. When executing in Java SE, we either need to begin and to commit the transaction in the operational methods, or we need to begin and to commit the transaction before and after calling an operational method. In either case, a transaction is started by calling getTransaction() on the entity manager to get the EntityTransaction and then invoking begin() on it. Likewise, to commit the transaction the commit() call is invoked on the EntityTransaction obtained from the entity manager. For example, starting and committing before and after the method would produce code that creates an employee the way it is done in Listing 2-7.

 

Listing 2-7. Beginning and Committing an EntityTransaction

 

em.getTransaction().begin();
createEmployee(158, "John Doe", 45000);
em.getTransaction().commit();

 

Further detail about resource-level transactions and the EntityTransaction API are contained in Chapter 6.

 

 

Queries

 

In general, given that most developers have used a relational database at some point or another in their lives, most of us pretty much know what a database query is. In JPA, a query is similar to a database query, except that instead of using Structured Query Language (SQL) to specify the query criteria, we are querying over entities and using a language called Java Persistence Query Language (JP QL).

 

A query is implemented in code as a Query or TypedQuery object. They are constructed using the EntityManager as a factory. The EntityManager interface includes a variety of API calls that return a new Query or TypedQuery object. As a first-class object, a query can in turn be customized according to the needs of the application.

 

A query can be defined either statically or dynamically. A static query is defined in either annotation or XML metadata, and it must include the query criteria as well as a user-assigned name. This kind of query is also called a named query, and it is later looked up by its name at the time it is executed.

 

A dynamic query can be issued at runtime by supplying the JP QL query criteria, or a criteria object. They may be a little more expensive to execute because the persistence provider cannot do any query preparation beforehand, but JP QL queries are nevertheless very simple to use and can be issued in response to program logic or even user logic.

 

Following is an example showing how to create a dynamic query and then execute it to obtain all the employees in the database. Of course, this may not be a very good query to execute if the database is large and contains hundreds of thousands of employees, but it is nevertheless a legitimate example. The simple query is as follows:

 

TypedQuery<Employee> query = em.createQuery("SELECT e FROM Employee e",
Employee.class);
List<Employee> emps = query.getResultList();

 

We create a TypedQuery object by issuing the createQuery() call on the EntityManager and passing in the JP QL string that specifies the query criteria. The JP QL string refers not to an EMPLOYEE database table but to the Employee entity, so this query is selecting all Employee objects without filtering them any further. You will be diving into queries in Chapter 7, JP QL in Chapters 7 and 8, and criteria queries in Chapter 9. You will see that you can be far more discretionary about which objects you want to be returned.

 

To execute the query we simply invoke getResultList() on it. This returns a List (a subinterface of Collection) containing the Employee objects that matched the query criteria. We can easily create a method that returns all of the employees, as shown in Listing 2-8.

 

Listing 2-8. Method for Issuing a Query

 

public List<Employee> findAllEmployees() {
	TypedQuery<Employee> query = em.createQuery("SELECT e FROM Employee e", Employee.class);
	return query.getResultList();
}

 

This example shows how simple queries are to create, execute, and process, but what this example does not show is how powerful they are. In Chapter 7, we will examine many other extremely useful and interesting ways of defining and using queries in an application.

 

Putting It All Together

 

We can now take all the methods that we have created and combine them into a class. The class will act

like a service class, which we will call EmployeeService and will allow us to perform operations on employees. The code should be pretty familiar by now. Listing 2-9 shows the complete implementation.

 

Listing 2-9. Service Class for Operating on Employee Entities

 

import javax.persistence.*;
import java.util.List;

public class EmployeeService {
	protected EntityManager em;
	public EmployeeService(EntityManager em) {
		this.em = em;
	}
	
	public Employee createEmployee(int id, String name, long salary) {
		Employee emp = new Employee(id);
		emp.setName(name);
		emp.setSalary(salary);
		em.persist(emp);
		return emp;
	}
	
	public void removeEmployee(int id) {
		Employee emp = findEmployee(id);
		if (emp != null) {
			em.remove(emp);
		}
	}

	public Employee raiseEmployeeSalary(int id, long raise) {
		Employee emp = em.find(Employee.class, id);
		if (emp != null) {
			emp.setSalary(emp.getSalary() + raise);
		}
		return emp;
	}
	
	public Employee findEmployee(int id) {
		return em.find(Employee.class, id);
	}
	

	public List<Employee> findAllEmployees() {
		TypedQuery<Employee> query = em.createQuery("SELECT e FROM Employee e", Employee.class);
		return query.getResultList();
	}
}

This is a simple yet fully functional class that can be used to issue the typical create, read, update, and delete (CRUD) operations on Employee entities. This class requires that an entity manager is created and passed into it by the caller and also that any required transactions are begun and committed by the caller. It may seem strange at first, but decoupling the transaction logic from the operation logic makes this class more portable to the Java EE environment. We will revisit this example in the next chapter, in which we focus on Java EE applications.

 

A simple main program that uses this service and performs all the required entity manager creation and transaction management is shown in Listing 2-10.

 

Listing 2-10. Using EmployeeService

 

import javax.persistence.*;
import java.util.List;

public class EmployeeTest {
	public static void main(String[] args) {
		EntityManagerFactory emf = Persistence
				.createEntityManagerFactory("EmployeeService");
		EntityManager em = emf.createEntityManager();
		EmployeeService service = new EmployeeService(em);
		// create and persist an employee
		em.getTransaction().begin();
		Employee emp = service.createEmployee(158, "John Doe", 45000);
		em.getTransaction().commit();
		System.out.println("Persisted " + emp);
		// find a specific employee
		emp = service.findEmployee(158);
		System.out.println("Found " + emp);
		// find all employees
		List<Employee> emps = service.findAllEmployees();
		for (Employee e : emps)
			System.out.println("Found employee: " + e);
		// update the employee
		em.getTransaction().begin();
		emp = service.raiseEmployeeSalary(158, 1000);
		em.getTransaction().commit();
		System.out.println("Updated " + emp);
		// remove an employee
		em.getTransaction().begin();
		service.removeEmployee(158);
		em.getTransaction().commit();
		System.out.println("Removed Employee 158");
		// close the EM and EMF when done
		em.close();
		emf.close();
	}
}

 

Note that at the end of the program we use the close() methods to clean up the entity manager and the factory that we used to create it. This ensures that all the resources they might have allocated are properly released.

 

Packaging It Up

 

Now that you know the basic building blocks of JPA, we are ready to organize the pieces into an application that runs in Java SE. The only thing left to discuss is how to put it together so that it runs. 

 

Persistence Unit

The configuration that describes the persistence unit is defined in an XML file called persistence.xml. Each persistence unit is named, so when a referencing application wants to specify the configuration for an entity it needs only to reference the name of the persistence unit that defines that configuration. A single persistence.xml file can contain one or more named persistence unit configurations, but each persistence unit is separate and distinct from the others, and they can be logically thought of as being in separate persistence.xml files.

 

Many of the persistence unit elements in the persistence.xml file apply to persistence units that are deployed within the Java EE container. The only ones that we need to specify for our example are name, transaction-type, class, and properties. There are a number of other elements that can be specified in the persistence unit configuration in the persistence.xml file, but they will be discussed in more detail in Chapter 13. Listing 2-11 shows the relevant part of the persistence.xml file for this example.

 

Listing 2-11. Elements in the persistence.xml File

 

<persistence>
	<persistence-unit name="EmployeeService" transaction-type="RESOURCE_LOCAL">
		<class>examples.model.Employee</class>
		<properties>
			<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
			<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/EmpServDB;create=true"/>
			<property name="javax.persistence.jdbc.user" value="APP"/>
			<property name="javax.persistence.jdbc.password" value="APP"/>
		</properties>
	</persistence-unit>
</persistence>

 

The name attribute of the persistence-unit element indicates the name of our persistence unit and is the string that we specify when we create the EntityManagerFactory. We have used “EmployeeService” as the name. The transaction-type attribute indicates that our persistence unit uses resource-level EntityTransaction instead of JTA transactions. The class element lists the entity that is part of the persistence unit. Multiple class elements can be specified when there is more than one entity. They would not normally be needed when deploying in a Java EE container because the container will scan for entities automatically as part of the deployment process, but they are needed for portable execution when running in Java SE. We have only a single Employee entity. 

 

The last section is just a list of properties that can be standard or vendor-specific. The JDBC database login parameters must be specified when running in a Java SE environment to tell the provider what resource to connect to. Other provider properties, such as logging options, are vendorspecific and might also be useful.

 

Persistence Archive

The persistence artifacts are packaged in what we will loosely call a persistence archive. This is really just a JAR-formatted file that contains the persistence.xml file in the META-INF directory and normally the entity class files.

 

Because we are running as a simple Java SE application, all we have to do is put the application JAR, the persistence provider JARs, and the JPA JAR on the classpath when the program is executed.

 

Summary

 

This chapter discussed just enough of the basics of the Java Persistence API to develop and run a simple

application in a Java SE runtime.

 

We started out discussing the entity, how to define one, and how to turn an existing Java class into one. We discussed entity managers and how they are obtained and constructed in the Java SE environment.

 

The next step was to instantiate an entity instance and use the entity manager to persist it in the database. After we inserted some new entities, we could retrieve them again and then remove them. We also made some updates and ensured that the changes were written back to the database.

 

We talked about the resource-local transaction API and how to use it. We then went over some of the different types of queries and how to define and execute them. Finally, we aggregated all these techniques and combined them into a simple application that we can execute in isolation from an enterprise environment.

 

In the next chapter, we will look at the impact of the Java EE environment when developing enterprise applications using the Java Persistence API.

 

 

 

 

 

 

 

 

 

  • 大小: 22 KB
分享到:
评论

相关推荐

    notes_JPA_JSF:使用JSF和JPA实施来重建Notes项目

    【标题】"notes_JPA_JSF:使用JSF和JPA实施来重建Notes项目"涉及的是在Java开发环境中,利用JavaServer Faces (JSF) 和 Java Persistence API (JPA) 技术重构建一个名为Notes的项目。JSF是Java EE平台上的一个用户...

    openjpa:Apache OpenJPA

    openjpa-project / RELEASE-NOTES.html 有关文档和项目信息,请访问我们的项目站点: : 编译中 自己编译Apache OpenJPA的最佳方法是针对默认的derby数据库运行构建。 $&gt; mvn clean install -Dsurefire.excludes....

    spring-boot-mysql-rest-api-tutorial:使用Spring Boot,Mysql,JPA和Hibernate构建Restful CRUD API

    Spring Boot,MySQL,JPA,Hibernate Rest API教程使用Spring Boot,Mysql,JPA和Hibernate为简单的记笔记应用程序构建Restful CRUD API。要求Java-1.8.x Maven-3.xx MySQL的-5.xx设定步骤1.克隆应用程序git clone ...

    Notes Java

    Java Enterprise Edition (Java EE) 是企业级应用开发的平台,包含了Servlet、JSP、EJB、JMS、JPA等技术,用于构建分布式、可扩展的应用系统。其中,Servlet和JSP是Web开发的基础,EJB则用于实现业务逻辑组件。 ...

    enseignants et étudiants (gestion des notes, annonces

    标题和描述中提到的"enseignants et étudiants (gestion des notes, annonces)"可以理解为一个教育管理系统,主要用于教师和学生之间的交互,特别是关于成绩管理和公告发布方面。在这个系统中,教师能够方便地记录...

    cccctyl-java-basic-notes-master_java_

    【标题】"cccctyl-java-basic-notes-master_java_" 指的可能是一个关于Java基础学习的项目,其中包含了作者cccctyl对Java编程语言核心概念的深入理解和实践笔记。这个项目特别关注了权限设置和安全模块,这在任何...

    hibernate(notes)

    Hibernate是JPA(Java Persistence API)的一个实现,它提供了一套API和工具,用于将Java对象与关系数据库进行映射。这使得开发人员可以专注于业务逻辑,而无需关心底层数据库操作的细节。 二、Hibernate 安装与...

    crm:使用Spring Boot,JPA用Java编写的简单客户关系管理系统Web应用程序

    客户管理系统 Java Spring启动项目 系统要求 阿帕奇Tomcat MySQL的 Maven 目前正在进行中: 与Frontend集成 REST API端点 实体 ... " Notes " : String ... " details " : String , //like notes " r

    springboot-notes-app

    4. **数据库存储(Database Storage)**:一般使用关系型数据库如MySQL或非关系型数据库如MongoDB存储笔记数据,通过JPA或MyBatis等持久层框架进行数据操作。 总结来说,"springboot-notes-app"是一个基于Spring ...

    notes_spring:使用Spring MVC和Spring Security实施重建Notes项目

    Spring Data JPA或MyBatis可以用来简化数据访问层的操作。你可能需要创建Repository接口,Spring Data会自动生成对应的实现,以执行CRUD操作。 总的来说,"notes_spring"项目将涵盖Web开发中的多个方面,包括但不...

    wicket-notes-demo:一个技术演示,使用Wicket,Spring,JPA2,JSR-303(Bean验证)在Java中实现多用户注释Webapp

    检票口演示 特拉维斯: 一个技术演示,该演示使用 , , , 在Java中实现多用户注释应用程序关于该Webapp提供了按用户备忘的功能,这意味着不同的用户可以登录和创建,阅读,更新和删除备忘。 此外,注释可以按其...

    Java+JDK+7 Learning notes.zip_java_jdk

    1. 多 Catch 块:在JDK 7中,你可以在一个try语句中捕获多个异常类型,而不需要创建多个catch块。 2. 字符串in switch:现在可以在switch语句中直接使用字符串,使得处理枚举类型和字符串更加便捷。 3. 自动资源管理...

    notes_javautils:java工具,框架整合

    Spring Data支持与各种数据存储的集成,如JPA(Java Persistence API)用于ORM(对象关系映射),而Spring MVC则是Web应用开发的强有力选择。 此外,"notes_javautils"可能还包括了对Java集合框架的深入理解,如...

    Assessment:Linux夏令营-Spring Notes-评估

    1. **Spring IoC容器**:IoC(Inversion of Control,控制反转)是Spring的核心特性,通过反转对象的创建和管理,使得开发者可以从繁杂的依赖关系中解脱出来。容器负责创建对象,管理它们的生命周期,并根据需要注入...

    spring-notes:用于演示Spring Boot的简单CRUD API

    1. **实体类(Entity)**:首先,你需要定义一个实体类,它代表数据库中的表。例如,你可以创建一个名为 `User` 的实体类,其中包含 `id`、`name`、`email` 等字段,并使用 `@Entity` 注解标记该类。 2. **...

    java8stream源码-StudyNotes:学习笔记

    StudyNotes [TOC] Java Java编程思想 Spring in action Java8实战 自己的Java学习笔记 为什么说HashMap是不安全的(即将开坑...) 什么是 APT(annotation processing tool) MARK 翻译 前端 Vue.js实战 HTTP 转载 JPA

    android-notes-app-backend:使用Java for Android及其Spring Boot REST API制作的Notes应用程序

    1. **Spring Data JPA**:这是一个Spring框架的扩展,用于简化数据库访问。它支持ORM(对象关系映射),如Hibernate,使得开发者可以使用Java对象直接操作数据库。 2. **H2数据库**:H2是一个轻量级的、开源的关系...

    javaNotes

    1. **Java基础**:包括Java的安装、环境变量配置、 HelloWorld程序,以及Java语法的基础部分,如变量、数据类型、运算符、流程控制(if-else、switch、for、while循环)等。 2. **类与对象**:这是面向对象编程的...

    java-notes

    11. **Java EE**:如果"java-notes"涉及到Java企业版,那么还可能包含Servlet、JSP、EJB、JMS、JPA等Web开发和企业服务的相关知识。 12. **最新的Java版本特性**:随着Java的不断更新,如Java 8的Lambda表达式、...

    Interview_Notes:互联网大厂技术面笔记,学习路线,进行开发

    1. **Java基础知识**:这是所有Java开发者的基础,包括但不限于语法、数据类型、运算符、流程控制语句(如if、switch、for、while)、类与对象、封装、继承和多态等概念。 2. **面向对象编程**:深入理解OOP(面向...

Global site tag (gtag.js) - Google Analytics