1. 程式人生 > >脫坑-JPA配置

脫坑-JPA配置

自己一點點摸索,沒人教的~哎~

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
	xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
	<persistence-unit name="MyJpa" transaction-type="RESOURCE_LOCAL">
		<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
		<class>com.java.entity.Users</class>
		<properties>
			<!--1、設定四本一言 -->
			<!--四本一言 四大基本項: 1、驅動類名 2、指明需要連線的url 3、使用者名稱 4、密碼 Hibernate支援不同的資料庫,但是每種資料庫語法可能有區別,可以使用方言,注意版本 -->
			<!--資料庫驅動類全稱 -->
			<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
			<!--資料庫url地址 -->
			<property name="hibernate.connection.url"
				value="jdbc:mysql://localhost:3306/lemon?characterEncoding=UTF-8" />
			<!--使用者名稱 -->
			<property name="hibernate.connection.username" value="root" />
			<!--密碼 -->
			<property name="hibernate.connection.password" value="MCY520" />
			<!--方言 -->
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />

			<!--執行DDL的類別: create:每次都刪除新建 update:存在就修改,不存在就新建 -->
			<property name="hibernate.hbm2ddl.auto" value="update" />
			<!--是否顯示SQL語句 -->
			<property name="hibernate.show_sql" value="true" />
			<!--是否格式化SQL語句 -->
			<property name="hibernate.format_sql" value="true" />

		</properties>
	</persistence-unit>
</persistence>

實體類

package com.java.entity;

import java.io.Serializable;

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

/**
 * @author AnxiangLemon
 * 客戶的實體類
 * 使用的註解都是Jpa規範  所以匯入javax.persistence包下的
 *
 */
@Entity  //表明該類是一個實體類
@Table(name="myjpa_user")  //建立當前類和資料的表的對應關係
public class Users  implements Serializable{
	
	@Id//表明當前欄位是主鍵
	@Column(name="jap_id")//表明對應資料的主鍵欄位是 jap_id
	@GeneratedValue(strategy=GenerationType.IDENTITY)//指定主鍵生成策略
	private int id;
	
	@Column(name="jap_username")
	private String username;
	
	@Column(name="jap_password")
	private String password;
	
	@Column(name="jap_nick")
	private String nick;
	
	@Column(name="jap_createtime")
	private String createtime;
	
	@Column(name="jap_lapsetime")
	private String  lapsetime;
	
	@Column(name="jap_type")
	private int type;
	
	@Column(name="jap_state")
	private int state;
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}


	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getNick() {
		return nick;
	}
	public void setNick(String nick) {
		this.nick = nick;
	}
	public String getCreatetime() {
		return createtime;
	}
	public void setCreatetime(String createtime) {
		this.createtime = createtime;
	}
	public String getLapsetime() {
		return lapsetime;
	}
	public void setLapsetime(String lapsetime) {
		this.lapsetime = lapsetime;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + ", nick=" + nick
				+ ", createtime=" + createtime + ", lapsetime=" + lapsetime + ", type=" + type + ", state=" + state
				+ "]";
	}

}

測試呼叫

package com.java.uitls;

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

import com.java.entity.Users;

/**
 * @author AnxiangLemon
 *Jpa 簡易工具類
 */
public class JpaUtils {
	private  static  EntityManagerFactory factory;
	
	static{
		factory = Persistence.createEntityManagerFactory("MyJpa");
	}
	
	public static EntityManager  createEntityManager(){
		return factory.createEntityManager();
	}
	
	public static void main(String[] args) {	
		createEntityManager();
	}
}