Maven Spring JPA

None-E 发布于 2016/11/18 17:43
阅读 237
收藏 1

我在spring中写了如下配置:


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

         xmlns:context="http://www.springframework.org/schema/context"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="

          http://www.springframework.org/schema/beans

          http://www.springframework.org/schema/beans/spring-beans.xsd

          http://www.springframework.org/schema/context

          http://www.springframework.org/schema/context/spring-context.xsd">

  

<context:component-scan base-package="com.clt"/>


<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

        <property name="locations">

            <list>

               <value>classpath:properties/*.properties</value>

                <!--multiple files, insert here -->

            </list>

        </property>

    </bean>


    <!-- dataSource Config -->

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="${mysql_driverClassName}" />

        <property name="url" value="${mysql_url}" />

        <property name="username" value="${mysql_username}" />

        <property name="password" value="${mysql_password}" />

    </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="persistenceUnitName" value="mysql"/>

    <property name="persistenceProvider" ref="persistenceProvider"/>

    <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>

    <property name="jpaDialect" ref="jpaDialect"/>

    <property name="jpaProperties">

        <props>

            <prop key="hibernate.format_sql">true</prop>

                <prop key="hibernate.show_sql">true</prop>

        </props>

    </property>

</bean>


<bean id="persistenceProvider" class="org.hibernate.ejb.HibernatePersistence" />

<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">

<property name="generateDdl" value="false" />

<property name="database" value="HSQL"/>

</bean>

<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>


<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">

        <property name="entityManagerFactory" ref="entityManagerFactory"/>

    </bean>

    <jpa:repositories base-package="footmark.springdata.jpa.dao" entity-manager-factory-ref="entityManagerFactory" 

transaction-manager-ref="transactionManager"/> 

</beans>

jpa persistence配置文件:

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

<!-- <persistence-unit name="CLT-Repository">


</persistence-unit> -->

<persistence-unit name="mysql" transaction-type="RESOURCE_LOCAL">

<class>com.clt.repository.pojo.BasicConfig</class>

<class>com.clt.repository.pojo.Button</class>

</persistence-unit>

</persistence>


dao 的一个普通类:

package com.clt.repository.dao.impl;


import javax.persistence.EntityManager;

import javax.persistence.PersistenceContext;


import org.springframework.stereotype.Repository;


import com.clt.repository.dao.BasicConfigDao;

import com.clt.repository.pojo.BasicConfig;


@Repository

public class BasicConfigDaoImpl implements BasicConfigDao {


@PersistenceContext  

    private EntityManager em;


public BasicConfigDaoImpl() {


// EntityManagerFactory factory = Persistence.createEntityManagerFactory("mysql");

// EntityManager em = factory.createEntityManager();

// em.getTransaction().begin();

// em.persist(new BasicConfig());

// em.getTransaction().commit();

// em.close();

// factory.close();

System.out.println(em);

}


public static void main(String[] args) {

BasicConfigDaoImpl bcdi = new BasicConfigDaoImpl();

System.out.println(bcdi.findByWechatAccount("gh_c55f730e90a2"));

}


@Override

public BasicConfig save(BasicConfig bc) {

em.persist(bc);

return bc;

}


@Override

public BasicConfig findByWechatAccount(String wechatAccount) {

return (BasicConfig) em.createNamedQuery(wechatAccount);

}


}


在运行时em总是报null。搞了一下午也没搞清楚。。。

加载中
返回顶部
顶部