1. 程式人生 > >Spring 註解方式 連結資料庫

Spring 註解方式 連結資料庫

下面是 spring的配置檔案

applicationContext-db.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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"
default-lazy-init="true">
<context:component-scan base-package="com" />
<!-- 配置資料來源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- 基本屬性 url、user、password -->
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="2" />
<property name="minIdle" value="1" />
<property name="maxActive" value="100" />
<!-- 配置獲取連線等待超時的時間 -->
<property name="maxWait" value="60000" />
<!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一個連線在池中最小生存的時間,單位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<!-- 用來檢測連線是否有效的sql,要求是一個查詢語句 -->
<property name="validationQuery" value="SELECT CURRENT_TIMESTAMP " />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 配置監控統計攔截的filters -->
<property name="filters" value="stat" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="plugins">
<array>
<ref bean="paginationInterceptor" />
</array>
</property>
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:mapping/*.xml"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hzdracom.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 註解方式配置事務-->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 攔截器方式配置事務 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointcut"
expression="execution(* com.hzdracom.service..*impl.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut"
advice-ref="transactionAdvice" />
</aop:config>
<!-- 配置事務管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>


<bean id="paginationInterceptor" class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=postgresql
reasonable=true
</value>
</property>
</bean>
</beans>

相關推薦

Spring 註解方式 連結資料庫

下面是 spring的配置檔案 applicationContext-db.xml的內容: <?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.o

spring-mybatis MapperScannerConfigurer,註解方式資料庫持久化四)

主流方式 xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://ww

eclipse-搭建maven的war項目集合spring註解方式

eap 初始化 打印 owin artifact mapping line welcom b- 工具:eclipse 4.4.2 版本號:20150219-0600 jdk:1.7 1.下圖創建maven工程,然後next 下圖選擇工程保存位置(這裏選擇默認),nex

spring註解方式註入bean

ota alt contex () implement bstr mage 所在 lap 用註解的方式註入bean,spring的配置文件也要增加一些約束和導入註解所在的包 applicationContext.xml 1 <?xml version="1.0"

Spring註解方式配置Redis

@Configuration public class RedisConfiguraion { @Bean public JedisConnectionFactory redisConnectionFactory() { RedisStandaloneConfigura

Spring註解方式大勢所趨(1)

目錄   [email protected] 和  @ComponentSacns 2. 給容器中註冊元件的四種方式 [email protected]和@Lazy [email protected] 和&

基於Spring註解方式配置Quartz

之前我們都是通過基於XML的方式實現Spring  Quartz 雖然配置起來特別的方便,但是Spring還支援基本註解的方式來配置,這樣做不僅更加簡單,而且程式碼量也更加少了很多。 1、配置需要排程的類,並添加註解 import java.text.SimpleDate

Spring 註解方式配置aop:

註解配置AOP(使用 AspectJ 類庫實現的),大致分為三步: 1. 使用註解@Aspect來定義一個切面,在切面中定義切入點(@Pointcut),通知型別(@Before, @AfterReturning,@After,@AfterThrowing,@Around)

mybatis檔案配置方式註解方式操作資料庫

以對question的表操作為例。 model層:無註解,有每個屬性的get,set方法(下面省略了一些) public class Question { private int id;

spring註解方式,使用jax-ws配置webservice,適合小白。看不會你打死我!

前提條件:java –spring框架,註解(能夠掃描@webService標籤) 1.   經過一天的煎熬和掙扎,終於把webservice的其中非常簡單的配置方法給弄明白了,就是JAX-WS配置webservice,真的非常簡單,只是細節一一旦出了點問題,全盤皆輸,為了

基於spring註解方式配置和使用spring AOP

spring AOP,面向切面程式設計,通常應用於系統的日誌,事物資訊輸出部分,如下圖中所示,當A呼叫B的f2方法時,如果我們想要讓系統在f2呼叫前輸出一些呼叫前的資訊,在f2呼叫結束後輸出一些呼叫結束後的資訊,一般的想法就是在呼叫f2前去呼叫C的f3,然後在呼

spring註解方式實現事務

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001

Spring註解方式建立的物件為單例

Java建立物件和對物件賦值(屬性)都可以通過Spring的方式來完成 一種方式是通過配置XML檔案 另一種方式是通過註解的方式 註解建立的物件是單例的。 (1)配置檔案,進行掃描 <context:component-scan base-

spring 註解方式動態代理

一:動態代理和靜態代理的區別? 動態代理是將代理物件下的所有的方法都集中到代理類中集中處理. 靜態代理是將代理物件的某個方法進行轉發處理 二:註解方式的動態代理 第一步: 引入相關的xsd 通過aop名稱空間的<aop:aspectj-autoproxy />

ActiveMQ學習總結(10)——ActiveMQ採用Spring註解方式傳送和監聽

對於ActiveMQ訊息的傳送,原聲的api操作繁瑣,而且如果不進行二次封裝,開啟關閉會話以及各種建立操作也是夠夠的了。那麼,Spring提供了一個很方便的去收發訊息的框架,spring jms。整

struts2整合spring 註解方式 注入為空問題原因之一

java.lang.NullPointerException  at com.action.TestForAction.addPersion(TestForAction.java:17)  at sun.reflect.NativeMethodAccessorImpl.in

spring配置dbcp連結資料庫

1.spring的配置檔案 springmvc-servlet.xml <bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"

Spring註解方式和配置檔案方式的混用

1.註解方式和配置檔案方式的混用:     1.配置檔案方式 : 用於配置物件的建立          <beans>             <!--開啟掃描-->             <context:component-scan bas

Spring註解方式實現定時任務

..... @EnableScheduling public class certManagerService { @Autowired private certManageDomain

Spring註解方式,異常 'sessionFactory' or 'hibernateTemplate' is required的解決方法 轉載的,勞記勞記

啟動工程時出現異常: Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required       at org.springframewor