JPA入门(一)

1.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">
   <persistence-unit name="default" 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="root" />
         <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/cet?useUnicode=true" />
		 <property name="hibernate.show_sql" value="true" />
         
      </properties>
   </persistence-unit>
</persistence>

2.导入jar  详见附件

3.实体类
package org.jzkangta.entity;

import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
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;

@Entity
@Table(name="User") //设置数据库表名
public class User {

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)   //主键生成策略  AUTO是默认值,可以省略
	private int uId;
	
	@Column(length=20,name="userName",nullable=false)
	private String name;
	private String password;
	
	@Temporal(TemporalType.DATE)
	private Date birthday;
	
	@Lob //大文本
	private String info;
	
	@Transient //不会添加到数据库表
	private String xxx;
	
	@Basic(fetch=FetchType.LAZY)  //延迟加载
	private Byte[] file;
	
	public User(){
		
	}

	public Date getBirthday() {
    	return birthday;
    }

	public void setBirthday( Date birthday ) {
    	this.birthday = birthday;
    }
	
	public User(String name,String pwd) {
		this.name = name;
		this.password = pwd;
	}
	public int getuId() {
    	return uId;
    }
	public void setuId( int uId ) {
    	this.uId = uId;
    }
	public String getName() {
    	return name;
    }
	public void setName( String name ) {
    	this.name = name;
    }
	public String getPassword() {
    	return password;
    }
	public void setPassword( String password ) {
    	this.password = password;
    }

	public String getInfo() {
    	return info;
    }

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

	public String getXxx() {
    	return xxx;
    }

	public void setXxx( String xxx ) {
    	this.xxx = xxx;
    }

	public Byte[] getFile() {
    	return file;
    }

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



4.测试类
package org.jzkangta.test;

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

public class Test {

	/**
	 * @param args
	 */
	public static void main( String[] args ) {
		//执行这里的时候就已经创建表了
		EntityManagerFactory factory = Persistence.createEntityManagerFactory( "default" );//"default"是在persistence.xml里面的配置
		EntityManager entityManager = factory.createEntityManager();
		//entityManager.getTransaction().begin();  查询的时候不需要开事务
		//entityManager.persist( new User("wangjun","123") );   新增
		//User user = entityManager.find( User.class, 1 );  //查找
		//entityManager.getReference( User.class, 1  );  等同于 find  但是延迟加载,得到的是一个代理对象
		//entityManager.merge( user ); 修改
		//entityManager.remove( user );  删除
		//System.out.println(user.getName());
		//entityManager.getTransaction().commit();
		
		//JPQL查询
		Query query = entityManager.createQuery( "select o from User o where o.uId = ?" );  //User 是实体名  o是别名 uId是字段名  ?是占位符
		//Query query = entityManager.createQuery( "select count(o) from User o where o.uId = ?" );  //User 是实体名  o是别名 uId是字段名  ?是占位符
		
		query.setParameter( 0, 1 );
		//query.getSingleResult();  //得到查询结果为一个的结果
		query.getResultList();
		
		//删除
		query = entityManager.createQuery( "delete from User o where o.uId = ?" );
		query.setParameter( 0, 1 );
		query.executeUpdate();//记得开事务
		
		//修改
		query = entityManager.createQuery( "update User o set o.name = ? where o.uId = ?" );
		
		entityManager.close();
		factory.close();
	}

}

你可能感兴趣的:(xml,jpa)