Hibernate框架JPA环境注解搭建配置

JPA环境搭建:
          1 16个hibernate的环境包

          2  1个JPA的包在   lib文件夹下

          
          3 2个文件 映射  数据库核心

          使用直接替代xxx.hbm.xml映射的配置文件
            
            类上:
            @Entity // 当前类是持久化类
            @Table(name="表名") //当前持久化类和哪个表做映射

            属性:
            @Id //当前的属性是oid属性
            @Column(name="主键名")// 和表的哪个字段做映射
            @GeneratedValue(strategy=GenerationType.IDENTITY) //指定oid的增长策略


             @Column(name="字段名") // 其它属性和表中的字段映射


        数据库核心文件:
             1 在src下面 有一个META-INF的文件夹
             2 在这个文件夹下面 有一个persistence.xml的文件
                     约束:hibernate-entitymanager-5.0.7.Final.jar
                                -----org.hibernate.jpa
                                        ---persistence_2_0.xsd
                                            ---24行到26行

             3 所有的数据库信息配置都在这个文件中来配置

persistence.xml配置,可配置多个数据库

package bean;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

//类上:
//	@Entity // 当前类是持久化类
//	@Table(name="表名") //当前持久化类和哪个表做映射
//
//	属性:
//	@Id //当前的属性是oid属性
//	@Column(name="主键名")// 和表的哪个字段做映射
//	@GeneratedValue(strategy=GenerationType.IDENTITY) //指定oid的增长策略
//
//
//	 @Column(name="字段名") // 其它属性和表中的字段映射
@Entity
@Table(name = "cst_customer")
public class Customer {
	@Id
	@Column(name = "cust_id")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long cust_id;// '客户编号(主键)',
	@Column(name = "cust_name")
	private String cust_name;// '客户名称(公司名称)',
	@Column(name = "cust_source")
	private String cust_source;// '客户信息来源',
	@Column(name = "cust_industry")
	private String cust_industry;// '客户所属行业',
	@Column(name = "cust_level")
	private String cust_level;// '客户级别',
	@Column(name = "cust_address")
	private String cust_address;// '客户联系地址',
	@Column(name = "cust_phone")
	private String cust_phone;// '客户联系电话',
	
	
	
	public Set<Linkman> getLinkmans() {
		return linkmans;
	}
	public void setLinkmans(Set<Linkman> linkmans) {
		this.linkmans = linkmans;
	}
	public Long getCust_id() {
		return cust_id;
	}
	public void setCust_id(Long cust_id) {
		this.cust_id = cust_id;
	}
	public String getCust_name() {
		return cust_name;
	}
	public void setCust_name(String cust_name) {
		this.cust_name = cust_name;
	}
	public String getCust_source() {
		return cust_source;
	}
	public void setCust_source(String cust_source) {
		this.cust_source = cust_source;
	}
	public String getCust_industry() {
		return cust_industry;
	}
	public void setCust_industry(String cust_industry) {
		this.cust_industry = cust_industry;
	}
	public String getCust_level() {
		return cust_level;
	}
	public void setCust_level(String cust_level) {
		this.cust_level = cust_level;
	}
	public String getCust_address() {
		return cust_address;
	}
	public void setCust_address(String cust_address) {
		this.cust_address = cust_address;
	}
	public String getCust_phone() {
		return cust_phone;
	}
	public void setCust_phone(String cust_phone) {
		this.cust_phone = cust_phone;
	}
	@Override
	public String toString() {
		return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_source=" + cust_source
				+ ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_address=" + cust_address
				+ ", cust_phone=" + cust_phone + "]";
	}
	
}
<?xml version="1.0" encoding="UTF-8"?>
<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_2_0.xsd" 
	version="2.0">
	<persistence-unit name="mysql">
		<properties>
			<property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver"/>
			<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jpa?useSSL=false&amp;serverTimezone=UTC"/>
			<property name="hibernate.connection.username" value="root"/>
			<property name="hibernate.connection.password" value="123"/>
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
			
			<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>
			
			<property name="hibernate.show_sql" value="true"/>
			<property name="hibernate.format_sql" value="true"/>
			
			<property name="hibernate.hbm2ddl.auto" value="update"/>
		</properties>
	</persistence-unit>
</persistence>

猜你喜欢

转载自blog.csdn.net/qq_43122641/article/details/89071832