创建Maven工程, 并添加依赖
XML
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.compilerVersion>17</maven.compiler.compilerVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 支持 JDK 17 的 Spring 版本 -->
<spring.version>5.3.31</spring.version>
</properties>
<dependencies>
<!-- junit 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- AOP + 事务(必须升级适配 JDK17)-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.19</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.13</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<!-- 分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.2</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--实体类插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.42</version>
</dependency>
</dependencies>
</project>
创建数据库表并创建实体类
java
@AllArgsConstructor//有参
@NoArgsConstructor//无参
@Data//get和set方法和toString方法
public class User implements Serializable {
private int id; //编号id
private String username; //用户名
private String password; //密码
private int age; //年龄
private String sex; //性别
private String email; //邮箱
public User(String username, String password, int age, String sex, String email) {
this.username = username;
this.password = password;
this.age = age;
this.sex = sex;
this.email = email;
}
}
创建Spring框架的配置文件applicationContext.xml并配置IOC管理的对象
XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/context
http://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
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="com.study"></context:component-scan>
<!--引入数据源db.properties文件-->
<context:property-placeholder location="db.properties"></context:property-placeholder>
<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--数据源注入-->
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--创建Mybatis核心对象:工厂对象-->
<!--工厂对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--加载sql映射文件-->
<property name="mapperLocations" value="classpath:com/study/mapper/*.xml"></property>
<!--自定义类型别名-->
<property name="typeAliasesPackage" value="com.study.pojo"></property>
<!--加载Mybatis核心配置文件-->
<!--<property name="configLocation" value="classpath:mybatis-config.xml"></property>-->
<!--分页插件配置-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor"></bean>
</array>
</property>
<property name="configuration">
<bean class="org.apache.ibatis.session.Configuration">
<!--mapUnderscoreToCamelCase是否开启驼峰命名规则映射-->
<property name="mapUnderscoreToCamelCase" value="true"></property>
<!--开启延时加载开关-->
<property name="lazyLoadingEnabled" value="true"></property>
<!--关闭立即加载,实施按需加载-->
<property name="aggressiveLazyLoading" value="false"></property>
<!-- 开启二级缓存的支持 -->
<property name="cacheEnabled" value="true"></property>
</bean>
</property>
</bean>
<!--配置扫描mapper接口的文件-->
<bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描com.study.mapper中所有的接口,mybatis自动创建mapper接口实现类对象,
并且把创建好的对象放在ioc容器,id值为当前接口的名字-->
<property name="basePackage" value="com.study.mapper"></property>
</bean>
<!--1.事务管理器对象-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--开启事务注解驱动-->
<!--<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>-->
<!--2.声明事务通知-->
<tx:advice id="interceptor" transaction-manager="transactionManager">
<!--配置事务-->
<tx:attributes>
<!--
name:方法名
read-only:表示是否只读
propagation:表示传播行为 默认配置REQUIRED(用于增删改)或者SUPPORTS(用于查询)
timeout:设置超时时间
-->
<tx:method name="transfer" read-only="false" propagation="REQUIRED" timeout="3"/>
<tx:method name="transferList" read-only="false" propagation="REQUIRED" timeout="3"/>
<!--只要方法名前有select find get 事务只只读为false -->
<tx:method name="select*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<!--如果方法名前有insert update delete -->
<tx:method name="inset*" read-only="false"/>
<tx:method name="update*" read-only="false"/>
<tx:method name="delete*" read-only="false"/>
<!--(表示所有方法都支持事务)-->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!--配置切面-->
<aop:config>
<!--配置切入点-->
<aop:pointcut id="txPointcut" expression="execution(* com.study.service.impl.*.*(..))"/>
<!--将事务与切入点联系在一起-->
<aop:advisor advice-ref="interceptor" pointcut-ref="txPointcut"></aop:advisor>
</aop:config>
</beans>