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="itdos" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
         
         <property name="hibernate.max_fetch_depth" value="3"/>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
         <property name="hibernate.jdbc.fetch_size" value="18"/>
         <property name="hibernate.jdbc.batch_size" value="10"/>
         <property name="hibernate.show_sql" value="false" />      
     <property name="hibernate.format_sql" value="false" />  
      </properties>
  </persistence-unit>
</persistence>
-------------------------------------------------------------------beans。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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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">
           <context:component-scan base-package="net.itdos"/>
 <context:annotation-config/>
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/shop?useUnicode=true&amp;characterEncoding=UTF-8"/>
    <property name="username" value="root"/>
    <property name="password" value="123"/>
     <!-- 连接池启动时的初始值 -->
 <property name="initialSize" value="1"/>
 <!-- 连接池的最大值 -->
 <property name="maxActive" value="500"/>
 <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
 <property name="maxIdle" value="2"/>
 <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
 <property name="minIdle" value="1"/>
  </bean>
  
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
     <property name="dataSource" ref="dataSource"/>
     <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
     <property name="loadTimeWeaver">
      <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
     </property>

</bean>
<!--  配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
   <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!--  打开事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
-----------------------------------------------
package net.itdos.bean.product;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 typeid; public Integer getTypeid() {
return typeid;
} public void setTypeid(Integer typeid) {
this.typeid = typeid;
}
}
---------------------------------------------------------------
package net.itdos.service.product.impl;import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;import net.itdos.bean.product.ProductType;
import net.itdos.service.product.ProductService;import org.springframework.stereotype.Service;@Service
public class ProductServiceBean implements ProductService {
@PersistenceContext EntityManager em;
public void save(ProductType type){
em.persist(type);
}
}
---------------------------------------
package test;import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.sql.DataSource;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import net.itdos.bean.product.ProductType;
import net.itdos.service.product.ProductService;public class tset { /**
 * @param args
 */
public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
DataSource d=(DataSource)ctx.getBean("dataSource");
System.out.print(d);
ProductService productService=(ProductService)ctx.getBean("productServiceBean");
productService.save(new ProductType());
System.out.print("djshfuk");
}}