SSM(Spring、SpringMvc、Mybatis)整合
今天要说的是SMM框架的整合,这三个框架分开来用并不能完全体现出他的简便与强大,所以就需要将它们整合起来。下面就来具体的操作。
首先看一下项目的结构:
先说一下java文件夹中每个package的作用
- controller:控制层,负责对前段数据做检验,加工。只负责数据安全和数据传递给业务层。
- service:业务层,负责书写业务,impl是它的实现类,负责具体的实现,这样写的好处就是实际开发时,你定义好接口,其它同事就可以调用了,不需要等你写完impl里面的实现,可以一块开发。
- mapper:就是操作数据库了
- model:实体类,对应数据库的model就是实体,对应数据库的
了解完每个package的作用后下面来整合的第一步:引入依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.version>4.3.11.RELEASE</spring.version>
</properties>
<dependencies>
<!--Junit单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--MyBatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--MySQL依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
</dependency>
<!--Spring-->
<!--SpringMVC依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<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-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!--MyBatis和Spring整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--C3p0-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.1</version>
</dependency>
<!--servlet的依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
这些依赖作用就不一一细说,上面都简单的注释,下面需要先配置好springMvc
配置SpringMvc首先需要在resources中新建一个spring的xml文件
新建完之后是这样的
需要手动在beans中添加一些配置文件,添加问之后是这样的:
代码在这:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
之所以要添加是因为下面需用用到context与mvc标签。
先来开启注解驱动:
<mvc:annotation-driven></mvc:annotation-driven>
然后打开注解扫描器
<context:component-scan base-package="com.ssm"></context:component-scan>
base-package是需要扫描哪些package,这里com.ssm是扫描二级目录的package,因为有多个package用到了注解。
最后是配置视图解析
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
用来跳转jsp页面。
这里面配置完之后还需要在web.xml中配置servlet
<servlet>
<servlet-name>springmvc-config</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc-config</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
然后springMvc的配置就完成了,先去controller中测试有没有问题在进行下面的操作。
@Controller
public class MainController {
@RequestMapping("test")
public String test(){
System.out.println("nice");
return "index";
}
}
@Controller声明这个类, @RequestMapping(“test”)跳转的地址 test就是具体的地址。
可以运行tomcat进行测试,控制台中输出“nice”就说明springmvc的运行是没有问题的。
接下来就是对Mybatis的配置,在SSM整合之前Mybatis一般都会单独创建一个xml的配置文件,但在SSM里面Mybatis的配置全都交给spring的管理了。所以Mybatis的各种配置都放进spring的xml配置文件里面了。
在配置Mybatis之前还需要在resources中新建一个数据库连接信息的db.properties文件
user=root
pwd=tiger
url=jdbc:mysql://localhost:3306/oracle?characterEncoding=utf-8
driverClass=com.mysql.jdbc.Driver
initPoolSize=3
maxPoolSize=20
user:用户名 pwd:密码 url:mysql的连接信息
driverClass:mysql的驱动包 initPoolSize:连接池最小连接数 maxPoolSize:连接池最大连接数
再来新建一个spring的配置文件,跟上面springmvc的配置文件新建方式是一样的,新建在resources中取名为applicationContext-common.xml
如图
里面的beans也需要加一些标签,这里就直接贴代码了:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
再来引用刚刚新建的db.properties
<!--引入数据库的配置文件-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
然后配置数据连接池
<!--数据连接池 数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${pwd}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="initialPoolSize" value="${initPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
</bean>
value里面都是对db.properties里面的调用
在用spring对SqlSessionFactory进行代理产生
<!--SqlSessionFactory的配置有spring工厂代理产生-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.ssm.model"></property>
<property name="mapperLocations" value="classpath:com/ssm/mapper/*.xml"></property>
</bean>
dataSource是上面数据源的依赖注入
typeAliasesPackage:是给实体类起的别名
mapperLocations:xml形式写Mybatis需要,如果用注解形式就不需要
还有对Mybatis注解扫描的配置
<!--扫描Mapper包下的映射文件的配置-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm.mapper"></property>
</bean>
下面就是配置事务管理器与切面、切点
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置切面-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"></tx:method>
<tx:method name="delete*" propagation="REQUIRED"></tx:method>
<tx:method name="update*" propagation="REQUIRED"></tx:method>
<tx:method name="*" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>
<aop:config>
<!--配置切点-->
<aop:pointcut id="pointCut" expression="execution(* com.ssm.services.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"></aop:advisor>
</aop:config>
整合完之后还需要在web.xml下加上监听与环境
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-common.xml</param-value>
</context-param>
<!--配置监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
不一一细说了。下面就来写Mybatis的操作语句,测试查询与新增。
下面的操作就不贴代码了,直接贴图片。
这里是要操作的实体类
这个是xml形式写的查询
上面的findStu是xml形式的查询,下面是注解形式的新增,@Service就是相当于bean形式的依赖注入
这里是业务层
业务层的实现类
@Autowired 就是自动引用@service注入的依赖
下面就是具体的测试操作了
到这里SSM的整合也就结束了!