JPA学习1

1导入相应的 jar


JPA学习1

 

2在项目的 src 下新建 META-INF 文件夹,再建立 persistence.xml 文件。

<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">
	<!-- 一堆实体bean的集合 -->
	<!-- 本地事务       全局事务 -->
	<persistence-unit name="jpa" transaction-type="RESOURCE_LOCAL">
		<properties>
			<!-- 使用 MySQL 5.0方言 -->
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
			<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>   <!--jdbc driver --> 
			<property name="hibernate.connection.username" value="root" />   <!--数据库登录名 -->
			<property name="hibernate.connection.password" value="sa" />   <!--登录用户密码 -->
			<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jpa" />   <!--数据库连接地址 -->
		</properties>
	</persistence-unit>
</persistence>
 

建立实体 bean    

   Person.java

package com.yk.jpa.bean;

import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
//@Table(name="xxx")
public class Person {
	private Integer id;
	private String name;
	private Date birthday; //1987-12-10
	private Gender gender=Gender.MAN;
	private String info;
	private Byte[] file;
	private String imagepath;
	
	//不希望此字段成为持久化字段
	@Transient
	public String getImagepath() {
		return imagepath;
	}

	public void setImagepath(String imagepath) {
		this.imagepath = imagepath;
	}

	//二进制文件
	@Lob
	//只要不访问,就不会把此数据装载到内存中,初次访问person的时候不会装载,只有在访问此数据的时候才会装载
	@Basic(fetch=FetchType.LAZY)
	public Byte[] getFile() {
		return file;
	}

	public void setFile(Byte[] file) {
		this.file = file;
	}

	//大文本类型
	@Lob
	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	@Enumerated(EnumType.STRING)
	@Column(length=5,nullable=false)
	public Gender getGender() {
		return gender;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	//设置日期的格式(1987-12-10)
	@Temporal(TemporalType.DATE)
	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public Person(){}  //无参的构造函数
	
	public Person(String name) {
		this.name = name;
	}
	
	//实体标识
	//@Id
	//主键生成策略
	//@GeneratedValue
	/*@GeneratedValue(strategy=GenerationType.AUTO)
	 * 因为使用的策略是AUTO,所以可以省略不写,是默认值,
	 * hibernate会自动根据方言判断使用的是什么数据库而使用哪种主键生成方式
	 **/
	@Id @GeneratedValue
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}

	@Column(length=10,nullable=false)
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}		
}

4  编写测试类   PersonTest.java (Junit4 进行测试 )

public class PersonTest {
    @BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}
	@Test
	public void save(){
		EntityManagerFactory factory=Persistence.createEntityManagerFactory("jpa"); //不执行下面的,已经完全可以建立表
		//-->sessionFactory-->session-->begin事务
		EntityManager em=factory.createEntityManager();
		em.getTransaction().begin();   //开始事务
		//session.save()-->persist()(推荐使用)
		em.persist(new Person("JPA学习"));
		em.getTransaction().commit();  //提交事务
		em.close();
		factory.close();
	}
}
 

此时查看MySQL 数据库:


JPA学习1
 

你可能感兴趣的:(Hibernate,mysql,bean,jdbc,jpa)