EJB3 Java 持久化API来标准化Java的持久化学习笔记

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&amp;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();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值