`

JPA(HIberante)环境的搭建

阅读更多

1.添加jar包

hibernate & jpa jar(15):
	hibernate-distribution-3.3.1.GA/hibernate3.jar
	hibernate-distribution-3.3.1.GA/lib/required/antlr-2.7.6.jar
	hibernate-distribution-3.3.1.GA/lib/required/dom4j-1.6.1.jar
	hibernate-distribution-3.3.1.GA/lib/required/commons-collections-3.1.jar
	hibernate-distribution-3.3.1.GA/lib/required/jta-1.1.jar
	hibernate-distribution-3.3.1.GA/lib/required/javassist-3.4.GA.jar
	hibernate-distribution-3.3.1.GA/lib/required/slf4j-api-1.5.2.jar
	hibernate-distribution-3.3.1.GA/lib/bytecode/cglib/hibernate-cglib-repack-2.1_3.jar
	hibernate-distribution-3.3.1.GA/lib/optional/ehcache-1.2.jar
	
	hibernate-annotations-3.4.0.GA/hibernate-annotations.jar
	hibernate-annotations-3.4.0.GA/lib/hibernate-commons-annotations.jar
	hibernate-annotations-3.4.0.GA/lib/test/slf4j-log4j12.jar
	hibernate-annotations-3.4.0.GA/lib/test/log4j.jar
	hibernate-annotations-3.4.0.GA/lib/ejb3-persistence.jar

	hibernate-entitymanager-3.4.0.GA/lib/hibernate-entitymanager.jar

 2.在src创建META-INF文件夹,在其下面创建persistence.xml文件,其内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<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="ssj_unit" transaction-type="RESOURCE_LOCAL">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<properties>
			<!--
			<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
			<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
			<property name="hibernate.connection.username" value="yxuser" />
			<property name="hibernate.connection.password" value="yxuser" />
			<property name="hibernate.connection.url" value="jdbc:oracle:thin:@192.168.1.96:1521:yxdb" />
			-->
			<!-- 连接参数 -->
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
			<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/ssj?useUnicode=true&amp;characterEncoding=UTF-8" />
			
			<!-- 数据源设置 -->
			<property name="hibernate.max_fetch_depth" value="3" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
			<property name="hibernate.jdbc.fetch_size" value="50" />
			<property name="hibernate.jdbc.batch_size" value="10" />
			<property name="hibernate.show_sql" value="true" />
			<property name="hibernate.format_sql" value="true" />
		</properties>
	</persistence-unit>

</persistence>

 3.创建实体类,其内容如下:

package com.taoistwar.ssj.producty.entity;

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

@Entity
public class ProductType {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;
	private String name;

	public ProductType() {
		super();
	}

	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;
	}

}

 4.创建测试类

package test.producty.entity;

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

import org.junit.Test;

import com.taoistwar.ssj.producty.entity.ProductType;

public class ProductTypeTest {

	@Test
	public void testRun() {
		try {
			EntityManagerFactory emf =  Persistence
					.createEntityManagerFactory("ssj_unit");
			EntityManager em = emf.createEntityManager();
			em.getTransaction().begin();
			ProductType pt = new ProductType();
			pt.setName("中文");
			em.persist(pt);
			em.getTransaction().commit();
			em.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
 

 

 

 

 

分享到:
评论

相关推荐

    spring4+hiberante4+struts2+maven

    Hibernate 4则增强了性能和并发处理能力,同时提供了更丰富的API和JPA 2.1的支持。Struts2则在安全性、性能和可扩展性上进行了改进。 Maven是这个项目中的构建工具,它管理项目的依赖关系,提供了一种标准的方式来...

    Hiberante lib

    总的来说,“Hiberante lib”压缩包提供了搭建和学习Hibernate环境的基础组件,对于想要进入Java ORM领域的开发者来说,这是一个很好的起点。通过这个压缩包,你可以开始探索和体验Hibernate的强大功能,以及它如何...

    疯狂Ajax讲义+Spring+hiberante

    5. **事务管理**:Hibernate整合了JTA(Java Transaction API)和JPA(Java Persistence API)的事务处理,确保数据操作的原子性和一致性。 这些知识点结合在一起,可以帮助开发者构建高效、灵活且易于维护的Web...

    带有jpa的spring-boot-web-web-with-jpa:

    前者包含了JPA和Hiberante,后者则提供了Spring Web MVC框架。 ```xml &lt;groupId&gt;org.springframework.boot &lt;artifactId&gt;spring-boot-starter-data-jpa &lt;groupId&gt;org.springframework.boot &lt;artifactId&gt;...

    eclipse hibernte Tools 插件

    通过Hibernate Tools,开发者可以自动生成包含getter和setter方法、构造函数以及JPA注解的实体类。这极大地减少了手动编写代码的工作量,提高了开发效率。 四、映射文件编辑 该插件提供了一个可视化的映射文件编辑...

    s2.1.8+spring2.5.6+hiberante3.3.2+dwr2.0.6

    通过这些配置和代码,开发者可以快速搭建一个功能完备的Java Web应用,并且能够根据需求进行扩展和定制。这个整合方案虽然现在已被更新的技术栈取代,但对于理解Web开发的基本架构和原理仍然具有重要的学习价值。

    SpringBootDemo (IEDA)

    4. **数据库连接**:Spring Data JPA和Hiberante是常见的ORM框架,它们可以帮助开发者与数据库交互。Spring Boot自动配置了DataSource和JPA,只需添加对应的依赖,即可实现数据库操作。 5. **日志**:Spring Boot...

    springboot + vue实现博客系统.zip

    2. **数据持久化**:通过集成JPA(Java Persistence API)和Hiberante,SpringBoot能够方便地操作数据库,如MySQL、MongoDB等。可以使用@Entity定义模型类,@Repository注解仓库接口,实现数据的CRUD操作。 3. **...

Global site tag (gtag.js) - Google Analytics