首先说一点:关于配置文件,叫persistence.xml放在META-INF下边,而这个文件夹放在src/main/resources中
关于具体的用法,在代码中做了详细的说明
首先看配置文件
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
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">
<!--此处的名字与文件加载配置文件的名字一样 -->
<persistence-unit name="jpa-2" transaction-type="RESOURCE_LOCAL">
<!-- 指定jpa的实现厂商 -->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<!-- 类的全路径,注意放的位置 -->
<!-- 即把所对应的实体应该要放到这一个配置文件中 -->
<class>com.qf.pojo.JpaUser</class>
<!-- 数据库连接和厂商的相关信息,都要包含在这一个节点之中 -->
<properties>
<property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
<property name="hibernate.connection.password" value="root"/>
<property name="hibernate.connection.url" value="jdbc:mysql:///jpa"/>
<!-- 关于乱码问题,在这儿的URL路径后边做拼接操作 -->
<property name="hibernate.connection.username" value="root"/>
<!-- 数据库连接的相关注册 -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL55Dialect"/>
<!-- 注意数据库的版本55 -->
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<!-- 这一句是自动创建表,再利用Hibernate.cfg.xml时,也可以自动创建 表
,但理论上都是先设计表结构 -->
<property name="hibernate.hbm2ddl.auto" value="update"/>
<!-- 数据库厂商的相关属性 -->
</properties>
</persistence-unit>
</persistence>
关于工具类的写法
package com.qf.jpaUtils;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class JpaUtils {
private static EntityManagerFactory entityManagerFactory ;
static{ //配置文件的名字
entityManagerFactory=Persistence.createEntityManagerFactory("jpa-2");
/*加载配置文件,获取工厂*/
}
public static EntityManager getentityManager(){
/*通过工厂得到实体对象*/
return entityManagerFactory.createEntityManager();
}
}
下边是基础的CRUD以及JPQL语句
package com.qf.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import com.qf.jpaUtils.JpaUtils;
import com.qf.pojo.JpaUser;
public class JpaUserDao {
/*
* 测试自动创建表
* public static void main(String[] args) {
加载配置,得到实体工厂,联系Hibernate.config.xml,步骤大体相同
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("jpa-2");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManagerFactory.close();
}*/
/* 开始做crud操作*/
//基础的添加数据
public void addJpaUser(JpaUser pojo){
EntityManager manager = JpaUtils.getentityManager();
EntityTransaction transaction = manager.getTransaction();
/*需要注意一下,先要获取事务,然后再开启事务*/
transaction.begin();
manager.persist(pojo);
transaction.commit();
manager.close();
}
//基础的删除数据
public void delJpaUser(long id){
//关于删除的操作,首先要根据id找到数据,然后再去删除
EntityManager manager = JpaUtils.getentityManager();
EntityTransaction transaction = manager.getTransaction();
transaction.begin();
JpaUser pojo = manager.find(JpaUser.class,id);
manager.remove(pojo);//根据ID去删除
transaction.commit();
manager.close();
}
//基础的查找数据
public JpaUser findJpaUser(long jpaid){
EntityManager manager = JpaUtils.getentityManager();
EntityTransaction transaction = manager.getTransaction();
transaction.begin();
JpaUser jpaUser = manager.find(JpaUser.class, jpaid);
transaction.commit();
manager.close();
return jpaUser;
}
//基础的更新数据
public void updateJpaUser(JpaUser pojo){
EntityManager manager = JpaUtils.getentityManager();
EntityTransaction transaction = manager.getTransaction();
transaction.begin();
JpaUser jpaUser = manager.merge(pojo);
/*更新数据的方法*/
System.err.println(jpaUser.getJpasex());
transaction.commit();
manager.close();
}
//查找全部数据
public void findall(){
EntityManager manager = JpaUtils.getentityManager();
String jpql="from JpaUser";
TypedQuery<JpaUser> query = manager.createQuery(jpql, JpaUser.class);
List<JpaUser> list = query.getResultList();
for (JpaUser jpaUser : list) {
System.out.println(jpaUser.getJpaname());
}
manager.close();
}
//根据条件查找,占位符的问题
public void findByName(String jpaname){
EntityManager manager = JpaUtils.getentityManager();
String jphl="from JpaUser j where j.jpaname=?1";
//此处的占位符可以把一省略,默认将是从0开始
TypedQuery<JpaUser> query = manager.createQuery(jphl,JpaUser.class);
@SuppressWarnings("unused")
TypedQuery<JpaUser> parameter = query.setParameter(1, jpaname);
List<JpaUser> list = query.getResultList();
for (JpaUser jpaUser : list) {
System.out.println(jpaUser.getJpaname()+"\t"+jpaUser.getJpasex());
}
manager.close();
}
//查找部分列数据,去调试
public void findsome(){
EntityManager manager = JpaUtils.getentityManager();
String jpql="select j.jpaname,j.jpaage from JpaUser j";
Query query = manager.createQuery(jpql);
List<?> list = query.getResultList();
for (int i = 0; i < list.size(); i++) {
//当去查询部分数据时,利用调试的方法去看,输出的什么内容
Object[] object= (Object[]) list.get(i);
System.out.println(object[0]+"\t"+object[1]);
}
manager.close();
}
//模糊查询
public void findLike(String jpaname){
EntityManager manager = JpaUtils.getentityManager();
String jpql="from JpaUser where jpaname like ?";
// String jpql="from JpaUser where jpaname like :jpaname";
//这样也可以
TypedQuery<JpaUser> query = manager.createQuery(jpql,JpaUser.class);
query.setParameter(0,"%"+ jpaname+"%");
List<JpaUser> list = query.getResultList();
for (JpaUser jpaUser : list) {
System.out.println(jpaUser.getJpaname()+"\t"+jpaUser.getJpaage());
}
manager.close();
}
//更新数据,根据id去更新数据
public void findUpUser(long jpaid,String jpaname){
EntityManager manager = JpaUtils.getentityManager();
EntityTransaction transaction = manager.getTransaction();
transaction.begin();
String jpql="update JpaUser set jpaname=:jpaname where jpaid=:jpaid";
Query createQuery = manager.createQuery(jpql);
createQuery.setParameter("jpaname", jpaname);
createQuery.setParameter("jpaid", jpaid);
createQuery.executeUpdate();//最容易忘记的语句
transaction.commit();
manager.close();
}
//根据条件去删除数据
public void dealUpUser(long jpaid){
EntityManager manager = JpaUtils.getentityManager();
EntityTransaction transaction = manager.getTransaction();
transaction.begin();
String jpql="delete from JpaUser where jpaid=:jpaid";
Query query = manager.createQuery(jpql);
query.setParameter("jpaid", jpaid);
int update = query.executeUpdate();
System.out.println(update);
transaction.commit();
manager.close();
}
//查询总记录数
public void countUpUser(){
EntityManager manager = JpaUtils.getentityManager();
String jpql="select count(jpaid) from JpaUser";
Query query = manager.createQuery(jpql);
long sum=(Long)query.getSingleResult();
System.out.println("总条数为"+sum);
manager.close();
}
//分页 开始的页数 和一页中显示的数量
public void findPag(int startIndex,int size){
EntityManager manager = JpaUtils.getentityManager();
String jpql="from JpaUser";
TypedQuery<JpaUser> query = manager.createQuery(jpql,JpaUser.class);
query.setFirstResult(startIndex);
//关于这两个方法
query.setMaxResults(size);
List<JpaUser> list = query.getResultList();
for (JpaUser jpaUser : list) {
System.out.println(jpaUser.getJpaname()+"\t"+jpaUser.getJpaage());
}
manager.close();
}
}
关于实体类的创建,使用注解的方式去创建(这是在单表)
package com.qf.pojo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="jpa_user")
/*数据库中表的表名*/
public class JpaUser {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
/*关于主键的自动生成*/
private long jpaid;
@Column(name="jpa_name")
private String jpaname;
private int jpaage;
@Column(name="jpa_sex")
/* @里边中name的属性都是数据库表中的字段的名字*/
private String jpasex;
public long getJpaid() {
return jpaid;
}
public void setJpaid(long jpaid) {
this.jpaid = jpaid;
}
public String getJpaname() {
return jpaname;
}
public void setJpaname(String jpaname) {
this.jpaname = jpaname;
}
public int getJpaage() {
return jpaage;
}
public void setJpaage(int jpaage) {
this.jpaage = jpaage;
}
public String getJpasex() {
return jpasex;
}
public void setJpasex(String jpasex) {
this.jpasex = jpasex;
}
}
若是在关联表中,请看下边的代码(配置文件中只需做这两个类的映射,即class标签)
package com.qf.pojo;
import java.util.HashSet;
import java.util.Set;
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="friendtype")
public class Friendtype {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long ftypeid;
@OneToMany(mappedBy="friendtype")
private Set friendinfo = new HashSet(0);//在主表中有一个set集合
public long getFtypeid() {
return ftypeid;
}
public void setFtypeid(long ftypeid) {
this.ftypeid = ftypeid;
}
public Set getFriendinfo() {
return friendinfo;
}
public void setFriendinfo(Set friendinfo) {
this.friendinfo = friendinfo;
}
public Friendtype(long ftypeid, Set friendinfo) {
super();
this.ftypeid = ftypeid;
this.friendinfo = friendinfo;
}
public Friendtype() {
super();
}
}
package com.qf.pojo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="friendinfo")
public class Friendinfo {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long friendid;
private String friendname;
private String friendage;
private String friendsex;
private String friendhobby;
@ManyToOne
@JoinColumn(name="ftypeid")
private Friendtype friendtype;//子表中有一个这个属性(关于主表的)
public long getFriendid() {
return friendid;
}
public void setFriendid(long friendid) {
this.friendid = friendid;
}
public String getFriendname() {
return friendname;
}
public void setFriendname(String friendname) {
this.friendname = friendname;
}
public String getFriendage() {
return friendage;
}
public void setFriendage(String friendage) {
this.friendage = friendage;
}
public String getFriendsex() {
return friendsex;
}
public void setFriendsex(String friendsex) {
this.friendsex = friendsex;
}
public String getFriendhobby() {
return friendhobby;
}
public void setFriendhobby(String friendhobby) {
this.friendhobby = friendhobby;
}
public Friendtype getFriendtype() {
return friendtype;
}
public void setFriendtype(Friendtype friendtype) {
this.friendtype = friendtype;
}
public Friendinfo(long friendid, String friendname, String friendage, String friendsex, String friendhobby,
Friendtype friendtype) {
super();
this.friendid = friendid;
this.friendname = friendname;
this.friendage = friendage;
this.friendsex = friendsex;
this.friendhobby = friendhobby;
this.friendtype = friendtype;
}
public Friendinfo() {
super();
}
}