EJB3 Java 持久化API来标准化Java的持久化学习笔记:
1、配置persistence.xml文件:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<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="manager1">
<class>study.entity.Customer</class>
<class>study.entity.Order</class>
<!-- 数据库配置 -->
<properties>
<!-- SQL方言,这边设定的是MySQL -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<!-- JDBC驱动程序 -->
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<!-- JDBC URL -->
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/sampledb?useUnicode=true&characterEncoding=gbk"/>
<!-- 数据库用户 -->
<property name="hibernate.connection.username" value="root"/>
<!-- 数据库密码 -->
<property name="hibernate.connection.password" value=""/>
</properties>
</persistence-unit>
</persistence>
2、建立数据库表:
CREATE TABLE `customers` (
`ID` bigint(20) NOT NULL auto_increment,
`NAME` varchar(15) default NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=gbk;
3、实体类:
package study.entity;
import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;
import javax.persistence.*;
@Entity()
@Table(name="customers")
@NamedQuery(name="findAllCustomers", query="select c from Customer c")
public class Customer implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
private Long id;
private String name;
public Customer() {
}
@Id()
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ID", unique=true, nullable=false, precision=20)
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@Basic()
@Column(name="NAME", length=15)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.toString();
}
}
4、运行新增和查询:
package study.entity;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1");
EntityManager entityManager = emf.createEntityManager();
EntityTransaction trans = entityManager.getTransaction();
Customer c = new Customer();
c.setName("测试name");
trans.begin();
Query query = entityManager.createNamedQuery("findAllCustomers");
Iterator e = query.getResultList().iterator();
while (e.hasNext()) {
Customer o = (Customer) e.next();
System.out.println("ID: "+o.getId()+" name: "+o.getName());
}
entityManager.persist(c);
trans.commit();
entityManager.close();
}
}
1、配置persistence.xml文件:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<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="manager1">
<class>study.entity.Customer</class>
<class>study.entity.Order</class>
<!-- 数据库配置 -->
<properties>
<!-- SQL方言,这边设定的是MySQL -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<!-- JDBC驱动程序 -->
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<!-- JDBC URL -->
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/sampledb?useUnicode=true&characterEncoding=gbk"/>
<!-- 数据库用户 -->
<property name="hibernate.connection.username" value="root"/>
<!-- 数据库密码 -->
<property name="hibernate.connection.password" value=""/>
</properties>
</persistence-unit>
</persistence>
2、建立数据库表:
CREATE TABLE `customers` (
`ID` bigint(20) NOT NULL auto_increment,
`NAME` varchar(15) default NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=gbk;
3、实体类:
package study.entity;
import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;
import javax.persistence.*;
@Entity()
@Table(name="customers")
@NamedQuery(name="findAllCustomers", query="select c from Customer c")
public class Customer implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
private Long id;
private String name;
public Customer() {
}
@Id()
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ID", unique=true, nullable=false, precision=20)
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@Basic()
@Column(name="NAME", length=15)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.toString();
}
}
4、运行新增和查询:
package study.entity;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1");
EntityManager entityManager = emf.createEntityManager();
EntityTransaction trans = entityManager.getTransaction();
Customer c = new Customer();
c.setName("测试name");
trans.begin();
Query query = entityManager.createNamedQuery("findAllCustomers");
Iterator e = query.getResultList().iterator();
while (e.hasNext()) {
Customer o = (Customer) e.next();
System.out.println("ID: "+o.getId()+" name: "+o.getName());
}
entityManager.persist(c);
trans.commit();
entityManager.close();
}
}