SSM三大框架整合
SSM整合步骤
Spring和Mybatis整合
- 加入Spring和mybatis的依赖
<!-- Spring相关依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.0.RELEASE</version> </dependency> <!-- mybatis相关依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!-- Spring、Mybatis整合依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency>
2.在Spring bean配置文件中,配置整合的Bean
在类路径下新建applicationContext.xml文件,做如下配置:
1)配置数据源
<context:property-placeholder location="classpath*:/jdbc.properties" /> <!-- 连接池 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean>
2)配置SqlSessionFactoryBean
<bean class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="typeAliasesPackage" value="com.lanou3g.ssm.bean" /> <property name="mapperLocations" value="classpath*:/mybatis/mappers/*.xml" /> </bean>
3.编写mybatis的mapper映射文件、mapper接口、核心配置文件(可选)
mapper映射文件和mapper接口可直接用mybatis逆向工程生成
4.配置扫描包路径
<context:component-scan base-package="com.lanou3g.ssm.service" />
5.配置mapper接口扫描路径
<mybatis:scan base-package="com.lanou3g.ssm.mapper" />
作用是让mybatis去指定的包下找Mapper接口,生成实现类,注入到IOC容器中
Spring和SpringMVC
1.添加SpringMVC依赖和Servlet、JSTL依赖
<!-- SpringMVC依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.0.RELEASE</version> </dependency> <!-- Servlet api 依赖 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>
2.配置SpringMVC
web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- 配置root applicationContext --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:application*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置请求编码 --> <filter> <filter-name>charchaterFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>charchaterFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置DispatcherServlet大总管 --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
dispatcher-servlet.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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置扫描Controller的包路径 --> <context:component-scan base-package="com.lanou3g.ssm.web" /> <!-- 配置视图解析器 --> <mvc:view-resolvers> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> </bean> </mvc:view-resolvers> <!-- 开启SpringMVC注解支持 --> <mvc:annotation-driven> <!-- 配置消息转换器,将REST返回的对象类型转成JSON --> <mvc:message-converters> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="defaultCharset" value="UTF-8" /> <property name="supportedMediaTypes"> <list> <!-- 这里配置的text/html主要是解决IE浏览器请求JSON会弹出下载框的问题 --> <value>text/html</value> <value>application/json</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 静态资源放行 --> <mvc:resources mapping="/static/**" location="/static/" /> </beans>
至此,SSM三个框架就整合完成了。 可以编写自己的service代码和controller层代码。
https://mp.weixin.qq.com/s/vkvYJnKfQyuUeD_BDQy_1g
获取更多学习资料,可以加群:473984645或扫描下方二维码
相关推荐
81314797 2020-11-18
柳木木的IT 2020-11-04
zhangpoou 2020-06-25
82384399 2020-06-16
xiamubawei 2020-06-04
dongxurr 2020-06-01
冒烟儿 2020-06-01
teresalxm 2020-05-09
smalllove 2020-05-05
Julywhj 2020-05-01
csuzxm000 2020-04-29
YangHuiLiang 2020-04-07
luojinbai 2020-02-21
米虚 2020-01-05
zhaojp0 2019-12-17
RickyIT 2019-11-17
hzflogo 2019-11-11
caidiehuang 2019-11-11
LiHansiyuan 2019-11-08
fayeyiwang 2019-11-06
emmm00 2019-11-05