在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>