openJPA1.2.2使用Tomcat6.0.29数据源

Tomcat6.0.29的数据源有2种

1. System wide:在tomcat里的所有webapp都可以使用
    全局的数据源,要配置 %TOMCAT_HOME%/conf/context.xml,同时要把数据库的jdbc驱动,拷贝到%TOMCAT_HOME%/lib下面.
2. WebApplication wide:只有指定的webapp可以使用
    web应用数据源,要配置 你的项目下的 META-INF/context.xml,同时拷贝到驱动到项目下的WEB-INF/lib下
  
下面是具体的配置文件,不做过多的解释(2种方式)
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
	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">
	<persistence-unit name="MyJPA" transaction-type="RESOURCE_LOCAL">
		<description>测试openjpa</description>
		<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
		<non-jta-data-source>java:comp/env/jdbc/net</non-jta-data-source>
		<class>entity.Animal</class>
	</persistence-unit>
</persistence>
<!--
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
	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">
	<persistence-unit name="MyJPA" transaction-type="RESOURCE_LOCAL">
		<description>测试openjpa</description>
		<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
		<class>entity.Animal</class>
		<properties>
			<property name="openjpa.jdbc.DBDictionary" value="mysql" />
			<property name="openjpa.ConnectionFactoryName" value="java:comp/env/jdbc/MyDataSource" />
			<property name="openjpa.Log" value="log4j" />
		</properties>
	</persistence-unit>
</persistence>
-->

context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Resource  
	name="jdbc/net" 
	type="javax.sql.DataSource" 
	maxActive="5" 
	maxIdle="5"  
	maxWait="5000" 
	username="root"  
	password="root"  
	driverClassName="com.mysql.jdbc.Driver"  
	url="jdbc:mysql://192.168.64.2/test" 
/>
</Context>

Index.jsp----其中,前半部分为测试访问jndi数据源代码,后半部分为使用jpa的代码
<%@ page language="java" contentType="text/html; charset=utf8"
	pageEncoding="utf8"%>
<%@ page import="java.util.*"%>
<%@ page import="javax.persistence.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>

<%@ page import="entity.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<title>Insert title here</title>
</head>
<body>
<%!
	final String JNDINAME = "java:comp/env/jdbc/net" ;
%>
<%
Connection conn = null ;
try
{
	Context ctx = new InitialContext() ;
	DataSource ds = (DataSource)ctx.lookup(JNDINAME) ;
	conn = ds.getConnection() ;
}
catch(Exception e)
{
	System.out.println(e) ;
}
out.write(conn.toString());
conn.close() ;



	EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyJPA");
	EntityManager em = emf.createEntityManager();
	Query query = em.createQuery("select a from Animal a");
	List<Animal> results = query.getResultList();
	for (int i =0;i<results.size();i++) {
		Animal animal = results.get(i);
		out.write(animal.getName()+"<br/>");
	}
%>
</body>
</html>

你可能感兴趣的:(java,tomcat,xml,jdbc,jpa)