Maven JPA 环境的简单搭建

工程目录结构:

Maven JPA 环境的简单搭建_第1张图片

1. 引入pom文件


  4.0.0

  com
  jpa
  0.0.1-SNAPSHOT
  jar

  jpa
  http://maven.apache.org

  
    UTF-8
    4.1.7.Final 
  
 
  
    
      junit
      junit
      3.8.1
      test
    
    
            org.hibernate
            hibernate-entitymanager
            ${project.hibernate.version}
        
         
        
        
            org.hibernate
            hibernate-c3p0
            ${project.hibernate.version}
        
         
        
            log4j
            log4j
            1.2.17
        
 
        
         
            mysql
            mysql-connector-java
            5.1.21
        
         
  

2. 建立持久化配置文件 persistence.xml 



    
        
        org.hibernate.ejb.HibernatePersistence
        
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
        
    

3. 建立基础bean

package com.jpa.beans;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Person {

    @Id @GeneratedValue
    private Integer id;
    private String name;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Person(String name) {
        super();
        this.name = name;
    }
    public Person() {
        super();
        // TODO Auto-generated constructor stub
    }
    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + "]";
    }
    
}

4.建立启动类

package com.jpa;

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

import com.jpa.beans.Person;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
    	EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistence-unit_demo");
        EntityManager manager = factory.createEntityManager();
        manager.getTransaction().begin();
        manager.persist(new Person("小天天"));
        manager.getTransaction().commit();
        manager.close();
        factory.close();
    }
}

5.log4j 配置文件(不知道是否有效)









    
        
        
            
        
    

    
        
        
        
        
            
        

    

    
        
        
        
    

右键启动类应该就可以跑起来了!!! 抄了一部分别人的博客不过也基本上记不住是谁的了,答谢一下。JPA相关视频谷粒学院教程

 

 

你可能感兴趣的:(Maven JPA 环境的简单搭建)