Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

绿色网站无广告
会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux集群 > Architecture > » 正文

jpa规范下的shh2配置要点

来源: mimimhmh 分享至:

必要包:

antlr-2.7.6.jar
aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.1_3.jar
common-annotations.jar
commons-beanutils-1.7.0.jar
commons-chain-1.2.jar
commons-collections-3.1.jar
commons-dbcp.jar
commons-digester-2.0.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang-2.5.jar
commons-logging-1.1.1.jar
commons-logging-api-1.1.jar
commons-logging.jar
commons-pool.jar
commons-validator-1.3.1.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
ejb3-persistence.jar
freemarker-2.3.16.jar
hibernate-annotations.jar
hibernate-cglib-repack-2.1_3.jar
hibernate-commons-annotations.jar
hibernate-entitymanager.jar
hibernate3.jar
jta-1.1.jar
log4j.jar
ognl-3.0.1.jar
oro-2.0.8.jar
slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.3.jar
spring.jar
struts-core-1.3.10.jar
struts2-core-2.2.3.1.jar
struts2-spring-plugin-2.2.3.1.jar
tiles-api-2.0.6.jar
tiles-core-2.0.6.jar
tiles-jsp-2.0.6.jar
xwork-core-2.2.3.1.jar
mysql-connector-java-5.1.7-bin.jar
javassist-3.7.ga.jar
c3p0-0.9.1.jar
javassist-3.12.0.GA.jar

spring的applicationContext.xml配置:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
			http://www.springframework.org/schema/context  
    		http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<context:annotation-config />
	<context:component-scan base-package="wxm" />
	<bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
		<property name="persistenceUnitName" value="wxm" />
	</bean>

	<bean id="txManager"
		class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory"
			ref="entityManagerFactory" />
	</bean>
	<tx:annotation-driven transaction-manager="txManager" />

</beans>

将META-INF文件夹放入classpath,并保存persistence.xml如下配置:
	
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
	xmlns:persistence="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 persistence_1_0.xsd ">

	<!-- 
		Name属性用于定义持久化单元的名字 (name必选,空值也合法); 
		transaction-type 指定事务类型(可选)(JTA or RESOURCE_LOCAL) 
	-->
	<persistence-unit name="wxm" transaction-type="RESOURCE_LOCAL">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<properties>
			<property name="hibernate.connection.provider_class"
				value="org.hibernate.connection.C3P0ConnectionProvider" />
			<property name="hibernate.dialect"
				value="org.hibernate.dialect.MySQL5Dialect" /><!--方言 -->
			<property name="hibernate.connection.driver_class"
				value="org.gjt.mm.mysql.Driver" /><!--jdbc 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/ssh2?useUnicode=true&characterEncoding=UTF-8" />
			<!--数据库连接地址 -->
			<!-- hibernate.hbm2ddl.auto= create-drop / create / update -->
			<property name="hibernate.hbm2ddl.auto" value="update" />
			<property name="hibernate_max_fetch_depth" value="3" />
			<property name="hibernate.show_sql" value="false" />
		</properties>
	</persistence-unit>
</persistence>


在服务层使用的一个例子(注解方式):

package wxm.service.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import wxm.beans.Employee;
import wxm.service.EmployeeService;

@Service
@Transactional
public class EmployeeServiceBean implements EmployeeService {

	@PersistenceContext
	private EntityManager em;

	@Override
	public void delete(Integer... ids) {
		for (Integer id : ids) {
			em.remove(em.getReference(Employee.class, id));
		}
		System.out.println("删除成功");
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Employee find(Integer id) {
		return em.find(Employee.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Employee> list() {
		return em.createQuery("from Employee").getResultList();

	}

	@Override
	public void save(Employee employee) {
		em.persist(employee);
		System.out.println("保存成功");
	}

	@Override
	public void update(Employee employee) {
		em.merge(employee);
		System.out.println("更新成功");
	}

}



Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史