CREATE TABLE Student( id INT NOT NULL AUTO_INCREMENT, username VARCHAR(200) NOT NULL, PASSWORD VARCHAR(20) NOT NULL, age INT, PRIMARY KEY(id) )ENGINE=MYISAM DEFAULT CHARSET=GBK;
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="JavaEE_JPAPU" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>dao.Student</class> <properties> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /> <property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1:3310/jpa" /> <property name="hibernate.connection.username" value="root" /> <property name="hibernate.connection.password" value="123456" /> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider" /> </properties> </persistence-unit> </persistence>
package dao; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import static javax.persistence.GenerationType.IDENTITY; import javax.persistence.Id; import javax.persistence.Table; /** * Student entity. @author MyEclipse Persistence Tools */ @Entity @Table(name = "student", catalog = "jpa") public class Student implements java.io.Serializable { // Fields private Integer id; private String username; private String password; private Integer age; // Constructors /** default constructor */ public Student() { } /** minimal constructor */ public Student(String username, String password) { this.username = username; this.password = password; } /** full constructor */ public Student(String username, String password, Integer age) { this.username = username; this.password = password; this.age = age; } // Property accessors @Id @Column(name = "id", unique = true, nullable = false, insertable = true, updatable=true) @javax.persistence.GeneratedValue(strategy=javax.persistence.GenerationType.IDENTITY) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } @Column(name = "username", nullable = false, length = 200) public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } @Column(name = "password", nullable = false, length = 20) public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } @Column(name = "age") public Integer getAge() { return this.age; } public void setAge(Integer age) { this.age = age; } }
package test; import java.util.List; import dao.*; public class JPATest { public static void main(String args[]){ //创建dao StudentDAO dao = new StudentDAO(); //创建实体类 Student user = new Student(); user.setUsername("hellojpa test"); user.setPassword("jpa password"); user.setAge(20); EntityManagerHelper.beginTransaction(); //保存 dao.save(user); //提交事务真正保存实体到数据库 EntityManagerHelper.commit(); //列出数据库中所有对象 List<Student> result = dao.findAll(); for(Student o: result){ System.out.println(o.getId()); System.out.println(o.getUsername()); } //更改用户名 user.setUsername("测试JPA"); //开始事务 EntityManagerHelper.beginTransaction(); dao.update(user); //提交事务真正保存实体到数据库 EntityManagerHelper.commit(); //查找所有年龄为20的用户,从第1个开始获取(0为第一个用户) result = dao.findByAge(20, 0); for(Student o:result){ System.out.println(o.getId()); System.out.println(o.getUsername()); } //根据用户ID查找 user = dao.findById(2); System.out.println(user.getUsername()); //删除 //开始事务 EntityManagerHelper.beginTransaction(); //保存(JPA会自动更新变动过的字段) dao.delete(user); //提交事务真正保存实体到数据库 EntityManagerHelper.commit(); } }