Spring JPA配置

2、修改配置文件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:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans.xsd 
						http://www.springframework.org/schema/tx 
						http://www.springframework.org/schema/tx/spring-tx.xsd" >
	
	<!-- bean post-processor for JPA annotations -->
  	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

	<import resource="applicationContext_db.xml"/>
	<import resource="applicationContext_bean.xml"/>
</beans>

applicationContext_db.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:p="http://www.springframework.org/schema/p"
	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.xsd 
						http://www.springframework.org/schema/tx 
						http://www.springframework.org/schema/tx/spring-tx.xsd
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	
	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
            <property name="persistenceUnitName" value="oracleDB"/>
   	</bean>
   	
   	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">  
            <property name="entityManagerFactory" ref="entityManagerFactory" />  
	</bean> 
	
	<tx:annotation-driven transaction-manager="transactionManager" /> 
</beans>

oracleDB指向persistence.xml配置文件中的数据库配置。

3、添加dao层
 
package com.yd4.struts2.dao;

import java.util.List;

import com.yd4.struts2.entity.Test;

/**
 * @author  作者:xiaoZhi
 * @version 创建时间:2013-12-6 下午03:22:23
 * @detail  
 **/
public interface TestDao {
	public List<Test> queryAllData();
}

4、添加Impl类

package com.yd4.struts2.dao.impl;

import java.util.List;

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

import org.springframework.transaction.annotation.Transactional;

import com.yd4.struts2.dao.TestDao;
import com.yd4.struts2.entity.Test;

/**
 * @author  作者:xiaoZhi
 * @version 创建时间:2013-12-6 下午03:44:28
 * @detail  
 **/
public class TestDaoImpl implements TestDao {
	@PersistenceContext
	private EntityManager em;
	
	@SuppressWarnings("unchecked")
	@Transactional
	public List<Test> queryAllData() {
		Query query = em.createQuery("from Test t");

		return query.getResultList(); 
	}

}

5、测试
package com.yd4.struts2.test;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.yd4.struts2.dao.TestDao;
import com.yd4.struts2.entity.Test;


/**
 * @author  作者:xiaoZhi
 * @version 创建时间:2013-12-6 下午04:07:05
 * @detail  
 **/
public class Main {
	public static void main(String[] args) {
		ApplicationContext cx = new FileSystemXmlApplicationContext("WebRoot\\WEB-INF\\applicationContext.xml");
		TestDao testDao = (TestDao) cx.getBean("testDao");
		
		List<Test> list = testDao.queryAllData();
		for (Test test : list) {
			System.out.println(test.getZuhao() + "\t" + test.getUser_name());
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值