欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jpa学习笔记——2

程序员文章站 2022-03-02 15:20:25
...

了解了以下几个注解,暂时学习的jpa只是将注解注释到mysql中,类的字段与表的字段一一对应,表可以自动生成,数据也可以自动添加

@Entity

@Transient

@Temporal

@Table

@Id

@GeneratedValue

@Column

@Basic


直接上代码

package com.huawei.test;



import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.xml.crypto.Data;

@Table(name="JPA_CUSTOMERS")
@Entity
public class Customer {

	private Integer id;
	private String lastName;
	private String email;
	private int age;
	private Date createTime;
	private Date birth;
	
	//生成主键的策略
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Id
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	@Column(name="LAST_NAME", length=50, nullable=false)
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	//默认是加上的 ,默认映射成数据表的一列
	@Basic
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	//指定时间的类型,datetime类型,精确到秒
	@Temporal(TemporalType.TIMESTAMP)
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	//指定时间的类型,date类型,精确到秒
	@Temporal(TemporalType.DATE)
	public Date getBirth() {
		return birth;
	}
	public void setBirth(Date birth) {
		this.birth = birth;
	}
	// 工具方法,不需要映射为数据表的一列
	//不映射成数据表的一列
	@Transient
	public String getInfo() {
		return "lastName" + this.lastName + " email:" + this.email;
	}
	
}