JPA完成单表注解配置说明包括日期,大文本,二进制文本,枚举

1,在src下面新建一个META-INF文件夹,在META-INF下面新建一个persistence.xml的配置文件,persistence.xml配置如下:我这里的JPA采用的是Hibernate实现,不用的实现下面的配置文件的内容不一样的。

<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="mengya" transaction-type="RESOURCE_LOCAL">
      <properties>
         <!-- 方言  -->
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
         <property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
         <property name="hibernate.connection.username" value="root"/>
         <property name="hibernate.connection.password" value="123"/>
         <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/mp?useUnicode=true&amp;characterEncoding=UTF-8"/>
      </properties>
   </persistence-unit>
</persistence>

 2,配置一个实体Bean如:

package com.mengya.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.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import com.mengya.util.Gender;

//表示实体Bean
@Entity
//设置表名
@Table(name = "PERSON")
public class Person {

	private Integer id;

	private String name;

	private Date birthday;
	
	private String info;
	
	private byte[] file;
	
	private String imagePath;
	
	// 枚举类型
	// 设置该列的默认值,在字段后面赋初始值
	private Gender sex = Gender.MAN;

	public Person() {

	}
	public Person(String name) {
		this.name = name;
	}

	// 放在get方法上面或字段上面
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	// 设置该列在数据中列名,长度,非空
	@Column(name = "p_name", length = 10, nullable = false)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	// 设置日期的格式为yyyy-MM-dd
	@Temporal(TemporalType.DATE)
	public Date getBirthday() {
		return birthday;
	}

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

	// 取其枚举的值
	@Enumerated(EnumType.STRING)
	// 取其枚举的索引
	// @Enumerated(EnumType.ORDINAL);
	@Column(length = 10, nullable = false)
	public Gender getSex() {
		return sex;
	}

	public void setSex(Gender sex) {
		this.sex = sex;
	}
	
	//@Lob表示大的文本字段
	//@Basic(fetch=FetchType.LAZY)设置该字段是否懒加载
	@Lob @Basic(fetch=FetchType.LAZY)
	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}
	
	//大的二进制字段
	@Lob
	public byte[] getFile() {
		return file;
	}

	public void setFile(byte[] file) {
		this.file = file;
	}
	
	//非持久化字段(数据库中没有字段与之对应)
	@Transient
	public String getImagePath() {
		return imagePath;
	}

	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}

}

性别枚举

package com.mengya.util;

public enum Gender {
	MAN,WOMAN;
}

 

测试用例如下:

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import org.junit.BeforeClass;
import org.junit.Test;

import com.mengya.bean.Person;
import com.mengya.util.Gender;

public class PersonTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}
	
	@Test public void save(){
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("mengya");
		EntityManager manager=factory.createEntityManager();
		manager.getTransaction().begin();
		
		Person p=new Person();
		p.setName("小酱油");
		DateFormat df=DateFormat.getDateInstance();
		try {
			Date d=df.parse("1986-8-25");
			p.setBirthday(d);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		p.setSex(Gender.WOMAN);
		
		manager.persist(p);
		
		manager.getTransaction().commit();
		manager.close();
		factory.close();
	}
}

 

 

 

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