Spring-data-jpa操作数据库环境配置
application.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:property-placeholder location="classpath*:properties/*.properties"/> //数据库连接参数
<context:component-scan base-package="com.jerry.service"/> //设置自动扫描service包
<context:annotation-config/> </beans>
applicationContext-dataSource.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"> <!--上面的xsd最好和当前使用的Spring版本号一致,如果换了Spring版本,这个最好也跟着改--> <!-- 使用阿里的druid配置数据源 start-->
<!--具体查看官网信息:https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_DruidDataSource%E5%8F%82%E8%80%83%E9%85%8D%E7%BD%AE-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!--这三个变量读取config.properties的-->
<property name="url" value="${data_source_url}"/>
<property name="username" value="${data_source_username}"/>
<property name="password" value="${data_source_password}"/> <!-- 初始化连接大小 -->
<property name="initialSize" value="1"/>
<!-- 初始化连接池最大使用连接数量 -->
<property name="maxActive" value="20"/>
<!-- 初始化连接池最小空闲 -->
<property name="minIdle" value="1"/> <!-- 获取连接最大等待时间,单位毫秒-->
<property name="maxWait" value="60000"/> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000"/> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<!--如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。分库分表较多的数据库,建议配置为false。-->
<property name="poolPreparedStatements" value="false" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <property name="validationQuery" value="${validation_query}"/>
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/> <!--当程序存在缺陷时,申请的连接忘记关闭,这时候,就存在连接泄漏了。Druid提供了RemoveAbandanded相关配置,用来关闭长时间不使用的连接-->
<!--配置removeAbandoned对性能会有一些影响,建议怀疑存在泄漏之后再打开。在上面的配置中,如果连接超过30分钟未关闭,就会被强行回收,并且日志记录连接申请时的调用堆栈。-->
<!--具体查看官网信息:https://github.com/alibaba/druid/wiki/%E8%BF%9E%E6%8E%A5%E6%B3%84%E6%BC%8F%E7%9B%91%E6%B5%8B-->
<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="true"/>
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="1800"/>
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true"/> <!-- 配置监控统计拦截的filters-->
<!--官网信息:https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_StatFilter-->
<!--mergeSql可以合并输出的sql,方便查看,但是在mybatis框架中使用这个则无法监控sql,需要用stat-->
<!--<property name="filters" value="mergeSql,log4j"/>-->
<!--<property name="filters" value="mergeSql,wall"/>-->
<!--<property name="filters" value="stat"/>-->
<!--<property name="filters" value="mergeSql"/>-->
<property name="filters" value="stat,log4j"/>
</bean>
<!-- 使用阿里的druid配置数据源 end--> </beans>
applicationContext-jpa.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:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> <!--上面的xsd最好和当前使用的Spring版本号一致,如果换了Spring版本,这个最好也跟着改--> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitName" value="Jerry"/>
<property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"></property> //persistence.xml文件配置(不展示) <property name="dataSource" ref="dataSource"/> <!--zchtodo 这个作用是什么?-->
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
</property> <!-- 指定Jpa持久化实现厂商类,这里以Hibernate为例 -->
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
<!-- 指定Entity实体类包路径 -->
<property name="packagesToScan">
<list>
<value>com.jerry.entity</value>
</list>
</property>
<!-- 指定JPA属性;如Hibernate中指定是否显示SQL的是否显示、方言等 -->
<property name="jpaProperties">
<!--hibernate 官网说明这些配置属性:http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/session-configuration.html-->
<props>
<!--常见的还有 MySQL5Dialect、MySQL5InnoDBDialect、MySQL57InnoDBDialect、MySQLDialect、MySQLInnoDBDialect、MySQLMyISAMDialect-->
<!--如果你使用的数据库是 5.7 的话可以考虑用:MySQL57InnoDBDialect-->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<!--是否显示 SQL-->
<prop key="hibernate.show_sql">true</prop>
<!--如果显示 SQL,输出的 SQL 时候要格式化-->
<prop key="hibernate.format_sql">true</prop>
<!--在显示的 SQL 中增加一些 Hibernate 提供的注释说明,依此来解释它生成的 SQL 意思-->
<!--配置如何根据java模型生成数据库表结构,常用update,validate-->
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
<!--关于这个属性可以看:http://blog.csdn.net/dracotianlong/article/details/27834143-->
<!--ImprovedNamingStrategy 是采用下划线,符合我们一般命名表字段的习惯-->
<!--所以,命名规则 My_NAME->MyName-->
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop> <prop key="hibernate.generate_statistics">false</prop>
</props>
</property> </bean> <!-- Hibernate对Jpa的实现 -->
<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> <!-- 重要配置:启用扫描并自动创建代理的功能 -->
<jpa:repositories base-package="com.jerry.reposity" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/> </beans> applicationContext-transaction.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor" />
<bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
<property name="patterns">
<list>
<value>com.youmeek.ssm.module.*.service.*</value>
</list>
</property>
</bean> <aop:config proxy-target-class="true">
<aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
</aop:config>
<!-- Druid 和 Spring 关联监控配置 end--> <!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean> <!-- 启用注解方式1:开启注解事务 start-->
<!--<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />-->
<!-- 启用注解方式1:开启注解事务 end--> <!-- 启用注解方式2:开启AOP事务方式 start-->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--REQUIRED 表示:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。-->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="register*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="edit*" propagation="REQUIRED"/>
<tx:method name="batch*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="deleteAndRepair" propagation="REQUIRED"/>
<tx:method name="remove*" propagation="REQUIRED"/>
<tx:method name="time*" propagation="REQUIRED"/><!--定时器方法-->
<tx:method name="repair" propagation="REQUIRED"/> <!--以这些单词开头的方法不加入事务-->
<!--SUPPORTS 表示:支持当前事务,如果当前没有事务,就以非事务方式执行。-->
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="load*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="search*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="datagrid*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="show*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* com.jerry.service.impl.*.*(..) )"/>
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice"/>
</aop:config>
<!-- 启用注解方式2:开启AOP事务方式 end--> </beans>
Spring-data-jpa操作数据库环境配置的更多相关文章
- spring data jpa 操作pipelinedb 的continuous view 与stream
一. 由于pipelinedb是postgreSQL的扩展,因此相关依赖于配置都合集成postgreSQL是一样的. springboot + spring data jpa + postgreSQL ...
- Spring Boot (五)Spring Data JPA 操作 MySQL 8
一.Spring Data JPA 介绍 JPA(Java Persistence API)Java持久化API,是 Java 持久化的标准规范,Hibernate是持久化规范的技术实现,而Sprin ...
- spring Data Jpa的依赖+配置
spring data jpa 是spring基于的orm框架,jpa规范的基础上封装的一套JPA应用框架 添加的相关依赖: <properties> <spring.version ...
- spring data jpa实体类映射配置
@Entity:用来标志实体类,知名这是一个和数据库表映射的实体类 @Id注解指明这个属性映射为数据库的主键 @GeneratedValue注解默认使用主键生成方式为自增,hibernate会自动生成 ...
- 干货|一文读懂 Spring Data Jpa!
有很多读者留言希望松哥能好好聊聊 Spring Data Jpa!其实这个话题松哥以前零零散散的介绍过,在我的书里也有介绍过,但是在公众号中还没和大伙聊过,因此本文就和大家来仔细聊聊 Spring D ...
- 初入spring boot(七 )Spring Data JPA
Spring Data JPA通过提供基于JPA的Repository极大地减少JPA作为数据访问方案的代码量. 1.定义数据访问层 使用Spring Data JPA建立数据访问层十分简单,只需定义 ...
- Spring Boot2 系列教程(二十三)理解 Spring Data Jpa
有很多读者留言希望松哥能好好聊聊 Spring Data Jpa! 其实这个话题松哥以前零零散散的介绍过,在我的书里也有介绍过,但是在公众号中还没和大伙聊过,因此本文就和大家来仔细聊聊 Spring ...
- SpringBoot学习笔记:Spring Data Jpa的使用
更多请关注公众号 Spring Data Jpa 简介 JPA JPA(Java Persistence API)意即Java持久化API,是Sun官方在JDK5.0后提出的Java持久化规范(JSR ...
- 最近项目中使用Spring data jpa 踩过的坑
最近在做一个有关OA项目中使用spring data JPA 操作数据库,结果遇到了补个不可思议的麻烦.困惑了好久. 首先看一下问题吧,这就是当时测试“设置角色时,需要首先删除该用户已经拥有的角色时” ...
- Spring Data JPA简单使用
用Spring Data JPA操作数据库 这份教程教你用Spring Data JPA从关系数据库mysql中存储和提取数据.总结来自https://spring.io/guides/gs/acce ...
随机推荐
- MR1和MR2的工作原理
MapReduce1 分为6个步骤: 1.作业的提交 1).客户端向jobtracker请求一个新的作业ID(通过JobTracker的getNewJobId()方法获取,见第2步 2).计算作业的输 ...
- [转] Maven 从命令行获取项目的版本号
[From]https://blog.soebes.de/blog/2018/06/09/help-plugin/ I bet you have been faced with the situati ...
- 061. Rotate List
题目链接:https://leetcode.com/problems/rotate-list/description/ Example 1: Input: 1->2->3->4-&g ...
- Ubuntu16.04系统Tensorflow源码安装
最近学习Tensorflow,记录一下安装过程.目前安装的是CPU版的 1.下载tensorflow源码 tensorflow是个开源库,在github上有源码,直接在上面下载.下载地址:https: ...
- API接口设计的五大公共参数
1.平台参数 2.操作系统参数 iOS.Android.PC等等 3.软件版本参数 4.udid号(设备唯一ID) 每个设备都会有一个唯一udid 5.渠道号 app软件从那个渠道下载
- 【DSP开发】HyperLink 编程和性能考量
冯华亮/Brighton Feng---Communication Infrastructure 摘要 HyperLink 为两个 KeyStone 架构 DSP 之间提供了一种高速,低延迟,引脚数量 ...
- mysql大数据量插入参考
Mysql 千万数据10秒批量插入只需三步第一步:配置my.ini文件文件中配置bulk_insert_buffer_size=120M 或者更大将insert语句的长度设为最大.Max_allowe ...
- flower 时区设置
celery 搭配flower使用,flower默认使用的是UTC时间,那么如何在flower中使用当前城市的时间呢 我的环境 celery 3.1.25 ,python 3.69 1.在 app设置 ...
- ASP.NET Core WebApi使用Swagger生成API说明文档【xml注释版】
⒈新建ASP.NET Core WebAPi项目 ⒉添加 NuGet 包 Install-Package Swashbuckle.AspNetCore ⒊Startup中配置 using System ...
- python-queue队列-生产者消费者
import threading,time import queue q = queue.Queue(maxsize=10) def Producer(name):#生产者 count=1 while ...