• jpa-addSpring


    导包

      1.hibernate中必须的包

      2.hibernate中c3p0的包

      3.hibernate中jpa的包

      4.mysql数据库驱动包

      5.spring中必须的包

    整合:三点特别重要

      1.applicationContext.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:tx="http://www.springframework.org/schema/tx"
        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-4.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    
    <!-- 1.扫描出db.properties -->
        <context:property-placeholder location="classpath:db.properties"/>
        
    <!-- 扫描基于注解的bean -->
        <context:component-scan base-package="springs"></context:component-scan>
        
    <!-- 配置dataSource,使用的是c3p0 -->
        <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
            <property name="jdbcUrl" value="${jdbc.url}"></property>
            <property name="user" value="${jdbc.user}"></property>
            <property name="password" value="${jdbc.password}"></property>
            <property name="driverClass" value="${jdbc.driverClass}"></property>
        </bean>
        
    <!-- 配置entityManagerFactory -->
        <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
        
            <!-- 添加dataSource -->
            <property name="dataSource" ref="dataSource"></property>
            
            <!-- 扫描的包,此包为实体类的包 -->
            <property name="packagesToScan" value="entities"></property>
            
            <!-- 为jpa提供适配的类 -->
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
            </property>
            
            <!-- 一些jpa的配置 -->
            <property name="jpaProperties">
                <props>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">true</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                </props>
            </property>
            
        </bean>
        
    <!-- 配置jpa的事务管理器 -->
        <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory"></property>
        </bean>
        
    <!-- 配置基于注解的事务 -->
        <tx:annotation-driven transaction-manager="transactionManager"/>
    
    
    </beans>

      2.dao类中特别重要的一点:要使用@PersistenceContext来获取线程同步的entityManager

    package springs;
    
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    
    import org.springframework.stereotype.Repository;
    
    import entities.XiaoYu;
    
    @Repository
    public class XiaoYuDAO {
        @PersistenceContext//加这个注解获取线程同步的manager
        private EntityManager entityManager;
    
        public void save(XiaoYu xiaoYu) {
            System.out.println(entityManager);
            entityManager.persist(xiaoYu);
        }
    }

      3.service中的一点:使用@Transaction来注解事务的方法,而dao中得到的entityManager只有在事务的方法中才能发送sql语句

    package springs;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    import entities.XiaoYu;
    
    @Service
    public class XiaoYuService {
        @Autowired
        private XiaoYuDAO dao;
    
        @Transactional//这句特别重要!!!!!!!!!!没有它就不能发sql
        public void saveDpuble(XiaoYu xiaoYu1, XiaoYu xiaoYu2) {//这个方法里面的是一个事务
            dao.save(xiaoYu1);
            dao.save(xiaoYu2);
        }
    }
  • 相关阅读:
    JavaSE教程-01初识Java-思维导图
    theano 模块 MLP示例
    EM理解(转)
    交叉验证(Cross Validation)方法思想简介
    imfilter()用法
    PSNR
    conv2、filter2、imfilter的区别
    图像上采样(图像插值)增取样(Upsampling)或内插(Interpolating)下采样(降采样),
    CSS清除浮动大全共8种方法
    支付宝轮播
  • 原文地址:https://www.cnblogs.com/feifeiyun/p/6934485.html
Copyright © 2020-2023  润新知