zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    前端-微信公众号开发(jssdk)
    wampserver 配置本地环境局域网内pc移动访问
    客户端缓存之localStorage and sessionStorage
    webpack+vue搭建基础
    高德地图javascriptAPI基本使用心得(下)
    高德地图javascriptAPI基本使用心得(上)
    表单单选多选项清除默认样式小技巧
    (原创)C#零基础学习笔记003-流程控制语句
    (原创)C#零基础学习笔记002-表达式与运算符
    (原创)C#零基础学习笔记000-学习结构
  • 原文地址:https://www.cnblogs.com/feifeiyun/p/6934485.html
Copyright © 2011-2022 走看看