JPA入门(openJPA)

阅读更多

1.环境搭建:

  1.1 JDK 1.6.0.22的下载地址:http://java.sun.com

  1.2 Eclipse IDE for Java EE Developers 3.6.2的下载地址:http://www.eclipse.org/downloads/

  1.3 OpenJPA 2.1.0的下载地址:http://openjpa.apache.org/downloads.html

  1.4 Mysql5.5.9的下载地址:http://dev.mysql.com/downloads/

  1.5 SQuirreL SQL ClientMysql client2.6.4 http://squirrel-sql.sourceforge.net/

  1.6 Apache Tomcat6.0.26的下载地址:http://tomcat.apache.org/download-60.cgi

2 Eclipse配置

2.1 配置Configure User Libraries for JPA


JPA入门(openJPA)_第1张图片
 

2.2 配置数据库连接


JPA入门(openJPA)_第2张图片

3 创建新的JPA Project


JPA入门(openJPA)_第3张图片
 


JPA入门(openJPA)_第4张图片
 


JPA入门(openJPA)_第5张图片
 

4 创建数据库和表

  4.1.SQuirreL SQL Client 创建数据库

 

create database jpadb;

 

 


JPA入门(openJPA)_第6张图片

 

  4.2.编写创建表sql语句:createtable.sql

 

drop table if exists yan_user;
Create TABLE yan_user(
  ID INT NOT NULL AUTO_INCREMENT,
  NAME VARCHAR(20) NOT NULL,
  LOGINID VARCHAR(20) NOT NULL,
  PASSWORD VARCHAR(32) NOT NULL,
  AGE INT NOT NULL,
  PRIMARY KEY(ID)
);

 

 


JPA入门(openJPA)_第7张图片

 


右键:执行所有,eclipse执行sql语句在数据库中创建表完成。


JPA入门(openJPA)_第8张图片
5.Java代码实现


JPA入门(openJPA)_第9张图片
 

5.1

 

/**
 *  Copyright(c) 2010-2011 Yan.Dev. All Rights Reserved.
 */
package com.yan.dev.jpa.dao;

import com.yan.dev.jpa.entity.User;

/**
 * Comment for IUserDao.java
 *
 * @author yan.dev
 * 
 * @blog:yan.dev's Blog
 * @version 1.0
 * @time 2011-3-24 下午05:06:31
 */
public interface IUserDao {
	public void save(User user) throws Exception;

}
 

 

5.2

 

/**
 *  Copyright(c) 2010-2011 Yan.Dev. All Rights Reserved.
 */
package com.yan.dev.jpa.dao;

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

import com.yan.dev.jpa.entity.User;

/**
 * Comment for UserDaoImpl.java
 * 
 * @author yan.dev
 * 
 * @blog:yan.dev's Blog
 * @version 1.0
 * @time 2011-3-24 下午05:07:34
 */
public class UserDaoImpl implements IUserDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yan.dao.IUserDao#save(com.yan.entity.User)
	 */
	public void save(User user) throws Exception {
		EntityManagerFactory factory = Persistence
				.createEntityManagerFactory("FirstJPA");
		EntityManager em = factory.createEntityManager(); 
		EntityTransaction t = em.getTransaction();
		t.begin();
		try {
			em.persist(user);
			t.commit();
		} catch (Exception e) { 
			e.printStackTrace();
			t.rollback();
			throw e;
		} finally { 
			em.close();
			factory.close();
		}
	}

}

 

 

5.3

 

/**
 *  Copyright(c) 2010-2011 Yan.Dev. All Rights Reserved.
 */
package com.yan.dev.jpa.entity;

import static javax.persistence.GenerationType.IDENTITY;

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

/**
 * Comment for User.java
 *
 * @author yan.dev
 * 
 * @blog:yan.dev's Blog
 * @version 1.0
 * @time 2011-3-24 下午07:01:33
 */
@Entity
@Table(name="yan_user")
public class User {
    public User(){
    	
    }
    @Id
    @GeneratedValue(strategy=IDENTITY) 
	private int ID;
    
    @Column(name = "NAME")
    private String name;
    
    @Column(name = "LOGINID")
    private String loginId;
    
