SSM整合准备工作;
1、创建web模块,引入依赖;
2、创建数据表;
3、创建web.xml;
4、创建SpringMVC配置文件。
5、创建Spring的配置文件,配置扫描组件、配置数据源、配置事务管理器、配置开启事务注解;
6、创建service层,包括service接口和实现类;
7、搭建MyBatis环境,创建MyBatis核心配置文件,创建mapper接口,创建映射文件;
8、Spring整合MyBatis,在Spring的配置文件中,配置SqlSessionFactoryBean。
Spring整合MyBatis
1、搭建MyBatis环境,创建核心配置文件,创建mapper,映射文件;
2、在Spring配置文件中,添加两个配置,配置SqlSessionFactoryBean,配置扫描mapper接口MapperScannerConfigurer。
配置SqlSessionFactoryBean,可以从Spring IOC容器获取SqlSessionFactory对象
配置扫描mapper接口,将指定路径下所有接口,通过SqlSession对象创建对应的代理实现类对象,并交给Spring IOC容器管理。
就可以在service层,直接对依赖的mapper接口类型的对象进行自动装配。
引入依赖
<properties>
<!--spring版本-->
<spring.version>5.3.1</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<!--springmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--事务管理依赖的jar-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Mybatis核心 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!--mybatis和spring的整合包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!-- junit测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!-- log4j日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
<!-- 日志 Thymeleaf依赖slf4j日志,slf4j日志的具体实现logback依赖的jar -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- ServletAPI -->
<!--前端控制器DispatcherServlet间接实现了HttpServlet所依赖的jar-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--json依赖的jar-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
</dependency>
<!--文件上传-->
<depend