在数据库中创建表
CREATE TABLE `books` ( `bid` VARCHAr(255) NOT NULL COLLATE 'utf8mb4_unicode_ci', `author` VARCHAr(255) NULL DEFAULT NULL COLLATE 'utf8mb4_unicode_ci', `status` VARCHAr(255) NULL DEFAULT NULL COLLATE 'utf8mb4_unicode_ci', `title` VARCHAr(255) NULL DEFAULT NULL COLLATE 'utf8mb4_unicode_ci', PRIMARY KEY (`bid`) USING BTREE ) COLLATE='utf8mb4_unicode_ci' ENGINE=InnoDB ;
在NetBeans IDE中创建maven管理的应用
使用向导从数据库表创建实体类:
使用向导从实体类创建控制器类:
手工编写main方法调用自动生成的Controller进行CRUD
package com.example.jpanetbeans4;
import java.util.List;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class Jpanetbeans4 {
public static void main(String[] args) throws Exception {
System.out.println("Hello World!");
String PERSISTENCE_UNIT_NAME = "com.example_jpanetbeans4_jar_1.0-SNAPSHOTPU";
EntityManagerFactory emFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
BooksJpaController bjc = new BooksJpaController(emFactory);
Books book1 = new Books();
book1.setAuthor("author1");
book1.setBid("1001");
book1.setTitle("title1");
book1.setStatus("status1");
bjc.create(book1);
Books book2 = new Books();
book2.setAuthor("author2");
book2.setBid("1002");
book2.setTitle("title2");
book2.setStatus("status2");
bjc.create(book2);
Books book1up = new Books();
book1up.setAuthor("author1up");
book1up.setBid("1001");
book1up.setTitle("title1up");
book1up.setStatus("status1up");
bjc.edit(book1up);
System.out.println(bjc.getBooksCount());
List booklist = bjc.findBooksEntities();
for (Books book : booklist) {
System.out.println(book.getBid() + ":" + book.getTitle() + ":" + book.getAuthor() + ":" + book.getStatus());
}
Books bookfind = bjc.findBooks("1001");
System.out.println(bookfind.getBid() + ":" + bookfind.getTitle() + ":" + bookfind.getAuthor() + ":" + bookfind.getStatus());
bjc.destroy("1001");
booklist = bjc.findBooksEntities();
for (Books book : booklist) {
System.out.println(book.getBid() + ":" + book.getTitle() + ":" + book.getAuthor() + ":" + book.getStatus());
}
}
}
在pom.xml中增加数据库驱动包
4.0.0 com.example jpanetbeans1.0-SNAPSHOT mysql mysql-connector-java8.0.28 org.eclipse.persistence org.eclipse.persistence.core2.7.9 org.eclipse.persistence org.eclipse.persistence.asm9.1.0 org.eclipse.persistence org.eclipse.persistence.antlr2.7.9 org.eclipse.persistence org.eclipse.persistence.jpa2.7.9 org.eclipse.persistence org.eclipse.persistence.jpa.jpql2.7.9 org.eclipse.persistence org.eclipse.persistence.moxy2.7.9 org.eclipse.persistence javax.persistence2.2.1 org.eclipse.persistence org.eclipse.persistence.jpa.modelgen.processor2.7.9 provided UTF-8 1.8 1.8 com.example.jpanetbeans.Jpanetbeans
向导使用的JPA实现是Eclipselink,也可以更改JPA实现为Hibernate:
org.hibernate.jpa.HibernatePersistenceProvider com.example.jpanetbeans2.Books
4.0.0 com.example jpanetbeans21.0-SNAPSHOT mysql mysql-connector-java8.0.28 org.hibernate hibernate-core5.6.5.Final UTF-8 1.8 1.8 com.example.jpanetbeans2.Jpanetbeans2
更改为OpenJPA
org.apache.openjpa.persistence.PersistenceProviderImpl com.example.jpanetbeans4.Books
4.0.0 com.example jpanetbeans41.0-SNAPSHOT org.apache.openjpa openjpa3.2.1 mysql mysql-connector-java8.0.28 UTF-8 1.8 1.8 com.example.jpanetbeans4.Jpanetbeans4
运行输出:
cd C:UsersAdministratordocumentsNetBeansProjectsjpanetbeans4; "JAVA_HOME=C:\Program Files\Java\jdk1.8.0_202" cmd /c ""C:\Program Files\NetBeans-13\netbeans\java\maven\bin\mvn.cmd" -Dexec.vmArgs= "-Dexec.args=${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}" "-Dexec.executable=C:\Program Files\Java\jdk1.8.0_202\bin\java.exe" -Dexec.mainClass=com.example.jpanetbeans4.Jpanetbeans4 -Dexec.classpathScope=runtime -Dexec.appArgs= "-Dmaven.ext.class.path=C:\Program Files\NetBeans-13\netbeans\java\maven-nblib\netbeans-eventspy.jar" -Dfile.encoding=UTF-8 org.codehaus.mojo:exec-maven-plugin:3.0.0:exec"
Running NetBeans Compile On Save execution. Phase execution is skipped and output directories of dependency projects (with Compile on Save turned on) will be used instead of their jar artifacts.
Scanning for projects...
----------------------< com.example:jpanetbeans4 >----------------------
Building jpanetbeans4 1.0-SNAPSHOT
--------------------------------[ jar ]---------------------------------
--- exec-maven-plugin:3.0.0:exec (default-cli) @ jpanetbeans4 ---
Hello World!
[EL Info]: 2022-03-26 22:38:31.319--ServerSession(893504292)--Eclipselink, version: Eclipse Persistence Services - 2.7.9.v20210604-2c549e2208
2
1001:title1up:author1up:status1up
1002:title2:author2:status2
1001:title1up:author1up:status1up
1002:title2:author2:status2
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 3.505 s
Finished at: 2022-03-26T22:38:32+08:00
------------------------------------------------------------------------



