首页 > 代码库 > spring-mabatis整合的配置文件
spring-mabatis整合的配置文件
1.spring.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xmlns:jaxws="http://cxf.apache.org/jaxws" 8 xsi:schemaLocation=" 9 http://www.springframework.org/schema/beans 10 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-3.0.xsd13 http://www.springframework.org/schema/jee 14 http://www.springframework.org/schema/jee/spring-jee-3.0.xsd15 http://www.springframework.org/schema/tx 16 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd17 http://www.springframework.org/schema/aop 18 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd19 http://cxf.apache.org/jaxws20 http://cxf.apache.org/schemas/jaxws.xsd">21 <!-- *************************导cxf的配置文件*************************** -->22 <!-- 引入属性文件 -->23 <context:property-placeholder location="classpath:config.properties"/>24 25 <!-- 自动扫描(自动注入) -->26 <context:component-scan base-package="sy.*"></context:component-scan>27 28 </beans>29
<context:property-placeholder location="classpath:config.properties"/>
自动引入配置文件
<context:component-scan base-package="sy.*"></context:component-scan>
自动扫描sy下的所有类,不需要再配置<beans id...>
2.spring-mybatis.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xmlns:jaxws="http://cxf.apache.org/jaxws" 8 xsi:schemaLocation=" 9 http://www.springframework.org/schema/beans 10 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-3.0.xsd 13 http://www.springframework.org/schema/jee 14 http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 15 http://www.springframework.org/schema/tx 16 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 17 http://www.springframework.org/schema/aop 18 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 19 http://cxf.apache.org/jaxws 20 http://cxf.apache.org/schemas/jaxws.xsd"> 21 <!-- *************************导cxf的配置文件*************************** --> 22 <!-- 配置数据源 --> 23 <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 24 <property name="url" value="${jdbc_url}"></property> 25 <property name="username" value="${jdbc_username}"></property> 26 <property name="password" value="${jdbc_password}"></property> 27 28 <!-- 初始化连接大小 --> 29 <property name="initialSize" value="0" /> 30 <!-- 连接池最大使用链接数量 --> 31 <property name="maxActive" value="20" /> 32 <!-- 连接池最大空闲 --> 33 <property name="maxIdle" value="20" /> 34 <!-- 连接池最小空闲 --> 35 <property name="minIdle" value="0" /> 36 <!-- 获取连接最大等待时间 --> 37 <property name="maxWait" value="60000" /> 38 39 <property name="validationQuery" value="${validationQuery}"/> 40 <property name="testOnBorrow" value="false"/> 41 <property name="testOnReturn" value="false"/> 42 <property name="testWhileIdle" value="true"/> 43 44 <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> 45 <property name="timeBetweenEvictionRunsMillis" value="60000"/> 46 <!-- 配置一个连接池在池中最小生存时间 单位毫秒 --> 47 <property name="minEvictableIdleTimeMillis" value="25200000"/> 48 49 <!-- 打开removeAbandoned功能 --> 50 <property name="removeAbandoned" value="true"/> 51 <!-- 1800秒,也就是30分钟 --> 52 <property name="removeAbandonedTimeout" value="1800"/> 53 <!-- 关闭abanded连接时输出错误日志 --> 54 <property name="logAbandoned" value="true" /> 55 56 <!-- 监控数据库 --> 57 <property name="filters" value="mergeStat" /> 58 </bean> 59 60 <!-- mybatis文件 --> 61 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 62 <property name="dataSource" ref="dataSource" /> 63 <!-- 自动扫描entity目录,省掉Configuration.xml手工配置 --> 64 <property name="mapperLocations" value="classpath:sy/mapping/*.xml"/> 65 </bean> 66 67 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 68 <property name="basePackage" value="sy.dao"/> 69 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 70 </bean> 71 72 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 73 <property name="dataSource" ref="dataSource" /> 74 </bean> 75 76 <!-- 拦截器方式配置事物 --> 77 <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> 78 <tx:attributes> 79 <tx:method name="add*" propagation="REQUIRED" /> 80 <tx:method name="append*" propagation="REQUIRED" /> 81 <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> 82 <tx:method name="save*" propagation="REQUIRED" /> 83 <tx:method name="update*" propagation="REQUIRED" /> 84 <tx:method name="modify*" propagation="REQUIRED" /> 85 <tx:method name="edit*" propagation="REQUIRED" /> 86 <tx:method name="delete*" propagation="REQUIRED" /> 87 <tx:method name="remove*" propagation="REQUIRED" /> 88 <tx:method name="repair" propagation="REQUIRED" /> 89 <tx:method name="delAndRepair" propagation="REQUIRED" /> 90 91 <tx:method name="get*" propagation="SUPPORTS" /> 92 <tx:method name="find*" propagation="SUPPORTS" /> 93 <tx:method name="load*" propagation="SUPPORTS" /> 94 <tx:method name="all*" propagation="SUPPORTS" /> 95 <tx:method name="search*" propagation="SUPPORTS" /> 96 <tx:method name="datagrid*" propagation="SUPPORTS" /> 97 98 <tx:method name="*" propagation="SUPPORTS" /> 99 </tx:attributes>100 </tx:advice>101 <aop:config>102 <aop:pointcut id="transactionPointcut" expression="execution(* com.zfy.service..*.*impl.*(..))" />103 <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />104 </aop:config>105 106 107 <!-- 配置druid监控spring jdbc -->108 <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">109 </bean>110 <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">111 <property name="patterns">112 <list>113 <value>sy.service.*</value>114 </list>115 </property>116 </bean>117 <aop:config>118 <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />119 </aop:config>120 </beans>121
<property name="url" value="http://www.mamicode.com/${jdbc_url}"></property>
<property name="username" value="http://www.mamicode.com/${jdbc_username}"></property>
<property name="password" value="http://www.mamicode.com/${jdbc_password}"></property>
读取配置文件的值
大部分代码都有注释,就不再一一说明了。
3.所需jar包
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework</groupId> 4 <artifactId>spring-core</artifactId> 5 <version>4.0.6.RELEASE</version> 6 </dependency> 7 <dependency> 8 <groupId>org.mybatis</groupId> 9 <artifactId>mybatis</artifactId>10 <version>3.2.7</version>11 </dependency>12 <dependency>13 <groupId>org.mybatis</groupId>14 <artifactId>mybatis-spring</artifactId>15 <version>1.1.1</version>16 </dependency>17 <dependency>18 <groupId>mysql</groupId>19 <artifactId>mysql-connector-java</artifactId>20 <version>5.1.32</version>21 </dependency>22 </dependencies>
此处是以maven形式给出的,其实都可以自己一一下载,下载方式:
下载地址:http://search.maven.org/
搜索对应的artifactId即可
spring-mabatis整合的配置文件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。