配置mysql,persistence.xml,JPA,SpringMVC

时间:2015-08-09 15:17:39

标签: java mysql spring spring-mvc jpa

我是javaEE的新手,使用 Spring MVC创建项目,使用JPA和Mysql作为数据库,Jboss作为服务器。 我被困在配置中,请在下面找到我遵循的步骤

1:customerdiatatcher-servlet.xml(保留在WebContent 中)

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

<context:property-placeholder location="classpath:jdbc.properties" />
<import resource="persistence.xml" />

<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />

<!-- Scans the classpath for annotated components that will be auto-registered 
    as Spring beans. For example @Controller and @Service. Make sure to set the 
    correct base-package -->
<context:component-scan base-package="com.vivek" />

<!-- declare a view resolver property -->
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" />

<!-- Add Transaction support -->
<bean id="myTxManager"
    class="org.springframework.transaction.jta.JtaTransactionManager" />

<!-- Use @Transaction annotations for managing transactions -->
<tx:annotation-driven transaction-manager="myTxManager" />

2:Perisistence.xml(保留在../ META-INF / persistence.xml 中)

<?xml version="1.0" encoding="UTF-8"?><persistence version="2.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_2_0.xsd">
<persistence-unit name="CustomerDetailsPU"
    transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>java:jboss/datasources/jpa-datasource.xml</jta-data-source><class>com.vivek.model.Customer</class>
</persistence-unit></persistence>

3:创建了一个datasource-jpa-datasource.xml(在WebContent内部

<?xml version="1.0" encoding="UTF-8"?><datasources xmlns="http://www.jboss.org/ironjacamar/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
<!-- The datasource is bound into JNDI at this location. We reference this 
    in META-INF/persistence.xml -->
<datasource jndi-name="java:jboss/datasources/jpa-datasource.xml"
    pool-name="sample-pool" enabled="true" use-java-context="true" jta="true">
    <connection-url>jdbc:mysql://localhost:3306/customerdb</connection-url>
     <driver-class>com.mysql.jdbc.Driver</driver-class>
    <driver>mysql</driver>
    <security>
        <user-name>root</user-name>
        <password>root</password>
    </security>
</datasource>

问题是我不知道我的方法是否正确,或者我的所有文件和配置是否正确当我部署此服务器时,它显示了很多错误。 请检查我是否在所有xmls中都写了正确的代码,否则我会遗漏一些东西。

0 个答案:

没有答案