JPA 增、删、改、查入门实例

package me.cjx.pojo;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "user")
public class User implements Serializable {
	/**
     * 
     */
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue
	@Column(name = "id")
	private int id;

	@Column(name = "name")
	private String name;

	@Column(name = "passwd")
	private String passwd;

	public User() {

	}

	public int getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

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

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}
}

package me.cjx.pojo;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class TestJPA {
	private static EntityManagerFactory factory = null;
	private static EntityManager entity = null;

	public EntityManager getEntityManager() {
		factory = Persistence.createEntityManagerFactory("JPA");
		entity = factory.createEntityManager();
		return entity;
	}

	public static void main(String[] args) {
		User user = new User();
		TestJPA test = new TestJPA();
		user.setId(1);
		user.setName("Robin");
		user.setPasswd("123456");
		test.insert(user);

		user = new User();
		test.update(user);

		user = test.findUser(13);

		test.delete(2);
	}

	public void insert(User user) {
		EntityManager entity = getEntityManager();
		EntityTransaction tran = entity.getTransaction();
		tran.begin();
		entity.persist(user);
		tran.commit();
	}

	public void update(User user) {
		EntityManager entity = getEntityManager();
		EntityTransaction tran = entity.getTransaction();
		tran.begin();
		entity.merge(user);
		tran.commit();
	}

	public void delete(int id) {
		EntityManager entity = getEntityManager();
		EntityTransaction tran = entity.getTransaction();
		tran.begin();
		User user = entity.find(User.class, id);
		entity.remove(user);
		tran.commit();
	}

	public User findUser(int id) {
		User user = new User();

		EntityManager entity = getEntityManager();
		EntityTransaction tran = entity.getTransaction();
		tran.begin();
		user = entity.find(User.class, id);
		tran.commit();

		return user;
	}

	public List<?> findAllUser() {
		User user = new User();
		EntityManager entity = getEntityManager();
		EntityTransaction tran = entity.getTransaction();
		tran.begin();
		Query query = entity.createQuery("select o from User o");
		List<?> list = query.getResultList();
		for (int i = 0; i < list.size(); i++) {
			user = (User) list.get(i);
			System.out.println(user.getId() + "/t" + user.getName() + "/t" + user.getPasswd());
		}
		tran.commit();
		return list;
	}
}

你可能感兴趣的:(jpa)