在maven項目中連接配接資料庫,并做增删改查操作,這裡主要展示配置檔案的代碼,資料庫的操作基本和以前的一毛一樣,是以就不做過多的展示
1、建立maven項目,在pom.xml檔案中導入需要的包
<!-- 上下文 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<!-- aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<!-- 切點 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<!-- 資料庫 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<!-- spring事物 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<!-- spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<!-- spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
2、建立resources檔案目錄,注意檔案名必須是resources,并辨別為資源目錄,
3、建立資料庫連接配接的配置檔案,這裡需要注意的是,在定義屬性時不能是jar包的參數相同,
4、建立spring的配置檔案,在檔案中做一下幾步操作
①導入配置檔案
②導入資料源
③定義JDBC模闆對象
④定義事務管理對象
⑤定義事務增強操作
⑥配置事務處理
⑦三層對象掃描
具體的實作代碼:
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 導入配置檔案 -->
<context:property-placeholder location="datasource.properties" />
<!-- 導入資料源,name的值為固定寫法,當DriverManagerDataSource中定義的,value的值是datasource.properties對應的值,這裡需要注意的是配置檔案中的屬性名不能和資料源中的參數名一樣 -->
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
<!-- 定義JDBC模闆對象,name的值為固定寫法,其值引用導入的資料源 -->
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="datasource" />
</bean>
<!-- 定義事務管理對象 -->
<bean id="transacitionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource" />
</bean>
<!-- 定義事務增強操作 -->
<tx:advice id="txAdvice" transaction-manager="transacitionManager">
<tx:attributes>
<tx:method name="query*" propagation="NEVER" read-only="true"/>
<tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="modify*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="remove*" propagation="REQUIRED" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!-- 配置事務處理 -->
<aop:config>
<aop:pointcut id="servicePoint" expression ="within(com.t8.service..*)"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePoint" />
</aop:config>
<!-- 三層對象掃描 -->
<context:component-scan base-package="com.t8.dao"/>
<context:component-scan base-package="com.t8.service"/>
<context:component-scan base-package="com.t8.view"/>
</beans>