Spring整合Jpa配置

1.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:redis="http://www.springframework.org/schema/redis"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
		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
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
		http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis-1.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- Spring IOC容器 扫描 base-package 注解:@Repository @Service @Controller @Component-->
	<context:component-scan base-package="com.wise.tiger" use-default-filters="true">
		<!-- exclude-filter是针对include-filter里的内容进行排除 -->
		<context:exclude-filter type="annotation" expression="com.wise.tiger.controller" />
	</context:component-scan>

	<!--添加DataSource的配置文件路径-->
	<context:property-placeholder location="classpath:pool-config.properties"/>  

	<!--向IoC容器注入DataSource-->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
	  <property name="driverClassName" value="${driver}" />
	  <property name="url" value="${url}" />
	  <property name="username" value="${user}" />
	  <property name="password" value="${password}" />
	  <property name="initialSize" value="5" />
	  <property name="minIdle" value="3" />
	  <property name="maxActive" value="10" />
	  <property name="maxWait" value="10000" />
	  <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
	  <property name="timeBetweenEvictionRunsMillis" value="60000" />
	  <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
	  <property name="minEvictableIdleTimeMillis" value="300000" />
	  <property name="testWhileIdle" value="true" />
	  <!-- 这里建议配置为TRUE,防止取到的连接不可用 -->
	  <property name="testOnBorrow" value="true" />
	  <property name="testOnReturn" value="false" />
	  <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
	  <property name="poolPreparedStatements" value="true" />
	  <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
	  <!-- 这里配置提交方式,默认就是TRUE,可以不用配置 -->
	  <property name="defaultAutoCommit" value="fasle" />
	  <!-- 配置监控统计拦截的filters -->
	  <property name="filters" value="stat" />
	</bean>

	<!-- 配置JPA适配器-->
	<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    </bean>

    <!-- 定义实体管理器工厂 Jpa配置 LocalContainerEntityManagerFactoryBean这个选项Spring扮演了容器的角色。完全掌管JPA -->  
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
        <!-- 指定数据源 -->  
        <property name="dataSource" ref="dataSource"/>  
        <!-- 指定Jpa持久化实现厂商类,这里以Hibernate为例 -->  
        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>  
        <!-- 指定Entity实体类包路径 -->  
        <property name="packagesToScan" value="com.wise.tiger.domain"></property>  
        <!-- 指定JPA属性;如Hibernate中指定是否显示SQL的是否显示、方言等 -->  
        <property name="jpaProperties">  
            <props>  
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</prop>  
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>  
                <prop key="hibernate.show_sql">true</prop>  
                <prop key="hibernate.format_sql">true</prop>  
                <prop key="hibernate.hbm2ddl.auto">update</prop>  
            </props>  
        </property>  
    </bean>  

    <!-- 事务管理 -->
	<bean id="transactionManager"  class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory"></property>
	</bean>

    <!--XML配置事务声明方式 开启注解声明事务 -->
 	<tx:annotation-driven transaction-manager="transactionManager" />
</beans>

2. 注解配置

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.HashMap;

@Configuration
@ComponentScan(basePackages = "com.wise.tiger")
@PropertySource(value = "classpath:pool-config.properties",encoding = "UTF-8")
@EnableTransactionManagement
public class ApplicationConfig {
    /**
     * 配置数据源
     * @param driverClassName
     * @param url
     * @param username
     * @param password
     * @param connectionProperties
     * @param isAutoCommit
     * @return
     */
    @Bean
    public DataSource dataSource(@Value("${driverClassName}") String driverClassName,
                                 @Value("${url}") String url,
                                 @Value("${jdbc.username}") String username,
                                 @Value("${password}") String password,
                                 @Value("${connectionProperties}") String connectionProperties,
                                 @Value("${defaultAutoCommit}") boolean isAutoCommit){
        var dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDefaultAutoCommit(isAutoCommit);
        dataSource.setConnectionProperties(connectionProperties);
        return dataSource;
    }

    /**
     * 配置事务工厂
     * @param dataSource
     * @return
     */
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Autowired DataSource dataSource){
        var entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactory.setDataSource(dataSource);
        entityManagerFactory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        entityManagerFactory.setPackagesToScan("com.wise.tiger.domain");
        var propertiesMap = new HashMap<String,String>();
        propertiesMap.put("hibernate.dialect","org.hibernate.dialect.MySQL8Dialect");
        propertiesMap.put("hibernate.show_sql","true");
        propertiesMap.put("hibernate.hbm2ddl.auto","update");
        entityManagerFactory.setJpaPropertyMap(propertiesMap);
        return entityManagerFactory;
    }

    /**
     * 配置事务管理器
     * @param factory
     * @return
     */
    @Bean
    public JpaTransactionManager txManager(EntityManagerFactory factory){
        return new JpaTransactionManager(factory);
    }
}

转载于:https://my.oschina.net/u/4134962/blog/3060825

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值