SSJ商城网站架设笔录 [壹]

一、环境配置

 

配置jpa的运行环境


一、提取jar


Hibernate-3.2.6.GA目录下hibernate3.jar以及lib目录下所有的jar.
hibernate-entitymanager-3.3.2.CR1目录下hibernate-entitymanager.jar以及lib目录下的三个jar.

拷贝到WebRoot的WEB-INF的lib的目录下。
jpa所需要用的jar已经拷贝完了。

 

二、MYSQL驱动


把mysql数据库驱动mysql-connector-java-5.0.5-bin.jar拷贝WebRoot的WEB-INF的lib的目录下.


三、在web项目的src目录下建立META-INF目录,并建立persistence.xml文件。

内容如下

 

<?xml version="1.0"?> <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="nbchinashop" transaction-type="RESOURCE_LOCAL"><!--transaction-type事务类型 --> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/> <!--若结合了JPA,则把如下四行去掉。因为已经使用JPA管理数据源--> <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="123456"/> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/nbchinashop?useUnicode=true&characterEncoding=UTF-8"/> <!--若结合了JPA,则把以上四行去掉。因为已经使用JPA管理数据源--> <property name="hibernate.hbm2ddl.auto" value="update"/> <!-- 值 update代表映射数据没有发生改变,而且表已经在数据库存在,不用更新表。当表不存在,在生成表。或当添加的新字段不存在的时候,则自动添加进去 --> <!-- 值 create-drop代表应用启动的时候建立表,关闭的时候删除表 --> <property name="hibernate.show_sql" value="false"/> <property name="hibernate.format_sql" value="false"/> <property name="hibernate.jdbc.fetch_size" value="18"/> <property name="hibernate.jdbc.batch_size" value="10"/> <property name="hibernate.max_fetch_depth" value="3"/> </properties> </persistence-unit> </persistence>

 

四、测试jpa环境

 

建立ProductType.java类

 

package com.nbchina.bean.product; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class ProductType { private Integer typeid; @Id @GeneratedValue(strategy=GenerationType.AUTO) public Integer getTypeid() { return typeid; } public void setTypeid(Integer typeid) { this.typeid = typeid; } }

 


建立测试ProductTest.java类,导入JUnit 4

 

package junit.test; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import org.junit.BeforeClass; import org.junit.Test; import com.nbchina.bean.product.ProductType; public class ProductTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } @Test public void runtest(){ EntityManagerFactory factory = Persistence.createEntityManagerFactory("nbchinashop"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); em.persist(new ProductType()); em.getTransaction().commit(); em.close(); factory.close(); } }

 

在数据库中建立nbchinashop数据库,运行测试,则能正常生成表。

 

Spring合成


一、提取jar


spring-framework-2.5.6/dist目录下spring.jar.
spring-framework-2.5.6/lib/aspectj目录下aspectjrt.jar、aspectjweaver.jar.  //切面编程
spring-framework-2.5.6/lib/cglib目录下cglib-nodep-2.1_3.jar //动态生成字节码
spring-framework-2.5.6/lib/j2ee目录下common-annotations.jar  //注解
spring-framework-2.5.6/lib/jakarta-commons目录下commons-dbcp.jar、commons-logging.jar、commons-pool.jar  //使用到了数据源

 

拷贝到WebRoot的WEB-INF的lib的目录下。
Spring所需要用的jar已经拷贝完了。

 

二、配置文件


在src目录下建立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="com.nbchina"/> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driverClassName}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> <property name="initialSize" value="${initialSize}"/> <property name="maxActive" value="${maxActive}"/> <property name="maxIdle" value="${maxIdle}"/> <property name="minIdle" value="${minIdle}"/> </bean> <!--Spring和Jpa结合--> <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> <!--Spring和Jpa结合--> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>

 

在src目录下建立jdbc.properties
内容如下

 

driverClassName=org.gjt.mm.mysql.Driver
url=jdbc:mysql://localhost:3306/nbchinashop?useUnicode=true&characterEncoding=UTF-8
username=root
password=123456
initialSize=1
maxActive=100
maxIdle=8
minIdle=1

 

三、测试JPA+Spring环境

1、建立ProductServiceBean.java

 

package com.nbchina.service.product.impl; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.nbchina.bean.product.ProductType; import com.nbchina.service.product.ProductService; @Service @Transactional public class ProductServiceBean implements ProductService { @PersistenceContext EntityManager em; public void save(ProductType type){ em.persist(type); } }

 

建立接口ProductService.java


package com.nbchina.service.product;

import com.nbchina.bean.product.ProductType;

public interface ProductService {

 public void save(ProductType type);

}

 

2、建立测试文件ProductJPASpringTest.java

 

package junit.test; //import javax.sql.DataSource; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.nbchina.bean.product.ProductType; import com.nbchina.service.product.ProductService; public class ProductJPASpringTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } @Test public void runtest(){ ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml"); //DataSource dataSource = (DataSource)cxt.getBean("dataSource"); //System.out.println(dataSource); //测试一,是否能取得dataSource ProductService productService = (ProductService)cxt.getBean("productServiceBean"); System.out.println(productService); productService.save(new ProductType()); } }

 

注意:需要把persistence.xml的数据连接部分给注释掉。

测试通过。

你可能感兴趣的:(spring,exception,jpa,Integer,Class,商城)