    @Column(name = "PASSWORD")
    private String password;
    
    @Column(name = "AGE")
    private int age;

    public int getId() {
        return ID;
    }

    public String getName() {
        return name;
    }

    public String getLoginId() {
        return loginId;
    }

    public String getPassword() {
        return password;
    }

    public int getAge() {
        return age;
    }

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

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

    public void setLoginId(String loginId) {
        this.loginId = loginId;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setAge(int age) {
        this.age = age;
    }    
}

 

 

5.4

 

/**
 *  Copyright(c) 2010-2011 Yan.Dev. All Rights Reserved.
 */
package com.yan.dev.jpa.service;

import com.yan.dev.jpa.entity.User;

/**
 * Comment for IUserService.java
 *
 * @author yan.dev
 * 
 * @blog:yan.dev's Blog
 * @version 1.0
 * @time 2011-3-24 下午05:17:01
 */
public interface IUserService {
	public boolean save(User user);

}

 

 

5.5

 

/**
 *  Copyright(c) 2010-2011 Yan.Dev. All Rights Reserved.
 */
package com.yan.dev.jpa.service;

import com.yan.dev.jpa.dao.IUserDao;
import com.yan.dev.jpa.dao.UserDaoImpl;
import com.yan.dev.jpa.entity.User;

/**
 * Comment for UserServiceImpl.java
 * 
 * @author yan.dev
 * 
 * @blog:yan.dev's Blog
 * @version 1.0
 * @time 2011-3-24 下午05:52:11
 */
public class UserServiceImpl implements IUserService {
	private IUserDao dao;

	public UserServiceImpl() {
		dao = new UserDaoImpl();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yan.service.IUserService#save(com.yan.entity.User)
	 */
	public boolean save(User user) {
		try {
			dao.save(user);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}

}

 

5.6.测试类的编写

 

/**
 *  Copyright(c) 2010-2011 Yan.Dev. All Rights Reserved.
 */
package com.yan.dev.jpa.test;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.yan.dev.jpa.entity.User;
import com.yan.dev.jpa.service.IUserService;
import com.yan.dev.jpa.service.UserServiceImpl;

/**
 * Comment for Testcase.java
 * 
 * @author yan.dev
 * 
 * @blog:yan.dev's Blog
 * @version 1.0
 * @time 2011-3-24 下午04:52:15
 */
public class Testcase {
	private IUserService service;

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		service=new UserServiceImpl();
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
		service=null;
	}

	@Test
	public void save() {
		User user=new User();
		user.setName("yan.dev2");
		user.setLoginId("yan2");
		user.setPassword("dev2");
		user.setAge(99);
		
		Assert.assertEquals(service.save(user), true);

	}

} 

  

6.persistence.xml的修改

 



	
		
		org.apache.openjpa.persistence.PersistenceProviderImpl
		
		com.yan.dev.jpa.entity.User
		
		
			
			
			
			
		
	

 

 

 

7.添加运行参数

VM arguments位置添加AspectJ的参数

 

-javaagent: …/openjpa-all-2.1.0.jar(…为openjpa的位置)

 


8.注意事项

创建表时注意大小写

运行时参数的设置

实体类中id的注释

SquirreL配置时,需要把mysql connetor驱动索引正确

 

  • JPA入门(openJPA)_第10张图片
  • 大小: 81.2 KB
  • JPA入门(openJPA)_第11张图片
  • 大小: 97.9 KB
  • JPA入门(openJPA)_第12张图片
  • 大小: 43.8 KB
  • JPA入门(openJPA)_第13张图片
  • 大小: 43.3 KB
  • JPA入门(openJPA)_第14张图片
  • 大小: 18.6 KB
  • JPA入门(openJPA)_第15张图片
  • 大小: 27.5 KB
  • JPA入门(openJPA)_第16张图片
  • 大小: 33.2 KB
  • JPA入门(openJPA)_第17张图片
  • 大小: 29.6 KB
  • JPA入门(openJPA)_第18张图片
  • 大小: 31.1 KB
  • 查看图片附件

你可能感兴趣的:(JPA,MySQL,DAO,Eclipse,SQL)