目的:
mapper层:
IOC/DI
-
数据源交给Spring管理
-
SqlSessionFactory交给Spring管理, 依赖数据源
-
Mapper接口的代理对象交给Spring管理, SqlSession.getMapper()
依赖: SqlSessionFactory
业务层:
IOC/DI
-
业务层对象交给Spring管理: 依赖Mapper
-
使用AOP技术, 进行事务管理, 事务加在业务层
web层:
-
监听服务器启动, 创建Spring的IOC容器, 把容器对象保存Application域
-
在Servlet类只能得到Spring容器, 通过getBean()方式得到业务层对象
搭建环境:
-
创建web项目
-
添加依赖
Spring依赖: spring-context spring-aop aspectj spring-jdbc spring-orm spring-web spring-test spring-tx(事务管理模块)
mybatis依赖: mybatis mybatis-spring(mybatis与Spring整合的jar)
数据库驱动: mysql
日志: log4j
json: fastJson(alibaba的)
servlet: servlet-api jsp-api
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-demo-sm</artifactId>
<packaging>war</packaging>
<parent>
<groupId>com.fs</groupId>
<artifactId>spring-demo-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<name>spring-demo-sm Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<dependencies>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!--封装jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<!--与ORM模型的框架整合-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<!--spring的事务管理器-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<!--aspectj: AOP框架 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!--mybatis与spring整合 : 使用2版本-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
<!--日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.36</version>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
</dependencies>
<build>
<finalName>spring-demo-sm</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
3.配置文件:
-
Spring配置文件
-
Mybatis配置文件: 全局配置文件(可以省略), sql映射文件
-
数据库四大参数文件: db.properties
-
日志文件:log4j.properties
4.编写实体类, 对应Mapper接口
5.Mybatis与Spring整合
<!--加载properties文件
location属性, 需要加classpath
-->
<context:property-placeholder location="classpath:db.properties"/>
<!--配置数据源-->
<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.user}" ></property>
<property name="password" value="${jdbc.password}" ></property>
<property name="acquireIncrement" value="${jdbc.acquireIncrement}" ></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}" ></property>
<property name="minPoolSize" value="${jdbc.minPoolSize}" ></property>
<property name="initialPoolSize" value="${jdbc.initialPoolSize}" ></property>
</bean>
<!--SqlSessionFactory-->
<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--mybatis的全局配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--mapper映射文件
*: 任意 一定需要在classpath中声明: classpath*
-->
<property name="mapperLocations" value="classpath*:mapper/*Mapper.xml"/>
<!--别名-->
<property name="typeAliasesPackage" value="com.fs.sm.pojo"></property>
</bean>
<!--mapper扫描器: 扫描mapper接口, 使用动态代理得到Mapper接口的代理对象,保存到Spring容器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描那个包下Mapper接口-->
<property name="basePackage" value="com.fs.sm.mapper"/>
<!--SqlSessionFactoryBean对象-->
<property name="sqlSessionFactoryBeanName" value="SqlSessionFactory"/>
</bean>
6.编写业务层,注入mapper
扫描注解
<!--扫描注解-->
<context:component-scan base-package="com.fs.sm"/>